uniapp uview裁剪组件源码修改(u-avatar-cropper),裁出可自定义固定大小图片

u-avatar-cropper修改后

javascript 复制代码
<template>
	<view class="index">
		<!-- {{userinfo}} -->
		<view class="top">
			<view class="bg">
				<image src="../../static/electronic_card/bg.png"></image>
			</view>
		</view>
		<view class="main">
			<view class="card" ref="card">
				<image src="../../static/card/bg.png" mode=""></image>
				<view class="content">
					<view class="">
						<view class="logo">
							<image src="../../static/card/logo.png" mode=""></image>
						</view>
						<view class="txt">
							姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名:{{userinfo.name}}
							<br>
							班&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;级:{{userinfo.class}}
							<br>
							联系方式:{{userinfo.phone}}
							<br>
							入学年份:{{userinfo.entrance_year}}
							<br>
							No:{{userinfo.code}}
						</view>
					</view>
					<view class="img">
						<view class="" v-if="userinfo.photo" class="photo" @click="chooseAvatar">
							<image :src="userinfo.photo" mode="widthFix" style="width: 100%;"></image>
						</view>
						<view class="applyschool-onlod-phops" @click="chooseAvatar" v-else>
							<view class="phops-img">
								<image src="../../static/applyschool/add_photo.png"></image>
							</view>
							<!-- <text>用于生成入校电子卡</text> -->
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="downbtn" @click="exportCardAsImage">查看与保存卡片</view>
		<u-modal v-model="show" @confirm="confirmChange" confirm-color="#781C27" :content="content"></u-modal>
	</view>
</template>

<script>
	import html2canvas from "html2canvas"

	import utlis from '../../common/utils.js'
	export default {
		mixins: [utlis],
		data() {
			return {
				userinfo: {},
				show: false,
				content: '未认证',
				url: ""
			}
		},
		onLoad() {
			this.getUserInfo()
			uni.$on('uAvatarCropper', path => {
				this.avatar = path;
				// 可以在此上传到服务端
				uni.uploadFile({
					url: this.$configs.$UP_URL,
					filePath: path,
					name: 'file',
					complete: (res) => {
						console.log("上传照片成功", JSON.parse(res.data).data.url);
						this.url = JSON.parse(res.data).data.url
						this.$http.post(this.$configs.api.updatePhoto, {
								'photo': this.url
							})
							.then(res => {
								this.getUserInfo()
							})
					}
				});
			})
		},
		onUnload() {
			uni.$off('uAvatarCropper')
		},
		methods: {
			exportCardAsImage() {
				const cardElement = this.$refs.card.$el;
				console.log(this.$refs.card);
				html2canvas(cardElement).then((canvas) => {
					const imageData = canvas.toDataURL('image/png');
					console.log("图片", imageData);
					this.pic(imageData)
				});
			},
			pic(src) {
				uni.previewImage({
					urls: Array.isArray(src) ? src : [src],
					current: '',
					success: function(res) {},
					fail: function(res) {},
					complete: function(res) {},
				});
			},
			chooseAvatar() {
				//裁剪组件
				this.$u.route({
					url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
					params: {
						destWidth: 182,
						destHeight: 230,
						rectWidth: 182,
						rectHeight: 230,
						fileType: 'jpg',
					}
				})
			},
			//获取校友卡信息
			async getUserInfo() {
				let res = await this.$http.post(this.$configs.api.aluminget, {
					'uid': this.uid
				})

				this.userinfo = res.data.data.info
				if (res.data.code !== 200) {
					uni.showModal({
						title: '提示',
						confirmColor: '#781C27',
						showCancel: false,
						content: res.data.message,
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '../index/index'
								})
							}
						}
					});
				}
				if (!res.data.data.info.status) {
					res.data.data.info.status = 0
				}
				let contnerArr = [
					'您还未认证成为校友',
					'审核通过',
					'待审核',
					'审核不通过'
				];
				if (res.data.data.info.status != 1) {
					this.show = true;
					this.content = contnerArr[res.data.data.info.status]
				}
			},
			confirmChange() {
				uni.navigateBack()
			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		background: #F9F9F9;
	}

	.index {
		width: 100vw;
		display: flex;
		flex-direction: column;
		// justify-content: center;
		align-items: center;
	}

	.top {
		width: 100vw;

		.bg {
			position: relative;
			z-index: 1;

			image {
				height: 554rpx;
				width: 100vw;
			}
		}
	}

	.applyschool-onlod-phops {
		width: auto;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		float: left;
		margin-top: -30rpx;
	}

	.main {
		position: relative;
		z-index: 2;
		margin-top: -100rpx;
		width: 716rpx;
		height: 402rpx;
		background: rgba(175, 0, 5, 0.78);
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		opacity: 1;
		overflow: hidden;
		// background: url("../../static/card/bg.png") repeat fixed center;

		.card {
			position: relative;
			z-index: 1;
			padding: 30rpx;

			&>image {
				position: absolute;
				z-index: 1;
				top: 0;
				left: 0;
				width: 716rpx;
				height: 402rpx;
			}

			.content {
				position: relative;
				z-index: 2;
				display: flex;
				justify-content: space-between;

				.logo {
					image {
						width: 296rpx;
						height: 72rpx;
					}
				}

				.txt {
					line-height: 50rpx;
					font-size: 24rpx;
					color: #FFFFFF;
					font-weight: 400;
				}

				.img {
					display: flex;
					// justify-content: center;
					align-items: center;
					margin-right: 88rpx;

					image {
						width: 182rpx;
						height: 230rpx;
					}
				}
			}
		}


	}

	.downbtn {

		margin: 0;
		width: 690rpx;
		height: 104rpx;
		background: #AF0005;
		border-radius: 72rpx 72rpx 72rpx 72rpx;
		opacity: 1;
		color: #FFFFFF;
		font-weight: 500;
		line-height: 104rpx;
		text-align: center;
		margin-top: 76rpx;
	}

	.photo {
		width: 182rpx;
		height: 230rpx;
		background-color: #fff;
		display: flex;
		align-items: center;

	}
</style>

使用

html 复制代码
<view class="" class="photo" @click="chooseAvatar">
							<image :src="userinfo.photo" mode="widthFix" style="width: 100%;"></image>
						</view>
javascript 复制代码
	onLoad() {
			uni.$on('uAvatarCropper', path => {
				this.avatar = path;
				// 可以在此上传到服务端
				uni.uploadFile({
					url: this.$configs.$UP_URL,
					filePath: path,
					name: 'file',
					complete: (res) => {
						console.log("上传照片成功", JSON.parse(res.data).data.url);
						
					}
				});
			})
		},
javascript 复制代码
chooseAvatar() {
				//裁剪组件
				this.$u.route({
					url: '/uview-ui/components/u-avatar-cropper/u-avatar-cropper',
					params: {
						destWidth: 182,
						destHeight: 230,
						rectWidth: 182,
						rectHeight: 230,
						fileType: 'jpg',
					}
				})
			},
相关推荐
freewlt2 小时前
前端性能优化实战:从 Lighthouse 分数到用户体验的全面升级
前端·性能优化·ux
0xDevNull2 小时前
Java反射机制深度解析:从原理到实战
java·开发语言·后端
小小亮012 小时前
Next.js基础
开发语言·前端·javascript
华洛3 小时前
我用AI做了一个48秒的真人精品漫剧,不难也不贵
前端·javascript·后端
华科易迅3 小时前
MybatisPlus增删改查操作
android·java·数据库
standovon3 小时前
Spring Boot整合Redisson的两种方式
java·spring boot·后端
Novlan13 小时前
我把 Claude Code 里的隐藏彩蛋提取出来了——零依赖的 ASCII 虚拟宠物系统
前端
IAUTOMOBILE4 小时前
Python 流程控制与函数定义:从调试现场到工程实践
java·前端·python
hutengyi4 小时前
PostgreSQL版本选择
java
皮皮林5514 小时前
重磅!JetBrains 正式发布全新的 AI 开发工具,定名 AI IDE AIR
java·intellij idea