edit.nvue 1.7 KB
Newer Older
芊里 已提交
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
<template>
	<view class="content">
		<uni-list>
			<uni-list-item>
				<template v-slot:body>
					<view class="avatar-box">
						<image class="avatar-img" :src="userInfo.avatar || '/static/logo.png'" @click="chooseImg" mode="aspectFill"></image>
					</view>
				</template>
			</uni-list-item>
		</uni-list>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	
	const db = uniCloud.database();
	export default {
		data() {
			return {
				
			}
		},
		computed: {
			...mapGetters({
				userInfo: 'user/info'
			})
		},
		onLoad() {
			console.log(this.userInfo);
		},
		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){
				db.collection('uni-id-users').where({
					_id: this.userInfo._id
				})
				.update(data)
				.then(res=>{
					console.log(res);
				})
				.catch(err=>{
					console.log(err);
				})
			}
		}
	}
</script>

<style>
.content{
		width: 750rpx;
		display: flex;
		flex-direction: column;
		/* align-items: center; */
		flex: 1;
	}
	.avatar-box{
		width: 700rpx;
		height: 200rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.avatar-img{
		width: 150rpx;
		height: 150rpx;
		border-radius: 75rpx;
		
		border-width: 1rpx;
		border-color: #999999;
	}
</style>