Config.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\admin\model\Config as ConfigModel;
  5. use app\admin\validate\ConfigValidate;
  6. use Exception;
  7. use think\facade\Db;
  8. use think\facade\Cache;
  9. class Config extends BaseController
  10. {
  11. /**
  12. * @api {get} /config/list 系统参数列表
  13. *
  14. */
  15. public function list()
  16. {
  17. $flag = $this->request->param('flag', 1);
  18. $list = ConfigModel::where('flag', $flag)->select();
  19. $select = ConfigModel::getSelect($this->lang, true);
  20. foreach ($list as &$item) {
  21. if (isset($select[$item['field']])) {
  22. $item['select'] = $select[$item['field']];
  23. }
  24. }
  25. return $this->success(['count' => count($list), 'list' => $list]);
  26. }
  27. /**
  28. * @api {post} /config/update 更新系统配置
  29. */
  30. function update()
  31. {
  32. Db::startTrans();
  33. try {
  34. $id = $this->request->post('id');
  35. if ($id) {
  36. $params = (new ConfigValidate())->post()->goCheck('edit');
  37. } else {
  38. $params = (new ConfigValidate())->post()->goCheck('add');
  39. }
  40. $params['language_code'] = $params['language_code'] ?? 'zh';
  41. if (!empty($id)) {
  42. $exist = ConfigModel::where('id', $id)->find();
  43. if (!$exist) {
  44. return $this->error('配置不存在');
  45. }
  46. $params['val'] = json_encode($params['val']);
  47. ConfigModel::where('id', $id)->update($params);
  48. } else {
  49. ConfigModel::create($params);
  50. }
  51. Db::commit();
  52. } catch (Exception $e) {
  53. Db::rollBack();
  54. return $this->error($e->getMessage());
  55. }
  56. //删除缓存
  57. Cache::delete('systemInfo');
  58. return $this->success();
  59. }
  60. }