Config.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. $params['val'] = $params['type'] == 'rich_text' ? json_encode($params['val']) : $params['val'];
  42. if (!empty($id)) {
  43. $exist = ConfigModel::where('id', $id)->find();
  44. if (!$exist) {
  45. return $this->error('配置不存在');
  46. }
  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. //删除客服接数量上限的缓存
  59. if (isset($params['field']) && $params['field'] == 'kefu_chat_max') {
  60. Cache::delete('config_kefu_chat_max');
  61. }
  62. return $this->success();
  63. }
  64. }