share.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const ORIGIN = 'https://www.yuban.co';
  2. const PAGE = 'common-page/pages/web-view/index';
  3. const HOME = ORIGIN + '/home';
  4. const ID = /^[a-zA-Z0-9_-]{1,128}$/;
  5. const REPORTS = {
  6. '/ai-report': 'mental-health',
  7. '/ai-life-attitude': 'life-attitude',
  8. '/ai-emotion-changes': 'emotion-change',
  9. '/ai-personality': 'personality',
  10. '/ai-custom-analysis': 'custom-analysis',
  11. };
  12. function unpack(value) {
  13. if (typeof value !== 'string') return '';
  14. if (/^https?:\/\//i.test(value)) return value;
  15. try { return decodeURIComponent(value); } catch (_) { return ''; }
  16. }
  17. // Use the Mini Program JS runtime's string primitives (URL is not available on all clients).
  18. // Rebuild the public destination from known route fields; a web-view URL can contain login data.
  19. function publicDestination(value) {
  20. const match = /^https:\/\/www\.yuban\.co(\/[^?#]*)?(?:\?([^#]*))?(?:#.*)?$/i.exec(unpack(value));
  21. if (!match) return HOME;
  22. const path = (match[1] || '/').replace(/\/$/, '') || '/';
  23. const query = Object.create(null);
  24. for (const part of (match[2] || '').split('&')) {
  25. const at = part.indexOf('=');
  26. if (at < 1) continue;
  27. try { query[decodeURIComponent(part.slice(0, at))] = decodeURIComponent(part.slice(at + 1)); }
  28. catch (_) { /* Ignore malformed optional fields. */ }
  29. }
  30. if (path === '/community') return ORIGIN + path + (query.tab === 'topic' ? '?tab=topic' : '');
  31. const detail = /^\/(story|article|topic|family-preview)\/([a-zA-Z0-9_-]{1,128})$/.exec(path);
  32. let kind;
  33. let id;
  34. let report;
  35. if (detail) {
  36. kind = detail[1] === 'article' ? 'story' : detail[1];
  37. id = detail[2];
  38. } else if (Object.prototype.hasOwnProperty.call(REPORTS, path)) {
  39. kind = 'ai-report';
  40. id = query.articleId;
  41. report = REPORTS[path];
  42. if (path === '/ai-custom-analysis' && query.reportType === 'interview-outline') report = query.reportType;
  43. } else if (path === '/' || path === '/startup') {
  44. kind = query.shareType;
  45. id = query.shareId;
  46. report = query.reportType;
  47. }
  48. if (!['story', 'topic', 'family-preview', 'ai-report'].includes(kind) || !id || !ID.test(id)) return HOME;
  49. const url = ORIGIN + '/?shareType=' + kind + '&shareId=' + id;
  50. if (kind !== 'ai-report') return url;
  51. const known = Object.values(REPORTS).includes(report) || report === 'interview-outline';
  52. return url + '&reportType=' + (known ? report : 'mental-health');
  53. }
  54. function shareCard(currentUrl) {
  55. const destination = publicDestination(currentUrl);
  56. return {
  57. title: destination === ORIGIN + '/community?tab=topic' ? '语伴 · 专题广场'
  58. : destination === ORIGIN + '/community' ? '语伴 · 社区广场' : '自传语伴',
  59. path: '/' + PAGE + '?path=' + encodeURIComponent(destination) + '&fromShare=1&skipAuth=true',
  60. };
  61. }
  62. function isLegacyChatEntry() {
  63. if (typeof wx.getEnterOptionsSync !== 'function' ||
  64. typeof getCurrentPages !== 'function' || getCurrentPages().length !== 1) return false;
  65. const entry = wx.getEnterOptionsSync() || {};
  66. return String(entry.path || '').replace(/^\//, '') === PAGE &&
  67. [1007, 1008, 1044, 1096].includes(Number(entry.scene));
  68. }
  69. module.exports = { publicDestination, shareCard, isLegacyChatEntry };