login.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. Page({
  2. data: {
  3. phoneNumber: '',
  4. code: '',
  5. hasPhoneNumber: false,
  6. hasCode: false,
  7. isSendingCode: false,
  8. isLoggedIn: false,
  9. // 初始不显示倒计时
  10. countdown: 0,
  11. // 控制是否显示倒计时
  12. showCountdown: false,
  13. },
  14. loginByCode: function(phoneNumber, code) {
  15. wx.request({
  16. url: 'https://your-backend.com/login', // 你的后端登录接口
  17. method: 'POST',
  18. data: {
  19. phoneNumber: phoneNumber,
  20. code: code
  21. },
  22. success(res) {
  23. if (res.data.success) {
  24. // 保存登录凭证
  25. wx.setStorageSync('loginToken', res.data.token);
  26. wx.showToast({
  27. title: '登录成功',
  28. icon: 'success',
  29. duration: 2000
  30. });
  31. // 跳转到主页面等
  32. } else {
  33. wx.showToast({
  34. title: '登录失败',
  35. icon: 'none',
  36. duration: 2000
  37. });
  38. }
  39. },
  40. fail(err) {
  41. console.error(err);
  42. wx.showToast({
  43. title: '网络请求失败',
  44. icon: 'none',
  45. duration: 2000
  46. });
  47. }
  48. });
  49. },
  50. sendVerificationCode:function(e) {
  51. let phoneNumber = e.detail.value.phoneNumber; // 假设这是从输入框获取的手机号
  52. // 调用API发送验证码
  53. wx.request({
  54. url: 'https://your-backend.com/api/send-verification-code',
  55. method: 'POST',
  56. data: {
  57. phoneNumber: phoneNumber
  58. },
  59. success: function(res) {
  60. if (res.statusCode === 200 && res.data.success) {
  61. wx.showToast({
  62. title: '验证码已发送',
  63. icon: 'success',
  64. duration: 2000
  65. });
  66. } else {
  67. wx.showToast({
  68. title: '发送验证码失败',
  69. icon: 'none',
  70. duration: 2000
  71. });
  72. }
  73. },
  74. fail: function() {
  75. wx.showToast({
  76. title: '网络请求失败',
  77. icon: 'none',
  78. duration: 2000
  79. });
  80. }
  81. });
  82. },
  83. handlePhoneNumberInput: function(e) {
  84. this.setData({
  85. phoneNumber: e.detail.value,
  86. hasPhoneNumber: e.detail.value.trim() !== '',
  87. isSendingCode: false, // 清除发送状态,如果用户修改了手机号
  88. });
  89. },
  90. handleCodeInput: function(e) {
  91. this.setData({
  92. code: e.detail.value,
  93. hasCode: e.detail.value.trim() !== '',
  94. });
  95. },
  96. sendCode: function() {
  97. console.log('123213');
  98. if (!this.data.hasPhoneNumber) return;
  99. this.setData({
  100. isSendingCode: true,
  101. });
  102. // this.sendVerificationCode(this.data.phoneNumber).then(() => {
  103. // wx.showToast({ title: '验证码已发送', icon: 'success' });
  104. // this.setData({
  105. // isSendingCode: false,
  106. // });
  107. // }).catch(err => {
  108. // wx.showToast({ title: '发送失败', icon: 'none' });
  109. // this.setData({
  110. // isSendingCode: false,
  111. // });
  112. // });
  113. console.log('验证码已发出');
  114. // 启动倒计时
  115. this.startCountdown(60);
  116. },
  117. login: function() {
  118. if (!this.data.hasPhoneNumber || !this.data.hasCode) {
  119. wx.showModal({
  120. title: '提示',
  121. content: '手机号或验证码不能为空',
  122. showCancel: false
  123. });
  124. return;
  125. }
  126. if (!(/^1[3-9]\d{9}$/.test(this.data.phoneNumber))) {
  127. wx.showModal({
  128. title: '提示',
  129. content: '手机号格式不正确',
  130. showCancel: false
  131. });
  132. return;
  133. }
  134. // this.loginByCode(this.data.phoneNumber, this.data.code).then(token => {
  135. // wx.setStorageSync('loginToken', token);
  136. // this.setData({
  137. // isLoggedIn: true,
  138. // });
  139. // wx.reLaunch({
  140. // url: '/pages/scan/scan',
  141. // });
  142. // }).catch(err => {
  143. // wx.showToast({ title: '登录失败', icon: 'none' });
  144. // });
  145. this.setData({
  146. isLoggedIn: true,
  147. });
  148. wx.setStorageSync('loginToken', 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoxLCJ1c2VyX2tleSI6IjQ1OTJhZDlhLTI0NjMtNDlhOS05YjlkLTQ0NTNhYWM5MDI1NSIsInVzZXJuYW1lIjoiYWRtaW4ifQ.efQTBQSRiiKgPWjiIbeOMyCiScrWuCiFn0T4CcylzZKaeMvN241puUV4OoloG98dw6J67pCCwbuIf7n9gFSZmQ');
  149. wx.showModal({
  150. title: '提示',
  151. content: '登录成功',
  152. showCancel: false
  153. });
  154. setTimeout(function() {
  155. wx.reLaunch({
  156. url: '/subpages/scan/scan',
  157. })
  158. }, 2000) // 设置延时时间,单位为毫秒
  159. },
  160. startCountdown: function(seconds) {
  161. let that = this;
  162. this.setData({
  163. showCountdown: true,
  164. countdown: seconds
  165. });
  166. let timer = setInterval(function() {
  167. seconds--;
  168. if (seconds <= 0) {
  169. clearInterval(timer);
  170. that.setData({
  171. showCountdown: false,
  172. countdown: 0
  173. });
  174. } else {
  175. that.setData({
  176. countdown: seconds
  177. });
  178. }
  179. }, 1000);
  180. },
  181. logout: function() {
  182. // 清除登录状态
  183. wx.removeStorageSync('loginToken'); // 清除token
  184. this.setData({
  185. isLoggedIn: false,
  186. // 可能还需要清除其他与登录状态相关的数据
  187. });
  188. // 跳转到登录页面或其他页面
  189. wx.reLaunch({
  190. url: '/pages/login/login',
  191. });
  192. },
  193. /**
  194. * 生命周期函数--监听页面卸载
  195. */
  196. onUnload: function () {
  197. // 清除登录状态
  198. // wx.removeStorageSync('loginToken'); // 清除token
  199. },
  200. })