Config.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. class Config extends BaseController
  9. {
  10. /**
  11. * @api {get} /config/list 系统参数列表
  12. *
  13. */
  14. public function list()
  15. {
  16. $flag = $this->request->param('flag', 1);
  17. $list = ConfigModel::where('flag', $flag)->select();
  18. $select = ConfigModel::getSelect();
  19. foreach ($list as &$item) {
  20. if (isset($select[$item['field']])) {
  21. $item['select'] = $select[$item['field']];
  22. }
  23. }
  24. return $this->success(['count' => count($list), 'list' => $list]);
  25. }
  26. /**
  27. * @api {post} /config/update 更新系统配置
  28. */
  29. function update()
  30. {
  31. DB::startTrans();
  32. try {
  33. $id = $this->request->post('id');
  34. if ($id) {
  35. $params = (new ConfigValidate())->post()->goCheck('edit');
  36. } else {
  37. $params = (new ConfigValidate())->post()->goCheck('add');
  38. }
  39. $params['language_code'] = $params['language_code'] ?? 'zh';
  40. // $params['val'] = json_encode($params['val'], JSON_UNESCAPED_UNICODE);
  41. if (!empty($id)) {
  42. $exist = ConfigModel::where('id', $id)->find();
  43. if (!$exist) {
  44. return $this->error('配置不存在');
  45. }
  46. ConfigModel::where('id', $id)->update($params);
  47. } else {
  48. ConfigModel::create($params);
  49. }
  50. DB::commit();
  51. } catch (Exception $e) {
  52. DB::rollBack();
  53. return $this->error($e->getMessage());
  54. }
  55. return $this->success();
  56. }
  57. }