Merge branch 'v1.2_crop' into v1.3

* v1.2_crop:
  feat: 裁剪添加放大缩小
  feat: 优化为先选择图片再裁剪
  feat:裁剪头像
This commit is contained in:
2026-06-15 11:20:13 +08:00
4 changed files with 658 additions and 25 deletions
+124
View File
@@ -0,0 +1,124 @@
<template>
<s-pages title="裁剪头像" :purple_bg="true">
<template #navRight>
<view class="nav-cancel" @click="cancel">取消</view>
</template>
<view class="avatar-crop-page">
<avatarCropper :src="src" @confirm="onConfirm" @error="onError" />
<view v-if="uploading" class="loading-mask">
<view class="loading-box">上传中...</view>
</view>
</view>
</s-pages>
</template>
<script setup>
import { ref, getCurrentInstance } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { baseUrl } from '@/sheep/config';
import avatarCropper from '@/components/avatar_uploader/avatar_cropper.vue';
const { proxy } = getCurrentInstance();
const src = ref('');
const uploading = ref(false);
const eventChannel = ref(null);
onLoad((options) => {
src.value = decodeURIComponent(String(options?.src || '').trim());
eventChannel.value = proxy.getOpenerEventChannel ? proxy.getOpenerEventChannel() : null;
});
const uploadAvatar = async (filePath) => {
const token = uni.getStorageSync('token') ? uni.getStorageSync('token') : undefined;
const tenantId = uni.getStorageSync('tenant-id');
const result = await new Promise((resolve, reject) => {
uni.uploadFile({
url: baseUrl + '/app-api/infra/file/upload',
filePath,
name: 'file',
formData: {
directory: 'head',
'tenant-id': tenantId,
token: token,
},
header: {},
success: (res) => resolve(res),
fail: (err) => reject(err),
});
}).catch(() => null);
if (!result) throw new Error('upload_failed');
let data = null;
try {
data = typeof result.data === 'string' ? JSON.parse(result.data) : result.data;
} catch (e) {
throw new Error('upload_parse_failed');
}
const url = data?.data || '';
if (!url) throw new Error('upload_no_url');
return url;
};
const onConfirm = async (croppedFilePath) => {
if (uploading.value) return;
uploading.value = true;
try {
const url = await uploadAvatar(croppedFilePath);
if (eventChannel.value && eventChannel.value.emit) {
eventChannel.value.emit('success', { url });
}
uni.navigateBack();
} catch (e) {
uni.showToast({ title: '上传失败,请重试', icon: 'none' });
} finally {
uploading.value = false;
}
};
const onError = () => {
uni.showToast({ title: '处理失败,请重试', icon: 'none' });
};
const cancel = () => {
if (uploading.value) return;
uni.navigateBack();
};
</script>
<style lang="scss" scoped>
.avatar-crop-page {
position: relative;
height: 100%;
}
.nav-cancel {
padding: 0 24rpx;
height: 88rpx;
display: flex;
align-items: center;
justify-content: center;
font-size: 30rpx;
color: rgba(51, 51, 51, 0.9);
}
.loading-mask {
position: absolute;
inset: 0;
z-index: 999;
display: flex;
align-items: center;
justify-content: center;
background: rgba(0, 0, 0, 0.35);
}
.loading-box {
padding: 18rpx 26rpx;
border-radius: 14rpx;
background: rgba(0, 0, 0, 0.55);
color: #fff;
font-size: 28rpx;
}
</style>
+16 -25
View File
@@ -30,7 +30,6 @@
import { ref, computed, reactive, onMounted } from 'vue';
import AuthUtil from '@/sheep/api/member/auth';
import card_list from '@/components/card_list/card_list.vue';
import { baseUrl } from '@/sheep/config';
import UserApi from '@/sheep/api/member/user';
const appInfo = computed(() => sheep.$store('app').info);
const isLogin = computed(() => sheep.$store('user').isLogin);
@@ -178,35 +177,27 @@
const upImg = () => {
uni.chooseImage({
count: 1, //默认9
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
success: function (res) {
const tempFilePaths = res.tempFilePaths;
const tempFiles = res.tempFiles;
let resSize = tempFiles[0].size;
if (resSize > 1048576) {
count: 1,
sizeType: ['original', 'compressed'],
success: (res) => {
const filePath = res?.tempFilePaths?.[0] || '';
const size = res?.tempFiles?.[0]?.size;
if (typeof size === 'number' && size > 1048576) {
uni.showToast({
title: '上传图片大小不能超过1MB',
icon: 'error',
icon: 'none',
});
return;
}
const token = uni.getStorageSync('token') ? uni.getStorageSync('token') : undefined;
let tenant_id = uni.getStorageSync('tenant-id');
uni.uploadFile({
url: baseUrl + '/app-api/infra/file/upload', //仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData: {
directory: 'head',
'tenant-id': tenant_id,
token: token,
},
header: {},
success: (uploadFileRes) => {
let url = JSON.parse(uploadFileRes.data).data;
upData({
avatar: url,
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 });
});
},
});