Yunxin.php 6.0 KB

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