CommonController.php
5.4 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
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use app\portal\model\MobileCodeModel;
use think\Db;
class CommonController extends HomeBaseController
{
private $url = 'http://api.mix2.zthysms.com/v2/sendSmsTp';//短信请求地址
private $username = 'dujiaoxinghy';//用户名
private $password = 'eU3OeYmx';//密码
private $tpId = 345;//模板id
public function index(){
return $this->fetch();
}
/**
* 发送手机验证码
*/
public function sendMobileCode(){
Db::startTrans();
$mobile = $this->request->param('mobile','');
$mc = new MobileCodeModel();
$search = '/^0?1[3|4|5|6|7|8][0-9]\d{8}$/';
if (!preg_match($search,$mobile)) {
$this->apiResponse(0,'手机号格式有误','');
}
$mobile_code = rand(100000, 999999);
$info = $mc->where([
'mobile' => $mobile,
'create_date' => date('Y-m-d')
])->find();
if($info){
if(time() < $info['create_time']+60 && $info['is_use'] == 0){
$this->apiResponse(0,'不能频繁发送验证码','');
}
if($info['count'] > 10){
$this->apiResponse(0,'今天发送验证码的次数已达到了上限','');
}
$res = $mc->where('id',$info['id'])->data([
'mobile' => $mobile,
'mobile_code' => $mobile_code,
'is_use' => 0,
'expire_time' => time()+300,
'count' => $info['count'] +1
])->update();
}else{
$res = $mc->insert([
'mobile' => $mobile,
'mobile_code' => $mobile_code,
'is_use' => 0,
'expire_time' => time()+300,
'count' => 1,
'create_time' => time(),
'create_date' => date('Y-m-d')
]);
}
if($res){
//发送验证码
$is_ok = $this->sendCode($mobile,$mobile_code);
$is_ok = json_decode($is_ok,true);
if($is_ok['code'] != 200){
Db::rollback();
$this->apiResponse(0,$is_ok['msg']);
}
Db::commit();
$this->apiResponse(1,'发送成功');
}
}
/**
* 验证手机验证码
*/
public function validateMobileCode($post){
$mc = new MobileCodeModel();
if(empty($post['mobile'])){
$this->apiResponse(0,'手机号不能为空!');
}
if(empty($post['mobile_code'])){
$this->apiResponse(0,'验证码不能为空!');
}
$res_find = $mc->where(['mobile' => $post['mobile'], 'mobile_code' => $post['mobile_code'], 'is_use' => 0, 'create_date' => date('Y-m-d'),])
->where('expire_time','gt',time())->find();
if($res_find){
$res_update = $mc->where('id',$res_find['id'])->setField('is_use',1);
if($res_update){
return true;
}
}else{
$this->apiResponse(0,'验证未通过',$post);
}
}
/**
* 发送短信验证码
* @param $mobile
* @param $mobile_code
* @return mixed
*/
private function sendCode($mobile,$mobile_code){
date_default_timezone_set('PRC');//设置时区
$url = $this->url;//提交地址
$tKey = time();
$password = $this->password;//密码
$data['username'] = $this->username;//用户名
$data['tKey'] = $tKey;
$data['password'] = md5(md5($password).$tKey);//原密码
$data['tpId'] = $this->tpId;//模板id
$data['records'][0]['mobile'] = $mobile;
$data['records'][0]['tpContent']['var1'] = $mobile_code;
$data['signature'] = '【独角星球】';
$headers = ['Content-Type: application/json;charset=UTF-8'];
$data = json_encode($data,true);
$curl = curl_init();// 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, true); // 开启header
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); // 执行操作
curl_close($curl);
return $result;
}
}