SmsConfig.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\adminapi\controller\v1\notification\sms;
  3. use app\models\sms\SmsRecord;
  4. use app\models\system\SystemConfig;
  5. use crmeb\services\CacheService;
  6. use crmeb\services\sms\Sms;
  7. use crmeb\services\UtilService;
  8. use app\adminapi\controller\AuthController;
  9. use app\models\system\SystemConfig as ConfigModel;
  10. use EasyWeChat\Foundation\Config;
  11. use think\facade\Cache;
  12. /**
  13. * 短信配置
  14. * Class SmsConfig
  15. * @package app\admin\controller\sms
  16. */
  17. class SmsConfig extends AuthController
  18. {
  19. /**
  20. * @var Sms
  21. */
  22. protected $smsHandle;
  23. /**
  24. * 保存短信配置
  25. * @return mixed
  26. */
  27. public function save_basics()
  28. {
  29. [$account, $token] = UtilService::postMore([
  30. ['sms_account', ''],
  31. ['sms_token', '']
  32. ], $this->request, true);
  33. $this->validate(['sms_account' => $account, 'sms_token' => $token], \app\adminapi\validates\notification\SmsConfigValidate::class);
  34. $this->smsHandle = new Sms('yunxin', [
  35. 'sms_account' => $account,
  36. 'sms_token' => $token,
  37. 'site_url' => sys_config('site_url')
  38. ]);
  39. ConfigModel::edit(['value' => json_encode($account)], 'sms_account', 'menu_name');
  40. ConfigModel::edit(['value' => json_encode($token)], 'sms_token', 'menu_name');
  41. //添加公共短信模板
  42. $templateList = $this->smsHandle->publictemp([]);
  43. if ($templateList['status'] != 400) {
  44. if ($templateList['data']['data']) {
  45. foreach ($templateList['data']['data'] as $v) {
  46. if ($v['is_have'] == 0)
  47. $this->smsHandle->use($v['id'], $v['templateid']);
  48. }
  49. }
  50. CacheService::redisHandler()->set('sms_account', $account);
  51. return $this->success('登录成功');
  52. } else {
  53. return $this->fail('账号或密码错误');
  54. }
  55. }
  56. /**
  57. * 检测登录
  58. * @return mixed
  59. */
  60. public function is_login()
  61. {
  62. $sms_info = CacheService::redisHandler()->get('sms_account');
  63. if ($sms_info) {
  64. return $this->success(['status' => true, 'info' => $sms_info]);
  65. } else {
  66. return $this->success(['status' => false]);
  67. }
  68. }
  69. /**
  70. * 退出
  71. * @return mixed
  72. * @throws \Psr\SimpleCache\InvalidArgumentException
  73. */
  74. public function logout()
  75. {
  76. $res = CacheService::redisHandler()->delete('sms_account');
  77. if ($res)
  78. return $this->success('退出成功');
  79. else
  80. return $this->fail('退出失败');
  81. }
  82. /**
  83. * 短信发送记录
  84. * @return mixed
  85. */
  86. public function record()
  87. {
  88. $where = UtilService::getMore([
  89. ['page', 1],
  90. ['limit', 20],
  91. ['type', '']
  92. ]);
  93. return $this->success(SmsRecord::getRecordList($where));
  94. }
  95. /**
  96. * @return mixed
  97. */
  98. public function data()
  99. {
  100. $this->smsHandle = new Sms('yunxin', [
  101. 'sms_account' => sys_config('sms_account'),
  102. 'sms_token' => sys_config('sms_token'),
  103. 'site_url' => sys_config('site_url')
  104. ]);
  105. $countInfo = $this->smsHandle->count();
  106. if ($countInfo['status'] == 400) {
  107. $info['number'] = 0;
  108. $info['total_number'] = 0;
  109. } else {
  110. $info['number'] = $countInfo['data']['number'];
  111. $info['total_number'] = $countInfo['data']['send_total'];
  112. }
  113. $info['record_number'] = SmsRecord::count();
  114. $info['sms_account'] = sys_config('sms_account');
  115. return $this->success($info);
  116. }
  117. }