edit.nvue 2.0 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 52 53 54 55 56 57 58 59 60 61 62 63
		},
		methods: {
			...mapMutations({
				login:'user/login'
			}),
			// 选择图片
			chooseImg(){
				uni.chooseImage({
					count:1,
					success:(res)=> {
						
						uni.$on('uploadAvatarAfter', ({url})=>{
							console.log(url);
							this.uploadUserInfo({avatar:url});
						})
						uni.navigateTo({
							url:'./uploadCutImageToUnicloud?path=' + res.tempFilePaths[0],
						});
					}
				})
			},
			/**
			 * @param {Object} data 更新用户信息
			 * key 需要更新的用户字段
			 * value 更新后的用户字段值
			 */
			uploadUserInfo(data){
L
123  
linju 已提交
64
				userTable.doc(this.userInfo._id)
芊里 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
				.update(data)
				.then(res=>{
					console.log(res);
				})
				.catch(err=>{
					console.log(err);
				})
			}
		}
	}
</script>

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