index.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. const Parse = getApp().Parse;
  2. const company = getApp().globalData.company;
  3. const login = require("../../../utils/login");
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. },
  10. data: {
  11. // 轮播图相关数据
  12. currentSwiperIndex: 0,
  13. },
  14. lifetimes: {
  15. created() {},
  16. attached: function () {
  17. // 页面加载时检查是否首次访问
  18. this.checkFirstVisit();
  19. // 在控制台显示当前登录用户信息
  20. this.showCurrentUser();
  21. },
  22. },
  23. pageLifetimes: {
  24. show: function() {
  25. // 页面显示时,检查并更新登录状态
  26. const currentUser = Parse.User.current();
  27. if (currentUser && currentUser.get('mobile')) {
  28. const userLogin = wx.getStorageSync('userLogin');
  29. if (!userLogin || userLogin !== currentUser.id) {
  30. wx.setStorageSync("userLogin", currentUser.id);
  31. console.log('✅ 页面显示时更新 userLogin:', currentUser.id);
  32. }
  33. }
  34. // 更新显示的用户信息
  35. this.showCurrentUser();
  36. }
  37. },
  38. /**
  39. * 组件的方法列表
  40. */
  41. methods: {
  42. /**
  43. * 在控制台显示当前登录用户信息
  44. */
  45. showCurrentUser() {
  46. const currentUser = Parse.User.current();
  47. console.log('===========================================');
  48. console.log('======= 当前登录用户信息 =======');
  49. console.log('===========================================');
  50. if (currentUser) {
  51. console.log('✅ 用户已登录');
  52. console.log('📱 用户ID:', currentUser.id);
  53. console.log('📞 手机号:', currentUser.get('mobile') || '未设置');
  54. console.log('👤 用户名:', currentUser.get('username') || '未设置');
  55. console.log('🔑 Session Token:', currentUser.getSessionToken());
  56. console.log('📧 邮箱:', currentUser.get('email') || '未设置');
  57. console.log('📦 完整用户对象:', currentUser);
  58. console.log('📋 所有属性:', currentUser.attributes);
  59. } else {
  60. console.log('❌ 当前没有登录用户');
  61. }
  62. console.log('===========================================');
  63. },
  64. /**
  65. * 检查是否首次访问
  66. */
  67. checkFirstVisit() {
  68. try {
  69. const hasVisited = wx.getStorageSync('hasVisitedWelcome');
  70. if (!hasVisited) {
  71. console.log('首次访问,显示引导页');
  72. } else {
  73. console.log('已访问过,可选择是否直接跳转');
  74. // 如果需要自动跳转到主页,可以在这里调用 this.navigateToHome()
  75. }
  76. } catch (e) {
  77. console.error('检查访问状态失败:', e);
  78. }
  79. },
  80. /**
  81. * 点击"立即开始探索"按钮
  82. */
  83. onStart() {
  84. console.log('===========================================');
  85. console.log('🚀 调用了 onStart 方法(立即开始探索)');
  86. console.log('===========================================');
  87. // 标记已访问
  88. wx.setStorageSync('hasVisitedWelcome', true);
  89. // 跳转到 H5 主页
  90. this.navigateToHome();
  91. },
  92. /**
  93. * 点击"跳过引导"按钮
  94. */
  95. onSkip() {
  96. // 标记已访问
  97. wx.setStorageSync('hasVisitedWelcome', true);
  98. // 跳转到 H5 主页
  99. this.navigateToHome();
  100. },
  101. /**
  102. * 点击"查看新手指南"
  103. */
  104. onGuide() {
  105. wx.showToast({
  106. title: '新手指南开发中',
  107. icon: 'none',
  108. duration: 2000
  109. });
  110. // TODO: 后续可以跳转到新手指南页面
  111. // wx.navigateTo({
  112. // url: '/pages/guide/index'
  113. // });
  114. },
  115. /**
  116. * 验证 token 是否有效
  117. */
  118. async validateToken(token) {
  119. try {
  120. console.log('🔍 开始验证 token 有效性...');
  121. // 使用 Parse.User.become() 验证 token
  122. const user = await Parse.User.become(token);
  123. if (user) {
  124. console.log('✅ Token 有效!用户:', user.id);
  125. return true;
  126. }
  127. console.log('⚠️ Token 验证返回空用户');
  128. return false;
  129. } catch (error) {
  130. console.error('❌ Token 验证失败:', error.message);
  131. return false;
  132. }
  133. },
  134. /**
  135. * 刷新用户 session token
  136. */
  137. async refreshToken() {
  138. try {
  139. console.log('🔄 开始刷新 token...');
  140. const currentUser = Parse.User.current();
  141. if (!currentUser) {
  142. console.error('❌ 当前没有登录用户,无法刷新 token');
  143. return null;
  144. }
  145. // 保存用户信息以触发 session token 刷新
  146. await currentUser.fetch();
  147. const newToken = currentUser.getSessionToken();
  148. console.log('✅ Token 刷新成功');
  149. console.log('🔑 新 Token (前20字符):', newToken.substring(0, 20));
  150. return newToken;
  151. } catch (error) {
  152. console.error('❌ Token 刷新失败:', error.message);
  153. return null;
  154. }
  155. },
  156. /**
  157. * 获取店铺 ID
  158. */
  159. async getStoreId() {
  160. try {
  161. console.log('🏪 开始获取店铺 ID...');
  162. const defaultStoreId = 'pkPdAnLAUZ'; // 超级门店ID
  163. const configuredStoreId =
  164. wx.getStorageSync('storeId') ||
  165. getApp().globalData.storeId ||
  166. defaultStoreId;
  167. console.log('✅ 使用店铺 ID:', configuredStoreId);
  168. return configuredStoreId;
  169. } catch (error) {
  170. console.error('❌ 获取店铺 ID 失败:', error);
  171. return null;
  172. }
  173. },
  174. /**
  175. * 获取店铺信息(id 与 名称)
  176. */
  177. async getStoreInfo() {
  178. try {
  179. const defaultStoreId = 'pkPdAnLAUZ'; // 超级门店ID
  180. const effectiveStoreId =
  181. wx.getStorageSync('storeId') ||
  182. getApp().globalData.storeId ||
  183. defaultStoreId;
  184. const query = new Parse.Query('ShopStore');
  185. query.equalTo('objectId', effectiveStoreId);
  186. const store = await query.first();
  187. if (store) {
  188. const name = store.get('storeName') || '';
  189. console.log('✅ 获取店铺信息:', { id: effectiveStoreId, name });
  190. return { id: effectiveStoreId, name };
  191. } else {
  192. console.log('⚠️ 未找到店铺,仅返回 ID');
  193. return { id: effectiveStoreId, name: '' };
  194. }
  195. } catch (e) {
  196. console.error('获取店铺信息失败:', e);
  197. return { id: wx.getStorageSync('storeId') || 'pkPdAnLAUZ', name: '' };
  198. }
  199. },
  200. /**
  201. * 跳转到 H5 主页
  202. */
  203. async navigateToHome() {
  204. console.log('===========================================');
  205. console.log('======= navigateToHome 被调用 =======');
  206. console.log('===========================================');
  207. const currentUser = Parse.User.current();
  208. let userInfo = wx.getStorageSync("userLogin");
  209. console.log('当前用户:', currentUser ? '已登录' : '未登录');
  210. console.log('userInfo:', userInfo);
  211. // 如果用户未登录,直接进入游客模式(简化流程)
  212. if (!currentUser || userInfo == '') {
  213. console.log('⚠️ 用户未登录,直接进入游客模式');
  214. // 直接进入游客模式,不显示提示框
  215. this.navigateToHomeAsGuest();
  216. return;
  217. }
  218. let token = currentUser.getSessionToken();
  219. if (!token) {
  220. console.error('❌ 无法获取 Session Token!');
  221. wx.showToast({
  222. title: '获取登录信息失败',
  223. icon: 'none'
  224. });
  225. return;
  226. }
  227. // 验证 token 是否有效
  228. const isValid = await this.validateToken(token);
  229. if (!isValid) {
  230. wx.showLoading({
  231. title: '正在刷新登录...'
  232. });
  233. // 尝试刷新 token
  234. const newToken = await this.refreshToken();
  235. wx.hideLoading();
  236. if (newToken) {
  237. token = newToken;
  238. } else {
  239. console.error('❌ Token 刷新失败,需要重新登录');
  240. wx.showModal({
  241. title: '提示',
  242. content: '登录已过期,请重新登录',
  243. showCancel: false,
  244. success: () => {
  245. wx.navigateTo({
  246. url: 'plugin://fm-plugin/fm-auth'
  247. });
  248. }
  249. });
  250. return;
  251. }
  252. } else {
  253. // token 验证通过
  254. }
  255. // 获取店铺信息
  256. const store = await this.getStoreInfo();
  257. const storeId = store && store.id ? store.id : null;
  258. const storeName = store && store.name ? store.name : '';
  259. // 构建 H5 URL(重要:storeId 和 token 作为查询参数)
  260. let h5Url = `https://app.fmode.cn/dev/pobingfeng/owner/nav/home?`;
  261. // 如果有 storeId,添加到 URL 中
  262. if (storeId) {
  263. h5Url += `storeId=${storeId}&`;
  264. }
  265. h5Url += `token=${token}`;
  266. console.log('🌐 H5 URL:', h5Url);
  267. console.log(' - URL 长度:', h5Url.length);
  268. if (storeId) {
  269. console.log(' - 店铺 ID:', storeId);
  270. }
  271. // 编码后的 URL
  272. const encodedUrl = encodeURIComponent(h5Url);
  273. console.log('📦 编码后的 URL:', encodedUrl.substring(0, 100) + '...');
  274. // 最终的小程序页面路径
  275. // 传递店铺信息给 web-view 页面,优先用于设置标题
  276. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}`;
  277. if (storeId) {
  278. webViewPath += `&storeId=${storeId}`;
  279. }
  280. if (storeName) {
  281. webViewPath += `&storeName=${encodeURIComponent(storeName)}`;
  282. }
  283. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  284. console.log('===========================================');
  285. wx.navigateTo({
  286. url: webViewPath,
  287. success: () => {
  288. console.log('✅ 跳转成功');
  289. },
  290. fail: (err) => {
  291. console.error('❌ 跳转失败:', err);
  292. wx.showToast({
  293. title: '跳转失败,请重试',
  294. icon: 'none'
  295. });
  296. }
  297. });
  298. },
  299. /**
  300. * 游客模式跳转到主页
  301. */
  302. async navigateToHomeAsGuest() {
  303. try {
  304. console.log('===========================================');
  305. console.log('======= 游客模式访问 =======');
  306. console.log('===========================================');
  307. // 标记为游客模式
  308. wx.setStorageSync('isGuestMode', true);
  309. // ✅ 游客默认使用超级门店ID,确保有数据
  310. const superStoreId = 'pkPdAnLAUZ'; // 超级门店ID
  311. // 设置超级门店ID到存储
  312. wx.setStorageSync('storeId', superStoreId);
  313. getApp().globalData.storeId = superStoreId;
  314. console.log('✅ 游客使用超级门店ID:', superStoreId);
  315. // 获取店铺信息
  316. const store = await this.getStoreInfo();
  317. const storeId = store && store.id ? store.id : superStoreId;
  318. const storeName = store && store.name ? store.name : '超级门店';
  319. console.log('✅ 店铺信息:', { id: storeId, name: storeName });
  320. // 构建 H5 URL(游客模式,不传 token)
  321. let h5Url = `https://app.fmode.cn/dev/pobingfeng/owner/nav/home?`;
  322. h5Url += `storeId=${storeId}&`;
  323. // 添加游客模式标识
  324. h5Url += `guestMode=true`;
  325. console.log('🌐 游客模式 H5 URL:', h5Url);
  326. console.log('');
  327. console.log('⚠️⚠️⚠️ 重要提示 ⚠️⚠️⚠️');
  328. console.log('如果H5页面显示的不是超级门店,说明H5端没有正确使用URL中的storeId参数!');
  329. console.log('H5端需要确保:');
  330. console.log('1. 从URL中读取 storeId 参数');
  331. console.log('2. 优先使用URL传递的 storeId,不要被其他逻辑覆盖');
  332. console.log('3. 不要使用localStorage缓存的旧门店ID');
  333. console.log('');
  334. console.log('请复制以下URL在浏览器中测试:');
  335. console.log(h5Url);
  336. console.log('===========================================');
  337. // 编码后的 URL
  338. const encodedUrl = encodeURIComponent(h5Url);
  339. // 跳转
  340. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}`;
  341. webViewPath += `&storeId=${storeId}`;
  342. if (storeName) {
  343. webViewPath += `&storeName=${encodeURIComponent(storeName)}`;
  344. }
  345. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  346. console.log('===========================================');
  347. wx.navigateTo({
  348. url: webViewPath,
  349. success: () => {
  350. console.log('✅ 游客模式跳转成功');
  351. },
  352. fail: (err) => {
  353. console.error('❌ 跳转失败:', err);
  354. wx.showToast({
  355. title: '跳转失败,请重试',
  356. icon: 'none'
  357. });
  358. }
  359. });
  360. } catch (error) {
  361. console.error('❌ 游客模式跳转失败:', error);
  362. wx.showToast({
  363. title: '加载失败,请重试',
  364. icon: 'none'
  365. });
  366. }
  367. },
  368. /**
  369. * 轮播图变化事件
  370. */
  371. onSwiperChange(e) {
  372. this.setData({
  373. currentSwiperIndex: e.detail.current
  374. });
  375. },
  376. /**
  377. * 跳转到案例展示页面
  378. */
  379. async navigateToCases() {
  380. console.log('===========================================');
  381. console.log('🏠 调用了 navigateToCases 方法(案例展示)');
  382. console.log('===========================================');
  383. // 案例展示允许游客访问,直接跳转
  384. const currentUser = Parse.User.current();
  385. if (!currentUser) {
  386. console.log('游客访问案例展示');
  387. wx.setStorageSync('isGuestMode', true);
  388. await this.navigateToH5PageAsGuest('owner/nav/cases');
  389. } else {
  390. await this.navigateToH5Page('owner/nav/cases');
  391. }
  392. },
  393. /**
  394. * 跳转到产品中心页面
  395. * @param {string} productId - 可选的产品ID,用于直接跳转到特定产品详情
  396. */
  397. async navigateToProducts(productId = null) {
  398. console.log('===========================================');
  399. console.log('📦 调用了 navigateToProducts 方法(产品中心)');
  400. console.log('===========================================');
  401. // 产品中心允许游客访问,直接跳转
  402. const currentUser = Parse.User.current();
  403. const params = productId ? { productId } : {};
  404. if (!currentUser) {
  405. console.log('游客访问产品中心');
  406. wx.setStorageSync('isGuestMode', true);
  407. await this.navigateToH5PageAsGuest('owner/nav/products', params);
  408. } else {
  409. await this.navigateToH5Page('owner/nav/products', params);
  410. }
  411. },
  412. /**
  413. * 跳转到方案定制/AI推荐页面
  414. */
  415. async navigateToConsultation() {
  416. console.log('===========================================');
  417. console.log('======= 点击咨询/方案定制 =======');
  418. console.log('===========================================');
  419. // 方案定制需要登录
  420. const currentUser = Parse.User.current();
  421. const isGuestMode = wx.getStorageSync('isGuestMode');
  422. const userLogin = wx.getStorageSync('userLogin');
  423. console.log('当前用户:', currentUser ? '已登录' : '未登录');
  424. console.log('用户手机号:', currentUser?.get('mobile'));
  425. console.log('游客模式:', isGuestMode);
  426. console.log('userLogin 存储:', userLogin);
  427. // 检查用户是否真正登录(有手机号且有 userLogin 存储)
  428. const isReallyLoggedIn = currentUser && currentUser.get('mobile') && userLogin;
  429. if (!isReallyLoggedIn || isGuestMode) {
  430. console.log('⚠️ 用户未完成登录或是游客,显示登录提示');
  431. // 如果是游客模式,清除游客标记
  432. if (isGuestMode) {
  433. wx.removeStorageSync('isGuestMode');
  434. wx.removeStorageSync('userLogin');
  435. }
  436. wx.showModal({
  437. title: '需要登录',
  438. content: '方案定制和咨询功能需要登录后使用,是否立即登录?',
  439. confirmText: '立即登录',
  440. cancelText: '取消',
  441. success: (res) => {
  442. if (res.confirm) {
  443. console.log('用户选择:立即登录');
  444. console.log('准备调用 login.loginNow()');
  445. // 直接调用登录方法,不需要延迟
  446. const loginResult = login.loginNow();
  447. console.log('login.loginNow() 返回值:', loginResult);
  448. // 如果返回 false,说明已经跳转到登录页面
  449. if (!loginResult) {
  450. console.log('✅ 已跳转到登录页面');
  451. }
  452. } else {
  453. console.log('用户选择:取消');
  454. }
  455. }
  456. });
  457. return;
  458. }
  459. console.log('✅ 用户已登录,跳转到咨询页面');
  460. await this.navigateToH5Page('owner/nav/consultation');
  461. },
  462. /**
  463. * 通用 H5 页面跳转方法
  464. * @param {string} pagePath - H5 页面路径(例如: 'owner/nav/cases')
  465. * @param {object} extraParams - 额外的URL参数对象(例如: { productId: 'xxx' })
  466. */
  467. async navigateToH5Page(pagePath, extraParams = {}) {
  468. console.log('===========================================');
  469. console.log(`======= 小程序跳转 H5: ${pagePath} =======`);
  470. console.log('===========================================');
  471. const currentUser = Parse.User.current();
  472. let userInfo = wx.getStorageSync("userLogin");
  473. // 检查是否是游客模式
  474. const isGuestMode = wx.getStorageSync('isGuestMode');
  475. // 定义允许游客访问的页面
  476. const guestAllowedPages = [
  477. 'owner/nav/home', // 首页
  478. 'owner/nav/products', // 产品中心
  479. 'owner/nav/cases' // 案例展示
  480. ];
  481. // 检查当前页面是否允许游客访问
  482. const isGuestAllowed = guestAllowedPages.some(page => pagePath.includes(page));
  483. if ((!currentUser || userInfo == '') && !isGuestMode) {
  484. // 未登录且不是游客模式,提示用户
  485. wx.showModal({
  486. title: '温馨提示',
  487. content: '登录后可以体验更多功能,是否立即登录?',
  488. confirmText: '立即登录',
  489. cancelText: '先随便看看',
  490. success: (res) => {
  491. if (res.confirm) {
  492. // 用户选择登录
  493. login.loginNow();
  494. } else {
  495. // 用户选择游客模式
  496. if (isGuestAllowed) {
  497. // 如果是允许游客访问的页面,继续访问
  498. wx.setStorageSync('isGuestMode', true);
  499. this.navigateToH5PageAsGuest(pagePath, extraParams);
  500. } else {
  501. // 如果不允许游客访问,提示需要登录
  502. wx.showToast({
  503. title: '此功能需要登录',
  504. icon: 'none'
  505. });
  506. }
  507. }
  508. }
  509. });
  510. return;
  511. }
  512. // 如果是游客模式,检查是否允许访问
  513. if (isGuestMode || !currentUser) {
  514. if (!isGuestAllowed) {
  515. // 游客不允许访问此页面
  516. wx.showModal({
  517. title: '需要登录',
  518. content: '此功能需要登录后使用,是否立即登录?',
  519. confirmText: '立即登录',
  520. cancelText: '取消',
  521. success: (res) => {
  522. if (res.confirm) {
  523. login.loginNow();
  524. }
  525. }
  526. });
  527. return;
  528. }
  529. // 游客访问允许的页面
  530. this.navigateToH5PageAsGuest(pagePath, extraParams);
  531. return;
  532. }
  533. let token = currentUser.getSessionToken();
  534. console.log('🔑 当前 Session Token:', token);
  535. console.log(' - Token 长度:', token ? token.length : 0);
  536. console.log(' - Token 前20个字符:', token ? token.substring(0, 20) : 'null');
  537. if (!token) {
  538. console.error('❌ 无法获取 Session Token!');
  539. wx.showToast({
  540. title: '获取登录信息失败',
  541. icon: 'none'
  542. });
  543. return;
  544. }
  545. // 验证 token 是否有效
  546. console.log('🔍 验证 token 有效性...');
  547. const isValid = await this.validateToken(token);
  548. if (!isValid) {
  549. console.log('⚠️ Token 已失效,尝试刷新...');
  550. wx.showLoading({
  551. title: '正在刷新登录...'
  552. });
  553. // 尝试刷新 token
  554. const newToken = await this.refreshToken();
  555. wx.hideLoading();
  556. if (newToken) {
  557. token = newToken;
  558. console.log('✅ 使用新的 token:', token.substring(0, 20));
  559. } else {
  560. console.error('❌ Token 刷新失败,需要重新登录');
  561. wx.showModal({
  562. title: '提示',
  563. content: '登录已过期,请重新登录',
  564. showCancel: false,
  565. success: () => {
  566. wx.navigateTo({
  567. url: 'plugin://fm-plugin/fm-auth'
  568. });
  569. }
  570. });
  571. return;
  572. }
  573. } else {
  574. console.log('✅ Token 验证通过');
  575. }
  576. // 获取店铺信息
  577. const store = await this.getStoreInfo();
  578. const storeId = store && store.id ? store.id : null;
  579. const storeName = store && store.name ? store.name : '';
  580. // 构建 H5 URL(重要:storeId 和 token 作为查询参数)
  581. let h5Url = `https://app.fmode.cn/dev/pobingfeng/${pagePath}?`;
  582. // 如果有 storeId,添加到 URL 中
  583. if (storeId) {
  584. h5Url += `storeId=${storeId}&`;
  585. }
  586. h5Url += `token=${token}`;
  587. // 添加额外的参数(如 productId)
  588. if (extraParams && Object.keys(extraParams).length > 0) {
  589. for (const [key, value] of Object.entries(extraParams)) {
  590. if (value) {
  591. h5Url += `&${key}=${encodeURIComponent(value)}`;
  592. console.log(` - 添加参数 ${key}:`, value);
  593. }
  594. }
  595. }
  596. // 编码后的 URL
  597. const encodedUrl = encodeURIComponent(h5Url);
  598. // 最终的小程序页面路径
  599. // 传递店铺信息给 web-view 页面,优先用于设置标题
  600. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}`;
  601. if (storeId) {
  602. webViewPath += `&storeId=${storeId}`;
  603. }
  604. if (storeName) {
  605. webViewPath += `&storeName=${encodeURIComponent(storeName)}`;
  606. }
  607. wx.navigateTo({
  608. url: webViewPath,
  609. success: () => {},
  610. fail: (err) => {
  611. console.error('❌ 跳转失败:', err);
  612. wx.showToast({
  613. title: '跳转失败,请重试',
  614. icon: 'none'
  615. });
  616. }
  617. });
  618. },
  619. /**
  620. * 游客模式跳转 H5 页面
  621. * @param {string} pagePath - H5 页面路径
  622. * @param {object} extraParams - 额外参数
  623. */
  624. async navigateToH5PageAsGuest(pagePath, extraParams = {}) {
  625. try {
  626. console.log('===========================================');
  627. console.log(`======= 游客模式跳转 H5: ${pagePath} =======`);
  628. console.log('===========================================');
  629. // ✅ 游客默认使用超级门店ID,确保有数据
  630. const superStoreId = 'pkPdAnLAUZ'; // 超级门店ID
  631. // 确保使用超级门店ID
  632. wx.setStorageSync('storeId', superStoreId);
  633. getApp().globalData.storeId = superStoreId;
  634. console.log('✅ 游客使用超级门店ID:', superStoreId);
  635. // 获取店铺信息
  636. const store = await this.getStoreInfo();
  637. const storeId = store && store.id ? store.id : superStoreId;
  638. const storeName = store && store.name ? store.name : '超级门店';
  639. console.log('✅ 店铺信息:', { id: storeId, name: storeName });
  640. // 构建 H5 URL(游客模式,不传 token)
  641. let h5Url = `https://app.fmode.cn/dev/pobingfeng/${pagePath}?`;
  642. h5Url += `storeId=${storeId}&`;
  643. // 添加游客模式标识
  644. h5Url += `guestMode=true`;
  645. // 添加额外的参数
  646. if (extraParams && Object.keys(extraParams).length > 0) {
  647. for (const [key, value] of Object.entries(extraParams)) {
  648. if (value) {
  649. h5Url += `&${key}=${encodeURIComponent(value)}`;
  650. console.log(` - 添加参数 ${key}:`, value);
  651. }
  652. }
  653. }
  654. console.log('🌐 游客模式 H5 URL:', h5Url);
  655. console.log('');
  656. console.log('⚠️ 如果H5显示的不是超级门店,请检查H5端是否正确读取了URL中的storeId参数');
  657. console.log('');
  658. // 编码后的 URL
  659. const encodedUrl = encodeURIComponent(h5Url);
  660. // 最终的小程序页面路径
  661. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}`;
  662. webViewPath += `&storeId=${storeId}`;
  663. if (storeName) {
  664. webViewPath += `&storeName=${encodeURIComponent(storeName)}`;
  665. }
  666. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  667. console.log('===========================================');
  668. wx.navigateTo({
  669. url: webViewPath,
  670. success: () => {
  671. console.log('✅ 游客模式跳转成功');
  672. },
  673. fail: (err) => {
  674. console.error('❌ 跳转失败:', err);
  675. wx.showToast({
  676. title: '跳转失败,请重试',
  677. icon: 'none'
  678. });
  679. }
  680. });
  681. } catch (error) {
  682. console.error('❌ 游客模式跳转失败:', error);
  683. wx.showToast({
  684. title: '加载失败,请重试',
  685. icon: 'none'
  686. });
  687. }
  688. }
  689. }
  690. })