RegisterController.php 6.9 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;

use app\admin\model\SlideItemModel;
use app\member\model\MemberModel;
use app\user\model\CodeModel;
use cmf\controller\HomeBaseController;
use think\Db;
use think\Session;
use think\Validate;
use app\user\model\UserModel;

class RegisterController extends HomeBaseController
{

    /**
     * 前台用户注册
     */
    public function index()
    {
        $url = url('index','','',true);
        $qrcode = $this->createQRcode($url,'Q',9);
        $name = '用户注册页';
        $this->qrcodePage($url,$name,$qrcode);
        $channel_id = $this->request->param('channel_id');
        $redirect = $this->request->post("redirect");
        if (empty($redirect)) {
            $redirect = $this->request->server('HTTP_REFERER');
        } else {
            $redirect = base64_decode($redirect);
        }
        session('login_http_referer', $redirect);

        if (cmf_is_user_login()) {
            if(!session('baby_age')) {
                return redirect(url('user/Login/chooseAge'));
            } else {
                return redirect($this->request->root() . '/');
            }
        } else {
            // 渠道参数
            $token = $this->request->param('token');
            if(!empty($token)) {
                $channel = Db::name('Channel')->where(['token'=>$token,'delete_time'=>0])->find();
                if(!$channel || $channel['is_stop'] == 1) {
                    return $this->fetch(":close");
                }
                Session::set('channel',$channel['id']);
            }
            // 协议文章内容
            $info = Db::name('PortalPost')->field(['id,create_time,post_title,post_excerpt'])->where(['id'=>3,'delete_time'=>0])->find();
            $this->assign('info',$info);

            // banner图
            $slide_model = new SlideItemModel();
            $slide_where = [
                'slide_id' => 5,
                'status' => 1
            ];
            $slide = $slide_model->where($slide_where)->order(['list_order'=>'ASC'])->find();
            $this->assign('slide',$slide);
            return $this->fetch(":register");
        }
    }

    /**
     * 前台用户注册提交
     */
    public function doRegister()
    {
        if ($this->request->isPost()) {
            $rules = [
                'baby_name'  => 'require',
                'baby_birth'     => 'require',
                'parent_name'  => 'require',
                'parent_status' => 'require',
//                'mobile' => 'require|mobile',
                'is_member' => 'require|in:0,1',
                'code' => 'require|number|length:6'
//                'password' => 'require|min:6|max:32',

            ];

//            $isOpenRegistration=cmf_is_open_registration();
//
//            if ($isOpenRegistration) {
//                unset($rules['code']);
//            }

            $validate = new Validate($rules);
            $validate->message([
                'baby_name.require'     => '宝宝姓名不能为空',
                'baby_birth.require' => '宝宝生日不能为空',
                'parent_name.require' => '家长姓名不能为空',
                'parent_status.require' => '家长身份不能为空',
                'mobile.require' => '手机号码不能为空',
//                'mobile.mobile' => '手机号码格式错误',
                'is_member.require' => '请选择是否为巧虎会员',
                'is_member.in' => '是否为巧虎会员参数错误',
                'code.require' => '验证码不能为空',
                'code.number' => '验证码必须为数字',
                'code.length' => '验证码长度为6位',
//                'password.max'     => '密码不能超过32个字符',
//                'password.min'     => '密码不能小于6个字符',
//                'captcha.require'  => '验证码不能为空',
            ]);

            $data = $this->request->post();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            if(!preg_match('/^1(3|4|5|7|8|9)\d{9}$/',$data['mobile'])) {
                $this->error('手机号码错误');
            }
//            if (!cmf_captcha_check($data['captcha'])) {
//                $this->error('验证码错误');
//            }

//            if(!$isOpenRegistration){
//                $errMsg = cmf_check_verification_code($data['username'], $data['code']);
//                if (!empty($errMsg)) {
//                    $this->error($errMsg);
//                }
//            }

            $code_where = [
                'mobile'  => $data['mobile'],
                'code'    => $data['code'],
                'is_used' => 0
            ];
            $code_check = Db::name('Code')->where($code_where)->find();
            if(!$code_check) {
                $this->error('手机验证码错误',json_encode($code_where));
            }
            Db::name('Code')->where($code_where)->update(['is_used' => 1]);
            $register          = new MemberModel();
//            $user['user_pass'] = $data['password'];
//            if (Validate::is($data['username'], 'email')) {
//                $user['user_email'] = $data['username'];
//                $log                = $register->registerEmail($user);
//            } else
            if (preg_match('/^1(3|4|5|7|8|9)\d{9}$/', $data['mobile'])) {
//                $user['mobile'] = $data['mobile'];
                $log            = $register->registerMobile($data);
            } else {
                $log = 2;
            }
            $sessionLoginHttpReferer = session('login_http_referer');
            $redirect                = empty($sessionLoginHttpReferer) ? cmf_get_root() . '/' : $sessionLoginHttpReferer;
            switch ($log) {
                case 0:
//                    $this->success('注册成功', $redirect);
                    $this->success('注册成功', url('user/Login/chooseAge'));
                    break;
                case 1:
                    $this->error("您的账户已注册过");
                    break;
                case 2:
                    $this->error("您输入的账号格式错误");
                    break;
                default :
                    $this->error('未受理的请求');
            }

        } else {
            $this->error("请求错误");
        }

    }
}