hyb
2025-05-14 87453ffd761425b9f363a09a0f8fe07d770cb325
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<template>
  <div class="nav-header">
    <div class="logo-and-title">
      <img
        class="side-logo"
        src="~@/assets/images/img.png"
        alt="sidebar-logo"
      />
      <span style="color: #607D8B; font-size: 26px;">{{
        $store.state.projectName
      }}</span>
    </div>
    <div class="right">
      <div style="float: right; color: #262626; margin-right: 35px;">
        <el-dropdown trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            <i class="el-icon-user"></i>
            {{ $store.state.name }}
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
 
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="changePassword">
              <i class="el-icon-edit"></i>修改密码
            </el-dropdown-item>
            <el-dropdown-item divided command="logout">
              <i class="el-icon-switch-button"></i>注销登录
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
 
    <!-- 密码修改弹窗 -->
    <el-dialog
      title="修改密码"
      :visible.sync="passwordDialogVisible"
      width="30%"
      :modal="false"
      :lock-scroll="false"
      :close-on-click-modal="false"
    >
      <el-form :model="passwordForm" :rules="passwordRules" ref="passwordForm">
        <el-form-item prop="old_password">
          <el-input
            v-model="passwordForm.old_password"
            type="password"
            placeholder="请输入旧密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="new_password">
          <el-input
            v-model="passwordForm.new_password"
            type="password"
            placeholder="请输入新密码(至少6位)"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="confirm_password">
          <el-input
            v-model="passwordForm.confirm_password"
            type="password"
            placeholder="请确认新密码"
            show-password
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="passwordDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitPasswordChange">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 
<script>
import { changePassword } from '@/api/user'
 
export default {
  data() {
    const validateConfirm = (rule, value, callback) => {
      if (value !== this.passwordForm.new_password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
 
    return {
      passwordDialogVisible: false,
      passwordForm: {
        old_password: '',
        new_password: '',
        confirm_password: ''
      },
      passwordRules: {
        old_password: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        new_password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少6位', trigger: 'blur' }
        ],
        confirm_password: [
          { required: true, message: '请确认新密码', trigger: 'blur' },
          { validator: validateConfirm, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleCommand(command) {
      if (command === 'changePassword') {
        // 重置表单数据和验证状态
        this.passwordForm = {
          old_password: '',
          new_password: '',
          confirm_password: ''
        }
        this.$nextTick(() => {
          if (this.$refs.passwordForm) {
            this.$refs.passwordForm.clearValidate()
          }
        })
        this.passwordDialogVisible = true
      } else if (command === 'logout') {
        this.$store.commit("isLogin", null);
        this.setLocalValue("token", null);
        this.setLocalValue("is_superuser", false);
        this.setLocalValue("show_hosts", false);
        this.$store.commit("setProjectName", "");
        this.$router.push({ name: "Login" });
      }
    },
    submitPasswordChange() {
      this.$refs.passwordForm.validate(valid => {
        if (valid) {
          this.$loading({ text: '正在修改密码...' })
          changePassword(this.passwordForm)
            .then(res => {
              this.$loading().close()
              if (res.code === "0000") {
                this.$message.success('密码修改成功,请重新登录')
                this.passwordDialogVisible = false
                // 重置表单数据
                this.passwordForm = {
                  old_password: '',
                  new_password: '',
                  confirm_password: ''
                }
                localStorage.removeItem('token')
                localStorage.removeItem('user')
                // 添加100ms延迟确保状态清除完成
                setTimeout(() => {
                  this.$router.push({ path: '/lunarlink/login' })
                }, 100)
              } else {
                this.$message.error(res.msg || '密码修改失败')
              }
            })
            .catch(error => {
              this.$loading().close()
              const errorMsg = (error.response && error.response.data && error.response.data.msg) || '请求失败,请稍后重试'
              this.$message.error(errorMsg)
            })
        }
      })
    }
  }
}
</script>
 
<style scoped>
.right {
    position: fixed;
    left: 300px;
    right: 0;
    top: 0;
}
 
.right div a:hover {
    color: darkcyan;
}
 
.nav-header {
    position: fixed;
    left: 0;
    right: 0;
    z-index: 100;
    background: #ffffff;
    margin: 0 auto;
    font-size: 14px;
    width: 100%;
    height: 49px;
    line-height: 49px;
}
 
.side-logo {
    width: 100px;
    height: 36px;
    padding-left: 10px;
    vertical-align: top;
}
 
.logo-and-title {
    display: flex;
    align-items: center;
}
 
.a-text {
    margin-right: 10px;
    font-size: 15px;
    color: #d9d9d9;
}
/* 确保下拉菜单样式 */
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
  padding: 0 15px;
}
</style>