index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. // 查询 ShopStore 表,获取第一个店铺
  145. const storeQuery = new Parse.Query('ShopStore');
  146. storeQuery.equalTo('company', company);
  147. storeQuery.ascending('score');
  148. storeQuery.limit(1);
  149. const store = await storeQuery.first();
  150. if (store) {
  151. const storeId = store.id;
  152. console.log('✅ 获取店铺 ID 成功:', storeId);
  153. return storeId;
  154. } else {
  155. console.log('⚠️ 未找到店铺,将不带 storeId 参数');
  156. return null;
  157. }
  158. } catch (error) {
  159. console.error('❌ 获取店铺 ID 失败:', error);
  160. return null;
  161. }
  162. },
  163. /**
  164. * 获取店铺信息(id 与 名称)
  165. */
  166. async getStoreInfo() {
  167. try {
  168. const query = new Parse.Query('ShopStore');
  169. query.equalTo('company', company);
  170. query.ascending('score');
  171. query.limit(1);
  172. const store = await query.first();
  173. if (!store) return null;
  174. return { id: store.id, name: store.get('storeName') || '' };
  175. } catch (e) {
  176. console.error('获取店铺信息失败:', e);
  177. return null;
  178. }
  179. },
  180. /**
  181. * 跳转到 H5 主页
  182. */
  183. async navigateToHome() {
  184. console.log('===========================================');
  185. console.log('======= 小程序跳转 H5 =======');
  186. console.log('===========================================');
  187. const currentUser = Parse.User.current();
  188. console.log('📱 当前用户:', currentUser);
  189. const isAuth = login.loginNow()
  190. if (!currentUser || !isAuth) {
  191. console.error('❌ 当前没有登录用户!');
  192. wx.showToast({
  193. title: '请先登录',
  194. icon: 'none'
  195. });
  196. // 跳转到登录页
  197. wx.navigateTo({
  198. url: 'plugin://fm-plugin/fm-auth'
  199. });
  200. return;
  201. }
  202. let token = currentUser.getSessionToken();
  203. console.log('🔑 当前 Session Token:', token);
  204. console.log(' - Token 长度:', token ? token.length : 0);
  205. console.log(' - Token 前20个字符:', token ? token.substring(0, 20) : 'null');
  206. if (!token) {
  207. console.error('❌ 无法获取 Session Token!');
  208. wx.showToast({
  209. title: '获取登录信息失败',
  210. icon: 'none'
  211. });
  212. return;
  213. }
  214. // 验证 token 是否有效
  215. console.log('🔍 验证 token 有效性...');
  216. const isValid = await this.validateToken(token);
  217. if (!isValid) {
  218. console.log('⚠️ Token 已失效,尝试刷新...');
  219. wx.showLoading({
  220. title: '正在刷新登录...'
  221. });
  222. // 尝试刷新 token
  223. const newToken = await this.refreshToken();
  224. wx.hideLoading();
  225. if (newToken) {
  226. token = newToken;
  227. console.log('✅ 使用新的 token:', token.substring(0, 20));
  228. } else {
  229. console.error('❌ Token 刷新失败,需要重新登录');
  230. wx.showModal({
  231. title: '提示',
  232. content: '登录已过期,请重新登录',
  233. showCancel: false,
  234. success: () => {
  235. wx.navigateTo({
  236. url: 'plugin://fm-plugin/fm-auth'
  237. });
  238. }
  239. });
  240. return;
  241. }
  242. } else {
  243. console.log('✅ Token 验证通过');
  244. }
  245. // 获取店铺信息
  246. const store = await this.getStoreInfo();
  247. const storeId = store && store.id ? store.id : null;
  248. const storeName = store && store.name ? store.name : '';
  249. // 构建 H5 URL(重要:storeId 和 token 作为查询参数)
  250. let h5Url = `https://app.fmode.cn/dev/pobingfeng/owner/nav/home?`;
  251. // 如果有 storeId,添加到 URL 中
  252. if (storeId) {
  253. h5Url += `storeId=${storeId}&`;
  254. }
  255. h5Url += `token=${token}`;
  256. console.log('🌐 H5 URL:', h5Url);
  257. console.log(' - URL 长度:', h5Url.length);
  258. if (storeId) {
  259. console.log(' - 店铺 ID:', storeId);
  260. }
  261. // 编码后的 URL
  262. const encodedUrl = encodeURIComponent(h5Url);
  263. console.log('📦 编码后的 URL:', encodedUrl.substring(0, 100) + '...');
  264. // 最终的小程序页面路径
  265. // 传递店铺信息给 web-view 页面,优先用于设置标题
  266. let webViewPath = `/common-page/pages/web-view/index?path=${encodedUrl}`;
  267. if (storeId) {
  268. webViewPath += `&storeId=${storeId}`;
  269. }
  270. if (storeName) {
  271. webViewPath += `&storeName=${encodeURIComponent(storeName)}`;
  272. }
  273. console.log('📄 web-view 页面路径:', webViewPath.substring(0, 100) + '...');
  274. console.log('===========================================');
  275. wx.navigateTo({
  276. url: webViewPath,
  277. success: () => {
  278. console.log('✅ 跳转成功');
  279. },
  280. fail: (err) => {
  281. console.error('❌ 跳转失败:', err);
  282. wx.showToast({
  283. title: '跳转失败,请重试',
  284. icon: 'none'
  285. });
  286. }
  287. });
  288. },
  289. /**
  290. * 轮播图变化事件
  291. */
  292. onSwiperChange(e) {
  293. this.setData({
  294. currentSwiperIndex: e.detail.current
  295. });
  296. }
  297. }
  298. })