SmsController.php
4.2 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
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/9/29
* Time: 10:46
*/
namespace app\sms\controller;
use cmf\controller\NoneBaseController;
use think\Db;
class SmsController extends NoneBaseController
{
public function sendSms()
{
require_once EXTEND_PATH . '/zt_sms/sms.php';
$mobile = request()->param('mobile');
$code = rand(100000, 999999);
$content = '【天生红】您的验证码为' . $code . ',有效期5分钟,请及时使用。';
$sms = new \SendMsg($mobile, $content);
$sms->sendSMS();
$time = time();
$code = [
'mobile' => $mobile,
'code' => $code,
'create_time' => $time,
'expire_time' => $time + 300
];
if (Db::name('zj_code')->where(['mobile' => $mobile])->count()) {
Db::name('zj_code')->where(['mobile' => $mobile])->update($code);
} else {
Db::name('zj_code')->insert($code);
}
}
//单纯发送验证码
public function checkCode()
{
$mobile = request()->param('mobile');
$code = request()->param('code');
$data = Db::name('zj_code')->field('mobile,code,expire_time')->where(['mobile' => $mobile])->find();
if (!empty($data)) {
if (time() > $data['expire_time']) {
$this->success('验证码已过期,请重新发送', '', false);
} else {
if ($code == $data['code'] && $code != null && $data['code'] != null && $mobile == $data['mobile']) {
Db::name('zj_code')->where(['mobile' => $mobile])->update(['code' => '']);
$this->success('身份信息核实成功', url(''), true);
} else {
$this->success('验证码有误', '', false);
}
}
} else {
$this->success('验证码有误', '', false);
}
}
//修改密码、个人信息时的校验(必须验证输入手机号和原绑定手机号一致)
public function checkCodeS()
{
$mobile = request()->param('mobile');
$code = request()->param('code');
$data = Db::name('zj_code')->field('mobile,code,expire_time')->where(['mobile' => $mobile])->find();
$mobile = DB::name('user')->where(['id' => session('user.id')])->value('mobile');
if (!empty($data)) {
if ($mobile == $data['mobile']) {
if (time() > $data['expire_time']) {
$this->success('验证码已过期,请重新发送', '', false);
} else {
if ($code == $data['code'] && $code != null && $data['code'] != null && $mobile == $data['mobile']) {
Db::name('zj_code')->where(['mobile' => $mobile])->update(['code' => '']);
$this->success('身份信息核实成功', url(''), true);
} else {
$this->success('验证码有误', '', false);
}
}
} else {
$this->success('请输入原始绑定手机号码', '', false);
}
} else {
$this->success('验证码有误', '', false);
}
}
public function bindNewMobile()
{
$mobile = request()->param('mobile');
$code = request()->param('code');
$data = Db::name('zj_code')->field('mobile,code,expire_time')->where(['mobile' => $mobile])->find();
if (!empty($data)) {
if (time() > $data['expire_time']) {
$this->success('验证码已过期,请重新发送', '', false);
} else {
if ($code == $data['code'] && $code != null && $data['code'] != null && $mobile == $data['mobile']) {
Db::name('zj_code')->where(['mobile' => $mobile])->update(['code' => '']);
Db::name('user')->update(['id' => session('user.id'), 'mobile' => $mobile]);
$this->success('绑定成功', url(''), true);
} else {
$this->success('验证码有误', '', false);
}
}
} else {
$this->success('验证码有误', '', false);
}
}
}