You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

login.js 3.2KB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // pages/login/login.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. isLoginIng: false,
  9. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  10. hasUserInfo: false,
  11. },
  12. /**
  13. * 登录
  14. */
  15. checkLogin(e) {
  16. if (this.data.isLoginIng) {
  17. return
  18. }
  19. var data = e.detail.value;
  20. if (data.userName == '') {
  21. wx.showToast({
  22. title: '请先输入账号',
  23. icon: 'none',
  24. duration: 2000
  25. })
  26. } else if (data.password == '') {
  27. wx.showToast({
  28. title: '请先输入密码',
  29. icon: 'none',
  30. duration: 2000
  31. })
  32. } else {
  33. this.setData({
  34. isLoginIng: true
  35. })
  36. this.login(data);
  37. }
  38. },
  39. login(data) {
  40. var data={
  41. account_id: data.userName,
  42. account_password: data.password
  43. }
  44. app.requestPost('register', data,res=>{
  45. this.setData({
  46. isLoginIng: false
  47. })
  48. if(res.code==200){
  49. app.globalData.storeData = res.data;
  50. wx.switchTab({
  51. url: '../home/home'
  52. })
  53. }else{
  54. wx.showToast({
  55. title: res.message,
  56. icon: 'none',
  57. duration: 2000
  58. })
  59. }
  60. })
  61. },
  62. /**
  63. * 生命周期函数--监听页面加载
  64. */
  65. onLoad: function(options) {
  66. wx.hideShareMenu();
  67. if (app.globalData.userInfo) {
  68. this.setData({
  69. hasUserInfo: true
  70. })
  71. } else if (this.data.canIUse) {
  72. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  73. // 所以此处加入 callback 以防止这种情况
  74. app.userInfoReadyCallback = res => {
  75. this.setData({
  76. hasUserInfo: true
  77. })
  78. }
  79. } else {
  80. // 在没有 open-type=getUserInfo 版本的兼容处理
  81. wx.getUserInfo({
  82. success: res => {
  83. app.globalData.userInfo = res.userInfo
  84. this.setData({
  85. hasUserInfo: true
  86. })
  87. }
  88. })
  89. }
  90. if (app.globalData.userState==1){//已注册
  91. wx.switchTab({
  92. url: '../home/home'
  93. })
  94. } else if (app.globalData.userState == -1){
  95. app.userStateReadyCallback = res => {
  96. if(res){
  97. wx.switchTab({
  98. url: '../home/home'
  99. })
  100. }
  101. }
  102. }
  103. },
  104. /**
  105. * 获取头像昵称
  106. */
  107. getUserInfo: function (e) {
  108. app.globalData.userInfo = e.detail.userInfo
  109. this.setData({
  110. hasUserInfo: true
  111. })
  112. if (e.detail.userInfo){
  113. app.requestPost('submit', e.detail.userInfo,res=>{
  114. console.log(res)
  115. })
  116. }
  117. },
  118. /**
  119. * 生命周期函数--监听页面初次渲染完成
  120. */
  121. onReady: function() {
  122. },
  123. /**
  124. * 生命周期函数--监听页面显示
  125. */
  126. onShow: function() {
  127. },
  128. /**
  129. * 生命周期函数--监听页面隐藏
  130. */
  131. onHide: function() {
  132. },
  133. /**
  134. * 生命周期函数--监听页面卸载
  135. */
  136. onUnload: function() {
  137. },
  138. /**
  139. * 页面相关事件处理函数--监听用户下拉动作
  140. */
  141. onPullDownRefresh: function() {
  142. },
  143. /**
  144. * 页面上拉触底事件的处理函数
  145. */
  146. onReachBottom: function() {
  147. },
  148. /**
  149. * 用户点击右上角分享
  150. */
  151. onShareAppMessage: function() {
  152. }
  153. })