本文采用 uni-app、u-view

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
<template>
<view>
<u-modal :show="show" @close="$emit('close')" @cancel="$emit('close')" @confirm="onConfirm" title="头像与昵称" :confirm-text="$t('confirm')" :cancel-text="$t('cancel')" wConfirmButton showCancelButton="!force">
<view class="slot-content flex flex-column" style="gap:32px">
<view class="avatar" style="position: relative;">
<button plain open-type="chooseAvatar" @chooseavatar="chooseAvatar">
<u-avatar :src="userprofile.avatar" :size="64" style="height: 100%;" mode="widthFix"></u-avatar>
</button>
<view class="flex" style="position: absolute;bottom:-16px;width: 100%;;justify-content: center">
<u-tag :text="$t('change')" type="warning" shape="circle" size="mini"></u-tag>
</view>
</view>
<input v-model="userprofile.nickname" type="nickname" @change="setNickname"></input>
</view>
</u-modal>
</view>
</template>

<script>
import api from '@/api';

export default {
props: {
show: {
type: Boolean,
default: false
},
force: {
type: Boolean,
default: false
},
newUser: {
type: Boolean,
default: true
}
},
data() {
return {
userprofile: {
nickname: null,
avatar: null,
}
};
},
onLoad() {},
watch: {
show(v) {
if (v) {
this.getData()
}
}
},
methods: {
async onConfirm() {
if (this.force && !this.userprofile.avatar) {
uni.showToast({
title: '请设置一个头像',
icon: 'none'
});
return;
}
if (this.force && !this.userprofile.nickname) {
uni.showToast({
title: '请填写昵称',
icon: 'none'
});
return;
}
uni.showLoading({
//title: '正在更新',
mask: true
});
try {
await api.profile.updateNickname(this.userprofile.nickname);
await api.profile.updateAvatar( this.userprofile.avatar);
this.userprofile = await api.profile.getUserProfile();
uni.showToast({
title: this.$t('success'),
icon: 'none'
});
} catch (e) {
uni.showToast({
title: this.$t('fail'),
icon: 'none'
});
}
this.$emit('confirm', {
avatar: this.userprofile.avatar,
nickname: this.userprofile.nickname
})
},
getData() {
api.profile.getUserProfile().then(res => {
this.userprofile = res;
if (this.newUser) {
this.userprofile = {
avatar: null,
nickname: null,
}
}
});
},
setNickname(v) {
console.log(this.userprofile.nickname)
//this.userprofile.nickname = v
},
chooseAvatar(e) {
api.fileManagement.uploadFile(e.detail.avatarUrl, [Your server domain address]).then(res => {
this.userprofile.avatar = res.downloadInfo.downloadUrl
}).catch(err => {
uni.showToast({
title: this.$t('fail'),
icon: 'none'
});
})
},
}
};
</script>

<style lang="less" scoped>
.avatar {
button {
border: none;
padding: 0px;
line-height: 30px;
display: flex;
justify-content: center;
align-items: center;
view {
height: 66px;
}
}
}
</style>