ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>[danger] 阿里云短信服务扩展封装类 ``` composer require flc/dysms ``` ```php <?php // 本文件放入TP6.0的extend目录下 extend/Dysms.php use Flc\Dysms\Client; use Flc\Dysms\Request\SendSms; // // 使用示例,返回数组 // Dysms::setConfig([ // 'signName' => 'it社区', // 'templateCode' => 'SMS_1792xxxxx', // 'accessKeyId' => 'LTAI4GJ6iaE7ij5YiGxxxxx', // 'accessKeySecret' => 'uljdxDoi8ocXNscPriqwl5xxxxx', // ]); // // $arr['result'] true 发送成功 // // $arr['result'] false 发送失败 msg 错误信息 // $arr = Dysms::sendSms(150378xxxx); /** * 阿里大于短信验证码封装 * composer require flc/dysms * * @author liang <23426945@qq.com> * @version 1.0.1 * @datetime 2020-07-20 * @homepage www.itqaq.com * * 阿里云短信服务默认流控 * 同一个签名同一个手机号短信验证码 1条/分钟 */ class Dysms { private static $obj; private static $config; private static $signName; private static $templateCode; /** * 私有化构造方法 * 禁止类在外部被实例化 */ private function __construct(){} /** * 设定配置参数 */ public static function setConfig($config) { // accessKey、accesssecret self::$config = [ 'accessKeyId' => $config['accessKeyId'], 'accessKeySecret' => $config['accessKeySecret'], ]; // 短信签名 self::$signName = $config['signName']; // 短信模板ID self::$templateCode = $config['templateCode']; } /** * 传入手机号发送短信验证码 * @param int $phoneNumbers 手机号 * @return array ['result' => true, 'msg' => '发送成功', 'verCode' => 370449] */ public static function sendSms($phoneNumbers) { $client = new Client(self::$config); $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'); // 返回标准类对象 发送失败 // object(stdClass)#59 (3) { // ["Message"]=> // string(30) "触发小时级流控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) "发送成功" // } // array(3) { // ["result"]=> // bool(false) // ["code"]=> // string(26) "isv.BUSINESS_LIMIT_CONTROL" // ["msg"]=> // string(30) "触发小时级流控Permits:5" // } if ($result->Code === 'OK') { return ['result' => true, 'msg' => '发送成功', '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' => '短信模板不存在,或未经审核通过', 'isv.SMS_SIGNATURE_ILLEGAL' => '短信签名不存在,或未经审核通过', 'isv.BUSINESS_LIMIT_CONTROL' => '短信发送频率超限', 'isv.AMOUNT_NOT_ENOUGH' => '当前账户余额不足,请及时充值', 'SignatureDoesNotMatch' => '签名加密错误,请注意accessKeyId和accessKeySecret字符串赋值正确', ]; return $errorMsg[$code] ?? $msg; } /** * 私有化克隆方法 * 禁止类的实例在外部被克隆 */ private function __clone(){} } ```