index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // common-page/pages/web-view/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. path: "",
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad: function (options) {
  15. console.log('===========================================');
  16. console.log('======= web-view 页面加载 =======');
  17. console.log('===========================================');
  18. console.log('📦 接收到的 options:', options);
  19. // 解码 URL(如果被编码了)
  20. let path = decodeURIComponent(options.path || '');
  21. console.log('🔓 解码后的 path:', path);
  22. console.log(' - path 长度:', path.length);
  23. // 检查 URL 中是否包含 token
  24. if (path.includes('token=')) {
  25. console.log('✅ URL 中包含 token 参数');
  26. const tokenMatch = path.match(/token=([^&]+)/);
  27. if (tokenMatch && tokenMatch[1]) {
  28. console.log('🔑 提取到的 token (前20字符):', tokenMatch[1].substring(0, 20));
  29. }
  30. } else {
  31. console.log('⚠️ URL 中没有 token 参数!');
  32. }
  33. // 如果还有额外的参数(除了 path 和 url),拼接到 URL 后面
  34. let hasQuery = path.indexOf('?') !== -1;
  35. let parsm = hasQuery ? '&' : '?';
  36. let params = [];
  37. for (const key in options) {
  38. if(key != 'path' && key != 'url'){
  39. params.push(key + '=' + options[key]);
  40. console.log(` - 额外参数: ${key} = ${options[key]}`);
  41. }
  42. }
  43. // 只有当有额外参数时才拼接
  44. if(params.length > 0) {
  45. parsm = parsm + params.join('&');
  46. path = path + parsm;
  47. console.log('🔗 拼接额外参数后的 path:', path);
  48. }
  49. console.log('🌐 最终加载的 URL:', path);
  50. console.log('===========================================');
  51. this.setData({
  52. path: path
  53. })
  54. // ✅ 立即设置标题(优先使用传入的 storeName)
  55. const passedStoreName = options.storeName ? decodeURIComponent(options.storeName) : '';
  56. const passedStoreId = options.storeId || '';
  57. console.log('🏷️ 接收到的店铺信息:');
  58. console.log(' - storeId:', passedStoreId);
  59. console.log(' - storeName (原始):', options.storeName);
  60. console.log(' - storeName (解码):', passedStoreName);
  61. // 立即同步设置标题
  62. if (passedStoreName) {
  63. console.log('🎯 立即设置标题为:', passedStoreName);
  64. wx.setNavigationBarTitle({
  65. title: passedStoreName,
  66. success: () => {
  67. console.log('✅✅✅ 标题设置成功:', passedStoreName);
  68. },
  69. fail: (err) => {
  70. console.error('❌❌❌ 标题设置失败:', err);
  71. }
  72. });
  73. }
  74. // 异步加载完整店铺信息(作为备份)
  75. this.loadAndSetStoreTitle(passedStoreId, passedStoreName)
  76. },
  77. /**
  78. * 生命周期函数--监听页面初次渲染完成
  79. */
  80. onReady: function () {
  81. console.log('📌 onReady: 页面渲染完成');
  82. // 不在这里设置,避免覆盖 onLoad 中的设置
  83. },
  84. /**
  85. * 生命周期函数--监听页面显示
  86. */
  87. onShow: function () {
  88. console.log('📌 onShow: 页面显示');
  89. // 不在这里设置,避免覆盖 onLoad 中的设置
  90. },
  91. /**
  92. * 生命周期函数--监听页面隐藏
  93. */
  94. onHide: function () {
  95. },
  96. /**
  97. * 生命周期函数--监听页面卸载
  98. */
  99. onUnload: function () {
  100. },
  101. /**
  102. * 页面相关事件处理函数--监听用户下拉动作
  103. */
  104. onPullDownRefresh: function () {
  105. },
  106. /**
  107. * 页面上拉触底事件的处理函数
  108. */
  109. onReachBottom: function () {
  110. },
  111. /**
  112. * 用户点击右上角分享
  113. */
  114. onShareAppMessage: function () {
  115. },
  116. /**
  117. * 加载店铺信息并设置页面标题
  118. */
  119. loadAndSetStoreTitle: async function (storeId = '', storeName = '') {
  120. try {
  121. let finalName = storeName;
  122. if (!finalName) {
  123. // 如果没有传入名字,按传入的 storeId 精确查询;再不行按 company 兜底
  124. if (storeId) {
  125. const q = new Parse.Query('ShopStore');
  126. const s = await q.get(storeId);
  127. if (s) finalName = s.get('storeName') || '';
  128. }
  129. if (!finalName) {
  130. const storeQuery = new Parse.Query('ShopStore');
  131. storeQuery.equalTo('company', company);
  132. storeQuery.ascending('score');
  133. storeQuery.limit(1);
  134. const store = await storeQuery.first();
  135. if (store) finalName = store.get('storeName') || '';
  136. }
  137. }
  138. if (!finalName) return;
  139. // 延迟到本帧结束设置标题,避免被覆盖
  140. setTimeout(() => {
  141. const pages = getCurrentPages();
  142. const current = pages[pages.length - 1];
  143. console.log('📌 web-view 当前页面路由:', current && current.route);
  144. wx.setNavigationBarTitle({
  145. title: finalName
  146. })
  147. }, 0)
  148. } catch (e) {
  149. console.error('设置 web-view 标题失败:', e);
  150. }
  151. }
  152. })