fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" title="修改密码" :close-on-click-modal="false"
               :modal-append-to-body="false" v-if="dialogVisible" width="40%">
      <el-form size="mini" :model="form" label-width="120px" ref="form" :rules="formRules">
        <el-form-item label="旧密码:" prop="oldPassword">
          <el-input v-model="form.oldPassword" type="password" placeholder="请输入旧密码" clearable>
          </el-input>
        </el-form-item>
        <el-form-item label="新密码:" prop="newPassword">
          <el-input v-model="form.newPassword" type="password" placeholder="请输入新密码" clearable>
          </el-input>
        </el-form-item>
        <el-form-item label="确认密码:" prop="checkPassword">
          <el-input v-model="form.checkPassword" type="password" placeholder="请输入确认密码" clearable>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="submit" type="primary" :loading="loading">保存</el-button>
        <el-button size="mini" @click="dialogVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { forceModify } from '../../../api/oauth/index'
import { mapState, mapMutations } from 'vuex'
// import config from '@/app.config.js'
import { validatePwd } from '@/utils/validator'
const sm2 = require('sm-crypto').sm2
 
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    show(newValue, oldValue) {
      this.dialogVisible = newValue
    },
    dialogVisible(newValue, oldValue) {
      this.$emit('update:show', newValue)
      if (!newValue) {
        this.initData()
      }
    }
  },
  data() {
    return {
      form: {
        oldPassword: null,
        newPassword: null,
        checkPassword: null
      },
      loading: false,
      formRules: {
        oldPassword: [{ required: true, message: '请输入旧密码', trigger: 'change' }],
        newPassword: [{ required: true, validator: validatePwd, trigger: 'change' }],
        checkPassword: [{
          required: true,
          validator: (rule, value, callback) => {
            if (!value) {
              callback(new Error('请再次输入密码'))
            } else if (value !== this.form.newPassword) {
              callback(new Error('两次输入密码不一致'))
            } else {
              callback()
            }
          }
        }]
      },
      dialogVisible: false
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  methods: {
    ...mapMutations(['logout']),
    async _forceModify(params) {
      if (this.loading) {
        return
      }
      this.loading = true
      try {
        const res = await forceModify(params)
        console.log(res)
        this.logout()
        this.$router.replace('/')
        this.loading = false
      } catch (error) {
        this.loading = false
      }
    },
    doSm3AndSm2Encrypt(sourceStr) {
      return '04' + sm2.doEncrypt(sourceStr, process.env.VUE_APP_PUBLICKEY, 1)
      // // 密码加密
      // /* eslint-disable */
      //       const sm2Utils = new Sm2Utils();
      //       const public_key = config.public_key
      //       const sm3_random_plain =
      //           Sm3Utils.encryptFromText(sourceStr) +
      //           '|' +
      //           sm2Utils.randomWord(8) +
      //           '|' +
      //           sourceStr;
      //       const sm3_sm2_plain = sm2Utils.encryptFromText(
      //           public_key,
      //           sm3_random_plain,
      //       );
      //       return '{crypto}' + sm3_sm2_plain;
      //       /* eslint-enable */
    },
    /**
       * 提交密码
       */
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          console.log(this.form.oldPassword, this.form.newPassword)
          /* eslint-disable */
          const params = {
            "appId": process.env.VUE_APP_ID, // 应用id
            "oldPsCode": this.doSm3AndSm2Encrypt(this.form.oldPassword),// 原密码
            "psCode": this.doSm3AndSm2Encrypt(this.form.newPassword), // 密码
            "confirmPsCode": this.doSm3AndSm2Encrypt(this.form.checkPassword) // 确认密码
          }
          /* eslint-enable */
          this._forceModify(params)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    /**
     * 初始化
     */
    initData() {
      for (const key in this.form) {
        this.form[key] = null
      }
    }
  }
}
</script>