Config.php 5.5 KB

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