SMSService.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace crmeb\services;
  3. /**
  4. * 短信服务
  5. * Class SMSService
  6. * @package crmeb\services
  7. */
  8. class SMSService
  9. {
  10. // 短信账号
  11. private static $SMSAccount;
  12. // 短信token
  13. private static $SMSToken;
  14. public static $status;
  15. // 短信请求地址
  16. private static $SMSUrl = 'https://sms.crmeb.net/api/';
  17. //短信支付回调地址
  18. private static $payNotify;
  19. //验证码
  20. const VERIFICATION_CODE = 518076;
  21. //支付成功
  22. const PAY_SUCCESS_CODE = 520268;
  23. //发货提醒
  24. const DELIVER_GOODS_CODE = 520269;
  25. //确认收货提醒
  26. const TAKE_DELIVERY_CODE = 520271;
  27. //管理员下单提醒
  28. const ADMIN_PLACE_ORDER_CODE = 520272;
  29. //管理员退货提醒
  30. const ADMIN_RETURN_GOODS_CODE = 520274;
  31. //管理员支付成功提醒
  32. const ADMIN_PAY_SUCCESS_CODE = 520273;
  33. //管理员确认收货
  34. const ADMIN_TAKE_DELIVERY_CODE = 520422;
  35. public function __construct()
  36. {
  37. self::$status = strlen(sys_config('sms_account')) != 0 ?? false && strlen(sys_config('sms_token')) != 0 ?? false;
  38. }
  39. public static function getConstants($code = '')
  40. {
  41. $oClass = new \ReflectionClass(__CLASS__);
  42. $stants = $oClass->getConstants();
  43. if ($code) return isset($stants[$code]) ? $stants[$code] : '';
  44. else return $stants;
  45. }
  46. private static function auto()
  47. {
  48. self::$SMSAccount = sys_config('sms_account');
  49. self::$SMSToken = md5(self::$SMSAccount . md5(sys_config('sms_token')));
  50. self::$payNotify = sys_config('site_url') . '/api/sms/pay/notify';
  51. }
  52. /**
  53. * 验证码接口
  54. * @return string
  55. */
  56. public static function code()
  57. {
  58. return self::$SMSUrl . 'sms/captcha';
  59. }
  60. /**
  61. * 短信注册
  62. * @param $account
  63. * @param $password
  64. * @param $url
  65. * @param $phone
  66. * @param $code
  67. * @param $sign
  68. * @return mixed
  69. */
  70. public static function register($account, $password, $url, $phone, $code, $sign)
  71. {
  72. self::auto();
  73. $data['account'] = $account;
  74. $data['password'] = $password;
  75. $data['url'] = $url;
  76. $data['phone'] = $phone;
  77. $data['sign'] = $sign;
  78. $data['code'] = $code;
  79. return json_decode(HttpService::postRequest(self::$SMSUrl . 'sms/register', $data), true);
  80. }
  81. /**
  82. * 公共短信模板列表
  83. * @param array $data
  84. * @return mixed
  85. */
  86. public static function publictemp(array $data)
  87. {
  88. self::auto();
  89. $data['account'] = self::$SMSAccount;
  90. $data['token'] = self::$SMSToken;
  91. return json_decode(HttpService::postRequest(self::$SMSUrl . 'sms/publictemp', $data), true);
  92. }
  93. /**
  94. * 公共短信模板添加
  95. * @param $id
  96. * @param $tempId
  97. * @return mixed
  98. */
  99. public static function use($id, $tempId)
  100. {
  101. self::auto();
  102. $data['account'] = self::$SMSAccount;
  103. $data['token'] = self::$SMSToken;
  104. $data['id'] = $id;
  105. $data['tempId'] = $tempId;
  106. return json_decode(HttpService::postRequest(self::$SMSUrl . 'sms/use', $data), true);
  107. }
  108. /**
  109. * 发送短信
  110. * @param $phone
  111. * @param $template
  112. * @param $param
  113. * @return bool|string
  114. */
  115. public static function send($phone, $template, array $param)
  116. {
  117. self::auto();
  118. $data['uid'] = self::$SMSAccount;
  119. $data['token'] = self::$SMSToken;
  120. $data['mobile'] = $phone;
  121. $data['template'] = $template;
  122. $data['param'] = json_encode($param);
  123. return json_decode(HttpService::postRequest(self::$SMSUrl . 'sms/send', $data), true);
  124. }
  125. /**
  126. * 账号信息
  127. * @return mixed
  128. */
  129. public static function count()
  130. {
  131. self::auto();
  132. $data['account'] = self::$SMSAccount;
  133. $data['token'] = self::$SMSToken;
  134. return json_decode(HttpService::postRequest(self::$SMSUrl . 'sms/userinfo', $data), true);
  135. }
  136. /**
  137. * 支付套餐
  138. * @param $page
  139. * @param $limit
  140. * @return mixed
  141. */
  142. public static function meal($page, $limit)
  143. {
  144. $data['page'] = $page;
  145. $data['limit'] = $limit;
  146. return json_decode(HttpService::getRequest(self::$SMSUrl . 'sms/meal', $data), true);
  147. }
  148. /**
  149. * 支付码
  150. * @param $payType
  151. * @param $mealId
  152. * @param $price
  153. * @param $attach
  154. * @return mixed
  155. */
  156. public static function pay($payType, $mealId, $price, $attach)
  157. {
  158. self::auto();
  159. $data['uid'] = self::$SMSAccount;
  160. $data['token'] = self::$SMSToken;
  161. $data['payType'] = $payType;
  162. $data['mealId'] = $mealId;
  163. $data['notify'] = self::$payNotify;
  164. $data['price'] = $price;
  165. $data['attach'] = $attach;
  166. return json_decode(HttpService::postRequest(self::$SMSUrl . 'sms/mealpay', $data), true);
  167. }
  168. /**
  169. * 申请模板消息
  170. * @param $title
  171. * @param $content
  172. * @param $type
  173. * @return mixed
  174. */
  175. public static function apply($title, $content, $type)
  176. {
  177. self::auto();
  178. $data['account'] = self::$SMSAccount;
  179. $data['token'] = self::$SMSToken;
  180. $data['title'] = $title;
  181. $data['content'] = $content;
  182. $data['type'] = $type;
  183. return json_decode(HttpService::postRequest(self::$SMSUrl . 'sms/apply', $data), true);
  184. }
  185. /**
  186. * 短信模板列表
  187. * @param $data
  188. * @return mixed
  189. */
  190. public static function template($data)
  191. {
  192. self::auto();
  193. $data['account'] = self::$SMSAccount;
  194. $data['token'] = self::$SMSToken;
  195. return json_decode(HttpService::postRequest(self::$SMSUrl . 'sms/template', $data), true);
  196. }
  197. /**
  198. * 获取短息记录状态
  199. * @param $record_id
  200. * @return mixed
  201. */
  202. public static function getStatus($record_id)
  203. {
  204. $data['record_id'] = json_encode($record_id);
  205. return json_decode(HttpService::postRequest(self::$SMSUrl . 'sms/status', $data), true);
  206. }
  207. }