Yihaotong.php 5.5 KB

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