uniapp上传图片

<template>
	<view class="photoPage">
		<navBar setColor="#434343"></navBar>
		<view class="arrow" @click="back()"></view>
		<view class="photo">
			<image class="image" v-if="img_url" :src="img_url"></image>
   			<image v-else src="../../../static/image/avatar-default.png" mode="widthFix"></image>
		</view>
		<!-- <input type="text"> -->
		<view class="button" @click="chooseImage">
			<text>更换头像</text>
		</view>
	</view>
</template>

<script>
	import navBar from '../../../components/navBar.vue'
	import { pathToBase64, base64ToPath } from 'image-tools'
	export default {
		components: {navBar},
		data() {
			return {
				img_url: ''
			};
		},
		onLoad(e) {
			this.img_url = e.avatar
		},
		methods: {
			back(){
				uni.navigateBack({
					delta: 1
				})
			},
			updateUserAvatar(image) {
				this.$store.dispatch('user/updateUserAvatar', {
					avatar: image,
				}).then(result => {
					this.img_url = result.avatar.url
					uni.showToast({
						icon: 'none',
						title: '上传成功!'
					})
				})
			},
			chooseImage() {
				let _this = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'],
					sourceType: ['album'],
					success: function (res) {
						console.log(res)
						pathToBase64(res.tempFilePaths[0]).then(path=>{
							_this.img_url = res.tempFilePaths[0]
							_this.updateUserAvatar(path.replace(/data:image\/jpeg;base64,/,''))
						})
					}
				})

			}
		}
	}
</script>

<style lang="less">
	.photoPage {
		width: 100vw;
		height: 100vh;
		background-image:url(../../../static/image/personal/photoPage-background.png);
		
		.arrow {
			transform: rotateZ(-225deg);
			width: 30rpx;
			height: 30rpx;
			border-color: rgba(255, 255, 255, 1);
			margin: 28rpx 0rpx 0rpx 40rpx;
		}
		
		.photo {
			width: 706rpx;
			height: 706rpx;
			border-radius: 50%;
			margin: 200rpx 22rpx 100rpx 22rpx;
			background-color: #eeeeee;
			.image {
				width: 100%;
				height: 100%;
				border-radius: 50%;
			}
		
		}
		
		.button {
			width: 636rpx;
			height: 96rpx;
			background-color: rgba(255, 255, 255, 1);
			border-radius: 60rpx;
			margin: 80rpx auto;
			text-align: center;
			text {
				color: rgba(86, 105, 255, 1.0);
				font-size: 32rpx;
				font-weight: bold;
				line-height: 96rpx;
			}
		}
	}
	
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值