const ORIGIN = 'https://www.yuban.co'; const PAGE = 'common-page/pages/web-view/index'; const HOME = ORIGIN + '/home'; const ID = /^[a-zA-Z0-9_-]{1,128}$/; const REPORTS = { '/ai-report': 'mental-health', '/ai-life-attitude': 'life-attitude', '/ai-emotion-changes': 'emotion-change', '/ai-personality': 'personality', '/ai-custom-analysis': 'custom-analysis', }; function unpack(value) { if (typeof value !== 'string') return ''; if (/^https?:\/\//i.test(value)) return value; try { return decodeURIComponent(value); } catch (_) { return ''; } } // Use the Mini Program JS runtime's string primitives (URL is not available on all clients). // Rebuild the public destination from known route fields; a web-view URL can contain login data. function publicDestination(value) { const match = /^https:\/\/www\.yuban\.co(\/[^?#]*)?(?:\?([^#]*))?(?:#.*)?$/i.exec(unpack(value)); if (!match) return HOME; const path = (match[1] || '/').replace(/\/$/, '') || '/'; const query = Object.create(null); for (const part of (match[2] || '').split('&')) { const at = part.indexOf('='); if (at < 1) continue; try { query[decodeURIComponent(part.slice(0, at))] = decodeURIComponent(part.slice(at + 1)); } catch (_) { /* Ignore malformed optional fields. */ } } if (path === '/community') return ORIGIN + path + (query.tab === 'topic' ? '?tab=topic' : ''); const detail = /^\/(story|article|topic|family-preview)\/([a-zA-Z0-9_-]{1,128})$/.exec(path); let kind; let id; let report; if (detail) { kind = detail[1] === 'article' ? 'story' : detail[1]; id = detail[2]; } else if (Object.prototype.hasOwnProperty.call(REPORTS, path)) { kind = 'ai-report'; id = query.articleId; report = REPORTS[path]; if (path === '/ai-custom-analysis' && query.reportType === 'interview-outline') report = query.reportType; } else if (path === '/' || path === '/startup') { kind = query.shareType; id = query.shareId; report = query.reportType; } if (!['story', 'topic', 'family-preview', 'ai-report'].includes(kind) || !id || !ID.test(id)) return HOME; const url = ORIGIN + '/?shareType=' + kind + '&shareId=' + id; if (kind !== 'ai-report') return url; const known = Object.values(REPORTS).includes(report) || report === 'interview-outline'; return url + '&reportType=' + (known ? report : 'mental-health'); } function shareCard(currentUrl) { const destination = publicDestination(currentUrl); return { title: destination === ORIGIN + '/community?tab=topic' ? '语伴 · 专题广场' : destination === ORIGIN + '/community' ? '语伴 · 社区广场' : '自传语伴', path: '/' + PAGE + '?path=' + encodeURIComponent(destination) + '&fromShare=1&skipAuth=true', }; } function isLegacyChatEntry() { if (typeof wx.getEnterOptionsSync !== 'function' || typeof getCurrentPages !== 'function' || getCurrentPages().length !== 1) return false; const entry = wx.getEnterOptionsSync() || {}; return String(entry.path || '').replace(/^\//, '') === PAGE && [1007, 1008, 1044, 1096].includes(Number(entry.scene)); } module.exports = { publicDestination, shareCard, isLegacyChatEntry };