SmsConfig.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\notification\sms;
  12. use app\services\system\config\SystemConfigServices;
  13. use app\services\yihaotong\SmsAdminServices;
  14. use app\services\serve\ServeServices;
  15. use app\adminapi\controller\AuthController;
  16. use crmeb\services\CacheService;
  17. use think\facade\App;
  18. /**
  19. * 短信配置
  20. * Class SmsConfig
  21. * @package app\admin\controller\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. * @throws \Psr\SimpleCache\InvalidArgumentException
  40. */
  41. public function save_basics()
  42. {
  43. [$account, $token] = $this->request->postMore([
  44. ['sms_account', ''],
  45. ['sms_token', '']
  46. ], true);
  47. $this->validate(['sms_account' => $account, 'sms_token' => $token], \app\adminapi\validate\notification\SmsConfigValidate::class);
  48. if ($this->services->login($account, $token)) {
  49. return app('json')->success(400139);
  50. } else {
  51. return app('json')->fail(400140);
  52. }
  53. }
  54. /**
  55. * 检测登录
  56. * @param ServeServices $services
  57. * @return mixed
  58. * @throws \Psr\SimpleCache\InvalidArgumentException
  59. */
  60. public function is_login(ServeServices $services)
  61. {
  62. $configServices = app()->make(SystemConfigServices::class);
  63. $sms_info = CacheService::get('sms_account');
  64. $data = ['status' => false, 'info' => ''];
  65. if ($sms_info) {
  66. try {
  67. $result = $services->user()->getUser();
  68. } catch (\Throwable $e) {
  69. $result = [];
  70. }
  71. if (!$result) {
  72. $this->logout();
  73. } else {
  74. $data['status'] = true;
  75. $data['info'] = $sms_info;
  76. }
  77. return app('json')->success($data);
  78. } else {
  79. CacheService::clear();
  80. $account = sys_config('sms_account');
  81. $password = sys_config('sms_token');
  82. //没有退出登录 清空这两个数据 自动登录
  83. if ($account && $password) {
  84. $res = $services->user()->login($account, $password);
  85. if ($res) {
  86. CacheService::set('sms_account', $account);
  87. $data['status'] = true;
  88. $data['info'] = $account;
  89. }
  90. }
  91. }
  92. return app('json')->success($data);
  93. }
  94. /**
  95. * 退出
  96. * @return mixed
  97. * @throws \Psr\SimpleCache\InvalidArgumentException
  98. */
  99. public function logout()
  100. {
  101. CacheService::delete('sms_account');
  102. $this->services->updateSmsConfig('', '');
  103. CacheService::clear();
  104. return app('json')->success(100042);
  105. }
  106. /**
  107. * 短信发送记录
  108. * @param ServeServices $services
  109. * @return mixed
  110. */
  111. public function record(ServeServices $services)
  112. {
  113. [$page, $limit, $status] = $this->request->getMore([
  114. [['page', 'd'], 0],
  115. [['limit', 'd'], 10],
  116. ['type', '', '', 'status'],
  117. ], true);
  118. return app('json')->success($services->user()->record($page, $limit, 1, $status));
  119. }
  120. /**
  121. * 获取当前登陆的短信账号信息
  122. * @return mixed
  123. */
  124. public function data()
  125. {
  126. return app('json')->success($this->services->getSmsData());
  127. }
  128. }