login.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // pages/my/my.js
  2. var app = getApp();
  3. Page({
  4. data: {
  5. buttonText: '发送验证码',
  6. phone: "",
  7. code: "",
  8. countDown: 60,
  9. timer: null,
  10. sendCodeDisabled: false,
  11. },
  12. onPhoneInput: function(e) {
  13. this.setData({
  14. phone: e.detail
  15. });
  16. },
  17. onCodeInput: function(e) {
  18. this.setData({
  19. code: e.detail
  20. });
  21. },
  22. sendCode: function() {
  23. if (this.data.sendCodeDisabled) return;
  24. // 手机号验证
  25. if (this.data.phone.length < 11) {
  26. wx.showToast({
  27. title: '手机号长度错误',
  28. icon: "none",
  29. });
  30. return;
  31. }
  32. var reg = /^(1[3|4|5|6|7|8|9])\d{9}$/;
  33. if (!reg.test(this.data.phone)) {
  34. wx.showToast({
  35. title: '手机号格式错误',
  36. icon: "none",
  37. });
  38. return;
  39. }
  40. wx.request({
  41. url: 'http://127.0.0.1:8000/api/message/',
  42. data: { phone: this.data.phone },
  43. method: 'GET',
  44. success: (res) => {
  45. if (res.data.status) {
  46. this.setData({
  47. sendCodeDisabled: true,
  48. timer: setInterval(this.countDown.bind(this), 1000)
  49. });
  50. wx.showToast({
  51. title: '验证码发送成功',
  52. icon: "none",
  53. });
  54. } else {
  55. wx.showToast({
  56. title: (res.data && res.data.message) || '验证码发送失败',
  57. icon: "none",
  58. });
  59. }
  60. },
  61. fail: (err) => {
  62. wx.showToast({
  63. title: '网络错误,请重试',
  64. icon: 'none'
  65. });
  66. }
  67. });
  68. },
  69. countDown: function() {
  70. var countDown = this.data.countDown - 1;
  71. if (countDown <= 0) {
  72. clearInterval(this.data.timer);
  73. this.setData({
  74. buttonText: '发送验证码',
  75. sendCodeDisabled: false,
  76. countDown: 60
  77. });
  78. return;
  79. }
  80. this.setData({
  81. buttonText: countDown + 's',
  82. countDown: countDown
  83. });
  84. },
  85. login: function() {
  86. if (!this.data.phone || !this.data.code) {
  87. wx.showToast({
  88. title: '请输入手机号和验证码',
  89. icon: 'none'
  90. });
  91. return;
  92. }
  93. wx.request({
  94. url: 'http://127.0.0.1:8000/api/login/',
  95. method: 'POST',
  96. header: {
  97. 'Content-Type': 'application/json'
  98. },
  99. data: {
  100. phone: this.data.phone,
  101. code: this.data.code
  102. },
  103. success: (res) => {
  104. if (res.data && res.data.status) {
  105. // 强化token存储逻辑
  106. try {
  107. const token = res.data.data.token;
  108. const userInfo = res.data.data;
  109. wx.setStorageSync('token', token);
  110. wx.setStorageSync('userInfo', userInfo);
  111. // 验证存储是否成功
  112. const storedToken = wx.getStorageSync('token');
  113. if (!storedToken) {
  114. throw new Error('Token存储失败');
  115. }
  116. app.globalData.userInfo = userInfo;
  117. app.globalData.token = token;
  118. wx.showToast({
  119. title: '登录成功',
  120. icon: 'success'
  121. });
  122. // 跳转前检查页面栈
  123. const pages = getCurrentPages();
  124. if (pages.length > 1) {
  125. wx.navigateBack();
  126. } else {
  127. wx.switchTab({
  128. url: '/pages/my/my'
  129. });
  130. }
  131. } catch (e) {
  132. console.error('登录处理错误:', e);
  133. wx.showToast({
  134. title: '登录状态保存失败',
  135. icon: 'none'
  136. });
  137. }
  138. } else {
  139. wx.showToast({
  140. title: (res.data && res.data.message) || '登录失败',
  141. icon: 'none'
  142. });
  143. }
  144. },
  145. fail: (err) => {
  146. wx.showToast({
  147. title: '网络错误,请重试',
  148. icon: 'none'
  149. });
  150. }
  151. });
  152. },
  153. onUnload: function() {
  154. if (this.data.timer) {
  155. clearInterval(this.data.timer);
  156. }
  157. }
  158. });