Yunxin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace crmeb\services\sms\storage;
  3. use crmeb\basic\BaseSms;
  4. use crmeb\services\HttpService;
  5. /**
  6. * 云信短信服务
  7. * Class SMSService
  8. * @package crmeb\services
  9. */
  10. class Yunxin extends BaseSms
  11. {
  12. /**
  13. * 短信账号
  14. * @var string
  15. */
  16. protected $smsAccount;
  17. /**
  18. * 短信token
  19. * @var string
  20. */
  21. protected $smsToken;
  22. /**
  23. * 是否登陆
  24. * @var bool
  25. */
  26. protected $status;
  27. /**
  28. * 短信请求地址
  29. * @var string
  30. */
  31. protected $smsUrl = 'https://sms.crmeb.net/api/';
  32. /**
  33. * 短信支付回调地址
  34. * @var string
  35. */
  36. protected $payNotify;
  37. /**
  38. * 初始化
  39. * @param array $config
  40. * @return mixed|void
  41. */
  42. protected function initialize(array $config)
  43. {
  44. parent::initialize($config);
  45. $this->smsAccount = $config['sms_account'] ?? null;
  46. $this->smsToken = $config['sms_token'] ?? null;
  47. $this->payNotify = ($config['site_url'] ?? '') . '/api/sms/pay/notify';
  48. if ($this->smsAccount && $this->smsToken) {
  49. $this->status = true;
  50. $this->smsToken = md5($this->smsAccount . md5($this->smsToken));
  51. } else {
  52. $this->status = false;
  53. }
  54. }
  55. /**
  56. * 登陆状态
  57. * @return bool
  58. */
  59. public function isLogin()
  60. {
  61. return $this->status;
  62. }
  63. /**
  64. * 验证码接口
  65. * @return string
  66. */
  67. public function getSmsUrl()
  68. {
  69. return $this->smsUrl . 'sms/captcha';
  70. }
  71. /**
  72. * 短信注册
  73. * @param $account
  74. * @param $password
  75. * @param $url
  76. * @param $phone
  77. * @param $code
  78. * @param $sign
  79. * @return mixed
  80. */
  81. public function register($account, $password, $url, $phone, $code, $sign)
  82. {
  83. $data['account'] = $account;
  84. $data['password'] = $password;
  85. $data['url'] = $url;
  86. $data['phone'] = $phone;
  87. $data['sign'] = $sign;
  88. $data['code'] = $code;
  89. return json_decode(HttpService::postRequest($this->smsUrl . 'sms/register', $data), true);
  90. }
  91. /**
  92. * 公共短信模板列表
  93. * @param array $data
  94. * @return mixed
  95. */
  96. public function publictemp(array $data)
  97. {
  98. $data['account'] = $this->smsAccount;
  99. $data['token'] = $this->smsToken;
  100. return json_decode(HttpService::postRequest($this->smsUrl . 'sms/publictemp', $data), true);
  101. }
  102. /**
  103. * 公共短信模板添加
  104. * @param $id
  105. * @param $tempId
  106. * @return mixed
  107. */
  108. public function use($id, $tempId)
  109. {
  110. $data['account'] = $this->smsAccount;
  111. $data['token'] = $this->smsToken;
  112. $data['id'] = $id;
  113. $data['tempId'] = $tempId;
  114. return json_decode(HttpService::postRequest($this->smsUrl . 'sms/use', $data), true);
  115. }
  116. /**
  117. * 发送短信
  118. * @param $phone
  119. * @param $template
  120. * @param $param
  121. * @return bool|string
  122. */
  123. public function send(string $phone, string $templateId, array $data = [])
  124. {
  125. if (!$phone) {
  126. return $this->setError('Mobile number cannot be empty');
  127. }
  128. if (!$this->smsAccount) {
  129. return $this->setError('Account does not exist');
  130. }
  131. if (!$this->smsToken) {
  132. return $this->setError('Access token does not exist');
  133. }
  134. $formData['uid'] = $this->smsAccount;
  135. $formData['token'] = $this->smsToken;
  136. $formData['mobile'] = $phone;
  137. $formData['template'] = $this->getTemplateCode($templateId);
  138. if (is_null($formData['template'])) {
  139. return $this->setError('Missing template number');
  140. }
  141. $formData['param'] = json_encode($data);
  142. $resource = json_decode(HttpService::postRequest($this->smsUrl . 'sms/send', $formData), true);
  143. if ($resource['status'] === 400) {
  144. return $this->setError($resource['msg']);
  145. }
  146. return $resource;
  147. }
  148. /**
  149. * 账号信息
  150. * @return mixed
  151. */
  152. public function count()
  153. {
  154. return json_decode(HttpService::postRequest($this->smsUrl . 'sms/userinfo', [
  155. 'account' => $this->smsAccount,
  156. 'token' => $this->smsToken
  157. ]), true);
  158. }
  159. /**
  160. * 支付套餐
  161. * @param $page
  162. * @param $limit
  163. * @return mixed
  164. */
  165. public function meal($page, $limit)
  166. {
  167. return json_decode(HttpService::getRequest($this->smsUrl . 'sms/meal', [
  168. 'page' => $page,
  169. 'limit' => $limit
  170. ]), true);
  171. }
  172. /**
  173. * 支付码
  174. * @param $payType
  175. * @param $mealId
  176. * @param $price
  177. * @param $attach
  178. * @return mixed
  179. */
  180. public function pay($payType, $mealId, $price, $attach)
  181. {
  182. $data['uid'] = $this->smsAccount;
  183. $data['token'] = $this->smsToken;
  184. $data['payType'] = $payType;
  185. $data['mealId'] = $mealId;
  186. $data['notify'] = $this->payNotify;
  187. $data['price'] = $price;
  188. $data['attach'] = $attach;
  189. return json_decode(HttpService::postRequest($this->smsUrl . 'sms/mealpay', $data), true);
  190. }
  191. /**
  192. * 申请模板消息
  193. * @param $title
  194. * @param $content
  195. * @param $type
  196. * @return mixed
  197. */
  198. public function apply($title, $content, $type)
  199. {
  200. $data['account'] = $this->smsAccount;
  201. $data['token'] = $this->smsToken;
  202. $data['title'] = $title;
  203. $data['content'] = $content;
  204. $data['type'] = $type;
  205. return json_decode(HttpService::postRequest($this->smsUrl . 'sms/apply', $data), true);
  206. }
  207. /**
  208. * 短信模板列表
  209. * @param $data
  210. * @return mixed
  211. */
  212. public function template($data)
  213. {
  214. return json_decode(HttpService::postRequest($this->smsUrl . 'sms/template', $data + [
  215. 'account' => $this->smsAccount, 'token' => $this->smsToken
  216. ]), true);
  217. }
  218. /**
  219. * 获取短息记录状态
  220. * @param $record_id
  221. * @return mixed
  222. */
  223. public function getStatus(array $record_id)
  224. {
  225. $data['record_id'] = json_encode($record_id);
  226. return json_decode(HttpService::postRequest($this->smsUrl . 'sms/status', $data), true);
  227. }
  228. }