Config.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\common\controller\AdminController;
  4. use app\http\IResponse;
  5. use Lettered\Support\OTP\Google2FA;
  6. class Config extends AdminController
  7. {
  8. /**
  9. *
  10. * @author 许祖兴 < zuxing.xu@lettered.cn>
  11. * @date 2020/3/28 12:23
  12. *
  13. * @return array|\PDOStatement|string|\think\Collection
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. * @throws \think\exception\DbException
  17. */
  18. public function webconfig()
  19. {
  20. $m = model('common/SystemConfig');
  21. $configs = $m->parseConfig($m->where(['web' => 1])
  22. ->order(['sort' => 'asc','id' => 'asc'])
  23. ->select()->toArray());
  24. $configs['otp'] = Google2FA::oath_hotp(Google2FA::base32_decode('PSP4RVMHXSWQH57U6AAYKNRO25'));
  25. return IResponse::success($configs);
  26. }
  27. /**
  28. * 配置列表
  29. *
  30. * @author 许祖兴 < zuxing.xu@lettered.cn>
  31. * @date 2020/3/18 11:08
  32. *
  33. * @return \think\response\Json
  34. * @throws \think\exception\DbException
  35. */
  36. public function index()
  37. {
  38. // 接收数据
  39. $params = $this->request->param();
  40. if (isset($params['group']) && $params != ''){
  41. $configs = model('common/SystemConfig')->where(['group' => $params['group']])
  42. ->order(['sort' => 'asc','id' => 'asc'])
  43. ->paginate(input('limit'));
  44. // 更新缓存
  45. model('common/SystemConfig')->getAppConfig('', true);
  46. return IResponse::paginate($configs);
  47. }
  48. $tabData= [];
  49. foreach (config('develop.config_group') as $key => $value) {
  50. $tabData[] = [
  51. 'name' => $value,
  52. 'policy' => $key
  53. ];
  54. }
  55. return IResponse::success($tabData);
  56. }
  57. /**
  58. * 新增配置
  59. *
  60. * @author 许祖兴 < zuxing.xu@lettered.cn>
  61. * @date 2020/3/18 11:18
  62. *
  63. * @return \think\response\Json
  64. */
  65. public function save()
  66. {
  67. // 接收数据
  68. $params = $this->request->param();
  69. // 数据校验
  70. $valid = $this->validate($params, [
  71. 'group|配置组' => 'require',
  72. 'name|配置名称' => 'require|alphaDash|unique:system_config',
  73. 'title|配置标题' => 'require',
  74. 'type|配置类型' => 'require',
  75. 'sort|排序值' => 'require',
  76. ], [
  77. 'name.alphaDash' => '配置名称仅支持英文!',
  78. 'name.unique' => '配置名称已存在!'
  79. ]);
  80. // 错误返回
  81. (true !== $valid) && IResponse::failure($valid);
  82. // 保存数据
  83. $res = model('common/SystemConfig')->storeBy($params);
  84. return $res ? IResponse::success([],'新增配置成功成功'):
  85. IResponse::failure('新增配置异常');
  86. }
  87. /**
  88. * 更新配置
  89. *
  90. * @author 许祖兴 < zuxing.xu@lettered.cn>
  91. * @date 2020/3/18 11:18
  92. *
  93. * @param $id
  94. * @return \think\response\Json
  95. */
  96. public function update($id)
  97. {
  98. // 接收数据
  99. $params = $this->request->param();
  100. // 数据校验
  101. if (isset($params['sort']) && $params['sort'] != '') {
  102. $valid = $this->validate($params, [
  103. 'sort|配置排序' => 'require|integer'
  104. ]);
  105. }elseif (isset($params['status']) && $params['status'] != '') {
  106. $valid = $this->validate($params, [
  107. 'status|配置状态' => 'require|integer'
  108. ]);
  109. }else {
  110. $valid = $this->validate($params, [
  111. 'group|配置组' => 'require',
  112. 'name|配置名称' => 'require|alphaDash',
  113. 'title|配置标题' => 'require',
  114. 'type|配置类型' => 'require',
  115. 'sort|排序值' => 'require',
  116. ], [
  117. 'name.alphaDash' => '配置名称仅支持英文!'
  118. ]);
  119. }
  120. // 校验失败
  121. (true !== $valid) && IResponse::failure($valid);
  122. // 查改
  123. $role = model('common/SystemConfig')->findBy($id);
  124. $role->updateBy($id, $params);
  125. return IResponse::success('更新配置信息成功');
  126. }
  127. /**
  128. * 删除配置
  129. *
  130. * @author 许祖兴 < zuxing.xu@lettered.cn>
  131. * @date 2020/3/18 11:18
  132. *
  133. * @param $id
  134. * @return \think\response\Json
  135. */
  136. public function delete($id)
  137. {
  138. $result = model('common/SystemConfig')::destroy($id);
  139. return IResponse::success($result,'删除系统配置成功');
  140. }
  141. /**
  142. * 配置批量操作
  143. *
  144. * @author 许祖兴 < zuxing.xu@lettered.cn>
  145. * @date 2020/3/23 11:38
  146. *
  147. * @return mixed
  148. */
  149. public function plectron(){
  150. // 收参数
  151. $params = $this->request->param();
  152. foreach (str2arr($params['ids']) as $id){
  153. $config = model('common/SystemConfig')->getBy($id);
  154. if ($config->system == 1){
  155. return IResponse::failure('系统配置,禁止操作');
  156. }
  157. if ($this->request->isDelete()){
  158. $config->deleteBy($id);
  159. return IResponse::success([],'删除配置成功');
  160. }
  161. $config->allowField(true)->updateBy($id, $params);
  162. }
  163. return IResponse::success([],'操作成功');
  164. }
  165. }