index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. currentTitle: "", // 当前标题
  11. },
  12. // 标题轮询定时器
  13. titlePollingTimer: null,
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad: function (options) {
  18. // 解码 URL
  19. let path = decodeURIComponent(options.path || '');
  20. // 拼接额外参数
  21. let hasQuery = path.indexOf('?') !== -1;
  22. let parsm = hasQuery ? '&' : '?';
  23. let params = [];
  24. for (const key in options) {
  25. if(key != 'path' && key != 'url'){
  26. params.push(key + '=' + options[key]);
  27. }
  28. }
  29. if(params.length > 0) {
  30. parsm = parsm + params.join('&');
  31. path = path + parsm;
  32. }
  33. this.setData({
  34. path: path
  35. })
  36. // 立即设置标题
  37. const passedStoreName = options.storeName ? decodeURIComponent(options.storeName) : '';
  38. const passedStoreId = options.storeId || '';
  39. if (passedStoreName) {
  40. this.setNavigationTitle(passedStoreName);
  41. }
  42. // 异步加载完整店铺信息(作为备份)
  43. this.loadAndSetStoreTitle(passedStoreId, passedStoreName);
  44. // 启动标题轮询监听
  45. this.startTitlePolling();
  46. },
  47. onReady: function () {
  48. },
  49. onShow: function () {
  50. this.startTitlePolling();
  51. },
  52. onHide: function () {
  53. this.stopTitlePolling();
  54. },
  55. onUnload: function () {
  56. this.stopTitlePolling();
  57. },
  58. /**
  59. * 页面相关事件处理函数--监听用户下拉动作
  60. */
  61. onPullDownRefresh: function () {
  62. },
  63. /**
  64. * 页面上拉触底事件的处理函数
  65. */
  66. onReachBottom: function () {
  67. },
  68. /**
  69. * 用户点击右上角分享
  70. */
  71. onShareAppMessage: function () {
  72. },
  73. /**
  74. * 处理来自 H5 页面的消息
  75. */
  76. handleMessage: function (e) {
  77. try {
  78. const messages = e.detail.data || [];
  79. // 找到最后一个标题更新消息
  80. let lastTitleMessage = null;
  81. for (let i = messages.length - 1; i >= 0; i--) {
  82. const msg = messages[i];
  83. if (msg.type === 'updateTitle' && msg.title) {
  84. lastTitleMessage = msg;
  85. break;
  86. }
  87. }
  88. // 更新标题
  89. if (lastTitleMessage) {
  90. this.setNavigationTitle(lastTitleMessage.title);
  91. }
  92. } catch (error) {
  93. console.error('❌ 处理消息失败:', error);
  94. }
  95. },
  96. /**
  97. * 设置导航栏标题(统一方法)
  98. */
  99. setNavigationTitle: function (title) {
  100. if (!title) {
  101. return;
  102. }
  103. // 若与当前标题一致则跳过,避免频繁触发
  104. if (title === this.data.currentTitle) {
  105. return;
  106. }
  107. // 简单节流:500ms 内重复更新跳过
  108. if (!this._lastTitleUpdateTs) {
  109. this._lastTitleUpdateTs = 0;
  110. }
  111. const now = Date.now();
  112. if (now - this._lastTitleUpdateTs < 500) {
  113. return;
  114. }
  115. this._lastTitleUpdateTs = now;
  116. // 更新当前标题记录
  117. this.setData({
  118. currentTitle: title
  119. });
  120. // 调用微信 API 设置标题
  121. wx.setNavigationBarTitle({
  122. title: title,
  123. success: () => {},
  124. fail: (err) => {
  125. console.error('❌ 标题设置失败:', err);
  126. }
  127. });
  128. },
  129. startTitlePolling: function () {
  130. this.stopTitlePolling();
  131. },
  132. stopTitlePolling: function () {
  133. if (this.titlePollingTimer) {
  134. clearInterval(this.titlePollingTimer);
  135. this.titlePollingTimer = null;
  136. }
  137. },
  138. /**
  139. * 加载店铺信息并设置页面标题
  140. */
  141. loadAndSetStoreTitle: async function (storeId = '', storeName = '') {
  142. try {
  143. let finalName = storeName;
  144. if (!finalName) {
  145. // 如果没有传入名字,按传入的 storeId 精确查询;再不行按 company 兜底
  146. if (storeId) {
  147. const q = new Parse.Query('ShopStore');
  148. const s = await q.get(storeId);
  149. if (s) finalName = s.get('storeName') || '';
  150. }
  151. if (!finalName) {
  152. const storeQuery = new Parse.Query('ShopStore');
  153. storeQuery.equalTo('company', company);
  154. storeQuery.ascending('score');
  155. storeQuery.limit(1);
  156. const store = await storeQuery.first();
  157. if (store) finalName = store.get('storeName') || '';
  158. }
  159. }
  160. if (!finalName) return;
  161. // 使用统一的设置标题方法
  162. this.setNavigationTitle(finalName);
  163. } catch (e) {
  164. console.error('设置 web-view 标题失败:', e);
  165. }
  166. }
  167. })