Skip to content

Commit 250bb86

Browse files
quanquan2100vanoneang
authored andcommitted
支持用户修改头像 (#165)
1 parent 3e97002 commit 250bb86

17 files changed

Lines changed: 129 additions & 19 deletions

File tree

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"good-storage": "^1.1.0",
2222
"js-cookie": "^2.2.0",
2323
"lodash": "^4.17.11",
24+
"moment": "^2.24.0",
2425
"screenfull": "^4.2.0",
2526
"vue": "^2.6.8",
2627
"vue-awesome-swiper": "^3.1.3",

src/components/layout/User.vue

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<el-dropdown trigger="click">
44
<span class="el-dropdown-link">
55
<div class="nav-avatar">
6-
<img src="../../assets/img/user/user.jpg" alt="头像">
6+
<img :src="user.avatar || '../../assets/img/user/user.jpg'" alt="头像">
77
</div>
88
</span>
99
<el-dropdown-menu slot="dropdown" class="user-box">
1010
<div class="user-info">
1111
<div class="avatar" title="点击修改头像">
12-
<img src="../../assets/img/user/user.jpg" alt="头像">
12+
<img :src="user.avatar || '../../assets/img/user/user.jpg'" alt="头像">
1313
<label class="mask">
1414
<i class="iconfont icon-icon-test" style="font-size: 20px;"></i>
1515
<input
@@ -110,7 +110,6 @@ import User from '@/lin/models/user'
110110
import Vue from 'vue'
111111
import Croppa from 'vue-croppa'
112112
import 'vue-croppa/dist/vue-croppa.css'
113-
import Config from '@/config'
114113
115114
Vue.use(Croppa)
116115
@@ -206,7 +205,7 @@ export default {
206205
this.init()
207206
},
208207
methods: {
209-
...mapActions(['loginOut']),
208+
...mapActions(['loginOut', 'setUserAndState']),
210209
fileChange(evt) {
211210
if (evt.target.files.length !== 1) {
212211
return
@@ -253,20 +252,52 @@ export default {
253252
}
254253
},
255254
async handleCrop() {
256-
// console.log(this.$refs.croppa)
255+
// 获取裁剪数据
257256
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+
})
258261
259-
// debugger
260-
// const res = await http({
261-
const res = await this.$axios({
262+
return this.$axios({
262263
method: 'post',
263-
url: `${Config.baseUrl}cms/file/`,
264+
url: '/cms/file/',
264265
data: {
265-
file: blob,
266+
file,
266267
},
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+
})
267300
})
268-
console.log(res)
269-
// debugger
270301
},
271302
init() {
272303
const { user } = this.$store.state

src/lin/directives/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Lin-directives
2+
3+
文件夹内容描述
4+
5+
更多内容请查看 [文档](#)

src/lin/filter/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Lin-filter
2+
3+
文件夹内容描述
4+
5+
更多内容请查看 [文档](#)

src/lin/mixin/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Lin-mixin
2+
3+
文件夹内容描述
4+
5+
更多内容请查看 [文档](#)

src/lin/models/user.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ export default class User {
2828
// 是否为超级管理员
2929
auths = [] // 拥有的权限
3030

31-
constructor(active, email, groupId, nickname, _super, auths) {
31+
constructor(active, email, groupId, nickname, _super, avatar, auths) {
3232
this.isActive = active === ACTIVE_VALUE
3333
this.email = email
3434
this.groupId = groupId
3535
this.nickname = nickname
36+
this.avatar = avatar
3637
this.isSuper = _super === SUPER_VALUE
3738
this.auths = auths || []
3839
}
@@ -64,15 +65,15 @@ export default class User {
6465
*/
6566
static async getInformation() {
6667
const info = await get('cms/user/information')
67-
return new User(info.active, info.email, info.group_id, info.nickname, info.admin)
68+
return new User(info.active, info.email, info.group_id, info.nickname, info.admin, info.avatar)
6869
}
6970

7071
/**
7172
* 获取当前用户信息和所拥有的权限
7273
*/
7374
static async getAuths() {
7475
const info = await get('cms/user/auths')
75-
return new User(info.active, info.email, info.group_id, info.nickname, info.admin, info.auths)
76+
return new User(info.active, info.email, info.group_id, info.nickname, info.admin, info.avatar, info.auths)
7677
}
7778

7879
/**

src/lin/plugins/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Lin-plugins
2+
3+
文件夹内容描述
4+
5+
更多内容请查看 [文档](#)

src/lin/utils/date.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import moment from 'moment'
2+
3+
// 设置语言为中文
4+
moment.locale('zh-cn')
5+
16
/**
27
* @param {number} hours
38
*/

src/lin/utils/exception.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// 已废弃
12
import Vue from 'vue'
23
import User from '../models/user'
34
import { refreshRequest } from './http'

0 commit comments

Comments
 (0)