feat: 优化为先选择图片再裁剪
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
<s-pages title="裁剪头像" :purple_bg="true">
|
<s-pages title="裁剪头像" :purple_bg="true">
|
||||||
<view class="avatar-crop-page">
|
<view class="avatar-crop-page">
|
||||||
<avatarCropper @confirm="onConfirm" @error="onError" />
|
<avatarCropper :src="src" @confirm="onConfirm" @error="onError" />
|
||||||
<view v-if="uploading" class="loading-mask">
|
<view v-if="uploading" class="loading-mask">
|
||||||
<view class="loading-box">上传中...</view>
|
<view class="loading-box">上传中...</view>
|
||||||
</view>
|
</view>
|
||||||
@@ -16,10 +16,12 @@
|
|||||||
import avatarCropper from '@/components/avatar_uploader/avatar_cropper.vue';
|
import avatarCropper from '@/components/avatar_uploader/avatar_cropper.vue';
|
||||||
|
|
||||||
const { proxy } = getCurrentInstance();
|
const { proxy } = getCurrentInstance();
|
||||||
|
const src = ref('');
|
||||||
const uploading = ref(false);
|
const uploading = ref(false);
|
||||||
const eventChannel = ref(null);
|
const eventChannel = ref(null);
|
||||||
|
|
||||||
onLoad(() => {
|
onLoad((options) => {
|
||||||
|
src.value = decodeURIComponent(String(options?.src || '').trim());
|
||||||
eventChannel.value = proxy.getOpenerEventChannel ? proxy.getOpenerEventChannel() : null;
|
eventChannel.value = proxy.getOpenerEventChannel ? proxy.getOpenerEventChannel() : null;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -102,4 +104,3 @@
|
|||||||
font-size: 28rpx;
|
font-size: 28rpx;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
|||||||
@@ -176,14 +176,30 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
const upImg = () => {
|
const upImg = () => {
|
||||||
uni.navigateTo({
|
uni.chooseImage({
|
||||||
url: '/pages/public/avatarCrop',
|
count: 1,
|
||||||
|
sizeType: ['original', 'compressed'],
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
const eventChannel = res?.eventChannel;
|
const filePath = res?.tempFilePaths?.[0] || '';
|
||||||
if (!eventChannel) return;
|
const size = res?.tempFiles?.[0]?.size;
|
||||||
eventChannel.on('success', ({ url }) => {
|
if (typeof size === 'number' && size > 1048576) {
|
||||||
if (!url) return;
|
uni.showToast({
|
||||||
upData({ avatar: url });
|
title: '上传图片大小不能超过1MB',
|
||||||
|
icon: 'none',
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!filePath) return;
|
||||||
|
uni.navigateTo({
|
||||||
|
url: `/pages/public/avatarCrop?src=${encodeURIComponent(filePath)}`,
|
||||||
|
success: (navRes) => {
|
||||||
|
const eventChannel = navRes?.eventChannel;
|
||||||
|
if (!eventChannel) return;
|
||||||
|
eventChannel.on('success', ({ url }) => {
|
||||||
|
if (!url) return;
|
||||||
|
upData({ avatar: url });
|
||||||
|
});
|
||||||
|
},
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user