* @Date: 2026-04-29 02:32:33 * @LastEditors: YwxApp * @LastEditTime: 2026-08-03 13:32:15 * @Description: * @FilePath: \ywxapp_dev\app\backend\controller\Configure.php * @CustomString: Copyright (c) 2026 YwxApp */ declare(strict_types=1); namespace app\backend\controller; use think\facade\View; use think\Request; use ywxapp\model\Configure as ConfigureModel; use think\facade\Db; use think\facade\Cache; use ywxapp\controller\BackendBase; /** * Configure 类 * * @author ywxapp */ class Configure extends BackendBase { /** * Summary of needLogin * @var array */ protected $noNeedLogin = []; /** * Summary of needRight * @var array */ protected $noNeedVerify = []; /** * 控制器初始化 initialize * @return void */ protected function initialize() { $this->model = new ConfigureModel; } /** * 显示资源列表 * * @param \think\Request $request * @return \think\Response */ public function index() { $page = $this->request->param('page', 1); $size = $this->request->param('size', 15); if ($this->request->isAjax()) { $data = ConfigureModel::order('id')->select(); $this->result->success($data); } View::assign('title', '登录'); return View::fetch(); } /** * 保存新建的资源 * * @param \think\Request $request * @return \think\Response */ public function update() { if (!$this->request->isPost()) { return $this->result->error('请求方式错误'); } $postData = $this->request->post(); $allConfigs = ConfigureModel::column('name,type,rule,value', 'name'); Db::startTrans(); try { foreach ($postData as $name => $value) { if (!isset($allConfigs[$name])) { continue; } $config = $allConfigs[$name]; if (!empty($config['rule'])) { $validate = validate([ $name => $config['rule'] ]); //if (!$validate->check([$name => $value])) { // throw new \Exception("配置项 [{$name}] 验证失败:" . $validate->getError()); // } } // 特殊处理:复选框数组转字符串 if (is_array($value)) { $value = implode(',', $value); } $exists = ConfigureModel::where('name', $name)->find(); if ($exists) { $exists->save(['value' => $value ?? ""]); } else { // 如果没有记录,创建新记录 ConfigureModel::create([ 'name' => $name, 'value' => $value, 'group' => $postData['group'] ?? 'default', 'type' => $config['type'] ?? 'string' ]); } } // Cache::delete('system_config_all'); Db::commit(); $this->result->success('配置保存成功'); } catch (\Exception $e) { Db::rollback(); $this->result->error('保存失败:' . $e->getMessage()); } } }