index.js 4.5 KB

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