
/*修改密码*/
<template>
  <div class="modify">
    <div class="container">
      <h3 class="title">修改密码</h3>
      <el-form
        ref="modifyForm"
        class="modify-form"
        :model="modifyForm"
        :rules="modifyRules"
        label-position="left"
      >
        <el-form-item prop="opwd">
          <el-input
            ref="opwd"
            v-model="modifyForm.opwd"
            :type="modifyForm.opwdType"
            size="small"
            name="opwd"
            minlength="6"
            maxlength="8"
            placeholder="请输入原密码"
          >
            <span slot="prefix" class="solt">原密码</span>
            <svg-icon
              slot="suffix"
              :icon-class="
                modifyForm.opwdType === 'password' ? 'eye' : 'eye-open'
              "
              @click="showOpwd"
            />
          </el-input>
        </el-form-item>

        <el-form-item prop="pwd">
          <el-input
            ref="pwd"
            v-model="modifyForm.pwd"
            :type="modifyForm.pwdType"
            size="small"
            name="pwd"
            placeholder="请输入密码"
          >
            <span slot="prefix" class="solt">密码</span>
            <svg-icon
              slot="suffix"
              :icon-class="
                modifyForm.pwdType === 'password' ? 'eye' : 'eye-open'
              "
              @click="showPwd"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="spwd">
          <el-input
            ref="spwd"
            v-model="modifyForm.spwd"
            :type="modifyForm.spwdType"
            size="small"
            name="spwd"
            placeholder="请确认密码"
          >
            <span slot="prefix" class="solt">确认密码</span>
            <svg-icon
              slot="suffix"
              :icon-class="
                modifyForm.spwdType === 'password' ? 'eye' : 'eye-open'
              "
              @click="showSpwd"
            />
          </el-input>
        </el-form-item>
        <el-button
          type="danger"
          size="small"
          class="next-btn"
          @click.native.prevent="Next"
          >确定</el-button
        >
      </el-form>
      <li class="toForg">
         <span @click="gotoMenu">忘记密码</span>
      </li>
    </div>
    <Forget :forgetShow="forgetShow" @putForget="getForget" />
  </div>
</template>

<script>
import { resetRouter } from '@/router'
import { validPwd } from '@/utils/validate'
import { request, enPassword } from '@/utils/request'
import Forget from '@/components/Forget'
export default {
  name: 'Modify',
  components:{
    Forget
  },
  data() {
    const validatePwd = (rule, value, callback) => {
      if (value) {
        if (!validPwd(value)) {
          callback(new Error('6~18位，只能包含字母、数字和下划线'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入密码'))
      }
    }
    const validateSpwd = (rule, value, callback) => {
      if (value) {
        if (value !== this.modifyForm.pwd) {
          callback(new Error('两次密码不一致'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请再次输入密码'))
      }
    }
    return {
      forgetShow: false,
      modifyForm: {
        opwd: '',
        pwd: '',
        spwd: '',
        opwdType: 'password',
        pwdType: 'password',
        spwdType: 'password',
      },
      modifyRules: {
        opwd: [{ required: true, message: '请输入原密码', trigger: 'blur' }],
        pwd: [{ required: true, trigger: 'blur', validator: validatePwd }],
        spwd: [{ required: true, trigger: 'blur', validator: validateSpwd }],
      },
    }
  },
  computed: {},
  methods: {
    // 切换显示原密码
    showOpwd() {
      this.modifyForm.opwdType =
        this.modifyForm.opwdType === 'password' ? '' : 'password'
      this.$nextTick(() => {
        this.$refs.opwd.focus()
      })
    },
    // 切换显示密码
    showPwd() {
      this.modifyForm.pwdType =
        this.modifyForm.pwdType === 'password' ? '' : 'password'
      this.$nextTick(() => {
        this.$refs.pwd.focus()
      })
    },
    // 切换显示二次密码
    showSpwd() {
      this.modifyForm.spwdType =
        this.modifyForm.spwdType === 'password' ? '' : 'password'
      this.$nextTick(() => {
        this.$refs.spwd.focus()
      })
    },
    gotoMenu(){
      this.forgetShow = true
    },
    getForget(data) {
        this.forgetShow = false
    },
    // 确定
    Next() {
      this.$refs.modifyForm.validate((valid) => {
        if (valid) {
          const params = {
            transCode: 'B00003',
            oldPassword: enPassword(this.modifyForm.opwd),
            newPassword: enPassword(this.modifyForm.pwd),
            authCode: '',
            pwdType: '',
            telNo: '',
          }
          request(params)
            .then((res) => {
              this.$alert('密码修改成功,请重新登录！', '提示', {
                confirmButtonText: '确定',
                center: true,
                callback: (action) => {
                  this.$store.dispatch('user/changeState', {
                    key: 'token',
                    value: '',
                  })
                  this.$store.dispatch('user/changeState', {
                    key: 'name',
                    value: '',
                  })
                  this.$store.dispatch('user/changeState', {
                    key: 'userId',
                    value: '',
                  })
                  this.$store.dispatch('user/changeState', {
                    key: 'loginStatus',
                    value: false,
                  })
                  resetRouter() // 重置路由
                  setTimeout(() => {
                    this.$router.replace(`/indexLayout/home`)
                  }, 1000)
                },
              })
            })
            .catch((err) => {})
        } else {
          return false
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';
@import '@/styles/mixin.scss';
.modify {
  padding: 50px 70px;
  @include w_h(100%, calc(100% - 80px));
  .container {
    @include w_h(430px);
    min-height: 405px;
    margin: 0 auto;
    padding: 30px;
    background: $color_f;
    box-shadow: 0 0 7px 7px rgba(0, 0, 0, 0.04);
    .title {
      margin: 30px auto;
      font-size: 20px;
      text-align: center;
      color: $color_3;
    }
    .modify-form {
      padding: 0 50px;
      @include w_h(100%);
      background: $color_f;
      .solt {
        padding: 0 6px;
        font-size: 12px;
        color: $color_6;
      }
      .next-btn {
        margin: 10px auto;
        @include w_h();
      }
    }
  }
}
.toForg{
  text-align: right;
  &:hover{
    color: #78a4db;
    cursor: default;
  }
}
</style>

<style lang="scss">
@import '@/styles/variables.scss';
@supports (-webkit-mask: none) and (not (cater-color: $color_9)) {
  .modify-form .el-input input {
    font-size: 12px;
    color: $color_9;
  }
}
.modify-form {
  .el-input {
    &__inner:focus {
      border-color: $color_9;
    }
    &--prefix .el-input__inner {
      color: $color_6;
      padding-left: 65px;
    }
  }
}
</style>>

