login.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. var Parse = getApp().Parse;
  2. function loginNow(authPage = 'plugin://fm-plugin/fm-auth') {
  3. console.log('===========================================');
  4. console.log('======= 开始登录流程 =======');
  5. const currentUser = Parse.User.current();
  6. const hasMobile = currentUser?.get('mobile');
  7. const userLogin = wx.getStorageSync('userLogin');
  8. console.log('当前用户:', currentUser ? currentUser.id : '无');
  9. console.log('手机号:', hasMobile || '无');
  10. console.log('userLogin 存储:', userLogin || '无');
  11. console.log('===========================================');
  12. // 只有同时满足以下条件才认为已登录:
  13. // 1. Parse.User.current() 存在
  14. // 2. 用户有手机号
  15. // 3. userLogin 存储存在
  16. if (currentUser && hasMobile && userLogin) {
  17. console.log('✅ 用户已完整登录');
  18. return true;
  19. }
  20. // 如果有 Parse 用户但没有手机号或 userLogin,清除状态重新登录
  21. if (currentUser && (!hasMobile || !userLogin)) {
  22. console.log('⚠️ 用户状态不完整,清除并重新登录');
  23. wx.removeStorageSync('userLogin');
  24. }
  25. // 跳转到授权页面
  26. console.log('🔄 准备跳转到授权页面:', authPage);
  27. // 检查页面栈
  28. const pages = getCurrentPages();
  29. console.log('当前页面栈层数:', pages.length);
  30. console.log('当前页面路径:', pages[pages.length - 1]?.route);
  31. // 添加短暂延迟,确保 UI 渲染完成
  32. setTimeout(() => {
  33. if (pages.length >= 9) {
  34. // 页面栈接近满,使用 redirectTo
  35. console.log('⚠️ 页面栈接近满,使用 redirectTo');
  36. wx.redirectTo({
  37. url: authPage,
  38. success: () => {
  39. console.log('✅ redirectTo 跳转成功');
  40. },
  41. fail: (err) => {
  42. console.error('❌ redirectTo 失败:', err);
  43. console.error('错误详情:', JSON.stringify(err));
  44. wx.showToast({
  45. title: '跳转失败,请重试',
  46. icon: 'none',
  47. duration: 2000
  48. });
  49. }
  50. });
  51. } else {
  52. // 使用 navigateTo
  53. console.log('📱 使用 navigateTo 跳转');
  54. wx.navigateTo({
  55. url: authPage,
  56. success: () => {
  57. console.log('✅ navigateTo 跳转成功');
  58. },
  59. fail: (err) => {
  60. console.error('❌ navigateTo 失败:', err);
  61. console.error('错误详情:', JSON.stringify(err));
  62. console.log('⚠️ 尝试使用 redirectTo');
  63. // 降级使用 redirectTo
  64. wx.redirectTo({
  65. url: authPage,
  66. success: () => {
  67. console.log('✅ redirectTo 跳转成功');
  68. },
  69. fail: (err2) => {
  70. console.error('❌ redirectTo 也失败:', err2);
  71. console.error('错误详情:', JSON.stringify(err2));
  72. // 最后尝试使用 reLaunch
  73. console.log('⚠️ 尝试使用 reLaunch');
  74. wx.reLaunch({
  75. url: '/index',
  76. success: () => {
  77. console.log('✅ reLaunch 到首页成功,请重新点击登录');
  78. wx.showToast({
  79. title: '请重新点击登录',
  80. icon: 'none',
  81. duration: 2000
  82. });
  83. },
  84. fail: (err3) => {
  85. console.error('❌ reLaunch 也失败:', err3);
  86. wx.showToast({
  87. title: '跳转失败,请重启小程序',
  88. icon: 'none',
  89. duration: 3000
  90. });
  91. }
  92. });
  93. }
  94. });
  95. }
  96. });
  97. }
  98. }, 100);
  99. console.log('===========================================');
  100. return false;
  101. }
  102. /* 通过小程序wx.login获取openid和session_key */
  103. function wxLogin() {
  104. return new Promise((resolve, reject) => {
  105. wx.login({
  106. success: function (res) {
  107. if (res.code) {
  108. let url = 'https://server.fmode.cn/api/wxapp/auth_wxapp'
  109. wx.request({
  110. url: url,
  111. data: {
  112. c: getApp().globalData.company,
  113. code: res.code,
  114. appType: getApp().globalData.appType ? getApp().globalData.appType : ''
  115. },
  116. async success(res) {
  117. wx.setStorageSync("userInfo", res.data);
  118. resolve(res)
  119. },
  120. });
  121. }
  122. },
  123. fail: function (err) {
  124. wx.showToast({
  125. title: '服务器繁忙',
  126. icon: 'error'
  127. })
  128. resolve()
  129. }
  130. });
  131. })
  132. }
  133. // async function loginParse() {
  134. // let userInfo = wx.getStorageSync("userInfo");
  135. // let appId = getApp().globalData.appid
  136. // let username, password;
  137. // username = userInfo.openid; // openid作为用户名
  138. // password = username.substr(username.length - 6, 6); // 后六位作为登录密码
  139. // let query = new Parse.Query("_User")
  140. // query.equalTo(`wxapp.${appId}.openid`, username);
  141. // let current = await query.first()
  142. // if (current) {
  143. // await Parse.User.logIn(username, password);
  144. // }
  145. // }
  146. module.exports = {
  147. loginNow,
  148. // loginParse,
  149. wxLogin
  150. }