// common-page/pages/web-view/index.js const Parse = getApp().Parse; const company = getApp().globalData.company; Page({ /** * 页面的初始数据 */ data: { path: "", currentTitle: "", // 当前标题 }, // 标题轮询定时器 titlePollingTimer: null, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { // 解码 URL let path = decodeURIComponent(options.path || ''); // 拼接额外参数 let hasQuery = path.indexOf('?') !== -1; let parsm = hasQuery ? '&' : '?'; let params = []; for (const key in options) { if(key != 'path' && key != 'url'){ params.push(key + '=' + options[key]); } } if(params.length > 0) { parsm = parsm + params.join('&'); path = path + parsm; } this.setData({ path: path }) // 立即设置标题 const passedStoreName = options.storeName ? decodeURIComponent(options.storeName) : ''; const passedStoreId = options.storeId || ''; if (passedStoreName) { this.setNavigationTitle(passedStoreName); } // 异步加载完整店铺信息(作为备份) this.loadAndSetStoreTitle(passedStoreId, passedStoreName); // 启动标题轮询监听 this.startTitlePolling(); }, onReady: function () { }, onShow: function () { this.startTitlePolling(); }, onHide: function () { this.stopTitlePolling(); }, onUnload: function () { this.stopTitlePolling(); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, /** * 处理来自 H5 页面的消息 */ handleMessage: function (e) { try { const messages = e.detail.data || []; // 找到最后一个标题更新消息 let lastTitleMessage = null; for (let i = messages.length - 1; i >= 0; i--) { const msg = messages[i]; if (msg.type === 'updateTitle' && msg.title) { lastTitleMessage = msg; break; } } // 更新标题 if (lastTitleMessage) { this.setNavigationTitle(lastTitleMessage.title); } } catch (error) { console.error('❌ 处理消息失败:', error); } }, /** * 设置导航栏标题(统一方法) */ setNavigationTitle: function (title) { if (!title) { return; } // 更新当前标题记录 this.setData({ currentTitle: title }); // 调用微信 API 设置标题 wx.setNavigationBarTitle({ title: title, success: () => { console.log('✅ 标题已更新:', title); }, fail: (err) => { console.error('❌ 标题设置失败:', err); } }); }, startTitlePolling: function () { this.stopTitlePolling(); }, stopTitlePolling: function () { if (this.titlePollingTimer) { clearInterval(this.titlePollingTimer); this.titlePollingTimer = null; } }, /** * 加载店铺信息并设置页面标题 */ loadAndSetStoreTitle: async function (storeId = '', storeName = '') { try { let finalName = storeName; if (!finalName) { // 如果没有传入名字,按传入的 storeId 精确查询;再不行按 company 兜底 if (storeId) { const q = new Parse.Query('ShopStore'); const s = await q.get(storeId); if (s) finalName = s.get('storeName') || ''; } if (!finalName) { const storeQuery = new Parse.Query('ShopStore'); storeQuery.equalTo('company', company); storeQuery.ascending('score'); storeQuery.limit(1); const store = await storeQuery.first(); if (store) finalName = store.get('storeName') || ''; } } if (!finalName) return; // 使用统一的设置标题方法 this.setNavigationTitle(finalName); } catch (e) { console.error('设置 web-view 标题失败:', e); } } })