PhoneMessage.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace common\util;
  3. use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
  4. use Aliyun\Core\Config;
  5. use Aliyun\Core\DefaultAcsClient;
  6. use Aliyun\Core\Profile\DefaultProfile;
  7. use common\models\LwtPhoneMessageList;
  8. class PhoneMessage
  9. {
  10. public static $phoneList = [15106211853];
  11. public static function saveMsg($company_name, $code)
  12. {
  13. $LwtPhoneMessage = new LwtPhoneMessageList();
  14. $LwtPhoneMessage->company_name = $company_name;
  15. $LwtPhoneMessage->code = $code;
  16. $LwtPhoneMessage->is_send = 0;
  17. $LwtPhoneMessage->create_time = time();
  18. $LwtPhoneMessage->update_time = time();
  19. $LwtPhoneMessage->save();
  20. }
  21. public static function sendNoticeMsg($to, $name, $status)
  22. {
  23. $templateParam = [
  24. "name" => $name,
  25. "status" => $status,
  26. ];
  27. $template_code = "SMS_273680618";
  28. return self::sendAliyunMsg($to, $templateParam, $template_code);
  29. }
  30. public static function sendVerifyCode($to, $code)
  31. {
  32. $templateParam = [
  33. "code" => $code
  34. ];
  35. $template_code = "SMS_271540041";
  36. return self::sendAliyunMsg($to, $templateParam, $template_code);
  37. }
  38. /**
  39. * 发送阿里云短信
  40. * @param $to
  41. * @param $templateParam
  42. * @param $template_code
  43. * @return mixed
  44. */
  45. protected static function sendAliyunMsg($to, $templateParam, $template_code)
  46. {
  47. require_once __DIR__ . '/../thirdparty/aliyun/vendor/autoload.php';
  48. $accessKeyId = 'LTAI5tDemxqCH7dpgTwWBPGt'; //创建的accessKey
  49. $accessKeySecret = '0IDsiApCaU4GU1Myp2eXBfTAFT4krO'; //创建的accessKeySecret
  50. $sign_name = '郅安科技';
  51. Config::load();//加载区域结点配置
  52. //短信API产品名(短信产品名固定,无需修改)
  53. $product = "Dysmsapi";
  54. //短信API产品域名(接口地址固定,无需修改)
  55. $domain = "dysmsapi.aliyuncs.com";
  56. //暂时不支持多Region(目前仅支持cn-hangzhou请勿修改)
  57. $region = "cn-hangzhou";
  58. // 初始化用户Profile实例
  59. $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
  60. // 增加服务结点
  61. DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
  62. // 初始化AcsClient用于发起请求
  63. $acsClient = new DefaultAcsClient($profile);
  64. // 初始化SendSmsRequest实例用于设置发送短信的参数
  65. $request = new SendSmsRequest();
  66. // 必填,设置雉短信接收号码
  67. $request->setPhoneNumbers($to);
  68. // 必填,设置签名名称
  69. $request->setSignName($sign_name);
  70. // 必填,设置模板CODE
  71. $request->setTemplateCode($template_code);
  72. // 可选,设置模板参数
  73. if ($templateParam) {
  74. $request->setTemplateParam(json_encode($templateParam)); //由于我的模板里需要传递我的短信验证码
  75. }
  76. //发起访问请求
  77. $acsResponse = $acsClient->getAcsResponse($request);
  78. //返回请求结果
  79. $result = json_decode(json_encode($acsResponse), true);
  80. // 具体返回值参考文档:https://help.aliyun.com/document_detail/55451.html?spm=a2c4g.11186623.6.563.YSe8FK
  81. return $result;
  82. }
  83. }