changePassword.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <view class="pages">
  3. <uni-forms class="form" :value="dataObj" ref="formData" :rules="rules" :border="true"
  4. label-width="100">
  5. <uni-forms-item label="原密码" name="password" :required="true" label-width="100">
  6. <input :value="dataObj.password" type="password" @input="binddata('password',$event.detail.value)"
  7. class="uni-input" placeholder="请输入原密码" />
  8. </uni-forms-item>
  9. <uni-forms-item label="新密码" name="oldPassword" :required="true" label-width="100">
  10. <input :value="dataObj.oldPassword" type="password" @input="binddata('oldPassword',$event.detail.value)"
  11. class="uni-input" placeholder="请输入新密码" />
  12. </uni-forms-item>
  13. <uni-forms-item label="确认密码" name="newPassword" :required="true" label-width="100">
  14. <input :value="dataObj.newPassword" type="password" @input="binddata('newPassword',$event.detail.value)"
  15. class="uni-input" placeholder="请再输一次新密码" />
  16. </uni-forms-item>
  17. </uni-forms>
  18. <button class="but" type="default" :loading="loading" :disabled="loading" @tap="changePass">确认</button>
  19. </view>
  20. </template>
  21. <script>
  22. import {
  23. updatePassword,
  24. exit
  25. } from '@/common/api.js'
  26. export default {
  27. data() {
  28. return {
  29. dataObj: {
  30. password: "",
  31. oldPassword: "",
  32. newPassword: ""
  33. },
  34. loading: false,
  35. rules: {
  36. password: {
  37. rules: [{
  38. required: true,
  39. errorMessage: '请输入原密码',
  40. }, ]
  41. },
  42. oldPassword: {
  43. rules: [{
  44. required: true,
  45. errorMessage: '请输入新密码',
  46. },
  47. {
  48. validateFunction: function(rule, value, data, callback) {
  49. let reg = /(?=.*([a-zA-Z].*))(?=.*[0-9].*)[a-zA-Z0-9-*/+.~!@#$%^&*()]{6,20}$/;
  50. if (!reg.test(value)) {
  51. callback('密码由6-20位数字、字母和字符组成,区分大小写')
  52. }
  53. return true
  54. }
  55. }
  56. ]
  57. },
  58. newPassword: {
  59. rules: [{
  60. required: true,
  61. errorMessage: '请再输一次新密码',
  62. },
  63. {
  64. validateFunction: function(rule, value, data, callback) {
  65. if (data.newPassword != data.oldPassword) {
  66. callback('两次密码输入不一致')
  67. }
  68. return true
  69. }
  70. }
  71. ]
  72. },
  73. }
  74. }
  75. },
  76. onReady() {
  77. this.$refs.formData.setRules(this.rules)
  78. },
  79. methods: {
  80. binddata(v, e) {
  81. this.dataObj[v] = e
  82. },
  83. changePass() {
  84. this.$refs.formData.submit().then(res => {
  85. this.loading = true
  86. console.log(this.dataObj)
  87. console.log(this.$store.state.Token)
  88. updatePassword({
  89. ...this.dataObj,
  90. userId:this.$store.state.Token
  91. }).then(res =>{
  92. console.log(res)
  93. this.loading = false
  94. plus.nativeUI.toast('密码修改成功,请重新登录')
  95. setTimeout(() =>{
  96. exit().then(res => {
  97. this.$store.dispatch('setToken', "")
  98. this.$store.dispatch('setUsers', "{}")
  99. uni.reLaunch({
  100. url: '/pages/native/login'
  101. });
  102. }).catch(e => {
  103. plus.nativeUI.toast('服务退出登录失败,正强制退出...')
  104. setTimeout(() =>{
  105. this.$store.dispatch('setToken', "")
  106. this.$store.dispatch('setUsers', "{}")
  107. uni.reLaunch({
  108. url: '/pages/native/login'
  109. });
  110. },1000)
  111. })
  112. },2000)
  113. }).catch(e =>{
  114. console.log(e)
  115. plus.nativeUI.toast(e.data.msg)
  116. this.loading = false
  117. })
  118. }).catch(err => {
  119. console.log('表单错误信息:', err);
  120. })
  121. }
  122. }
  123. }
  124. </script>
  125. <style scoped lang="scss">
  126. .form {
  127. border-bottom: 1px solid #EEEEEE;
  128. }
  129. .but {
  130. width: 600rpx;
  131. margin-top: 420rpx;
  132. background-color: #0099FF;
  133. color: #fff;
  134. }
  135. </style>