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.

147 lines
4.3KB

  1. // pages/login/login.js
  2. import {userLogin, getUser} from '../../api/login.js'
  3. import baseURL from '../../environments.js'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. form: {
  10. username: '',
  11. password: ''
  12. },
  13. remember: false,
  14. formRules: {
  15. username: {
  16. validator: function(value) {
  17. return value.trim() != ''
  18. },
  19. tips: '请输入用户名',
  20. placeholder: '请输入用户名',
  21. isPassword: false
  22. },
  23. password: {
  24. validator: function(value) {
  25. return value.trim() != ''
  26. },
  27. tips: '请输入密码',
  28. placeholder: '请输入密码',
  29. isPassword: true
  30. }
  31. }
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. },
  38. /**
  39. * 生命周期函数--监听页面初次渲染完成
  40. */
  41. onReady: function () {
  42. },
  43. /**
  44. * 生命周期函数--监听页面显示
  45. */
  46. onShow: function () {
  47. // 隐藏返回图标
  48. wx.hideHomeButton();
  49. // 判断是否有记住密码
  50. if(wx.getStorageSync('remember')) {
  51. this.setData({
  52. rememberChecked: true
  53. })
  54. this.setData({
  55. form: wx.getStorageSync('loginForm')
  56. })
  57. }
  58. },
  59. checkboxChange(e) {
  60. let checked = this.data.remember
  61. this.setData({remember: !checked})
  62. },
  63. bindValue(e) {
  64. let name = e.currentTarget.dataset.name;
  65. let form = this.data.form;
  66. form[name] = e.detail.value;
  67. this.setData({
  68. form,
  69. })
  70. this.validate(name, e.detail.value)
  71. },
  72. validate(name) {
  73. let formRules = this.data.formRules;
  74. let validator = this.data.formRules[name].validator
  75. let result = validator ? !validator(this.data.form[name]) : false;
  76. formRules[name].warning = result
  77. this.setData({
  78. formRules
  79. })
  80. return result
  81. },
  82. validateForm() {
  83. return new Promise((resolve, reject) => {
  84. try {
  85. let formRules = this.data.formRules;
  86. let result = false;
  87. for (let key in formRules) {
  88. let temp = this.validate(key)
  89. if (temp) {
  90. result = temp
  91. }
  92. }
  93. resolve(!result)
  94. } catch (e) {
  95. reject(e)
  96. }
  97. })
  98. },
  99. submitForm() {
  100. this.validateForm().then(res => {
  101. if (res) {
  102. wx.showLoading({title: "登录中", mask:true});
  103. // 判断是否有记住密码
  104. if(this.data.remember) {
  105. wx.setStorageSync('remember', true)
  106. wx.setStorageSync('loginForm', this.data.form)
  107. } else {
  108. wx.removeStorageSync('remember')
  109. wx.removeStorageSync('loginForm')
  110. }
  111. // 登录
  112. userLogin(this.data.form).then(res=> {
  113. wx.hideLoading()
  114. if (res?.code === 0) {
  115. // 以token获取用户信息
  116. let token = res.data.token
  117. wx.setStorageSync('token', token) // token存入storage
  118. // 获取用户信息
  119. getUser().then(response=> {
  120. if(response.code === 0) {
  121. wx.setStorageSync('userInfo', response.data)
  122. // 跳转至tabBar
  123. wx.switchTab({
  124. url: '/pages/task/task'
  125. })
  126. }
  127. }).catch(e=> {
  128. console.log(e);
  129. })
  130. } else{
  131. wx.showToast({
  132. title: res?.msg || '用户名密码错误',
  133. icon: 'none',
  134. duration: 2000,
  135. });
  136. }
  137. })
  138. }
  139. })
  140. }
  141. })