Yunxin.php 6.3 KB

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