editPwd.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //index.js
  2. import WxValidate from '../../utils/wxValidate';
  3. //获取应用实例
  4. const app = getApp();
  5. Page({
  6. data: {
  7. form: {}
  8. },
  9. initValidate() {
  10. const rules = {
  11. phone: {
  12. required: true,
  13. },
  14. oldPwd: {
  15. required: true,
  16. },
  17. password: {
  18. required: true,
  19. },
  20. isnewPwd: {
  21. required: false,
  22. },
  23. }
  24. // 验证字段的提示信息,若不传则调用默认的信息
  25. const messages = {
  26. phone: {
  27. required: '请输入手机号',
  28. },
  29. oldPwd: {
  30. required: '请输入原密码',
  31. },
  32. password: {
  33. required: '请输入新密码',
  34. },
  35. isnewPwd: {
  36. required: '请输入确认密码'
  37. }
  38. };
  39. this.WxValidate = new WxValidate(rules, messages)
  40. },
  41. //事件处理函数
  42. formSubmit: function (e) {
  43. const params = e.detail.value;
  44. if (!this.WxValidate.checkForm(params)) {
  45. const error = this.WxValidate.errorList[0];
  46. wx.showToast({
  47. title: error.msg,
  48. icon: 'none',
  49. duration: 2000
  50. })
  51. return false
  52. } else {
  53. // 判断新密码,确认新密码是否一致
  54. if (params.password === params.isnewPwd) {
  55. wx.request({
  56. url: app.globalData.publicUrl + '/api/htyd/card/passwd/' + this.data.form.id,
  57. method: "post",
  58. data: params,
  59. success: (res) => {
  60. if (res.data.errcode == '0') {
  61. wx.showToast({
  62. title: '修改密码成功',
  63. icon: 'success',
  64. duration: 2000
  65. })
  66. app.globalData.userInfo = {}
  67. wx.redirectTo({
  68. url: '/pages/login/login'
  69. })
  70. } else {
  71. wx.showToast({
  72. title: '修改失败',
  73. icon: 'error',
  74. duration: 2000
  75. })
  76. }
  77. }
  78. })
  79. } else {
  80. wx.showToast({
  81. title: '新密码与确认密码不一致,请重新输入',
  82. icon: 'none',
  83. duration: 2000
  84. })
  85. }
  86. }
  87. },
  88. onLoad: function (options) {
  89. this.initValidate()//验证规则函数
  90. let data = app.globalData.userInfo;
  91. if (data) {
  92. this.setData({ form: data })
  93. }
  94. },
  95. })