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