Aliyun.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\library\sms\engine;
  13. use app\common\library\helper;
  14. use app\common\library\sms\package\aliyun\SignatureHelper;
  15. /**
  16. * 阿里云短信模块引擎
  17. * Class Aliyun
  18. * @package app\common\library\sms\engine
  19. */
  20. class Aliyun extends Server
  21. {
  22. private $config;
  23. /**
  24. * 构造方法
  25. * Qiniu constructor.
  26. * @param $config
  27. */
  28. public function __construct($config)
  29. {
  30. $this->config = $config;
  31. }
  32. /**
  33. * 发送短信通知
  34. * @param array $sceneConfig 场景配置
  35. * @param array $templateParams 短信模板参数
  36. * @return bool
  37. */
  38. public function sendSms(array $sceneConfig, array $templateParams)
  39. {
  40. $params = [];
  41. // *** 需用户填写部分 ***
  42. // 必填: 短信接收号码
  43. $params["PhoneNumbers"] = $sceneConfig['acceptPhone'];
  44. // 必填: 短信签名,应严格按"签名名称"填写,请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
  45. $params["SignName"] = $this->config['sign'];
  46. // 必填: 短信模板Code,应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
  47. $params["TemplateCode"] = $sceneConfig['templateCode'];
  48. // 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
  49. $params['TemplateParam'] = $templateParams;
  50. // 可选: 设置发送短信流水号
  51. // $params['OutId'] = "12345";
  52. // 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
  53. // $params['SmsUpExtendCode'] = "1234567";
  54. // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
  55. if (!empty($params['TemplateParam']) && is_array($params['TemplateParam'])) {
  56. $params['TemplateParam'] = helper::jsonEncode($params['TemplateParam']);
  57. }
  58. // 初始化SignatureHelper实例用于设置参数,签名以及发送请求
  59. $helper = new SignatureHelper;
  60. // 此处可能会抛出异常,注意catch
  61. $response = $helper->request(
  62. $this->config['AccessKeyId']
  63. , $this->config['AccessKeySecret']
  64. , "dysmsapi.aliyuncs.com"
  65. , array_merge($params, [
  66. "RegionId" => "cn-hangzhou",
  67. "Action" => "SendSms",
  68. "Version" => "2017-05-25",
  69. ])
  70. // 选填: 启用https
  71. , true
  72. );
  73. // 记录日志
  74. log_record([
  75. 'name' => '发送短信',
  76. 'config' => $this->config,
  77. 'params' => $params
  78. ]);
  79. log_record($response);
  80. $this->error = $response->Message;
  81. return $response->Code === 'OK';
  82. }
  83. }