SmsController.php
2.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
<?php
/**
* Created by PhpStorm.
* User: yhbr
* Date: 2018/9/29
* Time: 10:46
*/
namespace app\sms\controller;
use cmf\controller\HomeBaseController;
use think\Db;
class SmsController extends HomeBaseController
{
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 (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);
}
}
}
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 (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);
}
}
}
}