文件

74 行
2.3 KiB
PHP

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use app\common\library\AgreementService;
/**
* 协议与隐私管理
*
* 统一编辑管理平台《用户服务协议》《隐私保护指引》《师傅入驻协议》《商家协同协议》,
* 保存后实时同步至小程序与 H5 独立协议展示页面。
*
* @icon fa fa-file-text-o
*/
class Agreement extends Backend
{
/**
* @var \app\admin\model\Agreement
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->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,
]);
}
}