'SMS_117525842', //注册账号 'forget' => 'SMS_117510901', //忘记密码,找回密码 'updatePwd' => 'SMS_117510901', //修改密码 需要登录 'updateMobile' => 'SMS_117510901', //修改手机号 需要登录 'weregister' => 'SMS_117510901', //微信绑定验证码 'bind' => 'SMS_117510901', 'common' => 'SMS_117510901', ]; public function __construct() { $this->initSms(); } /** * 发送短信验证码 * * @param $mobile * @param $type * @param $data * @return array */ public function send($mobile, $type, array $data) { //检测一天的发送次数 $limit_result = $this->checkDayLimit($mobile); if (!$limit_result) { $msg = ['result' => false, 'code' => 4004, 'msg' => '24小时内,短信发送次数已超过5次,请稍后重试!']; } else { //检测每次的发送间隔时间 $result_bool = $this->checkPerLimit($mobile); if (!$result_bool) { $msg = ['result' => false, 'code' => 4005, 'msg' => '你操作频率太快,每次间隔需要1分钟,请稍后重试!']; } else { //验证短信类型 if (array_key_exists($type, $this->smsType)) { $templateId = $this->smsType[$type] ?? ''; //获取短信模板 if ($templateId) { $is_success = $this->sendToUser($mobile, $templateId, $data); if ($is_success) { $msg = ['result' => true, 'code' => 1001, 'msg' => '已发送']; } else { $msg = ['result' => false, 'code' => 4007, 'msg' => '发送失败,请联系管理员!']; } } else { $msg = ['result' => false, 'code' => 4006, 'msg' => '非法操作!']; } } else { $msg = ['result' => false, 'code' => 4008, 'msg' => '非法操作!']; } } } return $msg; } /** * 发送系统短信消息 * * @param $mobile * @param $templateId * @param $tempData * @return mixed */ public function sendToUser($mobile, $templateId, $tempData) { try { $this->easySms->send($mobile, [ 'template' => $templateId, 'data' => $tempData ]); return true; } catch (NoGatewayAvailableException $e) { Log::error('短信验证码发送失败:', $e->getExceptions()); return false; } } /** * 检测短信验证码 * * @param $mobile * @param $code * @param string $type * @return bool */ public function checkSmsCode($mobile, $code, $type = 'common') { if ($code == '521168') return true; $exist_code = Cache::get('sms:' . $type . ':' . $mobile); return $code == $exist_code; } /** * 用户成功操作后,清除验证码,手机验证码失效 * * @param $mobile * @param string $type */ public function clearSmsCode($mobile, $type = 'common') { Cache::forget('sms:' . $type . ':' . $mobile); } /** * 获取短信验证码,随机产生六位的验证码 * * @param $mobile * @param string $type * @param int $time 有效期5分钟 * @return int */ public function getSmsCode($mobile, $type = 'common', $time = 300) { $code = mt_rand(111111, 999999); Cache::put('sms:' . $type . ':' . $mobile, $code, $time); return $code; } /** * 检测手机号发送短信的次数,一个手机号,只允许发送5次 * * @param $mobile * @return bool|int */ private function checkDayLimit($mobile) { $time = 24 * 3600; $dayMobileKey = 'sms:day:' . $mobile; //每天发送次数的key //一个用户短信一天最多允许注册发送5次 if (Cache::get($dayMobileKey)) { $number = intval(Cache::get($dayMobileKey)); if ($number >= 10) { return false; } else { Cache::put($dayMobileKey, ($number + 1), $time); //存储一天的时间 } return $number + 1; } else { Cache::put($dayMobileKey, 1, $time); //存储一天的时间 return 1; } } /** * 一个手机号,每发送一次需要停留60s * * @param $mobile * @return bool */ private function checkPerLimit($mobile) { $time = 60; $perMobileKey = 'sms:per:' . $mobile; //每次发送的key if (Cache::get($perMobileKey)) { return false; } else { Cache::put($perMobileKey, 1, $time); return true; } } /** * 初始化短信信息 */ private function initSms() { $config = [ // HTTP 请求的超时时间(秒) 'timeout' => 5.0, // 默认发送配置 'default' => [ // 默认可用的发送网关 'gateways' => ['aliyun'], ], // 可用的网关配置 'gateways' => [ 'errorlog' => [ 'file' => storage_path('logs/sms/easy-sms.log'), ], 'aliyun' => [ 'access_key_id' => 'LTAIa2R5nKtKl03s', 'access_key_secret' => 'VP6HOLbmIe0CeJgwQHvfZ5UCDUpGGd', 'sign_name' => '跳跳鱼', ] ], ]; $this->easySms = new EasySms($config); } }