微信小程序项目模板文件
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.

180 lines
6.2KB

  1. // pages/login/login.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. form: {
  8. username: '',
  9. password: ''
  10. },
  11. remember: false,
  12. formRules: {
  13. username: {
  14. validator: function(value) {
  15. return value.trim() != ''
  16. },
  17. tips: '请输入用户名',
  18. placeholder: '请输入用户名'
  19. },
  20. password: {
  21. validator: function(value) {
  22. return value.trim() != ''
  23. },
  24. tips: '请输入密码',
  25. placeholder: '请输入密码'
  26. }
  27. }
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad: function (options) {
  33. },
  34. /**
  35. * 生命周期函数--监听页面初次渲染完成
  36. */
  37. onReady: function () {
  38. },
  39. /**
  40. * 生命周期函数--监听页面显示
  41. */
  42. onShow: function () {
  43. // 隐藏返回图标
  44. wx.hideHomeButton();
  45. },
  46. checkboxChange(e) {
  47. let checked = this.data.remember
  48. this.setData({remember: !checked})
  49. },
  50. bindValue(e) {
  51. let name = e.currentTarget.dataset.name;
  52. let form = this.data.form;
  53. form[name] = e.detail.value;
  54. this.setData({
  55. form,
  56. })
  57. this.validate(name, e.detail.value)
  58. },
  59. validate(name) {
  60. let formRules = this.data.formRules;
  61. let validator = this.data.formRules[name].validator
  62. let result = validator ? !validator(this.data.form[name]) : false;
  63. formRules[name].warning = result
  64. this.setData({
  65. formRules
  66. })
  67. return result
  68. },
  69. validateForm() {
  70. return new Promise((resolve, reject) => {
  71. try {
  72. let formRules = this.data.formRules;
  73. let result = false;
  74. for (let key in formRules) {
  75. let temp = this.validate(key)
  76. if (temp) {
  77. result = temp
  78. }
  79. }
  80. resolve(!result)
  81. } catch (e) {
  82. reject(e)
  83. }
  84. })
  85. },
  86. submitForm() {
  87. this.validateForm().then(res => {
  88. if (res) {
  89. wx.showLoading({title: "登录中", mask:true});
  90. // 判断是否有记住密码
  91. if(this.data.remember) {
  92. wx.setStorageSync('remember', true)
  93. wx.setStorageSync('loginForm', this.data.form)
  94. } else {
  95. wx.removeStorageSync('remember')
  96. wx.removeStorageSync('loginForm')
  97. }
  98. wx.request({
  99. url: baseURL+"/member/login",
  100. method: "POST",
  101. data: this.data.form,
  102. header: {
  103. 'content-type': 'application/json'
  104. },
  105. success(res) {
  106. if (res.data.code == 0) {
  107. // 以token获取用户信息
  108. let token = res.data.data
  109. wx.setStorageSync('token', token) // token存入storage
  110. wx.request({
  111. url: baseURL+'/member/user',
  112. method: "POST",
  113. header: {
  114. 'content-type': 'application/x-www-form-urlencoded',
  115. "authorization": token
  116. },
  117. success: (res) => {
  118. wx.setStorageSync('userInfo', res.data.data)
  119. let app=getApp();
  120. let observe=app.globalData.observe;
  121. let getToDoNum=app.getToDoNum;
  122. // 跳转至tabBar
  123. wx.switchTab({
  124. url: '/pages/home/home'
  125. })
  126. getToDoNum(observe).then((res)=>{
  127. if(res){
  128. if (observe.value > 0) {
  129. wx.setTabBarBadge({
  130. index: 1,
  131. text: observe.value + "",
  132. })
  133. } else {
  134. wx.removeTabBarBadge({
  135. index: 1,
  136. })
  137. }
  138. }else{
  139. wx.removeTabBarBadge({
  140. index: 1,
  141. })
  142. }
  143. }).catch(e=>{console.log(e)});
  144. },
  145. fail: (res) => {
  146. },
  147. complete: (res) => {
  148. wx.hideLoading()
  149. },
  150. })
  151. }
  152. else{
  153. wx.hideLoading()
  154. wx.showToast({
  155. title: res.data.msg,
  156. icon: 'error',
  157. image: '',
  158. duration: 1500,
  159. });
  160. }
  161. },
  162. fail(error) {
  163. wx.hideLoading();
  164. wx.showToast({
  165. title:error.errMsg,
  166. icon:"error",
  167. duration:1000,
  168. })
  169. }
  170. })
  171. }
  172. })
  173. }
  174. })