changePassword.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div class="changePassword">
  3. <van-form validate-first>
  4. <van-field
  5. v-model="oldPassword"
  6. type="password"
  7. name="password"
  8. label="原密码"
  9. placeholder="请输入原密码"
  10. />
  11. <van-field
  12. @input="validPwd"
  13. v-model="password"
  14. type="password"
  15. name="password"
  16. label="密码"
  17. placeholder="请输入密码"
  18. />
  19. <van-field
  20. v-model="requirePassword"
  21. type="password"
  22. name="requirePassword"
  23. label="确认密码"
  24. placeholder="请再次输入密码"
  25. />
  26. <div class="tips">
  27. <div><van-icon name="success" class="v-middle" color="#14b914" v-if="s1==1"/><van-icon color="#ff0000" class="v-middle" v-else-if="s1==2" name="cross" />8-20个字符</div>
  28. <div><van-icon name="success" class="v-middle" color="#14b914" v-if="s2==1"/><van-icon color="#ff0000" class="v-middle" v-else-if="s2==2" name="cross" />只能设置大小写字母、数字及标点符号(不允许输入#:")。</div>
  29. <div><van-icon name="success" class="v-middle" color="#14b914" v-if="s3==1"/><van-icon color="#ff0000" class="v-middle" v-else-if="s3==2" name="cross" />必须包含大小写字母、数字及标点符号其中三种。</div>
  30. </div>
  31. <div style="margin: 40px;">
  32. <van-button round block type="info" @click="onSubmit">
  33. 提交
  34. </van-button>
  35. </div>
  36. </van-form>
  37. </div>
  38. </template>
  39. <script>
  40. import { changePwd } from "@/api/login";
  41. export default {
  42. data(){
  43. this.passwordRules = [
  44. { required: true, message: '请输入密码' },
  45. ];
  46. this.requirePasswordRules = [
  47. { required: true, message: '请再次输入密码' },
  48. { validator: this.setPassword, message: '两次输入密码不一致' },
  49. { validator:this.setTest, message: '密码规则错误'}
  50. ];
  51. return {
  52. oldPassword: '',
  53. password: '',
  54. requirePassword:'',
  55. testReq: /((?=[\x21-\x7e]+)[^A-Za-z0-9])/,
  56. testNumber: /[0-9]/,
  57. testLetter: /[A-Za-z]/,
  58. s1: -1,
  59. s2: -1,
  60. s3: -1,
  61. }
  62. },
  63. methods:{
  64. async onSubmit() {
  65. if(!this.oldPassword.length){
  66. this.$toast("原密码不能为空");
  67. return false
  68. }
  69. if(!this.password.length || !this.requirePassword.length){
  70. this.$toast("密码不能为空");
  71. return false
  72. }
  73. if(!this.setPassword()){
  74. this.$toast("两次输入密码不一致");
  75. return false
  76. }
  77. if(this.s1!=1 || this.s2!=1 || this.s3!=1 ){
  78. this.$toast("密码设置不符合要求");
  79. return false
  80. }
  81. var params = {
  82. id: this.user.id,
  83. pw: this.password,
  84. orgPw: this.oldPassword
  85. }
  86. changePwd(params)
  87. .then(res=>{
  88. if(res.status == 200 && res.obj){
  89. if(res.obj.response == "fail"){
  90. this.$toast(res.obj.msg);
  91. return
  92. }
  93. this.$toast('修改成功');
  94. this.$store
  95. .dispatch('LogOut')
  96. .then(res=>{
  97. setTimeout(()=>{
  98. this.$router.push('/login')
  99. },800)
  100. })
  101. } else {
  102. this.$toast(res.message || '修改失败');
  103. }
  104. })
  105. .catch(err=>{
  106. console.error(err)
  107. })
  108. },
  109. setPassword() {
  110. return this.password === this.requirePassword
  111. },
  112. validPwd(){
  113. if(this.password.length>=8&&this.password.length<=20){
  114. this.s1 = 1
  115. } else {
  116. this.s1 = 2
  117. }
  118. if(/^[A-Za-z0-9\x21-\x7e]{1,}$/.test(this.password) && /^((?!#|:|"|").)*$/.test(this.password)){
  119. this.s2 = 1
  120. } else {
  121. this.s2 = 2
  122. }
  123. if( (/[A-Z]/.test(this.password) && /[a-z]/.test(this.password) && (this.testReq.test(this.password) || this.testNumber.test(this.password)))
  124. ||
  125. (this.testReq.test(this.password) && this.testNumber.test(this.password) && this.testLetter.test(this.password))
  126. ){
  127. this.s3 = 1
  128. } else {
  129. this.s3 = 2
  130. }
  131. },
  132. }
  133. }
  134. </script>
  135. <style lang="scss" scoped>
  136. .changePassword{
  137. .van-button--info{
  138. background-color:#17b3ec;
  139. border-color: #17b3ec;
  140. }
  141. .tip{
  142. text-align: left;
  143. font-size:13px;
  144. color:red;
  145. margin-top:10px;
  146. letter-spacing: 1px;
  147. padding:0 15px;
  148. }
  149. .tips{
  150. margin-top:10px;
  151. padding:0 15px;
  152. font-size: 14px;
  153. color: #333;
  154. line-height: 20px;
  155. }
  156. .v-middle{
  157. vertical-align: middle;
  158. }
  159. }
  160. </style>