SmsConfig.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\controller\admin\v1\notification\sms;
  12. use app\services\message\sms\SmsAdminServices;
  13. use app\services\serve\ServeServices;
  14. use crmeb\services\CacheService;
  15. use app\controller\admin\AuthController;
  16. use crmeb\services\SystemConfigService;
  17. use think\facade\App;
  18. /**
  19. * 短信配置
  20. * Class SmsConfig
  21. * @package app\controller\admin\v1\notification\sms
  22. */
  23. class SmsConfig extends AuthController
  24. {
  25. /**
  26. * 构造方法
  27. * SmsConfig constructor.
  28. * @param App $app
  29. * @param SmsAdminServices $services
  30. */
  31. public function __construct(App $app, SmsAdminServices $services)
  32. {
  33. parent::__construct($app);
  34. $this->services = $services;
  35. }
  36. /**
  37. * 保存短信配置
  38. * @return mixed
  39. */
  40. public function save_basics()
  41. {
  42. [$account, $token] = $this->request->postMore([
  43. ['sms_account', ''],
  44. ['sms_token', '']
  45. ], true);
  46. $this->validate(['sms_account' => $account, 'sms_token' => $token], \app\validate\admin\notification\SmsConfigValidate::class);
  47. if ($this->services->login($account, $token)) {
  48. return $this->success('登录成功');
  49. } else {
  50. return $this->fail('账号或密码错误');
  51. }
  52. }
  53. /**
  54. * 检测登录
  55. * @return mixed
  56. */
  57. public function is_login(ServeServices $services)
  58. {
  59. $sms_info = CacheService::redisHandler()->get('sms_account');
  60. $data = ['status' => false, 'info' => ''];
  61. if ($sms_info) {
  62. try {
  63. $result = $services->user()->getUser();
  64. } catch (\Throwable $e) {
  65. $result = [];
  66. }
  67. if (!$result) {
  68. $this->logout();
  69. } else {
  70. $data['status'] = true;
  71. $data['info'] = $sms_info;
  72. }
  73. return $this->success($data);
  74. } else {
  75. \crmeb\services\SystemConfigService::clear();
  76. $data = SystemConfigService::more(['sms_account', 'sms_token']);
  77. $account = $data['sms_account'] ?? '';
  78. $password = $data['sms_token'] ?? '';
  79. //没有退出登录 清空这两个数据 自动登录
  80. if ($account && $password) {
  81. try {
  82. $res = $services->user()->login($account, $password);
  83. } catch (\Throwable $e) {
  84. return $this->success(['status' => false, 'info' => '']);
  85. }
  86. if ($res) {
  87. CacheService::redisHandler()->set('sms_account', $account);
  88. $data['status'] = true;
  89. $data['info'] = $account;
  90. }
  91. }
  92. }
  93. return $this->success($data);
  94. }
  95. /**
  96. * 退出
  97. * @return mixed
  98. * @throws \Psr\SimpleCache\InvalidArgumentException
  99. */
  100. public function logout()
  101. {
  102. CacheService::redisHandler()->delete('sms_account');
  103. $this->services->updateSmsConfig('', '');
  104. return $this->success('退出成功');
  105. }
  106. /**
  107. * 短信发送记录
  108. * @return mixed
  109. */
  110. public function record(ServeServices $services)
  111. {
  112. [$page, $limit, $status] = $this->request->getMore([
  113. [['page', 'd'], 0],
  114. [['limit', 'd'], 10],
  115. ['type', '', '', 'status'],
  116. ], true);
  117. return $this->success($services->user()->record($page, $limit, 1, $status));
  118. }
  119. /**
  120. * @return mixed
  121. */
  122. public function data()
  123. {
  124. return $this->success($this->services->getSmsData());
  125. }
  126. }