最新需要用到發送短信的功能,所以就在網上搜索一些寫好的擴展。
擴展地址:
https://github.com/MissMyCat/aliyun-sms
通過composer安裝:
composer require mrgoon/aliyun-sms dev-master
在 config/app.php 中 providers 加入:
Mrgoon\AliSms\ServiceProvider::class,
有需求的可以自行添加 aliases。
然后在控制台運行 :
php artisan vendor:publish
默認會在 config 目錄下創建一個 aliyunsms.php 文件:
<?php return [ 'access_key' => env('ALIYUN_SMS_AK'), // accessKey 'access_secret' => env('ALIYUN_SMS_AS'), // accessSecret 'sign_name' => env('ALIYUN_SMS_SIGN_NAME'), // 簽名 ];
然后在 .env 中配置相應參數:
ALIYUN_SMS_AK= ALIYUN_SMS_AS= ALIYUN_SMS_SIGN_NAME=
為了能夠方便的發送短信,我們可以在 app 目錄下,創建一個Services目錄,並添加 AliyunSms.php 文件。
<?php namespace App\Services; use Mrgoon\AliSms\AliSms; /** * 阿里雲短信類 */ class AliyunSms { //驗證碼 const VERIFICATION_CODE = 'verification_code'; //模板CODE public static $templateCodes = [ self::VERIFICATION_CODE => 'SMS_XXXXXXXXXX', ]; /** * 發送短信 */ public static function sendSms($mobile, $scene, $params = []) { if (empty($mobile)) { throw new \Exception('手機號不能為空'); } if (empty($scene)) { throw new \Exception('場景不能為空'); } if (!isset(self::$templateCodes[$scene])) { throw new \Exception('請配置場景的模板CODE'); } $template_code = self::$templateCodes[$scene]; try { $ali_sms = new AliSms(); $response = $ali_sms->sendSms($mobile, $template_code, $params); if ($response->Code == 'OK') { return true; } throw new \Exception($response->Message); } catch (\Throwable $e) { throw new \Exception($e->getMessage()); } } }
為了能夠記錄每次短信發送的狀態,我們可以創建一個 sms_logs 表。
CREATE TABLE `sms_logs` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '類型(0:短信驗證碼,1:語音驗證碼,2:短信消息通知)', `mobile` varchar(16) NOT NULL DEFAULT '' COMMENT '手機號', `code` varchar(12) NOT NULL DEFAULT '' COMMENT '驗證碼', `checked` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否驗證(0:未驗證,1:已驗證)', `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '狀態(0:未發送,1:已發送,2:發送失敗)', `reason` varchar(255) NOT NULL DEFAULT '' COMMENT '失敗原因', `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '備注', `operator_id` int(11) NOT NULL DEFAULT '0' COMMENT '操作人ID', `ip` varchar(16) NOT NULL DEFAULT '' COMMENT '操作IP', `created` int(11) NOT NULL DEFAULT '0' COMMENT '創建時間', `updated` int(11) NOT NULL DEFAULT '0' COMMENT '更新時間', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='短信表';
然后針對該表,我們創建一個 SmsLog 模型來管理。
<?php namespace App\Models; use App\Services\AliyunSms; class SmsLog extends Model { protected $fillable = [ 'type', 'mobile', 'code', 'checked', 'status', 'reason', 'remark', 'operator_id', 'ip', ]; //類型(0:短信驗證碼,1:語音驗證碼,2:短信消息通知) const TYPE_CODE = 0; const TYPE_VOICE = 1; const TYPE_MESSAGE = 2; //是否驗證(0:未驗證,1:已驗證) const CHECKED_UNVERIFIED = 0; const CHECKED_VERIFIED = 1; //狀態(0:未發送,1:已發送,2:發送失敗) const STATUS_NO_SEND = 0; const STATUS_SEND = 1; const STATUS_FAIL = 2; //短信發送間隔時間,默認60秒 const SEND_INTERVAL_TIME = 60; /** * 檢測短信驗證碼 */ protected function checkCode($mobile, $code) { if (!$mobile) { throw new \Exception('手機號不能為空'); } if (!checkMobile($mobile)) { throw new \Exception('手機號不正確'); } if (!$code) { throw new \Exception('驗證碼不能為空'); } $sms_log = $this->where([ ['type', self::TYPE_CODE], ['mobile', $mobile], ['status', self::STATUS_SEND], ['checked', self::CHECKED_UNVERIFIED], ])->orderBy('created', 'desc')->first(); if (!$sms_log) { throw new \Exception('驗證碼不存在,請重新獲取'); } if ($code != $sms_log->code) { throw new \Exception('驗證碼錯誤'); } $sms_log->checked = self::CHECKED_VERIFIED; $sms_log->save(); return true; } /** * 檢測短信頻率 */ protected function checkRate($mobile) { if (!$mobile) { throw new \Exception('手機號不能為空'); } $sms_log = $this->where([ ['mobile', $mobile], ['status', self::STATUS_SEND], ])->orderBy('created', 'desc')->first(); $now = time(); if ($sms_log) { if (($now - strtotime($sms_log->created)) < self::SEND_INTERVAL_TIME) { throw new \Exception('短信發送太頻繁,請稍后再試'); } } return true; } /** * 發送短信驗證碼 */ protected function sendVerifyCode($mobile) { self::checkRate($mobile); $code = mt_rand(1000, 9999); $sms_log = $this->create([ 'type' => self::TYPE_CODE, 'mobile' => $mobile, 'code' => $code, 'checked' => self::CHECKED_UNVERIFIED, 'status' => self::STATUS_NO_SEND, 'ip' => getRealIp(), ]); try { AliyunSms::sendSms($mobile, AliyunSms::VERIFICATION_CODE, ['code' => $code]); $sms_log->status = self::STATUS_SEND; $sms_log->save(); return true; } catch (\Exception $e) { $sms_log->status = self::STATUS_FAIL; $sms_log->reason = $e->getMessage(); $sms_log->save(); throw new \Exception($e->getMessage()); } } }
這樣,我們就可以在項目中通過 SmsLog::sendVerifyCode() 發送短信了。
getRealIp() 和 checkMobile() 方法為公共方法,存放在 app/Helpers 的 functions.php 中。
/** * 獲取真實IP地址 */ function getRealIp() { $ip = false; if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) { $ip = getenv("HTTP_CLIENT_IP"); } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) { $ips = explode(", ", getenv("HTTP_X_FORWARDED_FOR")); if ($ip) { array_unshift($ips, $ip); $ip = false; } $ipscount = count($ips); for ($i = 0; $i < $ipscount; $i++) { if (!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i])) { $ip = $ips[$i]; break; } } } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) { $ip = getenv("REMOTE_ADDR"); } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) { $ip = $_SERVER['REMOTE_ADDR']; } else { $ip = "unknown"; } return isIp($ip) ? $ip : "unknown"; } /** * 檢查是否是合法的IP */ function isIp($ip) { if (preg_match('/^((\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)(?:\.(\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)){3})$/', $ip)) { return true; } else { return false; } } /** * 驗證手機號 */ function checkMobile($mobile) { return preg_match('/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/i', $mobile); }