Config.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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($this->lang, true);
  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. if (!empty($id)) {
  41. $exist = ConfigModel::where('id', $id)->find();
  42. if (!$exist) {
  43. return $this->error('配置不存在');
  44. }
  45. ConfigModel::where('id', $id)->update($params);
  46. } else {
  47. ConfigModel::create($params);
  48. }
  49. DB::commit();
  50. } catch (Exception $e) {
  51. DB::rollBack();
  52. return $this->error($e->getMessage());
  53. }
  54. return $this->success();
  55. }
  56. }