SystemPlat.php 20 KB

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