index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // pages/login/login.js
  2. import WxValidate from '../../utils/wxValidate'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. // 主体高度
  10. infoHeight: '',
  11. frameStyle: { useTop: true, name: '修改密码', leftArrow: true, useBar: false },
  12. form: {
  13. }
  14. },
  15. back: function () {
  16. wx.navigateBack({ url: '/pages/me/index' })
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. //验证必填项
  22. initValidate() {
  23. const rules = { password: { required: true }, }
  24. // 验证字段的提示信息,若不传则调用默认的信息
  25. const messages = { password: { required: '请输入新密码' }, };
  26. this.WxValidate = new WxValidate(rules, messages)
  27. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onLoad: function (options) {
  32. // 计算高度
  33. this.searchHeight();
  34. // 监听用户是否登录
  35. this.watchLogin();
  36. //验证规则函数
  37. this.initValidate();
  38. },
  39. // 监听用户是否登录
  40. watchLogin: function () {
  41. const that = this;
  42. wx.getStorage({
  43. key: 'token',
  44. success: res => {
  45. //数据请求
  46. wx.request({
  47. url: `${app.globalData.publicUrl}/courtAdmin/api/user/${res.data.id}`, //接口地址
  48. method: "get",//请求方法
  49. data: {},//请求参数
  50. header: {},
  51. success: res => {
  52. console.log(res);
  53. that.setData({ form: res.data.data })
  54. },
  55. error: err => {
  56. console.log(err);
  57. }
  58. })
  59. },
  60. fail: res => {
  61. wx.redirectTo({ url: '/pages/login/index', })
  62. }
  63. })
  64. },
  65. //提交
  66. formSubmit: function (e) {
  67. const value = e.detail.value;
  68. if (!this.WxValidate.checkForm(value)) {
  69. const error = this.WxValidate.errorList[0];
  70. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  71. return false
  72. } else {
  73. wx.getStorage({
  74. key: 'token',
  75. success: function (res) {
  76. wx.request({
  77. url: `${app.globalData.publicUrl}/courtAdmin/api/user/updatePwd/${res.data.id}`, //接口地址
  78. method: "post",
  79. data: value,
  80. header: {},
  81. success: res => {
  82. if (res.data.errcode == 0) {
  83. wx.showToast({
  84. title: '保存成功',
  85. icon: 'success',
  86. duration: 2000//延迟两秒
  87. })
  88. } else {
  89. wx.showToast({
  90. title: '保存失败',
  91. icon: 'success',
  92. duration: 2000
  93. })
  94. }
  95. },
  96. })
  97. }
  98. })
  99. }
  100. },
  101. // 计算高度
  102. searchHeight: function () {
  103. let frameStyle = this.data.frameStyle;
  104. let client = app.globalData.client;
  105. // 减去状态栏
  106. let infoHeight = client.windowHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  107. // 是否减去底部菜单
  108. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  109. if (infoHeight) this.setData({ infoHeight: infoHeight })
  110. },
  111. /**
  112. * 生命周期函数--监听页面初次渲染完成
  113. */
  114. onReady: function () {
  115. },
  116. /**
  117. * 生命周期函数--监听页面显示
  118. */
  119. onShow: function () {
  120. },
  121. /**
  122. * 生命周期函数--监听页面隐藏
  123. */
  124. onHide: function () {
  125. },
  126. /**
  127. * 生命周期函数--监听页面卸载
  128. */
  129. onUnload: function () {
  130. },
  131. /**
  132. * 页面相关事件处理函数--监听用户下拉动作
  133. */
  134. onPullDownRefresh: function () {
  135. },
  136. /**
  137. * 页面上拉触底事件的处理函数
  138. */
  139. onReachBottom: function () {
  140. },
  141. /**
  142. * 用户点击右上角分享
  143. */
  144. onShareAppMessage: function () {
  145. }
  146. })