Sms.php
3.7 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
namespace app\common\library;
use think\Hook;
/**
* 短信验证码类
*/
class Sms
{
/**
* 验证码有效时长
* @var int
*/
protected static $expire = 120;
/**
* 最大允许检测的次数
* @var int
*/
protected static $maxCheckNums = 10;
/**
* 获取最后一次手机发送的数据
*
* @param int $mobile 手机号
* @param string $event 事件
* @return Sms
*/
public static function get($mobile, $event = 'default')
{
$sms = \app\common\model\Sms::
where(['mobile' => $mobile, 'event' => $event])
->order('id', 'DESC')
->find();
Hook::listen('sms_get', $sms, null, true);
return $sms ? $sms : NULL;
}
/**
* 发送验证码
*
* @param int $mobile 手机号
* @param int $code 验证码,为空时将自动生成4位数字
* @param string $event 事件
* @return boolean
*/
public static function send($mobile, $code = NULL, $event = 'default')
{
$code = is_null($code) ? mt_rand(1000, 9999) : $code;
$time = time();
$ip = request()->ip();
$sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
$result = Hook::listen('sms_send', $sms, null, true);
if (!$result)
{
$sms->delete();
return FALSE;
}
return TRUE;
}
/**
* 发送通知
*
* @param mixed $mobile 手机号,多个以,分隔
* @param string $msg 消息内容
* @param string $template 消息模板
* @return boolean
*/
public static function notice($mobile, $msg = '', $template = NULL)
{
$params = [
'mobile' => $mobile,
'msg' => $msg,
'template' => $template
];
$result = Hook::listen('sms_notice', $params, null, true);
return $result ? TRUE : FALSE;
}
/**
* 校验验证码
*
* @param int $mobile 手机号
* @param int $code 验证码
* @param string $event 事件
* @return boolean
*/
public static function check($mobile, $code, $event = 'default')
{
$time = time() - self::$expire;
$sms = \app\common\model\Sms::where(['mobile' => $mobile, 'event' => $event])
->order('id', 'DESC')
->find();
if ($sms)
{
if ($sms['createtime'] > $time && $sms['times'] <= self::$maxCheckNums)
{
$correct = $code == $sms['code'];
if (!$correct)
{
$sms->times = $sms->times + 1;
$sms->save();
return FALSE;
}
else
{
$result = Hook::listen('sms_check', $sms, null, true);
return $result;
}
}
else
{
// 过期则清空该手机验证码
self::flush($mobile, $event);
return FALSE;
}
}
else
{
return FALSE;
}
}
/**
* 清空指定手机号验证码
*
* @param int $mobile 手机号
* @param string $event 事件
* @return boolean
*/
public static function flush($mobile, $event = 'default')
{
\app\common\model\Sms::
where(['mobile' => $mobile, 'event' => $event])
->delete();
Hook::listen('sms_flush');
return TRUE;
}
}