Chuanglan.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 Chuanglan
  17. * @package crmeb\services\sms\storage
  18. */
  19. class Chuanglan 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. * 模板id
  64. * @var array
  65. */
  66. protected $templateIds = [];
  67. /** 初始化
  68. * @param array $config
  69. */
  70. protected function initialize(array $config = [])
  71. {
  72. parent::initialize($config);
  73. $this->templateIds = Config::get($this->configFile . '.stores.' . $this->name . '.template_id', []);
  74. }
  75. /**
  76. * 提取模板code
  77. * @param string $templateId
  78. * @return null
  79. */
  80. protected function getTemplateCode(string $templateId)
  81. {
  82. return $this->templateIds[$templateId] ?? null;
  83. }
  84. /**
  85. * 设置签名
  86. * @param $sign
  87. * @return $this
  88. */
  89. public function setSign($sign)
  90. {
  91. $this->sign = $sign;
  92. return $this;
  93. }
  94. /**
  95. * 获取验证码
  96. * @param string $phone
  97. * @return array|mixed
  98. */
  99. public function captcha(string $phone)
  100. {
  101. $params = [
  102. 'phone' => $phone
  103. ];
  104. return $this->accessToken->httpRequest('sms/captcha', $params, 'GET', false);
  105. }
  106. /**
  107. * 开通服务
  108. * @return array|bool|mixed
  109. */
  110. public function open()
  111. {
  112. $param = [
  113. 'sign' => $this->sign
  114. ];
  115. return $this->accessToken->httpRequest(self::SMS_OPEN, $param);
  116. }
  117. /**
  118. * 修改签名
  119. * @param string $sign
  120. * @return array|bool|mixed
  121. */
  122. public function modify(string $sign = null, string $phone, string $code)
  123. {
  124. $param = [
  125. 'sign' => $sign ?: $this->sign,
  126. 'verify_code' => $code,
  127. 'phone' => $phone
  128. ];
  129. return $this->accessToken->httpRequest(self::SMS_MODIFY, $param);
  130. }
  131. /**
  132. * 获取用户信息
  133. * @return array|bool|mixed
  134. */
  135. public function info()
  136. {
  137. return $this->accessToken->httpRequest(self::SMS_INFO, []);
  138. }
  139. /**
  140. * 获取短信模板
  141. * @param int $page
  142. * @param int $limit
  143. * @param int $type
  144. * @return array|mixed
  145. */
  146. public function temps(int $page = 0, int $limit = 10, int $type = 1)
  147. {
  148. $param = [
  149. 'page' => $page,
  150. 'limit' => $limit,
  151. 'temp_type' => $type
  152. ];
  153. return $this->accessToken->httpRequest(self::SMS_TEMPS, $param);
  154. }
  155. /**
  156. * 申请模版
  157. * @param $title
  158. * @param $content
  159. * @param $type
  160. * @return array|bool|mixed
  161. */
  162. public function apply(string $title, string $content, int $type)
  163. {
  164. $param = [
  165. 'title' => $title,
  166. 'content' => $content,
  167. 'type' => $type
  168. ];
  169. return $this->accessToken->httpRequest(self::SMS_APPLY, $param);
  170. }
  171. /**
  172. * 申请记录
  173. * @param $temp_type
  174. * @param int $page
  175. * @param int $limit
  176. * @return array|bool|mixed
  177. */
  178. public function applys(int $tempType, int $page, int $limit)
  179. {
  180. $param = [
  181. 'temp_type' => $tempType,
  182. 'page' => $page,
  183. 'limit' => $limit
  184. ];
  185. return $this->accessToken->httpRequest(self::SMS_APPLYS, $param);
  186. }
  187. /**
  188. * 发送短信
  189. * @param $phone
  190. * @param $template
  191. * @param $param
  192. * @return bool|string
  193. */
  194. public function send(string $phone, string $templateId, array $data = [])
  195. {
  196. if (!$phone) {
  197. throw new AdminException(400719);
  198. }
  199. $param = [
  200. 'phone' => $phone,
  201. 'host' => request()->host()
  202. ];
  203. $param['temp_id'] = $this->getTemplateCode($templateId);
  204. if (is_null($param['temp_id'])) {
  205. throw new AdminException(400720);
  206. }
  207. $param['param'] = json_encode($data);
  208. return $this->accessToken->httpRequest(self::SMS_SEND, $param);
  209. }
  210. /**
  211. * 发送记录
  212. * @param $record_id
  213. * @return array|bool|mixed
  214. */
  215. public function record($record_id)
  216. {
  217. $param = [
  218. 'record_id' => $record_id
  219. ];
  220. return $this->accessToken->httpRequest(self::SMS_RECORD, $param);
  221. }
  222. /**
  223. * 获取发送状态
  224. * @param array $recordIds
  225. * @return array|mixed
  226. */
  227. public function getStatus(array $recordIds)
  228. {
  229. $data['record_id'] = json_encode($recordIds);
  230. return $this->accessToken->httpRequest(self::SMS_STSTUS, $data, 'POST', false);
  231. }
  232. }