login.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const app = getApp();
  2. Page({
  3. data: {
  4. isLoading: false
  5. },
  6. onLoad: function (options) {
  7. if (app.globalData.isLoggedIn) {
  8. this.goBack();
  9. }
  10. },
  11. onGetPhoneNumber: function (e) {
  12. if (this.data.isLoading) return;
  13. const detail = e.detail;
  14. if (!detail.code) {
  15. wx.showToast({
  16. title: '授权失败',
  17. icon: 'none'
  18. });
  19. return;
  20. }
  21. this.setData({ isLoading: true });
  22. wx.login({
  23. success: (loginRes) => {
  24. if (loginRes.code) {
  25. this.doLogin(loginRes.code, detail.code);
  26. } else {
  27. this.setData({ isLoading: false });
  28. wx.showToast({
  29. title: '获取登录凭证失败',
  30. icon: 'none'
  31. });
  32. }
  33. },
  34. fail: () => {
  35. this.setData({ isLoading: false });
  36. wx.showToast({
  37. title: '登录失败',
  38. icon: 'none'
  39. });
  40. }
  41. });
  42. },
  43. doLogin: function (loginCode, phoneCode) {
  44. const requestUrl = `${app.globalData.baseUrl}/api/wechat/login`;
  45. console.log('[Login] baseUrl:', app.globalData.baseUrl);
  46. console.log('[Login] 请求地址:', requestUrl);
  47. console.log('[Login] loginCode:', loginCode ? loginCode.substring(0, 10) + '...' : 'empty');
  48. console.log('[Login] phoneCode:', phoneCode ? phoneCode.substring(0, 10) + '...' : 'empty');
  49. wx.request({
  50. url: requestUrl,
  51. method: 'POST',
  52. data: {
  53. code: loginCode,
  54. phoneCode: phoneCode
  55. },
  56. timeout: 30000,
  57. success: (response) => {
  58. this.setData({ isLoading: false });
  59. console.log('[Login] 响应状态码:', response.statusCode);
  60. console.log('[Login] 响应数据:', JSON.stringify(response.data));
  61. if (response.data && response.data.success) {
  62. app.globalData.token = response.data.token;
  63. app.globalData.userInfo = response.data.user;
  64. app.globalData.isLoggedIn = true;
  65. wx.setStorageSync('token', response.data.token);
  66. wx.setStorageSync('userInfo', response.data.user);
  67. wx.showToast({
  68. title: '登录成功',
  69. icon: 'success'
  70. });
  71. setTimeout(() => {
  72. this.goBack();
  73. }, 1500);
  74. } else {
  75. console.warn('[Login] 登录失败,服务端返回:', response.data);
  76. wx.showToast({
  77. title: response.data && response.data.message || '登录失败',
  78. icon: 'none'
  79. });
  80. }
  81. },
  82. fail: (err) => {
  83. this.setData({ isLoading: false });
  84. console.error('[Login] 网络请求失败:', JSON.stringify(err));
  85. console.error('[Login] errMsg:', err.errMsg);
  86. console.error('[Login] errno:', err.errno);
  87. wx.showToast({
  88. title: '网络请求失败,请重试',
  89. icon: 'none'
  90. });
  91. },
  92. complete: () => {
  93. if (this.data.isLoading) {
  94. this.setData({ isLoading: false });
  95. }
  96. }
  97. });
  98. },
  99. goBack: function () {
  100. const pages = getCurrentPages();
  101. if (pages.length > 1) {
  102. wx.navigateBack();
  103. } else {
  104. wx.switchTab({
  105. url: '/pages/index/index'
  106. });
  107. }
  108. },
  109. goToAgreement: function () {
  110. wx.showModal({
  111. title: '用户协议',
  112. content: '用户协议内容:您同意使用本服务即表示同意遵守相关条款...',
  113. showCancel: false
  114. });
  115. },
  116. goToPrivacy: function () {
  117. wx.showModal({
  118. title: '隐私政策',
  119. content: '隐私政策内容:我们重视您的隐私,将妥善保管您的个人信息...',
  120. showCancel: false
  121. });
  122. }
  123. });