Config.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. if ($item['type'] == 'rich_text') {
  25. $item['val'] = $item['val'] ? json_decode($item['val'], true) : '';
  26. }
  27. }
  28. return $this->success(['count' => count($list), 'list' => $list]);
  29. }
  30. /**
  31. * @api {post} /config/update 更新系统配置
  32. */
  33. function update()
  34. {
  35. Db::startTrans();
  36. try {
  37. $id = $this->request->post('id');
  38. if ($id) {
  39. $params = (new ConfigValidate())->post()->goCheck('edit');
  40. } else {
  41. $params = (new ConfigValidate())->post()->goCheck('add');
  42. }
  43. $params['language_code'] = $params['language_code'] ?? 'zh';
  44. if (!empty($id)) {
  45. $exist = ConfigModel::where('id', $id)->find();
  46. if (!$exist) {
  47. return $this->error('配置不存在');
  48. }
  49. $params['val'] = $exist['type'] == 'rich_text' ? json_encode($params['val']) : $params['val'];
  50. ConfigModel::where('id', $id)->update($params);
  51. } else {
  52. $params['val'] = $params['type'] == 'rich_text' ? json_encode($params['val']) : $params['val'];
  53. ConfigModel::create($params);
  54. }
  55. Db::commit();
  56. } catch (Exception $e) {
  57. Db::rollBack();
  58. return $this->error($e->getMessage());
  59. }
  60. //删除缓存
  61. Cache::delete('systemInfo');
  62. //删除客服接数量上限的缓存
  63. if (isset($params['field']) && $params['field'] == 'kefu_chat_max') {
  64. Cache::delete('config_kefu_chat_max');
  65. }
  66. return $this->success();
  67. }
  68. }