SystemPlat.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. <?php
  2. namespace app\admin\controller\setting;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\system\SystemConfig as ConfigModel;
  5. use app\models\system\Cache as CacheModel;
  6. use crmeb\services\{CacheService, ExpressService, FormBuilder, JsonService as Json, product\Product, UtilService};
  7. use EasyWeChat\Js\Js;
  8. use think\facade\Route as Url;
  9. use crmeb\services\CrmebPlatService;
  10. use crmeb\services\sms\Sms;
  11. use crmeb\services\express\Express;
  12. /**
  13. * crmeb 平台
  14. * Class SystemPlat
  15. * @package app\admin\controller\setting
  16. */
  17. class SystemPlat extends AuthController
  18. {
  19. protected $account = NULL;
  20. protected $secret = NULL;
  21. /**
  22. * @var $crmebPlatHandle
  23. */
  24. protected $crmebPlatHandle;
  25. /**
  26. * @var $smsHandle
  27. */
  28. protected $smsHandle;
  29. /**
  30. * @var $expressHandle
  31. */
  32. protected $expressHandle;
  33. /**
  34. * @var $productHandle
  35. */
  36. protected $productHandle;
  37. protected $allowAction = ['index', 'verify', 'login', 'go_login', 'register', 'go_register', 'modify', 'go_modify', 'forget', 'go_forget', 'loginOut', 'meal', 'sms_temp'];
  38. /**
  39. * @var string
  40. */
  41. protected $cacheTokenPrefix = "_crmeb_plat";
  42. protected $cacheKey;
  43. protected function initialize()
  44. {
  45. parent::initialize();
  46. $this->account = sys_config('sms_account');
  47. $this->secret = sys_config('sms_token');
  48. $config = ['account' => $this->account, 'secret' => $this->secret];
  49. $this->crmebPlatHandle = new CrmebPlatService();
  50. $this->smsHandle = new Sms('sms', $config);
  51. $this->expressHandle = new Express('express', $config);
  52. $this->productHandle = new Product('copy', $config);
  53. $this->cacheKey = md5($this->account . '_' . $this->secret . $this->cacheTokenPrefix);
  54. }
  55. /**
  56. * 显示资源列表
  57. *
  58. * @return \think\Response
  59. */
  60. public function index()
  61. {
  62. if (!CacheModel::getDbCache($this->cacheKey, '')) {
  63. return redirect(Url('login')->build() . '?url=index');
  64. }
  65. [$out, $type] = UtilService::postMore([
  66. ['out', 0],
  67. ['type', 'sms']
  68. ], null, true);
  69. try {
  70. $info = $this->crmebPlatHandle->info();
  71. } catch (\Throwable $e) {
  72. $info = [];
  73. }
  74. $this->assign('info', $info);
  75. $this->assign('type', $type);
  76. if ($out == 0 && $info) {
  77. return $this->fetch();
  78. } else {
  79. $this->assign('account', $this->account);
  80. $this->assign('password', $this->secret);
  81. return $this->fetch('login');
  82. }
  83. }
  84. /**
  85. * 获取短信验证码
  86. */
  87. public function verify()
  88. {
  89. [$phone] = UtilService::postMore([
  90. ['phone', '']
  91. ], null, true);
  92. if (!$phone) {
  93. return Json::fail('请输入手机号');
  94. }
  95. if (!check_phone($phone)) {
  96. return Json::fail('请输入正确的手机号');
  97. }
  98. $this->crmebPlatHandle->code($phone);
  99. return Json::success('获取成功');
  100. }
  101. /**
  102. * 登录页面
  103. * @return string
  104. * @throws \Exception
  105. */
  106. public function login()
  107. {
  108. $this->assign('account', $this->account);
  109. $this->assign('password', $this->secret);
  110. return $this->fetch();
  111. }
  112. /**
  113. * 退出登录
  114. * @return string
  115. * @throws \Exception
  116. */
  117. public function loginOut()
  118. {
  119. CacheModel::delectDbCache($this->cacheKey);
  120. return Json::success('退出成功', $this->crmebPlatHandle->loginOut());
  121. }
  122. /**
  123. * 登录逻辑
  124. */
  125. public function go_login()
  126. {
  127. $data = UtilService::postMore([
  128. ['account', ''],
  129. ['password', '']
  130. ]);
  131. if (!$data['account']) {
  132. return Json::fail('请输入账号');
  133. }
  134. if (!$data['password']) {
  135. return Json::fail('请输入秘钥');
  136. }
  137. $this->save_basics(['sms_account' => $data['account'], 'sms_token' => $data['password']]);
  138. $token = $this->crmebPlatHandle->login($data['account'], $data['password']);
  139. CacheModel::setDbCache($this->cacheKey, $token, 0);
  140. return Json::success('登录成功', $token);
  141. }
  142. /**
  143. * 注册页面
  144. * @return string
  145. * @throws \Exception
  146. */
  147. public function register()
  148. {
  149. return $this->fetch();
  150. }
  151. /**
  152. * 注册逻辑
  153. */
  154. public function go_register()
  155. {
  156. $data = UtilService::postMore([
  157. ['account', ''],
  158. ['phone', ''],
  159. ['password', ''],
  160. ['verify_code', ''],
  161. ]);
  162. if (!$data['account']) {
  163. return Json::fail('请输入账号');
  164. }
  165. if (!$data['phone']) {
  166. return Json::fail('请输入手机号');
  167. }
  168. if (!check_phone($data['phone'])) {
  169. return Json::fail('请输入正确的手机号');
  170. }
  171. if (!$data['password']) {
  172. return Json::fail('请设置秘钥');
  173. }
  174. if (strlen($data['password']) < 6 || strlen($data['password']) > 32) {
  175. return Json::fail('密码长度6~32位');
  176. }
  177. if (!$data['verify_code']) {
  178. return Json::fail('请先获取短信验证码');
  179. }
  180. $result = $this->crmebPlatHandle->register($data['account'], $data['phone'], $data['password'], $data['verify_code']);
  181. $this->save_basics(['sms_account' => $data['account'], 'sms_token' => $data['password']]);
  182. return Json::success('注册成功', $result);
  183. }
  184. /**
  185. * 修改秘钥页面
  186. * @return string
  187. * @throws \Exception
  188. */
  189. public function modify()
  190. {
  191. $this->assign('account', $this->account);
  192. return $this->fetch();
  193. }
  194. /**
  195. * 修改秘钥逻辑
  196. */
  197. public function go_modify()
  198. {
  199. $data = UtilService::postMore([
  200. ['account', ''],
  201. ['phone', ''],
  202. ['password', ''],
  203. ['verify_code', ''],
  204. ]);
  205. if (!$data['account']) {
  206. return Json::fail('请输入账号');
  207. }
  208. if (!$data['phone']) {
  209. return Json::fail('请输入手机号');
  210. }
  211. if (!check_phone($data['phone'])) {
  212. return Json::fail('请输入正确的手机号');
  213. }
  214. if (!$data['password']) {
  215. return Json::fail('请设置秘钥');
  216. }
  217. if (strlen($data['password']) < 6 || strlen($data['password']) > 32) {
  218. return Json::fail('密码长度6~32位');
  219. }
  220. if (!$data['verify_code']) {
  221. return Json::fail('请先获取短信验证码');
  222. }
  223. $result = $this->crmebPlatHandle->modify($data['account'], $data['phone'], $data['password'], $data['verify_code']);
  224. $this->save_basics(['sms_account' => $data['account'], 'sms_token' => $data['password']]);
  225. return Json::success('修改成功', $result);
  226. }
  227. /**
  228. * 找回账号
  229. * @return string
  230. * @throws \Exception
  231. */
  232. public function forget()
  233. {
  234. return $this->fetch();
  235. }
  236. /**
  237. * 找回账号逻辑
  238. */
  239. public function go_fotget()
  240. {
  241. $data = $where = UtilService::postMore([
  242. ['phone', ''],
  243. ['verify_code', ''],
  244. ]);
  245. if (!isset($data['phone']) || $data['phone']) {
  246. return Json::fail('请输入手机号');
  247. }
  248. if (!check_phone($data['phone'])) {
  249. return Json::fail('请输入正确的手机号');
  250. }
  251. if (!isset($data['verify_code']) || $data['verify_code']) {
  252. return Json::fail('请先获取短信验证码');
  253. }
  254. $result = $this->crmebPlatHandle->fotget($data['phone'], $data['verify_code']);
  255. return Json::success('修改成功', $result);
  256. }
  257. /**
  258. * 获取消费记录
  259. */
  260. public function record()
  261. {
  262. [$type, $page, $limit] = UtilService::getMore([
  263. ['type', 'sms'],
  264. ['page', 1],
  265. ['limit', 20]
  266. ], null, true);
  267. $result = $this->crmebPlatHandle->record($type, $page, $limit);
  268. if ($type == 'expr_query') {
  269. $express = ExpressService::expressList();
  270. $express = array_combine(array_column($express, 'code'), $express);
  271. foreach ($result['data'] as $key => $value) {
  272. $result['data'][$key]['name'] = $express[$value['code']]['name'] ?? '';
  273. $result['data'][$key]['num'] = $value['content']['num'] ?? '';
  274. }
  275. }
  276. return Json::successlayui($result);
  277. }
  278. /**
  279. * @return string
  280. * @throws \Exception
  281. */
  282. public function meal()
  283. {
  284. if (!CacheModel::getDbCache($this->cacheKey, '')) {
  285. return redirect(Url('login')->build() . '?url=meal');
  286. }
  287. return $this->fetch();
  288. }
  289. /**
  290. * 获取套餐列表
  291. */
  292. public function get_meal()
  293. {
  294. [$type] = UtilService::getMore([
  295. ['type', 'sms']
  296. ], null, true);
  297. return Json::success($this->crmebPlatHandle->meal($type));
  298. }
  299. /**
  300. * 获取支付二维码
  301. * @return string
  302. * @throws \Exception
  303. */
  304. public function pay()
  305. {
  306. [$meal_id, $price, $num, $type, $pay_type] = UtilService::postMore([
  307. ['meal_id', 0],
  308. ['price', ''],
  309. ['num', 0],
  310. ['type', ''],
  311. ['pay_type', 'weixin']
  312. ], null, true);
  313. if (!$meal_id) {
  314. return Json::fail('请选择套餐');
  315. }
  316. return Json::success($this->crmebPlatHandle->pay($type, $meal_id, $price, $num, $pay_type));
  317. }
  318. /**
  319. * 保存一号通配置
  320. */
  321. public function save_basics($data)
  322. {
  323. if ($data) {
  324. CacheService::clear();
  325. foreach ($data as $k => $v) {
  326. ConfigModel::edit(['value' => json_encode($v)], $k, 'menu_name');
  327. }
  328. }
  329. return true;
  330. }
  331. /**
  332. * 开通短信服务页面
  333. * @return string
  334. * @throws \Exception
  335. */
  336. public function sms_open()
  337. {
  338. try {
  339. $info = $this->crmebPlatHandle->info();
  340. } catch (\Throwable $e) {
  341. $info = [];
  342. }
  343. $this->assign('info', $info);
  344. return $this->fetch();
  345. }
  346. /**
  347. * 处理开通短信服务
  348. */
  349. public function go_sms_open()
  350. {
  351. [$sign] = UtilService::postMore([
  352. ['sign', '']
  353. ], null, true);
  354. if (!$sign) {
  355. return Json::fail('请输入短信签名');
  356. }
  357. return Json::success('开通成功', $this->smsHandle->setSign($sign)->open());
  358. }
  359. /**
  360. * 短信账户信息
  361. */
  362. public function sms_info()
  363. {
  364. return Json::success($this->smsHandle->info());
  365. }
  366. /**
  367. * 修改签名页面
  368. * @return string
  369. * @throws \Exception
  370. */
  371. public function sms_modify()
  372. {
  373. return $this->fetch();
  374. }
  375. /**
  376. * 处理修改签名
  377. */
  378. public function go_sms_modify()
  379. {
  380. [$sign] = UtilService::postMore([
  381. ['sign', '']
  382. ], null, true);
  383. if (!$sign) {
  384. return Json::fail('请输入短信签名');
  385. }
  386. return Json::success($this->smsHandle->modify($sign));
  387. }
  388. /**
  389. * 短信模版页面
  390. */
  391. public function sms_temp()
  392. {
  393. if (!CacheModel::getDbCache($this->cacheKey, '')) {
  394. return redirect(Url('login')->build() . '?url=sms_temp');
  395. }
  396. [$type] = UtilService::getMore([
  397. ['type', 'temps'],
  398. ], null, true);
  399. $this->assign('type', $type);
  400. return $this->fetch();
  401. }
  402. /**
  403. * 显示创建资源表单页.
  404. *
  405. * @return string
  406. * @throws \FormBuilder\exception\FormBuilderException
  407. */
  408. public function create()
  409. {
  410. $field = [
  411. FormBuilder::input('title', '模板名称'),
  412. FormBuilder::textarea('text', '模板内容示例', '您的验证码是:{$code},有效期为{$time}分钟。如非本人操作,可不用理会。(模板中的{$code}和{$time}需要替换成对应的变量,请开发者知晓。修改此项无效!)')->readonly(true),
  413. FormBuilder::input('content', '模板内容')->type('textarea'),
  414. FormBuilder::radio('type', '模板类型', 1)->options([['label' => '验证码', 'value' => 1], ['label' => '通知', 'value' => 2], ['label' => '推广', 'value' => 3]])
  415. ];
  416. $form = FormBuilder::make_post_form('申请短信模板', $field, Url::buildUrl('go_sms_temps_apply'), 2);
  417. $this->assign(compact('form'));
  418. return $this->fetch('public/form-builder');
  419. }
  420. /**
  421. * 短信模版
  422. */
  423. public function get_sms_temps()
  424. {
  425. [$page, $limit, $temp_type] = UtilService::getMore([
  426. ['page', 1],
  427. ['limit', 20],
  428. ['temp_type', ''],
  429. ], null, true);
  430. return Json::successlayui($this->smsHandle->temps($page, $limit, $temp_type));
  431. }
  432. /**
  433. * 短信模版申请记录
  434. */
  435. public function get_sms_appls()
  436. {
  437. [$temp_type, $page, $limit] = UtilService::getMore([
  438. ['temp_type', ''],
  439. ['page', 1],
  440. ['limit', 20]
  441. ], null, true);
  442. return Json::successlayui($this->smsHandle->applys($temp_type, $page, $limit));
  443. }
  444. /**
  445. * 短信发送记录
  446. */
  447. public function sms_record()
  448. {
  449. [$record_id] = UtilService::getMore([
  450. ['record_id', 0],
  451. ], null, true);
  452. return Json::success($this->smsHandle->record($record_id));
  453. }
  454. /**
  455. * 模版申请页面
  456. * @return string
  457. * @throws \Exception
  458. */
  459. public function sms_temps_apply()
  460. {
  461. return $this->fetch();
  462. }
  463. /**
  464. * 处理申请模版
  465. */
  466. public function go_sms_temps_apply()
  467. {
  468. [$type, $title, $content] = UtilService::getMore([
  469. ['type', 1],
  470. ['title', ''],
  471. ['content', '']
  472. ], null, true);
  473. if (!$type) {
  474. return Json::fail('请选择模版类型');
  475. }
  476. if (!$title) {
  477. return Json::fail('请输入模版标题');
  478. }
  479. if (!$content) {
  480. return Json::fail('请输入模版内容');
  481. }
  482. $this->smsHandle->apply($title, $content, $type);
  483. return Json::success('申请成功');
  484. }
  485. /**
  486. * 开通物流服务页面
  487. * @return string
  488. * @throws \Exception
  489. */
  490. public function express_open()
  491. {
  492. return $this->fetch();
  493. }
  494. /**
  495. * 处理开通物流服务
  496. */
  497. public function go_express_open()
  498. {
  499. return Json::success('开通成功', $this->expressHandle->open());
  500. }
  501. /**
  502. * 获取快递公司列表
  503. */
  504. public function express_list()
  505. {
  506. [$type, $page, $limit] = UtilService::postMore([
  507. ['sign', 1],
  508. ['page', 1],
  509. ['limit', 10]
  510. ], null, true);
  511. return Json::success($this->expressHandle->express($type, $page, $limit));
  512. }
  513. /**
  514. * 获取电子面单模版
  515. */
  516. public function express_temp()
  517. {
  518. [$com, $page, $limit] = UtilService::postMore([
  519. ['com', 0],
  520. ['page', 1],
  521. ['limit', 10]
  522. ], null, true);
  523. if (!$com) {
  524. return Json::fail('请选择快递');
  525. }
  526. return Json::success($this->expressHandle->temp($com, $page, $limit));
  527. }
  528. /**
  529. * 开通复制商品
  530. */
  531. public function go_copy_open()
  532. {
  533. return Json::success($this->productHandle->open());
  534. }
  535. }