YunxinSmsService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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;
  12. use app\common\dao\system\sms\SmsRecordDao;
  13. use app\common\repositories\store\broadcast\BroadcastRoomRepository;
  14. use app\common\repositories\store\order\StoreGroupOrderRepository;
  15. use app\common\repositories\store\order\StoreOrderRepository;
  16. use app\common\repositories\store\order\StoreRefundOrderRepository;
  17. use app\common\repositories\store\product\ProductRepository;
  18. use app\common\repositories\store\product\ProductTakeRepository;
  19. use app\common\repositories\store\service\StoreServiceRepository;
  20. use app\common\repositories\system\config\ConfigValueRepository;
  21. use app\common\repositories\system\notice\SystemNoticeConfigRepository;
  22. use crmeb\exceptions\SmsException;
  23. use FormBuilder\Exception\FormBuilderException;
  24. use FormBuilder\Factory\Elm;
  25. use FormBuilder\Form;
  26. use think\exception\ValidateException;
  27. use think\facade\Cache;
  28. use think\facade\Config;
  29. use think\facade\Route;
  30. /**
  31. * Class YunxinSmsService
  32. * @package crmeb\services
  33. * @author xaboy
  34. * @day 2020-05-18
  35. */
  36. class YunxinSmsService
  37. {
  38. /**
  39. * api
  40. */
  41. const API = 'https://sms.crmeb.net/api/';
  42. // const API = 'http://plat.crmeb.net/api/';
  43. /**
  44. * @var array
  45. */
  46. protected $config;
  47. /**
  48. * YunxinSmsService constructor.
  49. * @param array $config
  50. */
  51. public function __construct(array $config = [])
  52. {
  53. $this->config = $config;
  54. if (isset($this->config['sms_token'])) {
  55. $this->config['sms_token'] = $this->getToken();
  56. }
  57. }
  58. /**
  59. * @return string
  60. * @author xaboy
  61. * @day 2020-05-18
  62. */
  63. protected function getToken()
  64. {
  65. return md5($this->config['sms_account'] . $this->config['sms_token']);
  66. }
  67. /**
  68. * @author xaboy
  69. * @day 2020-05-18
  70. */
  71. public function checkConfig()
  72. {
  73. if (!isset($this->config['sms_account']) || !$this->config['sms_account']) {
  74. throw new ValidateException('请登录短信账户');
  75. }
  76. if (!isset($this->config['sms_token']) || !$this->config['sms_token']) {
  77. throw new ValidateException('请登录短信账户');
  78. }
  79. }
  80. /**
  81. * 发送注册验证码
  82. * @param $phone
  83. * @return mixed
  84. */
  85. public function captcha($phone)
  86. {
  87. return json_decode(HttpService::getRequest(self::API . 'sms/captcha', compact('phone')), true);
  88. }
  89. /**
  90. * 短信注册
  91. * @param $account
  92. * @param $password
  93. * @param $url
  94. * @param $phone
  95. * @param $code
  96. * @param $sign
  97. * @return mixed
  98. */
  99. public function register($account, $password, $url, $phone, $code, $sign)
  100. {
  101. return $this->registerData(compact('account', 'password', 'url', 'phone', 'code', 'sign'));
  102. }
  103. /**
  104. * @param array $data
  105. * @return mixed
  106. * @author xaboy
  107. * @day 2020-05-18
  108. */
  109. public function registerData(array $data)
  110. {
  111. return json_decode(HttpService::postRequest(self::API . 'sms/register', $data), true);
  112. }
  113. /**
  114. * 公共短信模板列表
  115. * @param array $data
  116. * @return mixed
  117. */
  118. public function publictemp(array $data = [])
  119. {
  120. $this->checkConfig();
  121. $data['account'] = $this->config['sms_account'];
  122. $data['token'] = $this->config['sms_token'];
  123. $data['source'] = 'crmeb_merchant';
  124. return json_decode(HttpService::postRequest(self::API . 'sms/publictemp', $data), true);
  125. }
  126. /**
  127. * 公共短信模板添加
  128. * @param $id
  129. * @param $tempId
  130. * @return mixed
  131. */
  132. public function use($id, $tempId)
  133. {
  134. $this->checkConfig();
  135. $data = [
  136. 'account' => $this->config['sms_account'],
  137. 'token' => $this->config['sms_token'],
  138. 'id' => $id,
  139. 'tempId' => $tempId,
  140. ];
  141. return json_decode(HttpService::postRequest(self::API . 'sms/use', $data), true);
  142. }
  143. /**
  144. * @param string $templateId
  145. * @return mixed
  146. * @author xaboy
  147. * @day 2020-05-18
  148. */
  149. public function getTemplateCode(string $templateId)
  150. {
  151. return Config::get('sms.template_id.' . $templateId);
  152. }
  153. /**
  154. * 原 send 方法 (弃用)
  155. * 发送短信
  156. * @param string $phone
  157. * @param string $templateId
  158. * @param array $data
  159. * @return bool|string
  160. * @throws SmsException
  161. */
  162. public function sendDe(string $phone, string $templateId, array $data = [])
  163. {
  164. if (!$phone) {
  165. throw new SmsException('Mobile number cannot be empty');
  166. }
  167. $this->checkConfig();
  168. $formData['uid'] = $this->config['sms_account'];
  169. $formData['token'] = $this->config['sms_token'];
  170. $formData['mobile'] = $phone;
  171. $formData['template'] = $this->getTemplateCode($templateId);
  172. if (is_null($formData['template']))
  173. throw new SmsException('Missing template number');
  174. $formData['param'] = json_encode($data);
  175. $resource = json_decode(HttpService::postRequest(self::API . 'sms/send', $formData), true);
  176. if ($resource['status'] === 400) {
  177. throw new SmsException($resource['msg']);
  178. } else {
  179. app()->make(SmsRecordDao::class)->create([
  180. 'uid' => $formData['uid'],
  181. 'phone' => $phone,
  182. 'content' => $resource['data']['content'],
  183. 'template' => $resource['data']['template'],
  184. 'record_id' => $resource['data']['id']
  185. ]);
  186. }
  187. return $resource;
  188. }
  189. /**
  190. * 账号信息
  191. * @return mixed
  192. */
  193. public function count()
  194. {
  195. $this->checkConfig();
  196. return json_decode(HttpService::postRequest(self::API . 'sms/userinfo', [
  197. 'account' => $this->config['sms_account'],
  198. 'token' => $this->config['sms_token']
  199. ]), true);
  200. }
  201. /**
  202. * 支付套餐
  203. * @param $page
  204. * @param $limit
  205. * @return mixed
  206. */
  207. public function meal($page, $limit)
  208. {
  209. return json_decode(HttpService::getRequest(self::API . 'sms/meal', [
  210. 'page' => $page,
  211. 'limit' => $limit
  212. ]), true);
  213. }
  214. /**
  215. * 支付码
  216. * @param $payType
  217. * @param $mealId
  218. * @param $price
  219. * @param $attach
  220. * @param $notify
  221. * @return mixed
  222. */
  223. public function pay($payType, $mealId, $price, $attach, $notify = null)
  224. {
  225. $this->checkConfig();
  226. $data['uid'] = $this->config['sms_account'];
  227. $data['token'] = $this->config['sms_token'];
  228. $data['payType'] = $payType;
  229. $data['mealId'] = $mealId;
  230. $data['notify'] = $notify ?? Route::buildUrl('SmsNotify')->build();
  231. $data['price'] = $price;
  232. $data['attach'] = $attach;
  233. return json_decode(HttpService::postRequest(self::API . 'sms/mealpay', $data), true);
  234. }
  235. /**
  236. * 申请模板消息
  237. * @param $title
  238. * @param $content
  239. * @param $type
  240. * @return mixed
  241. */
  242. public function apply($title, $content, $type)
  243. {
  244. $this->checkConfig();
  245. $data['account'] = $this->config['sms_account'];
  246. $data['token'] = $this->config['sms_token'];
  247. $data['title'] = $title;
  248. $data['content'] = $content;
  249. $data['type'] = $type;
  250. return json_decode(HttpService::postRequest(self::API . 'sms/apply', $data), true);
  251. }
  252. /**
  253. * 短信模板列表
  254. * @param $data
  255. * @return mixed
  256. */
  257. public function template(array $data)
  258. {
  259. $this->checkConfig();
  260. return json_decode(HttpService::postRequest(self::API . 'sms/template', $data + [
  261. 'account' => $this->config['sms_account'], 'token' => $this->config['sms_token']
  262. ]), true);
  263. }
  264. /**
  265. * 获取短息记录状态
  266. * @param $record_id
  267. * @return mixed
  268. */
  269. public function getStatus(array $record_id)
  270. {
  271. return json_decode(HttpService::postRequest(self::API . 'sms/status', [
  272. 'record_id' => json_encode($record_id)
  273. ]), true);
  274. }
  275. /**
  276. * @return YunxinSmsService
  277. * @author xaboy
  278. * @day 2020-05-18
  279. */
  280. public static function create()
  281. {
  282. /** @var ConfigValueRepository $make */
  283. $make = app()->make(ConfigValueRepository::class);
  284. $config = $make->more(['sms_account', 'sms_token'], 0);
  285. return new static($config);
  286. }
  287. /**
  288. * @param string $sms_account
  289. * @param string $sms_token
  290. * @return $this
  291. * @author xaboy
  292. * @day 2020-05-18
  293. */
  294. public function setConfig(string $sms_account, string $sms_token)
  295. {
  296. $this->config = compact('sms_token', 'sms_account');
  297. $this->config['sms_token'] = $this->getToken();
  298. return $this;
  299. }
  300. /**
  301. * @return Form
  302. * @throws FormBuilderException
  303. * @author xaboy
  304. * @day 2020-05-18
  305. */
  306. public function form()
  307. {
  308. return Elm::createForm(Route::buildUrl('smsCreate')->build(), [
  309. Elm::input('title', '模板名称:')->placeholder('请输入模版名称'),
  310. Elm::input('content', '模板内容:')->type('textarea')->placeholder('请输入模版内容'),
  311. Elm::radio('type', '模板类型:', 1)->options([['label' => '验证码', 'value' => 1], ['label' => '通知', 'value' => 2], ['label' => '推广', 'value' => 3]])
  312. ])->setTitle('申请短信模板');
  313. }
  314. /**
  315. * @return mixed
  316. * @author xaboy
  317. * @day 2020-05-18
  318. */
  319. public function account()
  320. {
  321. $this->checkConfig();
  322. return $this->config['sms_account'];
  323. }
  324. /**
  325. * @Author:Qinii
  326. * @Date: 2020/9/19
  327. * @param $data
  328. * @return mixed
  329. */
  330. public function smsChange($data)
  331. {
  332. $this->checkConfig();
  333. $data['account'] = $this->config['sms_account'];
  334. $data['token'] = $this->config['sms_token'];
  335. return json_decode(HttpService::postRequest(self::API . 'sms/modify', $data), true);
  336. }
  337. /**
  338. * @Author:Qinii
  339. * @Date: 2020/9/19
  340. * @param $phone
  341. * @param $code
  342. * @param $type
  343. * @return bool
  344. */
  345. public function checkSmsCode($phone, $code, $type)
  346. {
  347. if (!env('DEVELOPMENT',false)) {
  348. $sms_key = $this->sendSmsKey($phone, $type);
  349. if (!$cache_code = Cache::get($sms_key)) return false;
  350. if ($code != $cache_code) return false;
  351. Cache::delete($sms_key);
  352. }
  353. return true;
  354. }
  355. /**
  356. * @Author:Qinii
  357. * @Date: 2020/9/19
  358. * @param $phone
  359. * @param string $type
  360. * @return string
  361. */
  362. public function sendSmsKey($phone, $type = 'login')
  363. {
  364. switch ($type) {
  365. case 'login': //登录
  366. return 'api_login_' . $phone;
  367. break;
  368. case 'binding': //绑定手机号
  369. return 'api_binding_' . $phone;
  370. break;
  371. case 'intention': //申请入住
  372. return 'merchant_intention_' . $phone;
  373. break;
  374. case 'change_pwd': //修改密码
  375. return 'change_pwd_' . $phone;
  376. break;
  377. case 'change_phone': //修改手机号
  378. return 'change_phone_' . $phone;
  379. break;
  380. default:
  381. return 'crmeb_' . $phone;
  382. break;
  383. }
  384. }
  385. public function send(string $phone, string $templateId, array $data = [])
  386. {
  387. try {
  388. $make = app()->make(CrmebServeServices::class)->sms();
  389. $resource = $make->send($phone, $this->getTemplateCode($templateId), $data);
  390. if ($resource) {
  391. app()->make(SmsRecordDao::class)->create([
  392. 'uid' => $this->config['sms_account'],
  393. 'phone' => $phone,
  394. 'content' => $resource['content'],
  395. 'template' => $resource['template'],
  396. 'record_id' => $resource['id'],
  397. ]);
  398. }
  399. } catch (\Exception $exception) {
  400. throw new SmsException($exception->getMessage());
  401. }
  402. }
  403. }