AgentRegisterMobileController.php 5.9 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/4
 * Time: 11:27
 */

namespace app\index\controller;


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

use think\Db;

class AgentRegisterMobileController extends WeChatBaseController
{
    //授权
    function _initialize()
    {
        //判断用户是否微信浏览器打开
        $this->isWechat();
        //微信授权
        parent::_initialize();
        $this->checkWeChatUserLogin();
        //阻止拉黑用户
        $this->ban();
    }
    /**
     * @title 代理人注册手机号页面
     * @author XiangGang Wang
     * @time 2019年1月4日11:28:01
     */
    public function index(){
        $user_id = cmf_get_current_user_id();
        //判断当前用户是否为代理人
        $userModel = new UserModel();
        $user = $userModel->findUserData(array('id'=>$user_id));
        if($user['type'] == 2){
            //重定向到代理人页面
            $this->redirect('index/agent/index');
        }
        $is_select = $this->request->param('is_select', 0, 'intval');
        //协议
        $AgreementModel = new AgreementModel();
        $agreement = $AgreementModel->findData(array('id'=>2));
        $this->assign(
            array(
                'title'=>'代理人注册',
                'is_select'=>$is_select,
                'agreement'=>$agreement,
            )
        );
        return $this->fetch();
    }
    /**
     * @title 获取验证码
     * @author XiangGang Wang
     * @time 2019年1月4日11:27:56
     */
    public function getCode(){
        $content=$this->generate_code();
        $user_id = cmf_get_current_user_id();
        //获取手机号
        $mobile = $this->request->post('phone');
        //获取姓名
        $name = $this->request->post('name');
        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'=>2))->find();
            if($isin){
                $data1['name']=$name;
                $data1['code']=$content;
                $data1['update_time']=time();
                $data1['end_time']=time()+300;//过期时间
                $data1['type']=2;
                Db::name('code')->where(['user_id'=>$user_id,'phone'=>$mobile,'type'=>2])->update($data1);
            }else{
                $data1['name'] = $name;
                $data1['user_id']=$user_id;
                $data1['phone']=$mobile;
                $data1['code']=$content;
                $data1['create_time']=time();
                $data1['end_time']=time()+300;//过期时间
                $data1['type']=2;
                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 2019年1月4日11:27:51
     */
    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');
        $card_id = $this->request->post('card_id');
        $company = $this->request->post('company');
        $date = $this->request->post('date');
        if(empty($name) || empty($mobile) || empty($code) || empty($card_id) || empty($company)){
            $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'=>2))->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('mobile2'=>$mobile,'name'=>$name,'type'=>2));
        //写入申请表中
        $data = Db::name('application_agent')->where(array('delete_time'=>0,'user_id'=>$user_id))->find();
        if(empty($data)){
            Db::name('application_agent')->insert(array('user_id'=>$user_id,'status'=>1,'create_time'=>time(),'phone'=>$mobile,'date'=>$date,'card_id'=>$card_id,'company'=>$company,'name'=>$name));
        }else{
            Db::name('application_agent')->where('id',$data['id'])->update(array('user_id'=>$user_id,'status'=>1,'update_time'=>time(),'phone'=>$mobile,'date'=>$date,'card_id'=>$card_id,'company'=>$company,'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);
    }
}