Yunxin.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace ln\services\sms\storage;
  3. use ln\services\BaseSmss;
  4. use think\exception\ValidateException;
  5. use think\facade\Config;
  6. /**
  7. * Class Yunxin
  8. * @package ln\services\sms\storage
  9. */
  10. class Yunxin extends BaseSmss
  11. {
  12. /**
  13. * 开通
  14. */
  15. const SMS_OPEN = 'sms_v2/open';
  16. /**
  17. * 修改签名
  18. */
  19. const SMS_MODIFY = 'sms_v2/modify';
  20. /**
  21. * 用户信息
  22. */
  23. const SMS_INFO = 'sms_v2/info';
  24. /**
  25. * 发送短信
  26. */
  27. const SMS_SEND = 'sms_v2/send';
  28. /**
  29. * 短信模板
  30. */
  31. const SMS_TEMPS = 'sms_v2/temps';
  32. /**
  33. * 申请模板
  34. */
  35. const SMS_APPLY = 'sms_v2/apply';
  36. /**
  37. * 模板记录
  38. */
  39. const SMS_APPLYS = 'sms_v2/applys';
  40. /**
  41. * 发送记录
  42. */
  43. const SMS_RECORD = 'sms_v2/record';
  44. /**
  45. * 获取短信发送状态
  46. */
  47. const SMS_STSTUS = 'sms/status';
  48. /**
  49. * 短信签名
  50. * @var string
  51. */
  52. protected $sign = '';
  53. /**
  54. * 模板id
  55. * @var array
  56. */
  57. protected $templateIds = [];
  58. /** 初始化
  59. * @param array $config
  60. */
  61. protected function initialize(array $config = [])
  62. {
  63. parent::initialize($config);
  64. $this->templateIds = Config::get($this->configFile . '.stores.' . $this->name . '.template_id', []);
  65. }
  66. /**
  67. * 提取模板code
  68. * @param string $templateId
  69. * @return null
  70. */
  71. protected function getTemplateCode(string $templateId)
  72. {
  73. return $this->templateIds[$templateId] ?? null;
  74. }
  75. /**
  76. * 设置签名
  77. * @param $sign
  78. * @return $this
  79. */
  80. public function setSign($sign)
  81. {
  82. $this->sign = $sign;
  83. return $this;
  84. }
  85. /**
  86. * 获取验证码
  87. * @param string $phone
  88. * @return array|mixed
  89. */
  90. public function captcha(string $phone)
  91. {
  92. $params = [
  93. 'phone' => $phone
  94. ];
  95. return $this->accessToken->httpRequest('sms/captcha', $params, 'GET', false);
  96. }
  97. /**
  98. * 开通服务
  99. * @return array|bool|mixed
  100. */
  101. public function open()
  102. {
  103. $param = [
  104. 'sign' => $this->sign
  105. ];
  106. return $this->accessToken->httpRequest(self::SMS_OPEN, $param);
  107. }
  108. /**
  109. * 修改签名
  110. * @param string $sign
  111. * @return array|bool|mixed
  112. */
  113. public function modify(string $sign = null, string $phone, string $code)
  114. {
  115. $param = [
  116. 'sign' => $sign ?: $this->sign,
  117. 'verify_code' => $code,
  118. 'phone' => $phone
  119. ];
  120. return $this->accessToken->httpRequest(self::SMS_MODIFY, $param);
  121. }
  122. /**
  123. * 获取用户信息
  124. * @return array|bool|mixed
  125. */
  126. public function info()
  127. {
  128. return $this->accessToken->httpRequest(self::SMS_INFO, []);
  129. }
  130. /**
  131. * 获取短信模板
  132. * @param int $page
  133. * @param int $limit
  134. * @param int $type
  135. * @return array|mixed
  136. */
  137. public function temps(int $page = 0, int $limit = 10, int $type = 1)
  138. {
  139. $param = [
  140. 'page' => $page,
  141. 'limit' => $limit,
  142. 'temp_type' => $type
  143. ];
  144. return $this->accessToken->httpRequest(self::SMS_TEMPS, $param);
  145. }
  146. /**
  147. * 申请模版
  148. * @param $title
  149. * @param $content
  150. * @param $type
  151. * @return array|bool|mixed
  152. */
  153. public function apply(string $title, string $content, int $type)
  154. {
  155. $param = [
  156. 'title' => $title,
  157. 'content' => $content,
  158. 'type' => $type
  159. ];
  160. return $this->accessToken->httpRequest(self::SMS_APPLY, $param);
  161. }
  162. /**
  163. * 申请记录
  164. * @param $temp_type
  165. * @param int $page
  166. * @param int $limit
  167. * @return array|bool|mixed
  168. */
  169. public function applys(int $tempType, int $page, int $limit)
  170. {
  171. $param = [
  172. 'temp_type' => $tempType,
  173. 'page' => $page,
  174. 'limit' => $limit
  175. ];
  176. return $this->accessToken->httpRequest(self::SMS_APPLYS, $param);
  177. }
  178. /**
  179. * 发送短信
  180. * @param $phone
  181. * @param $template
  182. * @param $param
  183. * @return bool|string
  184. */
  185. public function send(string $phone, string $templateId, array $data = [])
  186. {
  187. if (!$phone) {
  188. throw new ValidateException('手机号不能为空');
  189. }
  190. $param = [
  191. 'phone' => $phone
  192. ];
  193. $param['temp_id'] = $templateId;
  194. if (is_null($param['temp_id'])) {
  195. throw new ValidateException('模版ID不存在');
  196. }
  197. $param['param'] = json_encode($data);
  198. return $this->accessToken->httpRequest(self::SMS_SEND, $param);
  199. }
  200. /**
  201. * 发送记录
  202. * @param $record_id
  203. * @return array|bool|mixed
  204. */
  205. public function record($record_id)
  206. {
  207. $param = [
  208. 'record_id' => $record_id
  209. ];
  210. return $this->accessToken->httpRequest(self::SMS_RECORD, $param);
  211. }
  212. /**
  213. * 获取发送状态
  214. * @param array $recordIds
  215. * @return array|mixed
  216. */
  217. public function getStatus(array $recordIds)
  218. {
  219. $data['record_id'] = json_encode($recordIds);
  220. return $this->accessToken->httpRequest(self::SMS_STSTUS, $data, 'POST', false);
  221. }
  222. }