index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. // 默认使用固定的店铺 ID
  145. const defaultStoreId = 'Zr65KGWXHx';
  146. console.log('✅ 使用默认店铺 ID:', defaultStoreId);
  147. return defaultStoreId;
  148. // 原有的查询逻辑(已注释)
  149. // const storeQuery = new Parse.Query('ShopStore');
  150. // storeQuery.equalTo('company', company);
  151. // storeQuery.ascending('score');
  152. // storeQuery.limit(1);
  153. //
  154. // const store = await storeQuery.first();
  155. //
  156. // if (store) {
  157. // const storeId = store.id;
  158. // console.log('✅ 获取店铺 ID 成功:', storeId);
  159. // return storeId;
  160. // } else {
  161. // console.log('⚠️ 未找到店铺,将不带 storeId 参数');
  162. // return null;
  163. // }
  164. } catch (error) {
  165. console.error('❌ 获取店铺 ID 失败:', error);
  166. return null;
  167. }
  168. },
  169. /**
  170. * 获取店铺信息(id 与 名称)
  171. */
  172. async getStoreInfo() {
  173. try {
  174. // 默认使用固定的店铺 ID
  175. const defaultStoreId = 'Zr65KGWXHx';
  176. // 尝试获取该店铺的名称
  177. const query = new Parse.Query('ShopStore');
  178. query.equalTo('objectId', defaultStoreId);
  179. const store = await query.first();
  180. if (store) {
  181. console.log('✅ 获取到默认店铺信息:', { id: defaultStoreId, name: store.get('storeName') || '' });
  182. return { id: defaultStoreId, name: store.get('storeName') || '' };
  183. } else {
  184. console.log('⚠️ 未找到默认店铺,仅返回 ID');
  185. return { id: defaultStoreId, name: '' };
  186. }
  187. } catch (e) {
  188. console.error('获取店铺信息失败:', e);
  189. // 即使出错也返回默认 ID
  190. return { id: 'Zr65KGWXHx', name: '' };
  191. }
  192. },
  193. /**
  194. * 跳转到 H5 主页
  195. */
  196. async navigateToHome() {
  197. console.log('===========================================');
  198. console.log('======= 小程序跳转 H5 =======');
  199. console.log('===========================================');
  200. const currentUser = Parse.User.current();
  201. console.log('📱 当前用户:', currentUser);
  202. const isAuth = login.loginNow()
  203. if (!currentUser || !isAuth) {
  204. console.error('❌ 当前没有登录用户!');
  205. wx.showToast({
  206. title: '请先登录',
  207. icon: 'none'
  208. });
  209. // 跳转到登录页
  210. wx.navigateTo({
  211. url: 'plugin://fm-plugin/fm-auth'
  212. });
  213. return;
  214. }
  215. let token = currentUser.getSessionToken();
  216. console.log('🔑 当前 Session Token:', token);
  217. console.log(' - Token 长度:', token ? token.length : 0);
  218. console.log(' - Token 前20个字符:', token ? token.substring(0, 20) : 'null');
  219. if (!token) {
  220. console.error('❌ 无法获取 Session Token!');
  221. wx.showToast({
  222. title: '获取登录信息失败',
  223. icon: 'none'
  224. });
  225. return;
  226. }
  227. // 验证 token 是否有效
  228. console.log('🔍 验证 token 有效性...');
  229. const isValid = await this.validateToken(token);
  230. if (!isValid) {
  231. console.log('⚠️ Token 已失效,尝试刷新...');
  232. wx.showLoading({
  233. title: '正在刷新登录...'
  234. });
  235. // 尝试刷新 token
  236. const newToken = await this.refreshToken();
  237. wx.hideLoading();
  238. if (newToken) {
  239. token = newToken;
  240. console.log('✅ 使用新的 token:', token.substring(0, 20));
  241. } else {
  242. console.error('❌ Token 刷新失败,需要重新登录');
  243. wx.showModal({
  244. title: '提示',
  245. content: '登录已过期,请重新登录',
  246. showCancel: false,
  247. success: () => {
  248. wx.navigateTo({
  249. url: 'plugin://fm-plugin/fm-auth'
  250. });
  251. }
  252. });
  253. return;
  254. }
  255. } else {
  256. console.log('✅ Token 验证通过');
  257. }
  258. // 获取店铺信息
  259. const store = await this.getStoreInfo();
  260. const storeId = store && store.id ? store.id : null;
  261. const storeName = store && store.name ? store.name : '';
  262. // 构建 H5 URL(重要:storeId 和 token 作为查询参数)
  263. let h5Url = `https://app.fmode.cn/dev/pobingfeng/owner/nav/home?`;
  264. // 如果有 storeId,添加到 URL 中
  265. if (storeId) {
  266. h5Url += `storeId=${storeId}&`;
  267. }
  268. h5Url += `token=${token}`;
  269. console.log('🌐 H5 URL:', h5Url);
  270. console.log(' - URL 长度:', h5Url.length);
  271. if (storeId) {
  272. console.log(' - 店铺 ID:', storeId);
  273. }
  274. // 编码后的 URL
  275. const encodedUrl = encodeURIComponent(h5Url);
  276. console.log('📦 编码后的 URL:', encodedUrl.substring(0, 100) + '...');
  277. // 最终的小程序页面路径
  278. // 传递店铺信息给 web-view 页面,优先用于设置标题
  279. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}`;
  280. if (storeId) {
  281. webViewPath += `&storeId=${storeId}`;
  282. }
  283. if (storeName) {
  284. webViewPath += `&storeName=${encodeURIComponent(storeName)}`;
  285. }
  286. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  287. console.log('===========================================');
  288. wx.navigateTo({
  289. url: webViewPath,
  290. success: () => {
  291. console.log('✅ 跳转成功');
  292. },
  293. fail: (err) => {
  294. console.error('❌ 跳转失败:', err);
  295. wx.showToast({
  296. title: '跳转失败,请重试',
  297. icon: 'none'
  298. });
  299. }
  300. });
  301. },
  302. /**
  303. * 轮播图变化事件
  304. */
  305. onSwiperChange(e) {
  306. this.setData({
  307. currentSwiperIndex: e.detail.current
  308. });
  309. },
  310. /**
  311. * 跳转到案例展示页面
  312. */
  313. async navigateToCases() {
  314. await this.navigateToH5Page('owner/nav/cases');
  315. },
  316. /**
  317. * 跳转到产品中心页面
  318. */
  319. async navigateToProducts() {
  320. await this.navigateToH5Page('owner/nav/products');
  321. },
  322. /**
  323. * 跳转到方案定制/AI推荐页面
  324. */
  325. async navigateToConsultation() {
  326. await this.navigateToH5Page('owner/nav/consultation');
  327. },
  328. /**
  329. * 通用 H5 页面跳转方法
  330. * @param {string} pagePath - H5 页面路径(例如: 'owner/nav/cases')
  331. */
  332. async navigateToH5Page(pagePath) {
  333. console.log('===========================================');
  334. console.log(`======= 小程序跳转 H5: ${pagePath} =======`);
  335. console.log('===========================================');
  336. const currentUser = Parse.User.current();
  337. console.log('📱 当前用户:', currentUser);
  338. const isAuth = login.loginNow()
  339. if (!currentUser || !isAuth) {
  340. console.error('❌ 当前没有登录用户!');
  341. wx.showToast({
  342. title: '请先登录',
  343. icon: 'none'
  344. });
  345. // 跳转到登录页
  346. wx.navigateTo({
  347. url: 'plugin://fm-plugin/fm-auth'
  348. });
  349. return;
  350. }
  351. let token = currentUser.getSessionToken();
  352. console.log('🔑 当前 Session Token:', token);
  353. console.log(' - Token 长度:', token ? token.length : 0);
  354. console.log(' - Token 前20个字符:', token ? token.substring(0, 20) : 'null');
  355. if (!token) {
  356. console.error('❌ 无法获取 Session Token!');
  357. wx.showToast({
  358. title: '获取登录信息失败',
  359. icon: 'none'
  360. });
  361. return;
  362. }
  363. // 验证 token 是否有效
  364. console.log('🔍 验证 token 有效性...');
  365. const isValid = await this.validateToken(token);
  366. if (!isValid) {
  367. console.log('⚠️ Token 已失效,尝试刷新...');
  368. wx.showLoading({
  369. title: '正在刷新登录...'
  370. });
  371. // 尝试刷新 token
  372. const newToken = await this.refreshToken();
  373. wx.hideLoading();
  374. if (newToken) {
  375. token = newToken;
  376. console.log('✅ 使用新的 token:', token.substring(0, 20));
  377. } else {
  378. console.error('❌ Token 刷新失败,需要重新登录');
  379. wx.showModal({
  380. title: '提示',
  381. content: '登录已过期,请重新登录',
  382. showCancel: false,
  383. success: () => {
  384. wx.navigateTo({
  385. url: 'plugin://fm-plugin/fm-auth'
  386. });
  387. }
  388. });
  389. return;
  390. }
  391. } else {
  392. console.log('✅ Token 验证通过');
  393. }
  394. // 获取店铺信息
  395. const store = await this.getStoreInfo();
  396. const storeId = store && store.id ? store.id : null;
  397. const storeName = store && store.name ? store.name : '';
  398. // 构建 H5 URL(重要:storeId 和 token 作为查询参数)
  399. let h5Url = `https://app.fmode.cn/dev/pobingfeng/${pagePath}?`;
  400. // 如果有 storeId,添加到 URL 中
  401. if (storeId) {
  402. h5Url += `storeId=${storeId}&`;
  403. }
  404. h5Url += `token=${token}`;
  405. console.log('🌐 H5 URL:', h5Url);
  406. console.log(' - URL 长度:', h5Url.length);
  407. console.log(' - 页面路径:', pagePath);
  408. if (storeId) {
  409. console.log(' - 店铺 ID:', storeId);
  410. }
  411. // 编码后的 URL
  412. const encodedUrl = encodeURIComponent(h5Url);
  413. console.log('📦 编码后的 URL:', encodedUrl.substring(0, 100) + '...');
  414. // 最终的小程序页面路径
  415. // 传递店铺信息给 web-view 页面,优先用于设置标题
  416. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}`;
  417. if (storeId) {
  418. webViewPath += `&storeId=${storeId}`;
  419. }
  420. if (storeName) {
  421. webViewPath += `&storeName=${encodeURIComponent(storeName)}`;
  422. }
  423. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  424. console.log('===========================================');
  425. wx.navigateTo({
  426. url: webViewPath,
  427. success: () => {
  428. console.log('✅ 跳转成功');
  429. },
  430. fail: (err) => {
  431. console.error('❌ 跳转失败:', err);
  432. wx.showToast({
  433. title: '跳转失败,请重试',
  434. icon: 'none'
  435. });
  436. }
  437. });
  438. }
  439. }
  440. })