~~~
/**
* 發送驗證碼
*
* @param string $mobile 手機號
* @param string $event 事件名稱
*/
public function send()
{
$mobile = $this->request->post("mobile");
$event = $this->request->post("event");
$event = $event ? $event : 'register';
if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('手機號不正確'));
}
$last = Smslib::get($mobile, $event);
if ($last && time() - $last['createtime'] < 60) {
$this->error(__('發送頻繁'));
}
$ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
if ($ipSendTotal >= 5) {
$this->error(__('發送頻繁'));
}
if ($event) {
$userinfo = User::getByMobile($mobile);
if ($event == 'register' && $userinfo) {
//已被注冊
$this->error(__('已被注冊'));
} elseif (in_array($event, ['changemobile']) && $userinfo) {
//被占用
$this->error(__('已被占用'));
} elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
//未注冊
$this->error(__('未注冊'));
}
}
// if (!Hook::get('sms_send')) {
// $this->error(__('請在后臺插件管理安裝短信驗證插件'));
// }
$ret = Smslib::send($mobile, null, $event);
if ($ret) {
$this->success(__('發送成功'));
} else {
$this->error(__('發送失敗,請檢查短信配置是否正確'));
}
}
~~~