Config.php 2.6 KB

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