SmsConfig.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace app\admin\controller\sms;
  3. use app\admin\controller\AuthController;
  4. use think\facade\Route;
  5. use app\admin\model\system\SystemConfig as ConfigModel;
  6. use crmeb\services\{FormBuilder, sms\Sms, SystemConfigService, UtilService, CacheService};
  7. /**
  8. * 短信配置
  9. * Class SmsConfig
  10. * @package app\admin\controller\sms
  11. */
  12. class SmsConfig extends AuthController
  13. {
  14. /**
  15. * @var Sms
  16. */
  17. protected $smsHandle;
  18. protected function initialize()
  19. {
  20. parent::initialize(); // TODO: Change the autogenerated stub
  21. }
  22. /**
  23. * 展示配置
  24. * @return string
  25. * @throws \FormBuilder\exception\FormBuilderException
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. * @throws \think\exception\DbException
  29. */
  30. public function index()
  31. {
  32. [$type, $tab_id] = UtilService::getMore([
  33. ['type', 0],
  34. ['tab_id', 0]
  35. ], null, true);
  36. if (!$tab_id) $tab_id = 1;
  37. $this->assign('tab_id', $tab_id);
  38. $list = ConfigModel::getAll($tab_id);
  39. if ($type == 3) {//其它分类
  40. $config_tab = null;
  41. } else {
  42. $config_tab = ConfigModel::getConfigTabAll($type);
  43. foreach ($config_tab as $kk => $vv) {
  44. $arr = ConfigModel::getAll($vv['value'])->toArray();
  45. if (empty($arr)) {
  46. unset($config_tab[$kk]);
  47. }
  48. }
  49. }
  50. $formBuilder = ConfigModel::builder_config_from_data($list);
  51. $form = FormBuilder::make_post_form('编辑配置', $formBuilder, Route::buildUrl('save_basics'));
  52. $this->assign(compact('form'));
  53. $this->assign('config_tab', $config_tab);
  54. $this->assign('list', $list);
  55. return $this->fetch();
  56. }
  57. /**
  58. * 保存配置
  59. */
  60. public function save_basics()
  61. {
  62. $request = app('request');
  63. if ($request->isPost()) {
  64. CacheService::clear();
  65. $post = $request->post();
  66. foreach ($post as $k => $v) {
  67. if (is_array($v)) {
  68. $res = ConfigModel::where('menu_name', $k)->column('upload_type', 'type');
  69. foreach ($res as $kk => $vv) {
  70. if ($kk == 'upload') {
  71. if ($vv == 1 || $vv == 3) {
  72. $post[$k] = $v[0];
  73. }
  74. }
  75. }
  76. }
  77. }
  78. foreach ($post as $k => $v) {
  79. ConfigModel::edit(['value' => json_encode($v)], $k, 'menu_name');
  80. }
  81. //添加公共短信模板
  82. $this->smsHandle = new Sms('yunxin', [
  83. 'sms_account' => SystemConfigService::get('sms_account','',true),
  84. 'sms_token' => SystemConfigService::get('sms_token','',true),
  85. 'site_url' => sys_config('site_url')
  86. ]);
  87. $templateList = $this->smsHandle->publictemp([]);
  88. if ($templateList['status'] != 400){
  89. if ($templateList['data']['data'])
  90. foreach ($templateList['data']['data'] as $v) {
  91. if ($v['is_have'] == 0)
  92. $this->smsHandle->use($v['id'], $v['templateid']);
  93. }
  94. return $this->successful('修改成功');
  95. }else{
  96. return $this->failed('账号或密码错误');
  97. }
  98. }
  99. }
  100. /**
  101. * 退出
  102. * @return mixed
  103. * @throws \Psr\SimpleCache\InvalidArgumentException
  104. */
  105. public function logout()
  106. {
  107. $post = [
  108. 'sms_account' => '',
  109. 'sms_token' => ''
  110. ];
  111. foreach ($post as $k => $v) {
  112. if (is_array($v)) {
  113. $res = ConfigModel::where('menu_name', $k)->column('upload_type', 'type');
  114. foreach ($res as $kk => $vv) {
  115. if ($kk == 'upload') {
  116. if ($vv == 1 || $vv == 3) {
  117. $post[$k] = $v[0];
  118. }
  119. }
  120. }
  121. }
  122. }
  123. foreach ($post as $k => $v) {
  124. ConfigModel::edit(['value' => json_encode($v)], $k, 'menu_name');
  125. }
  126. CacheService::clear();
  127. return redirect(url('sms.smsConfig/index') . '?type=4&tab_id=18');
  128. }
  129. }