index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const Parse = getApp().Parse;
  2. const company = getApp().globalData.company;
  3. const getTabs = require("../../../utils/getTabs")
  4. const login = require("../../../utils/login");
  5. const dateF = require("../../../utils/date")
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad: async function (options) {
  16. // login.loginNow()
  17. try {
  18. let finalStoreId = options && options.storeId ? options.storeId : '';
  19. // 标记是否是扫码进入(有明确的 storeId 参数)
  20. const isFromScan = !!finalStoreId;
  21. // 支持从 scene 读取(小程序码 getwxacodeunlimit 传参)
  22. if (!finalStoreId && options && options.scene) {
  23. const sceneStr = decodeURIComponent(options.scene);
  24. console.log('🎯 scene 参数:', sceneStr);
  25. if (sceneStr) {
  26. const pairs = sceneStr.split('&');
  27. for (const p of pairs) {
  28. const [k, v] = p.split('=');
  29. if (k === 'storeId' && v) {
  30. finalStoreId = v;
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. if (finalStoreId) {
  37. // 扫码进入时,强制设置店铺 ID,不被历史记录覆盖
  38. wx.setStorageSync('storeId', finalStoreId);
  39. getApp().globalData.storeId = finalStoreId;
  40. // 标记这是扫码进入的店铺,优先级最高
  41. if (isFromScan) {
  42. wx.setStorageSync('storeId_from_scan', true);
  43. console.log('✅ 扫码进入,已设置店铺 ID(优先级最高):', finalStoreId);
  44. } else {
  45. console.log('✅ 已设置店铺 ID:', finalStoreId);
  46. }
  47. } else {
  48. // 没有传入 storeId,检查是否有扫码标记
  49. const isFromScanBefore = wx.getStorageSync('storeId_from_scan');
  50. if (!isFromScanBefore) {
  51. // 不是扫码进入,可以使用历史店铺
  52. console.log('ℹ️ 未传入店铺 ID,使用历史店铺或默认值');
  53. } else {
  54. // 之前是扫码进入的,保持扫码的店铺
  55. console.log('ℹ️ 保持扫码进入的店铺 ID');
  56. }
  57. }
  58. } catch (e) {
  59. console.error('设置店铺 ID 失败:', e);
  60. }
  61. // ✅ 立即加载并设置店铺名称作为页面标题
  62. await this.loadAndSetStoreTitle()
  63. },
  64. /**
  65. * 生命周期函数--监听页面初次渲染完成
  66. */
  67. onReady: function () {
  68. console.log('📌 引导页 onReady');
  69. },
  70. /**
  71. * 生命周期函数--监听页面显示
  72. */
  73. onShow: function () {
  74. },
  75. /**
  76. * 生命周期函数--监听页面隐藏
  77. */
  78. onHide: function () {
  79. },
  80. /**
  81. * 生命周期函数--监听页面卸载
  82. */
  83. onUnload: function () {
  84. },
  85. /**
  86. * 页面相关事件处理函数--监听用户下拉动作
  87. */
  88. onPullDownRefresh: function () {
  89. wx.reLaunch({
  90. url: '/index',
  91. });
  92. },
  93. /**
  94. * 页面上拉触底事件的处理函数
  95. */
  96. onReachBottom: function () {
  97. },
  98. /**
  99. * 用户点击右上角分享
  100. */
  101. onShareAppMessage: function () {
  102. let uid = Parse.User.current().id
  103. return {
  104. title: '分享',
  105. // path: `index?invite=${uid}`,
  106. path: `index`,
  107. imageUrl: '',
  108. }
  109. },
  110. /**
  111. * 加载店铺信息并设置页面标题
  112. */
  113. loadAndSetStoreTitle: async function () {
  114. try {
  115. // 计算当前有效的店铺 ID
  116. const defaultStoreId = 'pkPdAnLAUZ'; // 超级门店ID
  117. const effectiveStoreId = wx.getStorageSync('storeId') || getApp().globalData.storeId || defaultStoreId;
  118. // 查询指定的店铺
  119. const storeQuery = new Parse.Query('ShopStore');
  120. storeQuery.equalTo('objectId', effectiveStoreId);
  121. const store = await storeQuery.first();
  122. if (store) {
  123. // 优先使用门店地址,如果没有地址则使用门店名称
  124. const storeAddress = store.get('address');
  125. const storeName = store.get('storeName');
  126. const title = storeAddress || storeName;
  127. if (title) {
  128. console.log('📍 门店信息:', {
  129. id: effectiveStoreId,
  130. name: storeName,
  131. address: storeAddress,
  132. displayTitle: title
  133. });
  134. // 延迟设置标题,确保页面已完全加载
  135. setTimeout(() => {
  136. wx.setNavigationBarTitle({
  137. title: title,
  138. success: () => {
  139. console.log('✅ 引导页标题设置成功:', title);
  140. },
  141. fail: (err) => {
  142. console.warn('⚠️ 引导页标题设置失败(可忽略):', err.errMsg);
  143. // 不影响主流程,静默失败
  144. }
  145. });
  146. }, 300);
  147. }
  148. }
  149. } catch (error) {
  150. console.error('❌ 加载店铺信息失败:', error);
  151. }
  152. }
  153. })