model = new \app\admin\model\Agreement; } /** * 协议与隐私编辑工作台 */ public function index() { $agreements = AgreementService::getAllForAdmin(); $this->view->assign([ 'agreements' => $agreements, 'agreementsJson' => json_encode($agreements, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP), 'canSave' => $this->auth->check('agreement/save'), ]); return $this->view->fetch(); } /** * 保存协议内容与元数据 */ public function save() { if (false === $this->request->isPost()) { $this->error(__('Invalid parameters')); } $this->token(); $code = trim((string)$this->request->post('code', '')); // 读取未剥离标签的原始数组,由 AgreementService::sanitizeHtml 执行严格 XSS 白名单净化 $row = $this->request->post('row/a', [], null); if ($code === '' || !is_array($row) || empty($row)) { $this->error('提交的协议数据不能为空', null, ['token' => $this->request->token()]); } try { $saved = AgreementService::saveByCode($code, $row); } catch (\InvalidArgumentException $e) { $this->error($e->getMessage(), null, ['token' => $this->request->token()]); } catch (\Throwable $e) { $this->error('保存协议失败:' . $e->getMessage(), null, ['token' => $this->request->token()]); } $this->success('协议内容已保存并同步生效', null, [ 'token' => $this->request->token(), 'agreement' => $saved, ]); } }