SystemPlat.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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 app\admin\controller\setting;
  12. use app\admin\controller\AuthController;
  13. use app\admin\model\system\SystemConfig as ConfigModel;
  14. use service\JsonService as Json;
  15. use service\FormBuilder as Form;
  16. use service\CacheService;
  17. use think\Url;
  18. use service\CrmebPlatService;
  19. use service\sms\storage\Sms;
  20. use service\express\storage\Express;
  21. use service\SystemConfigService;
  22. use app\admin\model\system\SmsAccessToken;
  23. use app\admin\model\system\Express as ExpressModel;
  24. /**
  25. * crmeb 平台
  26. * Class SystemPlat
  27. * @package app\admin\controller\setting
  28. */
  29. class SystemPlat extends AuthController
  30. {
  31. protected $account = NULL;
  32. protected $secret = NULL;
  33. /**
  34. * @var $crmebPlatHandle
  35. */
  36. protected $crmebPlatHandle;
  37. /**
  38. * @var $smsHandle
  39. */
  40. protected $smsHandle;
  41. /**
  42. * @var $expressHandle
  43. */
  44. protected $expressHandle;
  45. /**
  46. * @var $productHandle
  47. */
  48. protected $productHandle;
  49. protected $allowAction = ['index', 'verify', 'login', 'go_login', 'register', 'go_register', 'modify', 'go_modify', 'forget', 'go_forget', 'loginOut', 'meal', 'sms_temp'];
  50. /**
  51. * @var string
  52. */
  53. protected $cacheTokenPrefix = "_crmeb_plat";
  54. protected $cacheKey;
  55. protected function _initialize()
  56. {
  57. parent::_initialize();
  58. $this->account = SystemConfigService::get('sms_account');
  59. $this->secret = SystemConfigService::get('sms_token');
  60. $this->crmebPlatHandle = new CrmebPlatService();
  61. $this->smsHandle = new Sms();
  62. $this->expressHandle = new Express();
  63. $this->cacheKey = md5($this->account . '_' . $this->secret . $this->cacheTokenPrefix);
  64. }
  65. /**
  66. * 显示资源列表
  67. *
  68. * @return \think\Response
  69. */
  70. public function index()
  71. {
  72. if (!CacheService::get($this->cacheKey, '')) {
  73. return $this->redirect(Url::build('login') . '?url=index');
  74. }
  75. list($out, $type) = parent::getMore([
  76. ['out', 0],
  77. ['type', 'sms']
  78. ], null, true);
  79. try {
  80. $info = $this->crmebPlatHandle->info();
  81. if (!isset($info['status']) || $info['status'] != 200) {
  82. $info = [];
  83. } else {
  84. $info = $info['data'];
  85. }
  86. } catch (\Throwable $e) {
  87. $info = [];
  88. }
  89. $this->assign('info', $info);
  90. $this->assign('type', $type);
  91. if ($out == 0 && $info) {
  92. return $this->fetch();
  93. } else {
  94. $this->assign('account', $this->account);
  95. $this->assign('password', $this->secret);
  96. return $this->fetch('login');
  97. }
  98. }
  99. /**
  100. * 获取短信验证码
  101. */
  102. public function verify()
  103. {
  104. list($phone) = parent::postMore([
  105. ['phone', '']
  106. ], null, true);
  107. if (!$phone) {
  108. return Json::fail('请输入手机号');
  109. }
  110. if (!check_phone($phone)) {
  111. return Json::fail('请输入正确的手机号');
  112. }
  113. $data = $this->crmebPlatHandle->code($phone);
  114. if (!isset($data['status']) || $data['status'] != 200) {
  115. return Json::fail($data['msg']);
  116. } else {
  117. return Json::successful('获取成功');
  118. }
  119. }
  120. /**
  121. * 登录页面
  122. * @return string
  123. * @throws \Exception
  124. */
  125. public function login($url = '')
  126. {
  127. $this->assign('str', $url);
  128. $this->assign('account', $this->account);
  129. $this->assign('password', $this->secret);
  130. return $this->fetch();
  131. }
  132. /**
  133. * 退出登录
  134. * @return string
  135. * @throws \Exception
  136. */
  137. public function loginOut()
  138. {
  139. CacheService::rm($this->cacheKey);
  140. return Json::successful('退出成功', $this->crmebPlatHandle->loginOut());
  141. }
  142. /**
  143. * 登录逻辑
  144. */
  145. public function go_login()
  146. {
  147. $data = parent::postMore([
  148. ['account', ''],
  149. ['password', '']
  150. ]);
  151. if (!$data['account']) {
  152. return Json::fail('请输入账号');
  153. }
  154. if (!$data['password']) {
  155. return Json::fail('请输入秘钥');
  156. }
  157. $this->save_basics(['sms_account' => $data['account'], 'sms_token' => $data['password']]);
  158. $token = $this->crmebPlatHandle->login($data['account'], $data['password']);
  159. if ($token) {
  160. return Json::successful('登录成功', $token);
  161. } else {
  162. return Json::fail('登录失败,账号或密码有误!');
  163. }
  164. }
  165. /**
  166. * 注册页面
  167. * @return string
  168. * @throws \Exception
  169. */
  170. public function register()
  171. {
  172. return $this->fetch();
  173. }
  174. /**
  175. * 注册逻辑
  176. */
  177. public function go_register()
  178. {
  179. $data = parent::postMore([
  180. ['account', ''],
  181. ['phone', ''],
  182. ['password', ''],
  183. ['verify_code', ''],
  184. ]);
  185. if (!$data['account']) {
  186. return Json::fail('请输入账号');
  187. }
  188. if (!$data['phone']) {
  189. return Json::fail('请输入手机号');
  190. }
  191. if (!check_phone($data['phone'])) {
  192. return Json::fail('请输入正确的手机号');
  193. }
  194. if (!$data['password']) {
  195. return Json::fail('请设置秘钥');
  196. }
  197. if (strlen($data['password']) < 6 || strlen($data['password']) > 32) {
  198. return Json::fail('密码长度6~32位');
  199. }
  200. if (!$data['verify_code']) {
  201. return Json::fail('请先获取短信验证码');
  202. }
  203. $result = $this->crmebPlatHandle->register($data['account'], $data['phone'], $data['password'], $data['verify_code']);
  204. if (!isset($result['status']) || $result['status'] != 200) {
  205. return Json::fail($result['msg']);
  206. } else {
  207. $result = $result['data'];
  208. }
  209. $this->save_basics(['sms_account' => $data['account'], 'sms_token' => $data['password']]);
  210. return Json::successful('注册成功', $result);
  211. }
  212. /**
  213. * 修改秘钥页面
  214. * @return string
  215. * @throws \Exception
  216. */
  217. public function modify()
  218. {
  219. $this->assign('account', $this->account);
  220. return $this->fetch();
  221. }
  222. /**
  223. * 修改秘钥逻辑
  224. */
  225. public function go_modify()
  226. {
  227. $data = parent::postMore([
  228. ['account', ''],
  229. ['phone', ''],
  230. ['password', ''],
  231. ['verify_code', ''],
  232. ]);
  233. if (!$data['account']) {
  234. return Json::fail('请输入账号');
  235. }
  236. if (!$data['phone']) {
  237. return Json::fail('请输入手机号');
  238. }
  239. if (!check_phone($data['phone'])) {
  240. return Json::fail('请输入正确的手机号');
  241. }
  242. if (!$data['password']) {
  243. return Json::fail('请设置秘钥');
  244. }
  245. if (strlen($data['password']) < 6 || strlen($data['password']) > 32) {
  246. return Json::fail('密码长度6~32位');
  247. }
  248. if (!$data['verify_code']) {
  249. return Json::fail('请先获取短信验证码');
  250. }
  251. $result = $this->crmebPlatHandle->modify($data['account'], $data['phone'], $data['password'], $data['verify_code']);
  252. if (!isset($result['status']) || $result['status'] != 200) {
  253. return Json::fail($result['msg']);
  254. } else {
  255. $result = $result['data'];
  256. }
  257. $this->save_basics(['sms_account' => $data['account'], 'sms_token' => $data['password']]);
  258. return Json::successful('修改成功', $result);
  259. }
  260. /**
  261. * 找回账号
  262. * @return string
  263. * @throws \Exception
  264. */
  265. public function forget()
  266. {
  267. return $this->fetch();
  268. }
  269. /**
  270. * 找回账号逻辑
  271. */
  272. public function go_fotget()
  273. {
  274. $data = $where = parent::postMore([
  275. ['phone', ''],
  276. ['verify_code', ''],
  277. ]);
  278. if (!isset($data['phone']) || $data['phone']) {
  279. return Json::fail('请输入手机号');
  280. }
  281. if (!check_phone($data['phone'])) {
  282. return Json::fail('请输入正确的手机号');
  283. }
  284. if (!isset($data['verify_code']) || $data['verify_code']) {
  285. return Json::fail('请先获取短信验证码');
  286. }
  287. $result = $this->crmebPlatHandle->forget($data['phone'], $data['verify_code']);
  288. if (!isset($result['status']) || $result['status'] != 200) {
  289. return Json::fail($result['msg']);
  290. } else {
  291. $result = $result['data'];
  292. }
  293. return Json::successful('修改成功', $result);
  294. }
  295. /**
  296. * 获取消费记录
  297. */
  298. public function record()
  299. {
  300. list($type, $page, $limit) = parent::getMore([
  301. ['type', 'sms'],
  302. ['page', 1],
  303. ['limit', 20]
  304. ], null, true);
  305. $result = $this->crmebPlatHandle->record($type, $page, $limit);
  306. if (!isset($result['status']) || $result['status'] != 200) {
  307. return Json::fail($result['msg']);
  308. } else {
  309. $result = $result['data'];
  310. if ($type == 'expr_query') {
  311. $express = ExpressModel::expressList();
  312. $express = array_combine(array_column($express, 'code'), $express);
  313. foreach ($result['data'] as $key => $value) {
  314. $result['data'][$key]['name'] = $express[$value['content']['com']]['name'] ?? '';
  315. $result['data'][$key]['num'] = $value['content']['num'] ?? '';
  316. $result['data'][$key]['code'] = $value['content']['com'] ?? '';
  317. }
  318. }
  319. }
  320. return Json::successlayui($result['count'], $result['data']);
  321. }
  322. /**
  323. * @return string
  324. * @throws \Exception
  325. */
  326. public function meal()
  327. {
  328. if (!CacheService::get($this->cacheKey, '')) {
  329. return $this->redirect(Url::build('login') . '?url=meal');
  330. }
  331. try {
  332. $info = $this->crmebPlatHandle->info();
  333. if (!isset($info['status']) || $info['status'] != 200) {
  334. $info = [];
  335. } else {
  336. $info = $info['data'];
  337. }
  338. } catch (\Throwable $e) {
  339. $info = [];
  340. }
  341. $this->assign('info', $info);
  342. return $this->fetch();
  343. }
  344. /**
  345. * 获取套餐列表
  346. */
  347. public function get_meal()
  348. {
  349. list($type) = parent::getMore([
  350. ['type', 'sms']
  351. ], null, true);
  352. $result = $this->crmebPlatHandle->meal($type);
  353. if (!isset($result['status']) || $result['status'] != 200) {
  354. return Json::fail($result['msg']);
  355. } else {
  356. $result = $result['data'];
  357. }
  358. return Json::successful($result);
  359. }
  360. /**
  361. * 获取支付二维码
  362. * @return string
  363. * @throws \Exception
  364. */
  365. public function pay()
  366. {
  367. list($meal_id, $price, $num, $type, $pay_type) = parent::postMore([
  368. ['meal_id', 0],
  369. ['price', ''],
  370. ['num', 0],
  371. ['type', ''],
  372. ['pay_type', 'weixin']
  373. ], null, true);
  374. if (!$meal_id) {
  375. return Json::fail('请选择套餐');
  376. }
  377. try {
  378. $info = $this->crmebPlatHandle->info();
  379. if (!isset($info['status']) || $info['status'] != 200) {
  380. $info = [];
  381. } else {
  382. $info = $info['data'];
  383. }
  384. } catch (\Throwable $e) {
  385. $info = [];
  386. }
  387. if (!$info) {
  388. return Json::fail('用户信息不存在!');
  389. }
  390. $payContent = $this->crmebPlatHandle->pay($type, $meal_id, $price, $num, $pay_type);
  391. if (!isset($payContent['status']) || $payContent['status'] != 200) {
  392. $payContent = [];
  393. } else {
  394. $payContent = $payContent['data'];
  395. }
  396. if (isset($info['sms']['open']) && $info['sms']['open'] == 1) {
  397. $payContent['code_show'] = true;
  398. } else {
  399. $payContent['code_show'] = false;
  400. }
  401. return Json::successful($payContent);
  402. }
  403. /**
  404. * 保存一号通配置
  405. */
  406. public function save_basics($data)
  407. {
  408. if ($data) {
  409. CacheService::clear();
  410. foreach ($data as $k => $v) {
  411. ConfigModel::edit(['value' => json_encode($v)], $k, 'menu_name');
  412. }
  413. }
  414. return true;
  415. }
  416. /**
  417. * 开通短信服务页面
  418. * @return string
  419. * @throws \Exception
  420. */
  421. public function sms_open()
  422. {
  423. try {
  424. $info = $this->crmebPlatHandle->info();
  425. if (!isset($info['status']) || $info['status'] != 200) {
  426. $info = [];
  427. } else {
  428. $info = $info['data'];
  429. }
  430. } catch (\Throwable $e) {
  431. $info = [];
  432. }
  433. $this->assign('info', $info);
  434. return $this->fetch();
  435. }
  436. /**
  437. * 处理开通短信服务
  438. */
  439. public function go_sms_open()
  440. {
  441. list($sign) = parent::postMore([
  442. ['sign', '']
  443. ], null, true);
  444. if (!$sign) {
  445. return Json::fail('请输入短信签名');
  446. }
  447. try {
  448. $sign = $this->smsHandle->setSign($sign)->open();
  449. if (!isset($sign['status']) || $sign['status'] != 200) {
  450. return Json::fail($sign['msg']);
  451. } else {
  452. return Json::successful('开通成功,可以在短信账户中查看');
  453. }
  454. } catch (\Throwable $e) {
  455. return Json::fail('开通失败或服务已开通');
  456. }
  457. }
  458. /**
  459. * 短信账户信息
  460. */
  461. public function sms_info()
  462. {
  463. return Json::successful($this->smsHandle->info());
  464. }
  465. /**
  466. * 修改签名页面
  467. * @return string
  468. * @throws \Exception
  469. */
  470. public function sms_modify()
  471. {
  472. $this->assign('account', $this->account);
  473. return $this->fetch();
  474. }
  475. /**
  476. * 处理修改签名
  477. */
  478. public function go_sms_modify()
  479. {
  480. list($sign, $phone, $verify_code) = parent::postMore([
  481. ['sign', ''],
  482. ['phone', ''],
  483. ['verify_code', ''],
  484. ], null, true);
  485. if (!$sign) {
  486. return Json::fail('请输入短信签名');
  487. }
  488. if (!isset($phone) || !$phone) {
  489. return Json::fail('请输入手机号');
  490. }
  491. if (!check_phone($phone)) {
  492. return Json::fail('请输入正确的手机号');
  493. }
  494. if (!isset($verify_code) || !$verify_code) {
  495. return Json::fail('请先获取短信验证码');
  496. }
  497. try {
  498. $result = $this->smsHandle->modify($sign, $phone, $verify_code);
  499. if (!isset($result['status']) || $result['status'] != 200) {
  500. return Json::fail($result['msg'] ? $result['msg'] : '发生异常,请稍后重试');
  501. } else {
  502. return Json::successful($result['msg']);
  503. }
  504. } catch (\Throwable $e) {
  505. return Json::fail($e->getMessage());
  506. }
  507. }
  508. /**
  509. * 短信模版页面
  510. */
  511. public function sms_temp()
  512. {
  513. if (!CacheService::get($this->cacheKey, '')) {
  514. return $this->redirect(Url::build('login') . '?url=sms_temp');
  515. }
  516. list($type) = parent::getMore([
  517. ['type', 'temps'],
  518. ], null, true);
  519. $this->assign('type', $type);
  520. return $this->fetch();
  521. }
  522. /**
  523. * 显示创建资源表单页.
  524. *
  525. * @return string
  526. * @throws \FormBuilder\exception\FormBuilderException
  527. */
  528. public function create()
  529. {
  530. $field = [
  531. Form::input('title', '模板名称'),
  532. Form::textarea('text', '模板内容示例', '您的验证码是:{$code},有效期为{$time}分钟。如非本人操作,可不用理会。(模板中的{$code}和{$time}需要替换成对应的变量,请开发者知晓。修改此项无效!)')->readonly(true),
  533. Form::input('content', '模板内容')->type('textarea'),
  534. Form::radio('type', '模板类型', 1)->options([['label' => '验证码', 'value' => 1], ['label' => '通知', 'value' => 2], ['label' => '推广', 'value' => 3]])
  535. ];
  536. $form = Form::make_post_form('申请短信模板', $field, Url::build('go_sms_temps_apply'), 2);
  537. $this->assign(compact('form'));
  538. return $this->fetch('public/form-builder');
  539. }
  540. /**
  541. * 短信模版
  542. */
  543. public function get_sms_temps()
  544. {
  545. list($page, $limit, $temp_type) = parent::getMore([
  546. ['page', 1],
  547. ['limit', 20],
  548. ['temp_type', ''],
  549. ], null, true);
  550. $data = $this->smsHandle->temps($page, $limit, $temp_type);
  551. if (!isset($data['status']) || $data['status'] != 200) {
  552. return Json::fail($data['msg']);
  553. } else {
  554. $sms_platform_selection = SystemConfigService::get('sms_platform_selection');
  555. $smsTemplateCode = SystemConfigService::get('smsTemplateCode');
  556. if ($sms_platform_selection == 2) {
  557. foreach ($data['data']['data'] as &$value) {
  558. if ($value['temp_id'] == $smsTemplateCode) {
  559. $value['is_use'] = 1;
  560. } else {
  561. $value['is_use'] = 0;
  562. }
  563. }
  564. }
  565. return Json::successlayui($data['data']);
  566. }
  567. }
  568. /**
  569. * 使用短信模板
  570. */
  571. public function sms_temp_use()
  572. {
  573. list($temp_id) = parent::getMore([
  574. ['temp_id', 0],
  575. ], null, true);
  576. if ($sms_platform_selection = SystemConfigService::get('sms_platform_selection') != 1) {
  577. $info = $this->crmebPlatHandle->info();
  578. if (!isset($info['status']) || $info['status'] != 200) {
  579. $info = [];
  580. } else {
  581. $info = $info['data'];
  582. }
  583. $res1 = SystemConfigService::setOneValue('smsTemplateCode', $temp_id);
  584. $res2 = SystemConfigService::setOneValue('smsSignName', $info['sms']['sign']);
  585. $res = $res1 && $res2;
  586. if ($res) {
  587. return Json::successful('设置成功');
  588. } else {
  589. return Json::fail('设置失败');
  590. }
  591. } else {
  592. return Json::fail('请选择把短信平台切换成crmeb短信平台');
  593. }
  594. }
  595. /**
  596. * 短信模版申请记录
  597. */
  598. public function get_sms_appls()
  599. {
  600. list($temp_type, $page, $limit) = parent::getMore([
  601. ['temp_type', ''],
  602. ['page', 1],
  603. ['limit', 20]
  604. ], null, true);
  605. $data = $this->smsHandle->applys($temp_type, $page, $limit);
  606. if (!isset($data['status']) || $data['status'] != 200) {
  607. return Json::fail($data['msg']);
  608. } else {
  609. return Json::successlayui($data['data']);
  610. }
  611. }
  612. /**
  613. * 短信发送记录
  614. */
  615. public function sms_record()
  616. {
  617. list($record_id) = parent::getMore([
  618. ['record_id', 0],
  619. ], null, true);
  620. return Json::successful($this->smsHandle->record($record_id));
  621. }
  622. /**
  623. * 模版申请页面
  624. * @return string
  625. * @throws \Exception
  626. */
  627. public function sms_temps_apply()
  628. {
  629. return $this->fetch();
  630. }
  631. /**
  632. * 处理申请模版
  633. */
  634. public function go_sms_temps_apply()
  635. {
  636. list($type, $title, $content) = parent::postMore([
  637. ['type', 1],
  638. ['title', ''],
  639. ['content', '']
  640. ], null, true);
  641. if (!$type) {
  642. return Json::fail('请选择模版类型');
  643. }
  644. if (!$title) {
  645. return Json::fail('请输入模板名称');
  646. }
  647. if (!$content) {
  648. return Json::fail('请输入模版内容');
  649. }
  650. $data = $this->smsHandle->apply($title, $content, $type);
  651. if (!isset($data['status']) || $data['status'] != 200) {
  652. return Json::fail($data['msg']);
  653. } else {
  654. return Json::successful('申请成功');
  655. }
  656. }
  657. /**
  658. * 开通物流服务页面
  659. * @return string
  660. * @throws \Exception
  661. */
  662. public function express_open()
  663. {
  664. return $this->fetch();
  665. }
  666. /**
  667. * 处理开通物流服务
  668. */
  669. public function go_express_open()
  670. {
  671. return Json::successful('开通成功', $this->expressHandle->open());
  672. }
  673. /**
  674. * 获取快递公司列表
  675. */
  676. public function express_list()
  677. {
  678. [$type, $page, $limit] = parent::postMore([
  679. ['sign', 1],
  680. ['page', 1],
  681. ['limit', 10]
  682. ], null, true);
  683. return Json::successful($this->expressHandle->express($type, $page, $limit));
  684. }
  685. /**
  686. * 获取电子面单模版
  687. */
  688. public function express_temp()
  689. {
  690. [$com, $page, $limit] = parent::postMore([
  691. ['com', 0],
  692. ['page', 1],
  693. ['limit', 10]
  694. ], null, true);
  695. if (!$com) {
  696. return Json::fail('请选择快递');
  697. }
  698. return Json::successful($this->expressHandle->temp($com, $page, $limit));
  699. }
  700. }