[TOC] #### 1.引入 composr 包 ---- ``` composer require flc/dysms ``` #### 2. 阿里云短信服務驗證碼封裝類 v1.0.1 --- ```php <?php // 本文件放入TP6.0的extend目錄下 extend/Dysms.php use Flc\Dysms\Client; use Flc\Dysms\Request\SendSms; // 1.設置配置參數(shù) Dysms::setConfig([ 'signName' => 'it社區(qū)', 'templateCode' => 'SMS_17929xxxx', 'accessKeyId' => 'LTAI4GJ6iaE7ij5YiGRgxxxx', 'accessKeySecret' => 'uljdxDoi8ocXNscPriqwl5xxxxx', ]); // 2.查看配置參數(shù) //var_dump(Dysms::showConfig()); // 3.發(fā)送驗證碼 // $arr['result'] true 發(fā)送成功 msg 提示信息 verCode 發(fā)送的短信驗證碼 // ['result' => true, 'msg' => '發(fā)送成功', 'verCode' => 370449] // $arr['result'] false 發(fā)送失敗 msg 錯誤信息 code 錯誤碼 // ['result' => false, 'msg' => '錯誤信息', 'code' => 'isv.BUSINESS_LIMIT_CONTROL'] $arr = Dysms::sendSms(1503784xxxx); var_dump($arr); /** * 阿里大于短信驗證碼封裝 * composer require flc/dysms * * @author liang <23426945@qq.com> * @version 1.0.1 * @homepage beautifulforever.com.cn 辰風沐陽 * @datetime 2020-08-04 last modify time * * 阿里云短信服務默認流控 * 同一個簽名同一個手機號短信驗證碼 1條/分鐘 */ class Dysms { private static $accessKeyId; private static $accessKeySecret; private static $signName; private static $templateCode; /** * 私有化構造方法 * 禁止類在外部被實例化 */ private function __construct(){} /** * 設定配置參數(shù) */ public static function setConfig($config) { // accessKeyId self::$accessKeyId = $config['accessKeyId']; // accessKeySecret self::$accessKeySecret = $config['accessKeySecret']; // 短信簽名 self::$signName = $config['signName']; // 短信模板ID self::$templateCode = $config['templateCode']; } /** * 查看配置參數(shù) * Dysms::showConfig() */ public static function showConfig() { return [ 'accessKeyId' => self::$accessKeyId, 'accessKeySecret' => self::$accessKeySecret, 'signName' => self::$signName, 'templateCode' => self::$templateCode, ]; } /** * 傳入手機號發(fā)送短信驗證碼 * @param int $phoneNumbers 手機號 * @return array ['result' => true, 'msg' => '發(fā)送成功', 'verCode' => 370449] */ public static function sendSms($phoneNumbers) { $client = new Client([ 'accessKeyId' => self::$accessKeyId, 'accessKeySecret' => self::$accessKeySecret, ]); $sendSms = new SendSms; $verCode = mt_rand(100000, 999999); $sendSms->setPhoneNumbers($phoneNumbers); $sendSms->setSignName(self::$signName); $sendSms->setTemplateCode(self::$templateCode); $sendSms->setTemplateParam(['code' => $verCode]); $sendSms->setOutId('demo'); // 返回標準類對象 發(fā)送失敗 // object(stdClass)#59 (3) { // ["Message"]=> // string(30) "觸發(fā)小時級流控Permits:5" // ["RequestId"]=> // string(36) "B76061EE-2D9A-4E46-89B9-2418E8A5555E" // ["Code"]=> // string(26) "isv.BUSINESS_LIMIT_CONTROL" // } $result = $client->execute($sendSms); // 返回結果 // array(2) { // ["result"]=> // bool(true) // ["msg"]=> // string(12) "發(fā)送成功" // } // array(3) { // ["result"]=> // bool(false) // ["code"]=> // string(26) "isv.BUSINESS_LIMIT_CONTROL" // ["msg"]=> // string(30) "觸發(fā)小時級流控Permits:5" // } if ($result->Code === 'OK') { return [ 'result' => true, 'msg' => '發(fā)送成功', 'verCode' => $verCode ]; } else { return [ 'result' => false, 'code' => $result->Code, 'msg' => self::errorMsg($result->Code, $result->Message) ]; } } /** * 錯誤信息code轉換中文提示 */ private static function errorMsg($code, $msg) { $errorMsg = [ 'isv.ACCOUNT_NOT_EXISTS' => '使用了錯誤的賬戶名稱或AK', 'isv.ACCOUNT_ABNORMAL' => '賬戶異常,請確認賬號信息', 'isv.MOBILE_NUMBER_ILLEGAL' => '手機號碼格式錯誤', 'isv.SMS_TEMPLATE_ILLEGAL' => '短信模板不存在,或未經(jīng)審核通過', 'isv.SMS_SIGNATURE_ILLEGAL' => '短信簽名不存在,或未經(jīng)審核通過', 'isv.BUSINESS_LIMIT_CONTROL' => '短信發(fā)送頻率超限', 'isv.AMOUNT_NOT_ENOUGH' => '當前賬戶余額不足,請及時充值', 'SignatureDoesNotMatch' => '簽名加密錯誤,請注意accessKeyId和accessKeySecret字符串賦值正確', ]; return $errorMsg[$code] ?? $msg; } /** * 私有化克隆方法 * 禁止類的實例在外部被克隆 */ private function __clone(){} } ``` #### 3. 阿里云短信服務驗證碼封裝函數(shù) --- 將發(fā)送短信驗證碼封裝為類感覺復雜化了,我又整理成了一個函數(shù)如下所示: 當驗證碼發(fā)送失敗時,記錄發(fā)送失敗的錯誤信息日志,下面是我在 TP5.0 中記錄日志的寫法 ```php /** * 發(fā)送阿里云短信驗證碼 * @param $mobile 手機號 * @param $code 短信驗證碼 * @link https://www.aliyun.com/product/sms * @link https://packagist.org/packages/flc/dysms * @describe 基于 composer require flc/dysms 依賴包 */ function send_ali_sms($mobile, $code) { $config = [ 'signName' => '', 'templateCode' => '', 'accessKeyId' => '', 'accessKeySecret' => '', ]; $client = new \Flc\Dysms\Client([ 'accessKeyId' => $config['accessKeyId'], 'accessKeySecret' => $config['accessKeySecret'], ]); $sendSms = new \Flc\Dysms\Request\SendSms; $sendSms->setPhoneNumbers($mobile); $sendSms->setSignName($config['signName']); $sendSms->setTemplateCode($config['templateCode']); $sendSms->setTemplateParam(['code' => $code]); $sendSms->setOutId('demo'); $result = $client->execute($sendSms); if ($result->Code === 'OK') { return true; } else { // 錯誤信息 $error = [ 'isv.ACCOUNT_NOT_EXISTS' => '使用了錯誤的賬戶名稱或AK', 'isv.ACCOUNT_ABNORMAL' => '賬戶異常,請確認賬號信息', 'isv.MOBILE_NUMBER_ILLEGAL' => '手機號碼格式錯誤', 'isv.SMS_TEMPLATE_ILLEGAL' => '短信模板不存在,或未經(jīng)審核通過', 'isv.SMS_SIGNATURE_ILLEGAL' => '短信簽名不存在,或未經(jīng)審核通過', 'isv.BUSINESS_LIMIT_CONTROL' => '短信發(fā)送頻率超限', 'isv.AMOUNT_NOT_ENOUGH' => '當前賬戶余額不足,請及時充值', 'SignatureDoesNotMatch' => '簽名加密錯誤,請注意accessKeyId和accessKeySecret字符串賦值正確', ]; $tips = '阿里云短信驗證碼發(fā)送失敗'; $reason = $error[$result->Code] ?? $result->Message; $response = get_object_vars($result); // 標準類對象轉為數(shù)組 $record = compact('tips', 'mobile', 'code', 'reason', 'response'); \think\Log::write(json_encode($record, JSON_UNESCAPED_UNICODE), 'sms'); return false; } } ```