web-view-target.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Keep query strings intact on both raw and WeChat-encoded page options.
  2. function readTarget(value) {
  3. if (typeof value !== 'string' || !value || value.length > 8192) return '';
  4. let target = value;
  5. if (!target.startsWith('https://')) {
  6. try { target = decodeURIComponent(value); } catch (_) { return ''; }
  7. }
  8. if (/[\\\s\u0000-\u001f]/.test(target)) return '';
  9. const matched = /^https:\/\/([^/?#]+)([^?#]*)(?:[?#]|$)/.exec(target);
  10. if (!matched) return '';
  11. if (matched[1] === 'www.yuban.co') return target;
  12. if (matched[1] === 'app.fmode.cn' && /^\/dev\/pobingfeng\/owner\/nav\/(?:products|cases|home)\/?$/.test(matched[2])) return target;
  13. return '';
  14. }
  15. function queryKey(part) {
  16. try { return decodeURIComponent(part.split('=')[0]); } catch (_) { return ''; }
  17. }
  18. const identityKeys = new Set(['token','sessiontoken','accesstoken','access_token','refreshtoken','refresh_token','authorization','userid','mobile','openid','unionid']);
  19. function isIdentity(key) { return identityKeys.has(String(key).toLowerCase()); }
  20. function withQuery(target, values = {}, clean = false) {
  21. // Existing payment callbacks own their OAuth code and state.
  22. const payment = /^https:\/\/www\.yuban\.co\/user-profile\/(?:account-recharge|membership-renewal)\/?(?:[?#]|$)/.test(target);
  23. const omit = key => clean && (isIdentity(key) || (!payment && ['code','state'].includes(key)));
  24. const hashAt = target.indexOf('#');
  25. const url = hashAt < 0 ? target : target.slice(0, hashAt);
  26. let hash = hashAt < 0 ? '' : target.slice(hashAt);
  27. if (clean && hash.includes('?')) {
  28. const separator = hash.indexOf('?');
  29. const params = hash.slice(separator+1).split('&').filter(part => part && !omit(queryKey(part)));
  30. hash = hash.slice(0,separator) + (params.length ? '?' + params.join('&') : '');
  31. }
  32. const index = url.indexOf('?');
  33. const base = index < 0 ? url : url.slice(0, index);
  34. let parts = index < 0 ? [] : url.slice(index + 1).split('&').filter(Boolean);
  35. if (clean) parts = parts.filter(part => !omit(queryKey(part)));
  36. const keys = new Set(parts.map(queryKey));
  37. for (const [key,value] of Object.entries(values)) {
  38. if (omit(key) || keys.has(key) || value === '' || value == null) continue;
  39. parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(String(value)));
  40. }
  41. return base + (parts.length ? '?' + parts.join('&') : '') + hash;
  42. }
  43. function requestedTarget(options = {}) {
  44. const target = readTarget(options.path);
  45. if (!target) return '';
  46. const values = {};
  47. for (const [key,value] of Object.entries(options)) {
  48. if (!['path','url','skipAuth','__proto__','constructor','prototype'].includes(key) && typeof value === 'string') values[key] = value;
  49. }
  50. return withQuery(target, values, true);
  51. }
  52. function buildConversationWebViewPath() {
  53. const destination = withQuery('https://www.yuban.co/home', {
  54. fromStartup: '1', source: 'wechat-miniprogram',
  55. });
  56. return '/common-page/pages/web-view/index?path=' + encodeURIComponent(destination) + '&skipAuth=true';
  57. }
  58. module.exports = { readTarget, requestedTarget, withQuery, buildConversationWebViewPath };