login.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // pages/my/my.js
  2. var app=getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. buttonText:'发送验证码',
  9. phone:"",
  10. code:"",
  11. countDown:60,
  12. timer:null,
  13. sendCodeDisabled:false,
  14. },
  15. onPhoneInput(e){
  16. this.setData({
  17. phone:e.detail
  18. });
  19. },
  20. onCodeInput(e){
  21. this.setData({
  22. code:e.detail
  23. });
  24. },
  25. sendCode(){
  26. if(this.data.sendCodeDisabled){
  27. return;
  28. }
  29. if(this.data.phone.length<11){
  30. wx.showToast({
  31. title: '手机号长度错误',
  32. icon:"none",
  33. });
  34. return;
  35. };
  36. var reg=/^(1[3|4|5|6|7|8|9])\d{9}$/;
  37. if(!reg.test(this.data.phone)){
  38. wx.showToast({
  39. title: '手机号格式错误',
  40. icon:"none",
  41. });
  42. return;
  43. }
  44. wx.request({
  45. url: 'http://127.0.0.1:8000/api/message/',
  46. data: {
  47. phone:this.data.phone
  48. },
  49. method: 'GET',
  50. success: (res) => {
  51. console.log(res);
  52. if(res.data.status){
  53. this.setData({
  54. sendCodeDisabled:true,
  55. timer:setInterval(this.hcountDown,1000)
  56. });
  57. wx.showToast({
  58. title: res.data.message,
  59. icon:"none",
  60. });
  61. }else{
  62. wx.showToast({
  63. title: res.data.message,
  64. icon:"none",
  65. });
  66. }
  67. },
  68. })
  69. },
  70. hcountDown(){
  71. var countDown=this.data.countDown;
  72. if(countDown===0){
  73. clearInterval(this.data.timer);
  74. this.setData({
  75. buttonText:'发送验证码',
  76. sendCodeDisabled:false,
  77. countDown:60
  78. });
  79. return;
  80. }
  81. this.setData({
  82. buttonText:countDown+'s',
  83. countDown:countDown-1
  84. });
  85. },
  86. login2(e){
  87. wx.showLoading({
  88. title: '登录中...',
  89. mask: true // 防止触摸穿透
  90. });
  91. wx.request({
  92. url: 'http://127.0.0.1:8000/api/login/',
  93. data: {
  94. phone:this.data.phone,
  95. code:this.data.code
  96. },
  97. method: 'POST',
  98. dataType:'json',
  99. success: (res) => {
  100. console.log(res);
  101. if(res.data.status){
  102. console.log(res.data.data);
  103. console.log(e);
  104. app.initUserInfo(res.data.data,e.detail.userInfo)
  105. wx.navigateBack({});
  106. }else{
  107. wx.showToast({
  108. title:res.data.message,
  109. icon:"none",
  110. });
  111. }
  112. },
  113. });
  114. wx.hideLoading();
  115. },
  116. /**
  117. * 生命周期函数--监听页面加载
  118. */
  119. onLoad(options) {
  120. },
  121. /**
  122. * 生命周期函数--监听页面初次渲染完成
  123. */
  124. onReady() {
  125. },
  126. /**
  127. * 生命周期函数--监听页面显示
  128. */
  129. onShow() {
  130. },
  131. /**
  132. * 生命周期函数--监听页面隐藏
  133. */
  134. onHide() {
  135. },
  136. /**
  137. * 生命周期函数--监听页面卸载
  138. */
  139. onUnload() {
  140. clearInterval(this.data.timer);
  141. },
  142. /**
  143. * 页面相关事件处理函数--监听用户下拉动作
  144. */
  145. onPullDownRefresh() {
  146. },
  147. /**
  148. * 页面上拉触底事件的处理函数
  149. */
  150. onReachBottom() {
  151. },
  152. /**
  153. * 用户点击右上角分享
  154. */
  155. onShareAppMessage() {
  156. }
  157. })