| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace app\admin\controller;
- use app\BaseController;
- use app\admin\model\Config as ConfigModel;
- use app\admin\validate\ConfigValidate;
- use Exception;
- use think\facade\Db;
- use think\facade\Cache;
- class Config extends BaseController
- {
-
- /**
- * @api {get} /config/list 系统参数列表
- *
- */
- public function list()
- {
- $flag = $this->request->param('flag', 1);
- $list = ConfigModel::where('flag', $flag)->select();
- $select = ConfigModel::getSelect($this->lang, true);
- foreach ($list as &$item) {
- if (isset($select[$item['field']])) {
- $item['select'] = $select[$item['field']];
- }
- }
- return $this->success(['count' => count($list), 'list' => $list]);
- }
- /**
- * @api {post} /config/update 更新系统配置
- */
- function update()
- {
- Db::startTrans();
- try {
- $id = $this->request->post('id');
- if ($id) {
- $params = (new ConfigValidate())->post()->goCheck('edit');
- } else {
- $params = (new ConfigValidate())->post()->goCheck('add');
- }
- $params['language_code'] = $params['language_code'] ?? 'zh';
- $params['val'] = $params['type'] == 'rich_text' ? json_encode($params['val']) : $params['val'];
-
- if (!empty($id)) {
- $exist = ConfigModel::where('id', $id)->find();
- if (!$exist) {
- return $this->error('配置不存在');
- }
- ConfigModel::where('id', $id)->update($params);
- } else {
- ConfigModel::create($params);
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollBack();
- return $this->error($e->getMessage());
- }
- //删除缓存
- Cache::delete('systemInfo');
- //删除客服接数量上限的缓存
- if (isset($params['field']) && $params['field'] == 'kefu_chat_max') {
- Cache::delete('config_kefu_chat_max');
- }
- return $this->success();
- }
- }
|