login.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var Parse = getApp().Parse;
  2. function loginNow(forceAuth = true) {
  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. // 使用插件的 checkAuth 方法进行授权
  26. console.log('🔄 调用插件的 checkAuth 方法...');
  27. const checkAuth = getApp().checkAuth;
  28. if (checkAuth) {
  29. // 调用插件的授权方法
  30. checkAuth(forceAuth).then((result) => {
  31. console.log('✅ checkAuth 成功:', result);
  32. // 授权成功后,更新本地存储
  33. const newUser = Parse.User.current();
  34. if (newUser && newUser.get('mobile')) {
  35. wx.setStorageSync("userLogin", newUser.id);
  36. console.log('✅ 已设置 userLogin:', newUser.id);
  37. console.log('✅ 用户手机号:', newUser.get('mobile'));
  38. // 提示用户授权成功
  39. wx.showToast({
  40. title: '登录成功',
  41. icon: 'success',
  42. duration: 2000
  43. });
  44. // 返回上一页或刷新当前页
  45. setTimeout(() => {
  46. const pages = getCurrentPages();
  47. if (pages.length > 1) {
  48. wx.navigateBack();
  49. }
  50. }, 2000);
  51. }
  52. }).catch((err) => {
  53. console.error('❌ checkAuth 失败:', err);
  54. wx.showToast({
  55. title: '授权失败,请重试',
  56. icon: 'none',
  57. duration: 2000
  58. });
  59. });
  60. } else {
  61. console.error('❌ 找不到 checkAuth 方法');
  62. wx.showToast({
  63. title: '登录功能异常,请重启小程序',
  64. icon: 'none',
  65. duration: 3000
  66. });
  67. }
  68. console.log('===========================================');
  69. return false;
  70. }
  71. /* 通过小程序wx.login获取openid和session_key */
  72. function wxLogin() {
  73. return new Promise((resolve, reject) => {
  74. wx.login({
  75. success: function (res) {
  76. if (res.code) {
  77. let url = 'https://server.fmode.cn/api/wxapp/auth_wxapp'
  78. wx.request({
  79. url: url,
  80. data: {
  81. c: getApp().globalData.company,
  82. code: res.code,
  83. appType: getApp().globalData.appType ? getApp().globalData.appType : ''
  84. },
  85. async success(res) {
  86. wx.setStorageSync("userInfo", res.data);
  87. resolve(res)
  88. },
  89. });
  90. }
  91. },
  92. fail: function (err) {
  93. wx.showToast({
  94. title: '服务器繁忙',
  95. icon: 'error'
  96. })
  97. resolve()
  98. }
  99. });
  100. })
  101. }
  102. // async function loginParse() {
  103. // let userInfo = wx.getStorageSync("userInfo");
  104. // let appId = getApp().globalData.appid
  105. // let username, password;
  106. // username = userInfo.openid; // openid作为用户名
  107. // password = username.substr(username.length - 6, 6); // 后六位作为登录密码
  108. // let query = new Parse.Query("_User")
  109. // query.equalTo(`wxapp.${appId}.openid`, username);
  110. // let current = await query.first()
  111. // if (current) {
  112. // await Parse.User.logIn(username, password);
  113. // }
  114. // }
  115. module.exports = {
  116. loginNow,
  117. // loginParse,
  118. wxLogin
  119. }