Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

168 linhas
3.1KB

  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. wx.switchTab({
  50. url: '../home/home'
  51. })
  52. }else{
  53. wx.showToast({
  54. title: res.message,
  55. icon: 'none',
  56. duration: 2000
  57. })
  58. }
  59. })
  60. },
  61. /**
  62. * 生命周期函数--监听页面加载
  63. */
  64. onLoad: function(options) {
  65. wx.hideShareMenu();
  66. if (app.globalData.userInfo) {
  67. this.setData({
  68. hasUserInfo: true
  69. })
  70. } else if (this.data.canIUse) {
  71. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  72. // 所以此处加入 callback 以防止这种情况
  73. app.userInfoReadyCallback = res => {
  74. this.setData({
  75. hasUserInfo: true
  76. })
  77. }
  78. } else {
  79. // 在没有 open-type=getUserInfo 版本的兼容处理
  80. wx.getUserInfo({
  81. success: res => {
  82. app.globalData.userInfo = res.userInfo
  83. this.setData({
  84. hasUserInfo: true
  85. })
  86. }
  87. })
  88. }
  89. if (app.globalData.userState==1){//已注册
  90. wx.switchTab({
  91. url: '../home/home'
  92. })
  93. } else if (app.globalData.userState == -1){
  94. app.userStateReadyCallback = res => {
  95. if(res){
  96. wx.switchTab({
  97. url: '../home/home'
  98. })
  99. }
  100. }
  101. }
  102. },
  103. /**
  104. * 获取头像昵称
  105. */
  106. getUserInfo: function (e) {
  107. app.globalData.userInfo = e.detail.userInfo
  108. this.setData({
  109. hasUserInfo: true
  110. })
  111. if (e.detail.userInfo){
  112. app.requestPost('submit', e.detail.userInfo,res=>{
  113. console.log(res)
  114. })
  115. }
  116. },
  117. /**
  118. * 生命周期函数--监听页面初次渲染完成
  119. */
  120. onReady: function() {
  121. },
  122. /**
  123. * 生命周期函数--监听页面显示
  124. */
  125. onShow: function() {
  126. },
  127. /**
  128. * 生命周期函数--监听页面隐藏
  129. */
  130. onHide: function() {
  131. },
  132. /**
  133. * 生命周期函数--监听页面卸载
  134. */
  135. onUnload: function() {
  136. },
  137. /**
  138. * 页面相关事件处理函数--监听用户下拉动作
  139. */
  140. onPullDownRefresh: function() {
  141. },
  142. /**
  143. * 页面上拉触底事件的处理函数
  144. */
  145. onReachBottom: function() {
  146. },
  147. /**
  148. * 用户点击右上角分享
  149. */
  150. onShareAppMessage: function() {
  151. }
  152. })