123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- /**
- * hook钩子触发短信发送
- */
- namespace app\api\hook;
- //阿里云短信发送相关类 composer安装
- //composer require alibabacloud/sdk
- //use Swoft\Task\Bean\Annotation\Task;
- use AlibabaCloud\Client\AlibabaCloud;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Exception\ServerException;
- class Sms
- {
- //发送验证码
- //@sms 验证码发送记录数数据库实例
- //$sms->mobile 手机号码
- //['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]
- function smsSend($sms)
- {
- return '11111';
- return $this->sendMsg($sms->mobile,['code'=>$sms->code],'code');
- }
- function smsCheck($sms)
- {
- return $this->sendMsg($sms['mobile'],['code'=>$sms['code']],'code');
- if($this->sendMsg($sms['mobile'],['code'=>$sms['code']],'code')){
- $sms = \app\common\model\Sms::create(['event' => $sms['event'], 'mobile' => $sms['mobile'], 'code' => $sms['code'], 'ip' => '', 'createtime' => $sms['createtime']]);
- }
- }
- //@mobile 手机号码
- //@param 短信发送参数
- //@template 短信发送模板编号key值
- private function sendMsg($mobile,$param,$template){
- $config = \config('alisms');
- // return json($config);
- if(empty($param)||!isset($config)||empty($config)){
- return false;
- }
- AlibabaCloud::accessKeyClient($config['accessKeyId'], $config['accessSecret'])
- ->regionId($config['regionId'])
- ->asGlobalClient();
- $TemplateCode = isset($config['TemplateCode'][$template])?$config['TemplateCode'][$template]:$config['TemplateCode']['code'];
- try {
- $result = AlibabaCloud::rpcRequest()
- ->product('Dysmsapi')
- ->version('2017-05-25')
- ->action('SendSms')
- ->method('POST')
- ->options([
- 'query' => [
- 'PhoneNumbers' => $mobile,
- 'SignName' => $config['SignName'],
- 'TemplateCode' => $TemplateCode,
- 'TemplateParam' => json_encode($param)
- ],
- ])
- ->request();
- if($result->Code=="OK"){
- return true;
- }else{
- return false;
- }
- print_r($result->toArray());
- } catch (ClientException $e) {
- return false;
- echo $e->getErrorMessage() . PHP_EOL;
- } catch (ServerException $e) {
- return false;
- echo $e->getErrorMessage() . PHP_EOL;
- }
- }
- }
|