index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. this.setData({
  105. currentTitle: title
  106. });
  107. // 调用微信 API 设置标题
  108. wx.setNavigationBarTitle({
  109. title: title,
  110. success: () => {
  111. console.log('✅ 标题已更新:', title);
  112. },
  113. fail: (err) => {
  114. console.error('❌ 标题设置失败:', err);
  115. }
  116. });
  117. },
  118. startTitlePolling: function () {
  119. this.stopTitlePolling();
  120. },
  121. stopTitlePolling: function () {
  122. if (this.titlePollingTimer) {
  123. clearInterval(this.titlePollingTimer);
  124. this.titlePollingTimer = null;
  125. }
  126. },
  127. /**
  128. * 加载店铺信息并设置页面标题
  129. */
  130. loadAndSetStoreTitle: async function (storeId = '', storeName = '') {
  131. try {
  132. let finalName = storeName;
  133. if (!finalName) {
  134. // 如果没有传入名字,按传入的 storeId 精确查询;再不行按 company 兜底
  135. if (storeId) {
  136. const q = new Parse.Query('ShopStore');
  137. const s = await q.get(storeId);
  138. if (s) finalName = s.get('storeName') || '';
  139. }
  140. if (!finalName) {
  141. const storeQuery = new Parse.Query('ShopStore');
  142. storeQuery.equalTo('company', company);
  143. storeQuery.ascending('score');
  144. storeQuery.limit(1);
  145. const store = await storeQuery.first();
  146. if (store) finalName = store.get('storeName') || '';
  147. }
  148. }
  149. if (!finalName) return;
  150. // 使用统一的设置标题方法
  151. this.setNavigationTitle(finalName);
  152. } catch (e) {
  153. console.error('设置 web-view 标题失败:', e);
  154. }
  155. }
  156. })