Sms.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * hook钩子触发短信发送
  4. */
  5. namespace app\api\hook;
  6. //阿里云短信发送相关类 composer安装
  7. //composer require alibabacloud/sdk
  8. //use Swoft\Task\Bean\Annotation\Task;
  9. use AlibabaCloud\Client\AlibabaCloud;
  10. use AlibabaCloud\Client\Exception\ClientException;
  11. use AlibabaCloud\Client\Exception\ServerException;
  12. class Sms
  13. {
  14. //发送验证码
  15. //@sms 验证码发送记录数数据库实例
  16. //$sms->mobile 手机号码
  17. //['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]
  18. function smsSend($sms)
  19. {
  20. return '11111';
  21. return $this->sendMsg($sms->mobile,['code'=>$sms->code],'code');
  22. }
  23. function smsCheck($sms)
  24. {
  25. return $this->sendMsg($sms['mobile'],['code'=>$sms['code']],'code');
  26. if($this->sendMsg($sms['mobile'],['code'=>$sms['code']],'code')){
  27. $sms = \app\common\model\Sms::create(['event' => $sms['event'], 'mobile' => $sms['mobile'], 'code' => $sms['code'], 'ip' => '', 'createtime' => $sms['createtime']]);
  28. }
  29. }
  30. //@mobile 手机号码
  31. //@param 短信发送参数
  32. //@template 短信发送模板编号key值
  33. private function sendMsg($mobile,$param,$template){
  34. $config = \config('alisms');
  35. // return json($config);
  36. if(empty($param)||!isset($config)||empty($config)){
  37. return false;
  38. }
  39. AlibabaCloud::accessKeyClient($config['accessKeyId'], $config['accessSecret'])
  40. ->regionId($config['regionId'])
  41. ->asGlobalClient();
  42. $TemplateCode = isset($config['TemplateCode'][$template])?$config['TemplateCode'][$template]:$config['TemplateCode']['code'];
  43. try {
  44. $result = AlibabaCloud::rpcRequest()
  45. ->product('Dysmsapi')
  46. ->version('2017-05-25')
  47. ->action('SendSms')
  48. ->method('POST')
  49. ->options([
  50. 'query' => [
  51. 'PhoneNumbers' => $mobile,
  52. 'SignName' => $config['SignName'],
  53. 'TemplateCode' => $TemplateCode,
  54. 'TemplateParam' => json_encode($param)
  55. ],
  56. ])
  57. ->request();
  58. if($result->Code=="OK"){
  59. return true;
  60. }else{
  61. return false;
  62. }
  63. print_r($result->toArray());
  64. } catch (ClientException $e) {
  65. return false;
  66. echo $e->getErrorMessage() . PHP_EOL;
  67. } catch (ServerException $e) {
  68. return false;
  69. echo $e->getErrorMessage() . PHP_EOL;
  70. }
  71. }
  72. }