index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. /**
  24. * 组件的方法列表
  25. */
  26. methods: {
  27. /**
  28. * 在控制台显示当前登录用户信息
  29. */
  30. showCurrentUser() {
  31. const currentUser = Parse.User.current();
  32. console.log('===========================================');
  33. console.log('======= 当前登录用户信息 =======');
  34. console.log('===========================================');
  35. if (currentUser) {
  36. console.log('✅ 用户已登录');
  37. console.log('📱 用户ID:', currentUser.id);
  38. console.log('📞 手机号:', currentUser.get('mobile') || '未设置');
  39. console.log('👤 用户名:', currentUser.get('username') || '未设置');
  40. console.log('🔑 Session Token:', currentUser.getSessionToken());
  41. console.log('📧 邮箱:', currentUser.get('email') || '未设置');
  42. console.log('📦 完整用户对象:', currentUser);
  43. console.log('📋 所有属性:', currentUser.attributes);
  44. } else {
  45. console.log('❌ 当前没有登录用户');
  46. }
  47. console.log('===========================================');
  48. },
  49. /**
  50. * 检查是否首次访问
  51. */
  52. checkFirstVisit() {
  53. try {
  54. const hasVisited = wx.getStorageSync('hasVisitedWelcome');
  55. if (!hasVisited) {
  56. console.log('首次访问,显示引导页');
  57. } else {
  58. console.log('已访问过,可选择是否直接跳转');
  59. // 如果需要自动跳转到主页,可以在这里调用 this.navigateToHome()
  60. }
  61. } catch (e) {
  62. console.error('检查访问状态失败:', e);
  63. }
  64. },
  65. /**
  66. * 点击"立即开始探索"按钮
  67. */
  68. onStart() {
  69. // 标记已访问
  70. wx.setStorageSync('hasVisitedWelcome', true);
  71. // 跳转到 H5 主页
  72. this.navigateToHome();
  73. },
  74. /**
  75. * 点击"跳过引导"按钮
  76. */
  77. onSkip() {
  78. // 标记已访问
  79. wx.setStorageSync('hasVisitedWelcome', true);
  80. // 跳转到 H5 主页
  81. this.navigateToHome();
  82. },
  83. /**
  84. * 点击"查看新手指南"
  85. */
  86. onGuide() {
  87. wx.showToast({
  88. title: '新手指南开发中',
  89. icon: 'none',
  90. duration: 2000
  91. });
  92. // TODO: 后续可以跳转到新手指南页面
  93. // wx.navigateTo({
  94. // url: '/pages/guide/index'
  95. // });
  96. },
  97. /**
  98. * 验证 token 是否有效
  99. */
  100. async validateToken(token) {
  101. try {
  102. console.log('🔍 开始验证 token 有效性...');
  103. // 使用 Parse.User.become() 验证 token
  104. const user = await Parse.User.become(token);
  105. if (user) {
  106. console.log('✅ Token 有效!用户:', user.id);
  107. return true;
  108. }
  109. console.log('⚠️ Token 验证返回空用户');
  110. return false;
  111. } catch (error) {
  112. console.error('❌ Token 验证失败:', error.message);
  113. return false;
  114. }
  115. },
  116. /**
  117. * 刷新用户 session token
  118. */
  119. async refreshToken() {
  120. try {
  121. console.log('🔄 开始刷新 token...');
  122. const currentUser = Parse.User.current();
  123. if (!currentUser) {
  124. console.error('❌ 当前没有登录用户,无法刷新 token');
  125. return null;
  126. }
  127. // 保存用户信息以触发 session token 刷新
  128. await currentUser.fetch();
  129. const newToken = currentUser.getSessionToken();
  130. console.log('✅ Token 刷新成功');
  131. console.log('🔑 新 Token (前20字符):', newToken.substring(0, 20));
  132. return newToken;
  133. } catch (error) {
  134. console.error('❌ Token 刷新失败:', error.message);
  135. return null;
  136. }
  137. },
  138. /**
  139. * 获取店铺 ID
  140. */
  141. async getStoreId() {
  142. try {
  143. console.log('🏪 开始获取店铺 ID...');
  144. const defaultStoreId = 'Zr65KGWXHx';
  145. const configuredStoreId =
  146. wx.getStorageSync('storeId') ||
  147. getApp().globalData.storeId ||
  148. defaultStoreId;
  149. console.log('✅ 使用店铺 ID:', configuredStoreId);
  150. return configuredStoreId;
  151. } catch (error) {
  152. console.error('❌ 获取店铺 ID 失败:', error);
  153. return null;
  154. }
  155. },
  156. /**
  157. * 获取店铺信息(id 与 名称)
  158. */
  159. async getStoreInfo() {
  160. try {
  161. const defaultStoreId = 'Zr65KGWXHx';
  162. const effectiveStoreId =
  163. wx.getStorageSync('storeId') ||
  164. getApp().globalData.storeId ||
  165. defaultStoreId;
  166. const query = new Parse.Query('ShopStore');
  167. query.equalTo('objectId', effectiveStoreId);
  168. const store = await query.first();
  169. if (store) {
  170. const name = store.get('storeName') || '';
  171. console.log('✅ 获取店铺信息:', { id: effectiveStoreId, name });
  172. return { id: effectiveStoreId, name };
  173. } else {
  174. console.log('⚠️ 未找到店铺,仅返回 ID');
  175. return { id: effectiveStoreId, name: '' };
  176. }
  177. } catch (e) {
  178. console.error('获取店铺信息失败:', e);
  179. return { id: wx.getStorageSync('storeId') || 'Zr65KGWXHx', name: '' };
  180. }
  181. },
  182. /**
  183. * 跳转到 H5 主页
  184. */
  185. async navigateToHome() {
  186. console.log('===========================================');
  187. console.log('======= 小程序跳转 H5 =======');
  188. console.log('===========================================');
  189. const currentUser = Parse.User.current();
  190. console.log('📱 当前用户:', currentUser);
  191. const isAuth = login.loginNow()
  192. if (!currentUser || !isAuth) {
  193. console.error('❌ 当前没有登录用户!');
  194. wx.showToast({
  195. title: '请先登录',
  196. icon: 'none'
  197. });
  198. // 跳转到登录页
  199. wx.navigateTo({
  200. url: 'plugin://fm-plugin/fm-auth'
  201. });
  202. return;
  203. }
  204. let token = currentUser.getSessionToken();
  205. console.log('🔑 当前 Session Token:', token);
  206. console.log(' - Token 长度:', token ? token.length : 0);
  207. console.log(' - Token 前20个字符:', token ? token.substring(0, 20) : 'null');
  208. if (!token) {
  209. console.error('❌ 无法获取 Session Token!');
  210. wx.showToast({
  211. title: '获取登录信息失败',
  212. icon: 'none'
  213. });
  214. return;
  215. }
  216. // 验证 token 是否有效
  217. console.log('🔍 验证 token 有效性...');
  218. const isValid = await this.validateToken(token);
  219. if (!isValid) {
  220. console.log('⚠️ Token 已失效,尝试刷新...');
  221. wx.showLoading({
  222. title: '正在刷新登录...'
  223. });
  224. // 尝试刷新 token
  225. const newToken = await this.refreshToken();
  226. wx.hideLoading();
  227. if (newToken) {
  228. token = newToken;
  229. console.log('✅ 使用新的 token:', token.substring(0, 20));
  230. } else {
  231. console.error('❌ Token 刷新失败,需要重新登录');
  232. wx.showModal({
  233. title: '提示',
  234. content: '登录已过期,请重新登录',
  235. showCancel: false,
  236. success: () => {
  237. wx.navigateTo({
  238. url: 'plugin://fm-plugin/fm-auth'
  239. });
  240. }
  241. });
  242. return;
  243. }
  244. } else {
  245. console.log('✅ Token 验证通过');
  246. }
  247. // 获取店铺信息
  248. const store = await this.getStoreInfo();
  249. const storeId = store && store.id ? store.id : null;
  250. const storeName = store && store.name ? store.name : '';
  251. // 构建 H5 URL(重要:storeId 和 token 作为查询参数)
  252. let h5Url = `https://app.fmode.cn/dev/pobingfeng/owner/nav/home?`;
  253. // 如果有 storeId,添加到 URL 中
  254. if (storeId) {
  255. h5Url += `storeId=${storeId}&`;
  256. }
  257. h5Url += `token=${token}`;
  258. console.log('🌐 H5 URL:', h5Url);
  259. console.log(' - URL 长度:', h5Url.length);
  260. if (storeId) {
  261. console.log(' - 店铺 ID:', storeId);
  262. }
  263. // 编码后的 URL
  264. const encodedUrl = encodeURIComponent(h5Url);
  265. console.log('📦 编码后的 URL:', encodedUrl.substring(0, 100) + '...');
  266. // 最终的小程序页面路径
  267. // 传递店铺信息给 web-view 页面,优先用于设置标题
  268. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}`;
  269. if (storeId) {
  270. webViewPath += `&storeId=${storeId}`;
  271. }
  272. if (storeName) {
  273. webViewPath += `&storeName=${encodeURIComponent(storeName)}`;
  274. }
  275. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  276. console.log('===========================================');
  277. wx.navigateTo({
  278. url: webViewPath,
  279. success: () => {
  280. console.log('✅ 跳转成功');
  281. },
  282. fail: (err) => {
  283. console.error('❌ 跳转失败:', err);
  284. wx.showToast({
  285. title: '跳转失败,请重试',
  286. icon: 'none'
  287. });
  288. }
  289. });
  290. },
  291. /**
  292. * 轮播图变化事件
  293. */
  294. onSwiperChange(e) {
  295. this.setData({
  296. currentSwiperIndex: e.detail.current
  297. });
  298. },
  299. /**
  300. * 跳转到案例展示页面
  301. */
  302. async navigateToCases() {
  303. await this.navigateToH5Page('owner/nav/cases');
  304. },
  305. /**
  306. * 跳转到产品中心页面
  307. */
  308. async navigateToProducts() {
  309. await this.navigateToH5Page('owner/nav/products');
  310. },
  311. /**
  312. * 跳转到方案定制/AI推荐页面
  313. */
  314. async navigateToConsultation() {
  315. await this.navigateToH5Page('owner/nav/consultation');
  316. },
  317. /**
  318. * 通用 H5 页面跳转方法
  319. * @param {string} pagePath - H5 页面路径(例如: 'owner/nav/cases')
  320. */
  321. async navigateToH5Page(pagePath) {
  322. console.log('===========================================');
  323. console.log(`======= 小程序跳转 H5: ${pagePath} =======`);
  324. console.log('===========================================');
  325. const currentUser = Parse.User.current();
  326. console.log('📱 当前用户:', currentUser);
  327. const isAuth = login.loginNow()
  328. if (!currentUser || !isAuth) {
  329. console.error('❌ 当前没有登录用户!');
  330. wx.showToast({
  331. title: '请先登录',
  332. icon: 'none'
  333. });
  334. // 跳转到登录页
  335. wx.navigateTo({
  336. url: 'plugin://fm-plugin/fm-auth'
  337. });
  338. return;
  339. }
  340. let token = currentUser.getSessionToken();
  341. console.log('🔑 当前 Session Token:', token);
  342. console.log(' - Token 长度:', token ? token.length : 0);
  343. console.log(' - Token 前20个字符:', token ? token.substring(0, 20) : 'null');
  344. if (!token) {
  345. console.error('❌ 无法获取 Session Token!');
  346. wx.showToast({
  347. title: '获取登录信息失败',
  348. icon: 'none'
  349. });
  350. return;
  351. }
  352. // 验证 token 是否有效
  353. console.log('🔍 验证 token 有效性...');
  354. const isValid = await this.validateToken(token);
  355. if (!isValid) {
  356. console.log('⚠️ Token 已失效,尝试刷新...');
  357. wx.showLoading({
  358. title: '正在刷新登录...'
  359. });
  360. // 尝试刷新 token
  361. const newToken = await this.refreshToken();
  362. wx.hideLoading();
  363. if (newToken) {
  364. token = newToken;
  365. console.log('✅ 使用新的 token:', token.substring(0, 20));
  366. } else {
  367. console.error('❌ Token 刷新失败,需要重新登录');
  368. wx.showModal({
  369. title: '提示',
  370. content: '登录已过期,请重新登录',
  371. showCancel: false,
  372. success: () => {
  373. wx.navigateTo({
  374. url: 'plugin://fm-plugin/fm-auth'
  375. });
  376. }
  377. });
  378. return;
  379. }
  380. } else {
  381. console.log('✅ Token 验证通过');
  382. }
  383. // 获取店铺信息
  384. const store = await this.getStoreInfo();
  385. const storeId = store && store.id ? store.id : null;
  386. const storeName = store && store.name ? store.name : '';
  387. // 构建 H5 URL(重要:storeId 和 token 作为查询参数)
  388. let h5Url = `https://app.fmode.cn/dev/pobingfeng/${pagePath}?`;
  389. // 如果有 storeId,添加到 URL 中
  390. if (storeId) {
  391. h5Url += `storeId=${storeId}&`;
  392. }
  393. h5Url += `token=${token}`;
  394. console.log('🌐 H5 URL:', h5Url);
  395. console.log(' - URL 长度:', h5Url.length);
  396. console.log(' - 页面路径:', pagePath);
  397. if (storeId) {
  398. console.log(' - 店铺 ID:', storeId);
  399. }
  400. // 编码后的 URL
  401. const encodedUrl = encodeURIComponent(h5Url);
  402. console.log('📦 编码后的 URL:', encodedUrl.substring(0, 100) + '...');
  403. // 最终的小程序页面路径
  404. // 传递店铺信息给 web-view 页面,优先用于设置标题
  405. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}`;
  406. if (storeId) {
  407. webViewPath += `&storeId=${storeId}`;
  408. }
  409. if (storeName) {
  410. webViewPath += `&storeName=${encodeURIComponent(storeName)}`;
  411. }
  412. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  413. console.log('===========================================');
  414. wx.navigateTo({
  415. url: webViewPath,
  416. success: () => {
  417. console.log('✅ 跳转成功');
  418. },
  419. fail: (err) => {
  420. console.error('❌ 跳转失败:', err);
  421. wx.showToast({
  422. title: '跳转失败,请重试',
  423. icon: 'none'
  424. });
  425. }
  426. });
  427. }
  428. }
  429. })