| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- // 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 || '');
- console.log('===========================================');
- console.log('======= web-view 页面加载 =======');
- console.log('原始 options.path:', options.path);
- console.log('解码后的 path:', path);
- console.log('===========================================');
- // 拼接额外参数(避免重复添加已存在的参数)
- let hasQuery = path.indexOf('?') !== -1;
- let parsm = hasQuery ? '&' : '?';
- let params = [];
-
- // 提取 path 中已有的参数
- let existingParams = new Set();
- if (hasQuery) {
- const queryString = path.split('?')[1];
- if (queryString) {
- queryString.split('&').forEach(param => {
- const key = param.split('=')[0];
- existingParams.add(key);
- });
- }
- }
- // 只添加 path 中不存在的参数
- for (const key in options) {
- if(key != 'path' && key != 'url' && !existingParams.has(key)){
- params.push(key + '=' + options[key]);
- }
- }
- if(params.length > 0) {
- parsm = parsm + params.join('&');
- path = path + parsm;
- }
- console.log('最终 web-view URL:', path);
- console.log('URL 长度:', path.length);
- console.log('===========================================');
- 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;
- }
- // 若与当前标题一致则跳过,避免频繁触发
- if (title === this.data.currentTitle) {
- return;
- }
- // 简单节流:500ms 内重复更新跳过
- if (!this._lastTitleUpdateTs) {
- this._lastTitleUpdateTs = 0;
- }
- const now = Date.now();
- if (now - this._lastTitleUpdateTs < 500) {
- return;
- }
- this._lastTitleUpdateTs = now;
- // 更新当前标题记录
- this.setData({
- currentTitle: title
- });
- // 延迟调用微信 API 设置标题,确保页面已准备好
- setTimeout(() => {
- wx.setNavigationBarTitle({
- title: title,
- success: () => {
- console.log('✅ web-view 标题设置成功:', title);
- },
- fail: (err) => {
- console.warn('⚠️ web-view 标题设置失败(可忽略):', err.errMsg);
- // 不影响主流程,静默失败
- }
- });
- }, 100);
- },
- startTitlePolling: function () {
- this.stopTitlePolling();
- },
- stopTitlePolling: function () {
- if (this.titlePollingTimer) {
- clearInterval(this.titlePollingTimer);
- this.titlePollingTimer = null;
- }
- },
- /**
- * 加载店铺信息并设置页面标题
- */
- loadAndSetStoreTitle: async function (storeId = '', storeName = '') {
- try {
- let finalTitle = storeName;
- if (!finalTitle) {
- // 如果没有传入名字,按传入的 storeId 精确查询;再不行按 company 兜底
- if (storeId) {
- const q = new Parse.Query('ShopStore');
- const s = await q.get(storeId);
- if (s) {
- // 优先使用门店地址,如果没有地址则使用门店名称
- const address = s.get('address');
- const name = s.get('storeName');
- finalTitle = address || name || '';
-
- console.log('📍 web-view 门店信息:', {
- id: storeId,
- name: name,
- address: address,
- displayTitle: finalTitle
- });
- }
- }
- if (!finalTitle) {
- const storeQuery = new Parse.Query('ShopStore');
- storeQuery.equalTo('company', company);
- storeQuery.ascending('score');
- storeQuery.limit(1);
- const store = await storeQuery.first();
- if (store) {
- // 优先使用门店地址,如果没有地址则使用门店名称
- const address = store.get('address');
- const name = store.get('storeName');
- finalTitle = address || name || '';
- }
- }
- }
- if (!finalTitle) return;
- // 使用统一的设置标题方法
- this.setNavigationTitle(finalTitle);
- } catch (e) {
- console.error('设置 web-view 标题失败:', e);
- }
- }
- })
|