edit.vue 2.2 KB
Newer Older
芊里 已提交
1 2 3 4 5 6
<template>
	<view class="content">
		<uni-list>
			<uni-list-item>
				<template v-slot:body>
					<view class="avatar-box">
芊里 已提交
7
						<image class="avatar-img" :src="(userInfo && userInfo.avatar) || '/static/uni-center/logo.png'" @click="chooseImg" mode="aspectFill"></image>
芊里 已提交
8 9 10
					</view>
				</template>
			</uni-list-item>
芊里 已提交
11
			<uni-list-item title="姓名" :right-text="userInfo && userInfo.name"></uni-list-item>
芊里 已提交
12 13 14 15 16 17 18 19 20 21 22
		</uni-list>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	
	const db = uniCloud.database();
L
123  
linju 已提交
23
	const userTable = db.collection('uni-id-users')
芊里 已提交
24 25 26 27 28 29 30 31 32 33 34 35
	export default {
		data() {
			return {
				
			}
		},
		computed: {
			...mapGetters({
				userInfo: 'user/info'
			})
		},
		onLoad() {
芊里 已提交
36
			if(!this.userInfo)console.error('判断登录状态,如果没有登录则没有userinfo,页面报错');
芊里 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
		},
		methods: {
			...mapMutations({
				login:'user/login'
			}),
			// 选择图片
			chooseImg(){
				uni.chooseImage({
					count:1,
					success:(res)=> {
						
						uni.$on('uploadAvatarAfter', ({url})=>{
							console.log(url);
							this.uploadUserInfo({avatar:url});
						})
芊里 已提交
52
						let options = {
L
2  
linju 已提交
53 54
							width:600,
							height:600
芊里 已提交
55
						}
芊里 已提交
56
						uni.navigateTo({
芊里 已提交
57
							url:'./uploadCutImageToUnicloud?path=' + res.tempFilePaths[0] + `&options=${JSON.stringify(options)}`,
L
2  
linju 已提交
58
							animationType:"fade-in"
芊里 已提交
59 60 61 62 63 64 65 66 67 68
						});
					}
				})
			},
			/**
			 * @param {Object} data 更新用户信息
			 * key 需要更新的用户字段
			 * value 更新后的用户字段值
			 */
			uploadUserInfo(data){
L
2  
linju 已提交
69
				console.log(this.userInfo._id)
L
123  
linju 已提交
70
				userTable.doc(this.userInfo._id)
芊里 已提交
71 72
				.update(data)
				.then(res=>{
L
2  
linju 已提交
73
					console.log(data)
芊里 已提交
74
					this.login(data)
芊里 已提交
75 76 77 78 79 80 81 82 83 84 85 86
				})
				.catch(err=>{
					console.log(err);
				})
			}
		}
	}
</script>

<style>
.content{
		width: 750rpx;
87
		/* #ifndef APP-NVUE */
芊里 已提交
88
		display: flex;
89
		/* #endif */
芊里 已提交
90 91 92 93 94 95 96
		flex-direction: column;
		/* align-items: center; */
		flex: 1;
	}
	.avatar-box{
		width: 700rpx;
		height: 200rpx;
97
		/* #ifndef APP-NVUE */
芊里 已提交
98
		display: flex;
99
		/* #endif */
芊里 已提交
100 101 102 103 104 105 106 107 108 109 110 111
		justify-content: center;
		align-items: center;
	}
	.avatar-img{
		width: 150rpx;
		height: 150rpx;
		border-radius: 75rpx;
		
		border-width: 1rpx;
		border-color: #999999;
	}
</style>