Sms.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service\sms\storage;
  12. use basic\BaseSms;
  13. use service\AccessTokenServeService;
  14. use think\exception\ValidateException;
  15. use think\Config;
  16. use service\SystemConfigService;
  17. /**
  18. * Class Copy
  19. * @package crmeb\services\product\storage
  20. */
  21. class Sms extends BaseSms
  22. {
  23. /**
  24. * 开通
  25. */
  26. const SMS_OPEN = 'sms_v2/open';
  27. /**
  28. * 修改签名
  29. */
  30. const SMS_MODIFY = 'sms_v2/modify';
  31. /**
  32. * 用户信息
  33. */
  34. const SMS_INFO = 'sms_v2/info';
  35. /**
  36. * 发送短信
  37. */
  38. const SMS_SEND = 'sms_v2/send';
  39. /**
  40. * 短信模板
  41. */
  42. const SMS_TEMPS = 'sms_v2/temps';
  43. /**
  44. * 申请模板
  45. */
  46. const SMS_APPLY = 'sms_v2/apply';
  47. /**
  48. * 模板记录
  49. */
  50. const SMS_APPLYS = 'sms_v2/applys';
  51. /**
  52. * 发送记录
  53. */
  54. const SMS_RECORD = 'sms_v2/record';
  55. /**
  56. * 短信签名
  57. * @var string
  58. */
  59. protected $sign = '';
  60. /**
  61. * 短信签名
  62. * @var string
  63. */
  64. protected $templateId = '518076';
  65. /**
  66. * 模板id
  67. * @var array
  68. */
  69. protected $templateIds = [];
  70. public function __construct()
  71. {
  72. $this->accessToken = $this->getAccessToken();
  73. $this->templateIds = Config::get('sms.stores.sms.template_id', []);
  74. }
  75. protected function getAccessToken()
  76. {
  77. $this->account = SystemConfigService::get('sms_account');
  78. $this->sercet = SystemConfigService::get('sms_token');
  79. return new AccessTokenServeService($this->account, $this->sercet);
  80. }
  81. /** 初始化
  82. * @param array $config
  83. */
  84. protected function _initialize(array $config = [])
  85. {
  86. parent::_initialize($config);
  87. }
  88. /**
  89. * 提取模板code
  90. * @param string $templateId
  91. * @return null
  92. */
  93. protected function getTemplateCode($templateId)
  94. {
  95. return $this->templateIds[$templateId] ? $this->templateIds[$templateId] : null;
  96. }
  97. /**
  98. * 设置签名
  99. * @param $sign
  100. * @return $this
  101. */
  102. public function setSign($sign)
  103. {
  104. $this->sign = $sign;
  105. return $this;
  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 $sign
  121. * @return array|bool|mixed
  122. */
  123. public function modify($sign = '', $phone = '', $verify_code = '')
  124. {
  125. $param = [
  126. 'sign' => $sign,
  127. 'phone' => $phone,
  128. 'verify_code' => $verify_code
  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 $temp_type
  145. * @return array|bool|mixed
  146. */
  147. public function temps($page = 1, $limit = 10, $temp_type = '')
  148. {
  149. $param = [
  150. 'page' => $page,
  151. 'limit' => $limit,
  152. 'temp_type' => $temp_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($title, $content, $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($temp_type, $page, $limit)
  180. {
  181. $param = [
  182. 'temp_type' => $temp_type,
  183. 'page' => $page,
  184. 'limit' => $limit
  185. ];
  186. return $this->accessToken->httpRequest(self::SMS_APPLYS, $param);
  187. }
  188. /**返回结果
  189. * @param string $code
  190. * @param string $data
  191. * @param string $message
  192. * @return array
  193. */
  194. public function returnData($code = 'OK', $data = '', $message = '')
  195. {
  196. return [
  197. 'data' => $data,
  198. 'Code' => $code,
  199. 'Message' => $message
  200. ];
  201. }
  202. /**
  203. * 发送短信
  204. * @param $phone
  205. * @param $template
  206. * @param $param
  207. * @return bool|string
  208. */
  209. public function send($phone, $templateId = '', $data = [])
  210. {
  211. if (!$phone) {
  212. return $this->returnData('手机号不能为空', 'err', '手机号不能为空');
  213. }
  214. $param = [
  215. 'phone' => $phone
  216. ];
  217. $param['temp_id'] = $templateId;
  218. // $param['temp_id'] = $this->templateId;
  219. if (is_null($param['temp_id']) || $param['temp_id'] == '') {
  220. return $this->returnData('模版ID不存在', 'err', '模版ID不存在');
  221. }
  222. $param['param'] = json_encode($data);
  223. $data = $this->accessToken->httpRequest(self::SMS_SEND, $param);
  224. if (!isset($data['status']) || $data['status'] != 200) {
  225. return $this->returnData('err', $data['msg'], $data['msg']);
  226. } else {
  227. return $this->returnData('OK', $data['data']['id'], '发送成功');
  228. }
  229. }
  230. /**
  231. * 发送记录
  232. * @param $record_id
  233. * @return array|bool|mixed
  234. */
  235. public function record($record_id)
  236. {
  237. $param = [
  238. 'record_id' => $record_id
  239. ];
  240. return $this->accessToken->httpRequest(self::SMS_RECORD, $param);
  241. }
  242. }