Aliyun.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Aliyun.php UTF-8
  4. * 阿里云短信发送
  5. *
  6. * @date : 2018/1/29 10:38
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : linjiebin <ljb@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. * 短信发送API(SendSms)---PHP https://help.aliyun.com/document_detail/55451.html?spm=5176.10629532.106.2.1b471cbeRLbrO3
  12. */
  13. namespace huolib\sms\driver;
  14. use huolib\sms\SmsType;
  15. use huolib\status\MemberStatus;
  16. use think\Log;
  17. class Aliyun {
  18. protected $config
  19. = [
  20. 'REGIONID' => 'cn-hangzhou',
  21. 'APPKEY' => '',
  22. 'APPSECRET' => '',
  23. 'PRODUCT' => '',
  24. 'SIGNNAME' => '', /*签名名称*/
  25. 'SMSTEMPAUTH' => '', //身份验证验证码
  26. 'SMSTEMPTEST' => '', //短信测试
  27. 'SMSTEMPLOGIN' => '', //登录确认验证码
  28. 'SMSTEMPLOGINERROR' => '', //登录异常验证码
  29. 'SMSTEMPREG' => '', //用户注册验证码
  30. 'SMSTEMPACT' => '', //活动确认验证码
  31. 'SMSTEMPPWDMOD' => '', //修改密码验证码
  32. 'SMSTEMPINFOMOD' => '', //信息变更验证码
  33. ];
  34. public function __construct() {
  35. if (file_exists(GLOBAL_CONF_PATH."extra/sms/aliyun.php")) {
  36. $_config = include GLOBAL_CONF_PATH."extra/sms/aliyun.php";
  37. } else {
  38. $_config = array();
  39. }
  40. $this->config = array_merge($this->config, $_config);
  41. }
  42. /**
  43. * 标准返回
  44. *
  45. * @param $code
  46. *
  47. * @return mixed
  48. */
  49. private function getReturnByCode($code) {
  50. $_rdata['code'] = $code;
  51. $_rdata['msg'] = MemberStatus::getMsg($code);
  52. return $_rdata;
  53. }
  54. public function send($mobile, $type, $sms_code) {
  55. include_once EXTEND_PATH.'aliyun/aliyun-php-sdk-core/Config.php';
  56. include_once EXTEND_PATH.'aliyun/Dysmsapi/Request/V20170525/SendSmsRequest.php';
  57. include_once EXTEND_PATH.'aliyun/Dysmsapi/Request/V20170525/QuerySendDetailsRequest.php';
  58. if (empty($this->config)) {
  59. Log::write("func=".__FUNCTION__."&class=".__CLASS__." config error", LOG::ERROR);
  60. return MemberStatus::CONFIG_ERROR;
  61. }
  62. $_profile = \DefaultProfile::getProfile(
  63. $this->config['REGIONID'], $this->config['APPKEY'], $this->config['APPSECRET']
  64. );
  65. switch ($type) {
  66. case SmsType::SMS_REG:
  67. {
  68. $_template_code = $this->config['SMSTEMPREG'];
  69. break;
  70. }
  71. case SmsType::SMS_LOGIN:
  72. {
  73. $_template_code = $this->config['SMSTEMPLOGIN'];
  74. break;
  75. }
  76. case SmsType::SMS_MODIFY:
  77. {
  78. $_template_code = $this->config['SMSTEMPPWDMOD'];
  79. break;
  80. }
  81. case SmsType::SMS_ID_VERIFY:
  82. {
  83. $_template_code = $this->config['SMSTEMPAUTH'];
  84. break;
  85. }
  86. case SmsType::SMS_FIND_PWD:
  87. {
  88. $_template_code = $this->config['SMSTEMPINFOMOD'];
  89. break;
  90. }
  91. case SmsType::SMS_BIND:
  92. {
  93. $_template_code = $this->config['SMSTEMPAUTH'];
  94. break;
  95. }
  96. case SmsType::SMS_OTHER:
  97. {
  98. $_template_code = $this->config['SMSTEMPACT'];
  99. break;
  100. }
  101. default:
  102. {
  103. $_template_code = $this->config['SMSTEMPAUTH'];
  104. break;
  105. }
  106. }
  107. \DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
  108. $_acs_client = new \DefaultAcsClient($_profile);
  109. $_request = new \Dysmsapi\Request\V20170525\SendSmsRequest;
  110. //必填-短信接收号码
  111. $_request->setPhoneNumbers($mobile);
  112. //必填-短信签名
  113. $_request->setSignName($this->config['SIGNNAME']);
  114. //必填-短信模板Code
  115. $_request->setTemplateCode($this->config['SMSTEMPAUTH']);
  116. //选填-假如模板中存在变量需要替换则为必填(JSON格式)
  117. $_content = array(
  118. "code" => ''.$sms_code
  119. );
  120. $_request->setTemplateParam(json_encode($_content));
  121. //选填-发送短信流水号
  122. $_request->setOutId("1234");
  123. //发起访问请求
  124. $_acsResponse = $_acs_client->getAcsResponse($_request);
  125. try {
  126. if ('OK' == $_acsResponse->Code) {
  127. return MemberStatus::NO_ERROR;
  128. } else {
  129. Log::write(
  130. "func=".__FUNCTION__."&class=".__CLASS__."&mobile=".$mobile."&type=".$type."&code="
  131. .$_acsResponse->Code
  132. ."&msg=".$_acsResponse->Code, LOG::ERROR
  133. );
  134. if ('isv.BUSINESS_LIMIT_CONTROL' == $_acsResponse->Code) {
  135. return MemberStatus::PHONE_SEND_MORE;
  136. }
  137. return MemberStatus::PHONE_SEND_ERROR;
  138. }
  139. } catch (\ClientException $e) {
  140. Log::write(
  141. "func=".__FUNCTION__."&class=".__CLASS__."&code=".MemberStatus::PHONE_SEND_ERROR
  142. ." ClientException error ".$e->getMessage(), LOG::ERROR
  143. );
  144. return MemberStatus::PHONE_SEND_ERROR;
  145. } catch (\ServerException $e) {
  146. Log::write(
  147. "func=".__FUNCTION__."&class=".__CLASS__."&code=".MemberStatus::PHONE_SEND_ERROR
  148. ." ServerException error ".$e->getMessage(), LOG::ERROR
  149. );
  150. return MemberStatus::PHONE_SEND_ERROR;
  151. }
  152. }
  153. }