马宇豪
2024-11-05 1f26c30a8d1c5e3d9ab262b382284c8ea96f29b2
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
150
151
152
153
154
155
<template>
    <el-dialog title="修改" v-model="isShowUserDialog" width="400px">
      <el-form :model="userForm" size="default" ref="userRef" :rules="userFormRules" label-width="100px">
        <el-row :gutter="35">
          <el-col class="mb20">
            <el-form-item label="原密码" prop="oldPwd">
              <el-input v-model.trim="userForm.oldPwd" placeholder="原密码" type="password" show-password>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col class="mb20">
            <el-form-item label="新密码" prop="newPwd">
              <el-input v-model.trim="userForm.newPwd" placeholder="新密码" type="password" show-password>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col class="mb20">
            <el-form-item label="确认新密码" prop="reNewPwd">
              <el-input v-model.trim="userForm.reNewPwd" placeholder="再次确认新密码" type="password" show-password>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
              <span class="dialog-footer">
                  <el-button @click="isShowUserDialog = !isShowUserDialog" size="default">取 消</el-button>
                  <el-button type="primary" v-throttle @click="onSubmit" size="default">确 定</el-button>
              </span>
      </template>
    </el-dialog>
</template>
 
<script lang="ts">
import { reactive, toRefs, onMounted, defineComponent, ref } from 'vue';
import { ElMessage } from 'element-plus';
import { userApi } from '/@/api/systemManage/user'
import {verifyPwd} from "/@/utils/toolsValidate"
import {useUserInfo} from "/@/stores/userInfo";
import {storeToRefs} from "pinia";
import {useLoginApi} from "/@/api/login";
import {Session} from "/@/utils/storage";
const userInfo = useUserInfo();
const { userInfos } = storeToRefs(userInfo);
interface UserState {
  isShowUserDialog: boolean;
  userForm: {
    uid: null | number | string,
    oldPwd: string,
    newPwd: string,
    reNewPwd: string
  };
  userFormRules:{},
}
 
export default defineComponent({
  name: 'pwdDialog',
  setup(props, context) {
    const userRef = ref()
    const validatePwd = (rule: any, value: any, callback: any)=>{
      if(value === ''){
        callback(new Error('请输入密码'))
      }else{
        if(!verifyPwd(value)){
          callback(new Error('密码须包含大小写字母、数字、特殊字符,长度不少于8位'))
        }else{
          callback()
        }
      }
    }
 
    const validatePass2 = (rule: any, value: any, callback: any) => {
      if (value === '') {
        callback(new Error('该内容不能为空'))
      } else if (value !== state.userForm.newPwd) {
        callback(new Error("两次输入的密码不一致!"))
      } else {
        callback()
      }
    }
 
    const state = reactive<UserState>({
      isShowUserDialog: false,
      userForm: {
        uid: null,
        oldPwd: '',
        newPwd: '',
        reNewPwd: ''
      },
      userFormRules:{
        oldPwd: [{ required: true, message: '该内容不能为空', trigger: 'blur' }],
        newPwd: [{ required: true, validator: validatePwd, trigger: 'blur' }],
        reNewPwd: [{ required: true, validator: validatePass2, trigger: 'blur' }],
      },
    })
 
 
    // 打开弹窗
    const openDialog = () => {
      state.userForm = {
        uid: null,
        oldPwd: '',
        newPwd: '',
        reNewPwd: ''
      }
      state.isShowUserDialog = true
      state.userForm.uid = Number(userInfos.value.uid)
    }
 
    // 新增修改
    const onSubmit = async () => {
      userRef.value.validate(async (valid:Boolean) => {
        if(valid){
          const {reNewPwd,...data} = state.userForm
          const res = await userApi().modPwd(data)
          if(res.data.code == 100){
            ElMessage({
              type:'success',
              message: '密码修改成功,需重新登录'
            })
            useLoginApi()
                .signOut()
                .then(() => {
                  Session.clear();
                  window.location.href = '/';
                });
          }else{
            ElMessage({
              type:'warning',
              message: res.data.msg
            })
          }
        }else{
          ElMessage({
            type:'warning',
            message:'请完善基本信息'
          })
        }
      })
 
    }
 
    // 页面加载时
    onMounted(() => {
 
    });
    return {
      userRef,
      openDialog,
      onSubmit,
      ...toRefs(state)
    };
  }
});
</script>