UserRegisterMobileController.php 4.6 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/25
 * Time: 15:34
 */

namespace app\index\controller;


use app\index\model\AgreementModel;
use cmf\controller\WeChatBaseController;

use think\Db;

class UserRegisterMobileController extends WeChatBaseController
{
    //授权
    function _initialize()
    {
        //判断用户是否微信浏览器打开
        $this->isWechat();
        //微信授权
        parent::_initialize();
        $this->checkWeChatUserLogin();
        //阻止拉黑用户
        $this->ban();
    }
    /**
     * @title 注册手机号页面
     * @author XiangGang Wang
     * @time 2018年12月25日15:38:58
     */
    public function index(){
        $is_select = $this->request->param('is_select', 0, 'intval');
        //协议
        $AgreementModel = new AgreementModel();
        $agreement = $AgreementModel->findData(array('id'=>1));
        $this->assign(
            array(
                'title'=>'注册',
                'is_select'=>$is_select,
                'agreement'=>$agreement,
            )
        );
        return $this->fetch();
    }
    /**
     * @title 获取验证码
     * @author XiangGang Wang
     * @time 2018年12月25日15:36:20
     */
    public function getCode(){
        $content=$this->generate_code();
        $user_id = cmf_get_current_user_id();
        //获取手机号
        $mobile = $this->request->post('phone');
        require_once CMF_PATH . 'common.php';
        $data = array(
            'content' 	=> "【橙象保险】您的验证码是:".$content.",请于5分钟内使用,如非本人操作,可忽略此消息。",//短信内容
            'mobile' 	=> $mobile,//手机号码
            'productid' => '887361',//产品id
            'xh'		=> ''//小号
        );
        $result = send_sms($data);
        if(substr($result,0,strpos($result,',')) == "1"){
            //保存到数据库
            $isin=Db::name('code')->where(array('phone'=>$mobile,'user_id'=>$user_id,'type'=>1))->find();
            if($isin){
                $data1['code']=$content;
                $data1['update_time']=time();
                $data1['end_time']=time()+300;//过期时间
                $data1['type']=1;
                Db::name('code')->where(array('user_id'=>$user_id,'type'=>1))->update($data1);
            }else{
                $data1['user_id']=$user_id;
                $data1['phone']=$mobile;
                $data1['code']=$content;
                $data1['create_time']=time();
                $data1['end_time']=time()+300;//过期时间
                $data1['type']=1;
                Db::name('code')->insert($data1);
            }
            $arr['code'] = 20000;
            $arr['msg'] = "验证码获取成功!";
            return json_encode($arr);
        }else{
            $arr['code'] = 40000;
            $arr['msg'] = '短信接口出错';
            $arr['data'] = $result;
            return $arr;
        }
    }
    /**
     * @title 注册手机号
     * @author XiangGang Wang
     * @time 2018年12月25日15:35:52
     */
    public function register_mobile(){
        $user_id = cmf_get_current_user_id();
        $name= $this->request->post('name');
        $mobile = $this->request->post('phone');
        $code = $this->request->post('code');
        $consent = $this->request->post('consent');
        if(empty($name) || empty($mobile) || empty($code)){
            $arr['code'] = 40001;
            $arr['msg'] = "缺少必要参数!";
            return json_encode($arr);
        }
        if(empty($consent)){
            $arr['code'] = 40004;
            $arr['msg'] = "请同意协议!";
            return json_encode($arr);
        }
        $codeData = Db::name('code')->where(array('phone'=>$mobile,'user_id'=>$user_id,'type'=>1))->find();
        //判断验证码是否正确
        if($codeData['code'] != $code){
            $arr['code'] = 40002;
            $arr['msg'] = "验证码错误!";
            return json_encode($arr);
        }
        //判断验证码是否过期
        if($codeData['end_time']<time()){
            $arr['code'] = 40003;
            $arr['msg'] = "验证码已过期!";
            return json_encode($arr);
        }
        Db::name('user')->where('id',$user_id)->update(array('mobile'=>$mobile,'name'=>$name));
        $arr['code'] = 20000;
        $arr['msg'] = "注册成功!";
        return json_encode($arr);
    }
    /**
     * 生成6位随机数
     * @param   string
     * @return  boolean
     */
    public function generate_code($length = 6) {
        $min = pow(10 , ($length - 1));
        $max = pow(10, $length) - 1;
        return rand($min, $max);
    }
}