'user_service', 'service' => 'user_service', 'user_service' => 'user_service', 'privacy' => 'privacy', 'master' => 'master', 'merchant' => 'merchant', ]; if (isset($aliasMap[$type])) { return $aliasMap[$type]; } // 支持自定义合法编码(仅允许小写字母/数字/下划线) return preg_match('/^[a-z0-9_]{2,32}$/', $type) ? $type : 'user_service'; } /** * 返回默认协议字典定义(按权重降序) */ public static function defaultDefinitions(): array { $today = date('Y-m-d'); return [ 'user_service' => [ 'code' => 'user_service', 'legacy_key' => 'agreement_service', 'tab_name' => '用户服务协议', 'desc' => '在小程序登录页、个人中心「协议与规则」中展示', 'title' => '优匠优服用户服务协议', 'version' => 'v1.0', 'effective_date' => $today, 'weigh' => 100, 'status' => 'normal', 'content' => '
1. 优匠优服(以下简称“本平台”)依法为个人业主、企业商家与认证技能师傅提供上门安装、维修、拆旧及维保信息撮合与交易资金托管服务。
2. 您在注册、登录或下单使用本平台服务前,应当仔细阅读并充分理解本协议全部条款。
1. 一口价项目按平台页面公示标准计费;师傅报价订单由接单师傅根据现场工况公开报价,经客户确认并支付后生效;自主定价订单按客户确认的订单金额执行。
2. 如施工现场存在特殊工况需增项补差,须经双方提前沟通一致,并由师傅在工单内发起规范补差申请,客户核实无误后通过平台支付。
1. 订单支付资金由平台合规托管,师傅完成施工并上传现场完工照片后,经客户验收确认(或向师傅提供 6 位核销码完成核验)后触发结算。
2. 为保障您的售后权益、施工质保与资金安全,请全程在平台内完成沟通、下单、补差与支付,切勿脱离平台私下转账交易。
', ], 'privacy' => [ 'code' => 'privacy', 'legacy_key' => 'agreement_privacy', 'tab_name' => '隐私保护指引', 'desc' => '在小程序登录页、个人中心「协议与规则」中展示', 'title' => '优匠优服隐私保护指引', 'version' => 'v1.0', 'effective_date' => $today, 'weigh' => 90, 'status' => 'normal', 'content' => '1. 本平台遵循“合法、正当、必要、诚信”原则收集和使用您的个人信息,仅在为您提供同城上门安装、维修服务所必需的场景下收集相关数据。
2. 账号与联系信息:在您授权登录或下单时,我们需要收集您的手机号码及联系人称呼,用于账号识别、订单通知及师傅上门沟通。
3. 服务地址与位置信息:在您创建工单或管理地址簿时,我们需要收集上门服务地址及定位信息,以便为您匹配就近认证师傅并核验服务范围。
1. 您的真实手机号码、门牌号等敏感联系信息,仅对已正式接单履约的当单认证师傅可见;订单未被接单前及非当单人员均无法获取,平台全程采用脱敏展示。
1. 本平台全链路采用加密传输与存储技术,严格管控内部数据访问权限,绝不向无关第三方共享或出售您的个人信息。
您可随时在小程序「个人中心」查询、修改个人资料及管理服务地址,或联系平台客服咨询、撤回授权。
', ], 'master' => [ 'code' => 'master', 'legacy_key' => 'agreement_master', 'tab_name' => '师傅入驻协议', 'desc' => '在师傅实名入驻申请页中展示', 'title' => '师傅入驻与履约服务协议', 'version' => 'v1.0', 'effective_date' => $today, 'weigh' => 80, 'status' => 'normal', 'content' => '1. 申请入驻的师傅须提交本人真实有效身份证件及服务技能信息,严禁冒用他人资质。
1. 接单后须在平台规定时效内联系客户确认上门时间,准时到达现场并规范施工。
1. 如需现场增项补差,必须提前向客户说明并在工单内发起补差申请,严禁私自索要额外费用。
1. 施工完成后须上传真实清晰的现场完工照片,凭客户验收或核销码触发系统结算。
', ], 'merchant' => [ 'code' => 'merchant', 'legacy_key' => 'agreement_merchant', 'tab_name' => '商家协同协议', 'desc' => '在企业商家用户的「协议与规则」中展示', 'title' => '企业商家入驻与协同服务协议', 'version' => 'v1.0', 'effective_date' => $today, 'weigh' => 70, 'status' => 'normal', 'content' => '1. 申请入驻的企业或商户须提供真实有效的营业执照与主体信息。
1. 主账号可绑定企业协同成员共同发起批量工单与跟进履约,并对企业关联工单承担管理责任。
1. 平台按实际支付与完工订单汇总企业消费明细,支持在线申请正规增值税普通发票或专用发票。
', ], ]; } /** * 读取后台管理页面所需的全部协议集合(按默认顺序与数据库合并) */ public static function getAllForAdmin(): array { $defs = self::defaultDefinitions(); $dbRows = []; try { $rows = Db::name('agreement') ->field('id,code,title,version,effective_date,content,revision,prev_content,diff_content,diff_summary,status,weigh,createtime,updatetime') ->order('weigh desc, id asc') ->select(); if (is_array($rows)) { foreach ($rows as $row) { $dbRows[$row['code']] = $row; } } } catch (\Throwable $e) { $dbRows = []; } $result = []; foreach ($defs as $code => $def) { $row = $dbRows[$code] ?? null; $legacyContent = ''; if (!empty($def['legacy_key'])) { $cfgVal = Config::get('site.' . $def['legacy_key']); if (is_string($cfgVal) && trim($cfgVal) !== '') { $legacyContent = self::plainTextToHtml(trim($cfgVal)); } } $content = $row && trim((string)$row['content']) !== '' ? (string)$row['content'] : ($legacyContent !== '' ? $legacyContent : $def['content']); $updatetime = $row && !empty($row['updatetime']) ? (int)$row['updatetime'] : time(); $revision = $row && !empty($row['revision']) ? max(1, (int)$row['revision']) : 1; $diffContent = $row ? (string)($row['diff_content'] ?? '') : ''; $diffSummary = $row ? (string)($row['diff_summary'] ?? '') : ''; $hasDiff = $revision > 1 && trim($diffContent) !== ''; $result[] = [ 'id' => $row ? (int)$row['id'] : 0, 'code' => $code, 'tab_name' => $def['tab_name'], 'desc' => $def['desc'], 'is_core' => in_array($code, self::CORE_CODES, true), 'title' => $row && trim((string)$row['title']) !== '' ? (string)$row['title'] : $def['title'], 'version' => $row && trim((string)$row['version']) !== '' ? (string)$row['version'] : $def['version'], 'effective_date' => $row && trim((string)$row['effective_date']) !== '' ? (string)$row['effective_date'] : $def['effective_date'], 'content' => $content, 'revision' => $revision, 'has_diff' => $hasDiff, 'diff_summary' => $diffSummary, 'diff_content' => $hasDiff ? $diffContent : '', 'status' => $row ? (string)$row['status'] : 'normal', 'updatetime' => $updatetime, 'update_time_text' => date('Y-m-d H:i', $updatetime), ]; } return $result; } /** * 按前端请求的 type 读取单条启用协议(API 使用,含变更首看高亮判断) */ public static function getByType(string $type, int $userId = 0): ?array { $rawType = trim($type); $code = self::canonicalCode($rawType); $defs = self::defaultDefinitions(); $row = null; try { $row = Db::name('agreement') ->field('id,code,title,version,effective_date,content,revision,prev_content,diff_content,diff_summary,status,updatetime') ->where('code', $code) ->find(); } catch (\Throwable $e) { $row = null; } if ($row) { if (($row['status'] ?? 'normal') !== 'normal') { return null; } $updatetime = (int)($row['updatetime'] ?? 0); $revision = max(1, (int)($row['revision'] ?? 1)); $content = (string)$row['content']; $prevContent = (string)($row['prev_content'] ?? ''); $diffContent = (string)($row['diff_content'] ?? ''); $diffSummary = (string)($row['diff_summary'] ?? ''); // 若存在上一版本正文但尚无预计算高亮,即时补充生成一次 if ($revision > 1 && trim($prevContent) !== '' && trim($diffContent) === '') { $computed = self::buildChangeHighlightedHtml($prevContent, $content); if ($computed['has_changes']) { $diffContent = $computed['html']; $diffSummary = $computed['summary']; } } $hasDiff = ($revision > 1 && trim($diffContent) !== ''); $showDiff = false; if ($hasDiff) { if ($userId > 0) { $readRevision = self::getUserReadRevision($userId, $code); $showDiff = ($readRevision < $revision); } else { // 未登录游客由前端结合本机已阅修订版本进行首看门控 $showDiff = true; } } return [ 'type' => $rawType !== '' ? $rawType : $code, 'code' => $code, 'title' => (string)$row['title'], 'version' => (string)($row['version'] ?? 'v1.0'), 'effective_date' => (string)($row['effective_date'] ?? ''), 'update_time' => $updatetime > 0 ? date('Y-m-d H:i', $updatetime) : '', 'content' => $content, 'revision' => $revision, 'has_diff' => $hasDiff, 'show_diff' => $showDiff, 'diff_summary' => $showDiff ? $diffSummary : '', 'diff_content' => $showDiff ? $diffContent : '', ]; } // 数据库尚未入库该协议时,仅兼容历史 site 配置;均未配置则明确返回 null,禁用静态默认文案兜底 if (!isset($defs[$code])) { return null; } $def = $defs[$code]; $cfgVal = !empty($def['legacy_key']) ? Config::get('site.' . $def['legacy_key']) : ''; if (!is_string($cfgVal) || trim($cfgVal) === '') { return null; } $content = self::plainTextToHtml(trim($cfgVal)); return [ 'type' => $rawType !== '' ? $rawType : $code, 'code' => $code, 'title' => $def['title'], 'version' => $def['version'], 'effective_date' => $def['effective_date'], 'update_time' => '', 'content' => $content, 'revision' => 1, 'has_diff' => false, 'show_diff' => false, 'diff_summary' => '', 'diff_content' => '', ]; } /** * 查询登录用户已查看过的协议最高修订序号 */ public static function getUserReadRevision(int $userId, string $code): int { if ($userId <= 0 || $code === '') { return 0; } try { $rev = Db::name('agreement_read') ->where('user_id', $userId) ->where('agreement_code', $code) ->value('read_revision'); return $rev ? (int)$rev : 0; } catch (\Throwable $e) { return 0; } } /** * 标记登录用户已首次查看当前变更版协议,防止后续打开重复高亮 */ public static function markUserRead(int $userId, string $type, int $revision = 0): array { $code = self::canonicalCode($type); $currentRev = 1; try { $dbRev = Db::name('agreement')->where('code', $code)->value('revision'); if ($dbRev) { $currentRev = max(1, (int)$dbRev); } } catch (\Throwable $e) { $currentRev = 1; } $targetRevision = ($revision > 0 && $revision <= $currentRev) ? $revision : $currentRev; if ($userId <= 0) { return [ 'recorded' => false, 'code' => $code, 'read_revision' => 0, ]; } $now = time(); $prefix = (string)Config::get('database.prefix'); $table = $prefix . 'agreement_read'; Db::execute( "INSERT INTO `{$table}` (`user_id`, `agreement_code`, `read_revision`, `read_time`, `createtime`, `updatetime`) " . "VALUES (:uid, :code, :rev, :rtime, :ctime, :utime) " . "ON DUPLICATE KEY UPDATE `read_revision` = GREATEST(`read_revision`, VALUES(`read_revision`)), " . "`read_time` = VALUES(`read_time`), `updatetime` = VALUES(`updatetime`)", [ 'uid' => $userId, 'code' => $code, 'rev' => $targetRevision, 'rtime' => $now, 'ctime' => $now, 'utime' => $now, ] ); return [ 'recorded' => true, 'code' => $code, 'read_revision' => self::getUserReadRevision($userId, $code), ]; } /** * 保存后台编辑的协议内容(含白名单校验、XSS 净化及自动生成变更背景色高亮) * * @throws \InvalidArgumentException */ public static function saveByCode(string $code, array $input): array { $defs = self::defaultDefinitions(); $code = self::canonicalCode($code); if (!isset($defs[$code])) { throw new \InvalidArgumentException('不支持的协议类型编码'); } $title = trim(strip_tags((string)($input['title'] ?? ''))); if ($title === '') { throw new \InvalidArgumentException('请填写协议标题'); } if (mb_strlen($title, 'UTF-8') > 100) { throw new \InvalidArgumentException('协议标题不能超过 100 个字符'); } $version = trim(strip_tags((string)($input['version'] ?? 'v1.0'))); if ($version === '') { $version = 'v1.0'; } if (mb_strlen($version, 'UTF-8') > 32) { throw new \InvalidArgumentException('版本号不能超过 32 个字符'); } $effectiveDate = trim(strip_tags((string)($input['effective_date'] ?? ''))); if ($effectiveDate === '') { $effectiveDate = date('Y-m-d'); } elseif (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $effectiveDate)) { throw new \InvalidArgumentException('生效日期格式须为 YYYY-MM-DD'); } $status = trim((string)($input['status'] ?? 'normal')); if (!in_array($status, ['normal', 'hidden'], true)) { $status = 'normal'; } if (in_array($code, self::CORE_CODES, true)) { // 用户服务协议与隐私保护指引为平台合规必选,始终保持启用状态 $status = 'normal'; } $rawContent = (string)($input['content'] ?? ''); $cleanContent = self::sanitizeHtml($rawContent); $plainCheck = trim(html_entity_decode(strip_tags($cleanContent), ENT_QUOTES, 'UTF-8')); $plainCheck = preg_replace('/[\s\x{00A0}]+/u', '', $plainCheck); if ($plainCheck === '') { throw new \InvalidArgumentException('协议正文不能为空,请填写具体条款内容'); } $now = time(); $existing = Db::name('agreement')->where('code', $code)->find(); if ($existing) { $oldContent = (string)($existing['content'] ?? ''); $revision = max(1, (int)($existing['revision'] ?? 1)); $prevContent = (string)($existing['prev_content'] ?? ''); $diffContent = (string)($existing['diff_content'] ?? ''); $diffSummary = (string)($existing['diff_summary'] ?? ''); // 仅当正文实质文本发生变更时才递增修订号并重建变更高亮,避免仅修改元数据或编辑器空标签微调覆盖历史差异 $oldPlainNorm = self::normalizeComparableText(strip_tags(html_entity_decode($oldContent, ENT_QUOTES, 'UTF-8'))); $newPlainNorm = self::normalizeComparableText(strip_tags(html_entity_decode($cleanContent, ENT_QUOTES, 'UTF-8'))); $isSubstantiveChange = ($oldPlainNorm !== $newPlainNorm); if ($isSubstantiveChange) { $diffRes = self::buildChangeHighlightedHtml($oldContent, $cleanContent); $revision++; $prevContent = $oldContent; $diffContent = $diffRes['has_changes'] ? $diffRes['html'] : ''; $diffSummary = $diffRes['has_changes'] ? $diffRes['summary'] : ''; } Db::name('agreement')->where('id', (int)$existing['id'])->update([ 'title' => $title, 'version' => $version, 'effective_date' => $effectiveDate, 'content' => $cleanContent, 'revision' => $revision, 'prev_content' => $prevContent, 'diff_content' => $diffContent, 'diff_summary' => $diffSummary, 'status' => $status, 'updatetime' => $now, ]); $id = (int)$existing['id']; } else { $revision = 1; $diffContent = ''; $diffSummary = ''; $id = (int)Db::name('agreement')->insertGetId([ 'code' => $code, 'title' => $title, 'version' => $version, 'effective_date' => $effectiveDate, 'content' => $cleanContent, 'revision' => $revision, 'prev_content' => '', 'diff_content' => '', 'diff_summary' => '', 'status' => $status, 'weigh' => (int)$defs[$code]['weigh'], 'createtime' => $now, 'updatetime' => $now, ]); } $hasDiff = ($revision > 1 && trim($diffContent) !== ''); return [ 'id' => $id, 'code' => $code, 'title' => $title, 'version' => $version, 'effective_date' => $effectiveDate, 'content' => $cleanContent, 'revision' => $revision, 'has_diff' => $hasDiff, 'diff_summary' => $diffSummary, 'diff_content' => $hasDiff ? $diffContent : '', 'status' => $status, 'updatetime' => $now, 'update_time_text' => date('Y-m-d H:i', $now), ]; } /** * 将纯文本换行内容转为结构化 HTML 段落 */ public static function plainTextToHtml(string $text): string { $text = str_replace(["\r\n", "\r"], "\n", trim($text)); if ($text === '') { return ''; } // 若本身已包含 HTML 块标签则直接进行安全清洗后返回 if (preg_match('/<\s*(p|h[1-6]|div|ul|ol|li|blockquote|br)\b/i', $text)) { return self::sanitizeHtml($text); } $blocks = preg_split("/\n{2,}/", $text); $html = ''; foreach ($blocks as $block) { $block = trim($block); if ($block === '') { continue; } $escaped = htmlspecialchars($block, ENT_QUOTES, 'UTF-8'); $html .= '' . str_replace("\n", '
', $escaped) . '
' . htmlspecialchars($plain, ENT_QUOTES, 'UTF-8') . '
', ]]; } [$dom, $root] = $loaded; $blocks = []; foreach ($root->childNodes as $child) { if ($child instanceof \DOMText) { $txt = trim($child->nodeValue); if ($txt === '') { continue; } $blocks[] = [ 'tag' => 'p', 'norm' => self::normalizeComparableText($txt), 'raw_text' => $txt, 'html' => '' . htmlspecialchars($txt, ENT_QUOTES, 'UTF-8') . '
', ]; continue; } if (!($child instanceof \DOMElement)) { continue; } $tag = strtolower($child->tagName); $nodeHtml = trim($dom->saveHTML($child)); if ($tag === 'hr') { $blocks[] = [ 'tag' => 'hr', 'norm' => '---hr---', 'raw_text' => '', 'html' => $nodeHtml, ]; continue; } $rawText = str_replace("\xC2\xA0", ' ', $child->textContent); $norm = self::normalizeComparableText($rawText); if ($norm === '') { continue; } $blocks[] = [ 'tag' => $tag, 'norm' => $norm, 'raw_text' => trim($rawText), 'html' => $nodeHtml, ]; } return $blocks; } /** * 递归收集容器下的所有非空 DOMText 节点并记录字符下标映射 */ protected static function collectDomTextNodes(\DOMNode $node, array &$out, int &$cursor): void { if (!$node->hasChildNodes()) { return; } foreach ($node->childNodes as $child) { if ($child instanceof \DOMText) { $val = (string)$child->nodeValue; $len = mb_strlen($val, 'UTF-8'); if ($len > 0) { $out[] = [ 'node' => $child, 'text' => $val, 'len' => $len, 'start' => $cursor, 'end' => $cursor + $len, ]; $cursor += $len; } } elseif ($child instanceof \DOMElement) { self::collectDomTextNodes($child, $out, $cursor); } } } /** * 为顶层元素注入/追加块级高亮背景样式 */ protected static function wrapElementWithBlockStyle(string $elementHtml, string $tag, string $styleAdd): string { if ($styleAdd === '') { return $elementHtml; } $loaded = self::loadHtmlFragmentDom($elementHtml); if ($loaded !== null) { [$dom, $root] = $loaded; foreach ($root->childNodes as $child) { if ($child instanceof \DOMElement) { $old = trim((string)$child->getAttribute('style')); $sep = ($old !== '' && substr($old, -1) !== ';') ? ';' : ''; $child->setAttribute('style', $old . $sep . $styleAdd); return trim(self::innerHtmlOfNode($dom, $root)); } } } return '<' . $tag . ' style="' . $styleAdd . '">' . $elementHtml . '' . $tag . '>'; } /** * 新增段落/区块浅橙高亮背景样式(针对列表项保留左缩进) */ protected static function getAddedBlockStyle(string $tag): string { $leftPad = in_array($tag, ['ul', 'ol'], true) ? '26px' : '10px'; return "background-color:#FFF5E6;border-left:3px solid #F0710D;padding:8px 10px 8px {$leftPad};border-radius:6px;"; } /** * 修改段落外层定位标识样式 */ protected static function getRevisedContainerStyle(string $tag, bool $subtleTint): string { $leftPad = in_array($tag, ['ul', 'ol'], true) ? '24px' : '10px'; if ($subtleTint) { return "background-color:#FFF9F2;border-left:3px solid #FDBA74;padding:6px 10px 6px {$leftPad};border-radius:6px;"; } return "border-left:3px solid #FDBA74;padding-left:{$leftPad};"; } /** * 规范化纯文本用于语义等价比对(忽略全角空白/多余空格) */ public static function normalizeComparableText(string $text): string { $text = str_replace("\xC2\xA0", ' ', $text); $text = preg_replace('/[\s\r\n\t]+/u', '', trim($text)); return (string)$text; } /** * 中文按单字、英文与数字按连续单词、其余标点/空白按单符切分,适合精确协议文本对比 * * @return array