tj
2025-06-05 bba272999cc546f65781bf3d20245a3f819af67f
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<template>
  <div class="user">
    <el-dropdown>
      <span class="el-dropdown-link">
        <div class="nav-avatar"><img :src="user.avatar || defaultAvatar" alt="头像" /></div>
      </span>
      <template v-slot:dropdown>
        <el-dropdown-menu class="user-box">
          <div class="user-info">
            <div class="avatar" title="点击修改头像">
              <img :src="user.avatar || defaultAvatar" alt="头像" />
              <label class="mask">
                <i class="iconfont icon-icon-test" style="font-size: 20px;"></i>
                <input ref="avatarInput" type="file" accept="image/*" @change="fileChange" />
              </label>
            </div>
            <div class="text">
              <div class="username" @click="changeNickname" v-if="!nicknameChanged">{{ nickname }}</div>
              <el-input
                placeholder="请输入内容"
                size="small"
                v-else
                v-model="nickname"
                ref="input"
                @blur="blur"
              ></el-input>
              <div class="desc" v-if="!nicknameChanged">{{ groupName }}</div>
            </div>
            <img src="../../assets/image/user/corner.png" class="corner" />
          </div>
          <ul class="dropdown-box">
            <li class="password" @click="goToCenter">
              <i class="iconfont icon-weibaoxitongshangchuanlogo-"></i> <span>个人中心</span>
            </li>
            <li class="account" @click="outLogin"><i class="iconfont icon-tuichu"></i> <span>退出账户</span></li>
          </ul>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
    <!-- 修改头像 -->
    <avatar :originalImage="cropImg" :cropVisible="cropVisible" @switchCropVisible="switchCropVisible"></avatar>
  </div>
</template>
 
<script>
import User from 'lin/model/user'
import axios from 'lin/plugin/axios'
import { mapActions, mapGetters } from 'vuex'
import defaultAvatar from '@/assets/image/user/user.png'
import Avatar from './avatar.vue'
 
export default {
  name: 'User',
  components: { Avatar },
  data() {
    return {
      cropImg: '',
      defaultAvatar,
      username: null,
      nickname: null,
      groupName: null,
      cropVisible: false,
      nicknameChanged: false,
      dialogFormVisible: false,
    }
  },
  computed: {
    ...mapGetters(['user']),
  },
  watch: {
    cropVisible(val) {
      if (!val) {
        this.cropImg = ''
      }
    },
  },
  created() {
    const { user } = this.$store.state
    this.nickname = user?.nickname ? user.nickname : '佚名'
    this.username = user?.username ? user.username : '未登录'
  },
  methods: {
    ...mapActions(['loginOut', 'setUserAndState']),
    fileChange(event) {
      if (event.target.files.length !== 1) {
        return
      }
 
      const imgFile = event.target.files[0]
      // 验证文件大小是否符合要求, 不大于 5M
      if (imgFile.size > 1024 * 1024 * 5) {
        this.$message.error('文件过大超过5M')
        // 清空输入框
        this.clearFileInput(this.$refs.avatarInput)
        return
      }
 
      // 验证图像是否符合要求
      const imgSrc = window.URL.createObjectURL(imgFile)
      const image = new Image()
      image.src = imgSrc
      image.onload = () => {
        const w = image.width
        const h = image.height
        if (w < 50) {
          this.$message.error('图像宽度过小, 请选择大于50px的图像')
          // 清空输入框
          this.clearFileInput(this.$refs.avatarInput)
          return
        }
        if (h < 50) {
          this.$message.error('图像高度过小, 请选择大于50px的图像')
          // 清空输入框
          this.clearFileInput(this.$refs.avatarInput)
          return
        }
        // 验证通过, 打开裁剪框
        this.cropImg = imgSrc
        this.cropVisible = true
      }
      image.onerror = () => {
        this.$message.error('获取本地图片出现错误, 请重试')
        // 清空输入框
        this.clearFileInput(this.$refs.avatarInput)
      }
    },
    switchCropVisible(flag) {
      this.cropVisible = flag
    },
    changeNickname() {
      this.nicknameChanged = true
      setTimeout(() => {
        this.$refs.input.focus()
      }, 200)
    },
    async blur() {
      if (this.nickname) {
        const { user } = this.$store.state
        if (this.nickname !== user.nickname && this.nickname !== '佚名') {
          axios({
            method: 'put',
            url: '/cms/user',
            data: {
              nickname: this.nickname,
            },
            showBackend: true,
          })
            .then(res => {
              if (res.code < window.MAX_SUCCESS_CODE) {
                this.$message({
                  type: 'success',
                  message: '更新昵称成功',
                })
                // 触发重新获取用户信息
                return User.getInformation()
              }
            })
            .then(res => {
              this.setUserAndState(res)
              this.nickname = res.nickname
            })
        }
      }
      this.nicknameChanged = false
    },
    goToCenter() {
      this.$router.push('/center')
    },
    outLogin() {
      this.loginOut()
      window.location.reload()
    },
    clearFileInput(ele) {
      ele.value = ''
    },
  },
}
</script>
 
<style lang="scss" scoped>
.user {
  height: 40px;
 
  .el-dropdown-link {
    cursor: pointer;
 
    .nav-avatar {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
      margin-right: 10px;
    }
  }
}
 
.user-box {
  width: 326px;
  background-color: none;
  background: transparent;
  margin-bottom: 0;
  padding-bottom: 0;
  border: none;
 
  .user-info {
    background-image: url('../../assets/image/user/user-bg.png');
    background-size: 100% 100%;
    transform: translateY(-10px);
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    display: flex;
    flex-direction: row;
    padding: 35px 20px 25px 30px;
    z-index: 100;
    position: relative;
 
    .corner {
      position: absolute;
      right: 18px;
      top: -9px;
      width: 27px;
      height: 10px;
    }
 
    .avatar {
      width: 80px;
      height: 80px;
      border-radius: 50%;
      cursor: pointer;
      overflow: hidden;
      position: relative;
 
      .mask {
        opacity: 0;
        transition: all 0.2s;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.3);
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        color: white;
 
        input {
          display: none;
        }
      }
 
      &:hover {
        .mask {
          opacity: 1;
        }
      }
    }
 
    .text {
      margin-left: 20px;
      color: #fff;
      display: flex;
      flex-direction: column;
      justify-content: center;
 
      .username {
        margin-bottom: 10px;
        font-size: 16px;
        height: 32px;
        line-height: 32px;
        cursor: pointer;
      }
 
      .desc {
        font-size: 14px;
        color: rgba(222, 226, 230, 1);
      }
    }
 
    .info {
      position: absolute;
      bottom: 10px;
      right: 10px;
      display: flex;
      color: #fff;
      font-size: 14px;
      height: 20px;
      line-height: 20px;
 
      .mid {
        padding: 0 5px;
      }
    }
  }
 
  .dropdown-box {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    padding-left: 35px;
    height: 122px;
    color: #596c8e;
    font-size: 14px;
    background: white;
    margin-top: -10px;
 
    li {
      cursor: pointer;
 
      &:nth-child(1) {
        margin-top: 20px;
      }
 
      &:nth-child(2) {
        margin-bottom: 20px;
      }
 
      i {
        margin-right: 10px;
      }
 
      &:hover {
        color: $theme !important;
 
        i {
          color: $theme !important;
        }
      }
    }
  }
}
 
.popper__arrow {
  display: none !important;
}
 
.avatar-croppa-container {
  display: inline-block;
  border-color: #3862bc;
  border-style: dashed;
  font-size: 0;
  border-width: 2px;
}
</style>