UserRegisterMobileController.php
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?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);
}
}