index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. const moment = require("../../utils/moment.min");
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. form: {},
  10. },
  11. // 提交保存
  12. async toSave(e) {
  13. const that = this;
  14. const params = e.detail.value;
  15. if (!this.WxValidate.checkForm(params)) {
  16. const error = that.WxValidate.errorList[0];
  17. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  18. return false
  19. } else {
  20. if (params.password !== params.is_password) {
  21. wx.showToast({ title: '密码输入不一致', duration: 2000, icon: 'error', })
  22. } else {
  23. // 判断id使用
  24. let form = that.data.form;
  25. let data = { _id: form._id, password: params.password }
  26. let res;
  27. if (form._id) res = await app.$api(`user/rp`, 'POST', data);
  28. else wx.showToast({ title: `未登录无法修改密码`, icon: 'none' });
  29. if (res.errcode == '0') {
  30. wx.removeStorage({
  31. key: 'user',
  32. success(res) {
  33. wx.showToast({ title: `修改密码成功`, icon: 'success' });
  34. wx.redirectTo({ url: '/pagesHome/home/index' })
  35. }
  36. })
  37. } else {
  38. wx.showToast({ title: `${res.errmsg}`, icon: 'none' });
  39. }
  40. }
  41. }
  42. },
  43. /**
  44. * 生命周期函数--监听页面加载
  45. */
  46. async onLoad(options) {
  47. const that = this;
  48. wx.showLoading({ title: '加载中', mask: true })
  49. //验证规则函数
  50. that.initValidate();
  51. that.search()
  52. wx.hideLoading()
  53. },
  54. initValidate() {
  55. const rules = { password: { required: true }, is_password: { required: true } }
  56. const messages = { password: { required: '请输入新密码' }, is_password: { required: '请确认新密码' } };
  57. this.WxValidate = new WxValidate(rules, messages)
  58. },
  59. search() {
  60. const that = this;
  61. wx.getStorage({
  62. key: 'user',
  63. async success(res) {
  64. let form = {}
  65. let aee = await app.$api(`user/${res.data._id}`, 'GET', {})
  66. if (aee.errcode == '0') {
  67. form = aee.data;
  68. } else {
  69. wx.showToast({ title: `${aee.errmsg}`, icon: 'error' });
  70. }
  71. that.setData({ form })
  72. },
  73. fail(err) {
  74. // console.log(err);
  75. }
  76. })
  77. },
  78. /**
  79. * 生命周期函数--监听页面初次渲染完成
  80. */
  81. onReady() {
  82. },
  83. /**
  84. * 生命周期函数--监听页面显示
  85. */
  86. onShow() {
  87. },
  88. /**
  89. * 生命周期函数--监听页面隐藏
  90. */
  91. onHide() {
  92. },
  93. /**
  94. * 生命周期函数--监听页面卸载
  95. */
  96. onUnload() {
  97. },
  98. /**
  99. * 页面相关事件处理函数--监听用户下拉动作
  100. */
  101. onPullDownRefresh() {
  102. },
  103. /**
  104. * 页面上拉触底事件的处理函数
  105. */
  106. onReachBottom() {
  107. },
  108. /**
  109. * 用户点击右上角分享
  110. */
  111. onShareAppMessage() {
  112. }
  113. })