|
3 | 3 | <el-dropdown trigger="click"> |
4 | 4 | <span class="el-dropdown-link"> |
5 | 5 | <div class="nav-avatar"> |
6 | | - <img src="../../assets/img/user/user.jpg" alt="头像"> |
| 6 | + <img :src="user.avatar || '../../assets/img/user/user.jpg'" alt="头像"> |
7 | 7 | </div> |
8 | 8 | </span> |
9 | 9 | <el-dropdown-menu slot="dropdown" class="user-box"> |
10 | 10 | <div class="user-info"> |
11 | 11 | <div class="avatar" title="点击修改头像"> |
12 | | - <img src="../../assets/img/user/user.jpg" alt="头像"> |
| 12 | + <img :src="user.avatar || '../../assets/img/user/user.jpg'" alt="头像"> |
13 | 13 | <label class="mask"> |
14 | 14 | <i class="iconfont icon-icon-test" style="font-size: 20px;"></i> |
15 | 15 | <input |
@@ -110,7 +110,6 @@ import User from '@/lin/models/user' |
110 | 110 | import Vue from 'vue' |
111 | 111 | import Croppa from 'vue-croppa' |
112 | 112 | import 'vue-croppa/dist/vue-croppa.css' |
113 | | -import Config from '@/config' |
114 | 113 |
|
115 | 114 | Vue.use(Croppa) |
116 | 115 |
|
@@ -206,7 +205,7 @@ export default { |
206 | 205 | this.init() |
207 | 206 | }, |
208 | 207 | methods: { |
209 | | - ...mapActions(['loginOut']), |
| 208 | + ...mapActions(['loginOut', 'setUserAndState']), |
210 | 209 | fileChange(evt) { |
211 | 210 | if (evt.target.files.length !== 1) { |
212 | 211 | return |
@@ -253,20 +252,52 @@ export default { |
253 | 252 | } |
254 | 253 | }, |
255 | 254 | async handleCrop() { |
256 | | - // console.log(this.$refs.croppa) |
| 255 | + // 获取裁剪数据 |
257 | 256 | const blob = await this.$refs.croppa.promisedBlob('image/jpeg', 0.8) |
| 257 | + // 构造为文件对象 |
| 258 | + const file = new File([blob], 'avatar.jpg', { |
| 259 | + type: 'image/jpeg', |
| 260 | + }) |
258 | 261 |
|
259 | | - // debugger |
260 | | - // const res = await http({ |
261 | | - const res = await this.$axios({ |
| 262 | + return this.$axios({ |
262 | 263 | method: 'post', |
263 | | - url: `${Config.baseUrl}cms/file/`, |
| 264 | + url: '/cms/file/', |
264 | 265 | data: { |
265 | | - file: blob, |
| 266 | + file, |
266 | 267 | }, |
| 268 | + }).then((res) => { |
| 269 | + if (!Array.isArray(res) || res.length !== 1) { |
| 270 | + this.$message.error('头像上传失败, 请重试') |
| 271 | + return false |
| 272 | + } |
| 273 | + // TODO: 错误码处理 |
| 274 | + // if (res.error_code === 10110) { |
| 275 | + // throw new Error('文件体积过大') |
| 276 | + // } |
| 277 | + return this.$axios({ |
| 278 | + method: 'put', |
| 279 | + url: '/cms/user/avatar', |
| 280 | + data: { |
| 281 | + avatar: res[0].url, |
| 282 | + }, |
| 283 | + }).then((res) => { // eslint-disable-line |
| 284 | + if (res.error_code === 0) { |
| 285 | + this.$message({ |
| 286 | + type: 'success', |
| 287 | + message: '更新头像成功', |
| 288 | + }) |
| 289 | + this.cropVisible = false |
| 290 | + // 触发重新获取用户信息 |
| 291 | + // this.$store.dispatch() |
| 292 | + return User.getInformation() |
| 293 | + } |
| 294 | + return Promise.reject(new Error('更新头像失败')) |
| 295 | + }).then((res) => { // eslint-disable-line |
| 296 | + // 尝试获取当前用户信息 |
| 297 | + const user = res |
| 298 | + this.setUserAndState(user) |
| 299 | + }) |
267 | 300 | }) |
268 | | - console.log(res) |
269 | | - // debugger |
270 | 301 | }, |
271 | 302 | init() { |
272 | 303 | const { user } = this.$store.state |
|
0 commit comments