Sms.php 4.9 KB

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