更新记录
1.0.0(2024-01-06)
下载此版本
图片裁切1.0
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.0 |
× |
√ |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
√ |
× |
× |
× |
× |
<template>
<view>
<image v-if="avatar" class="avatar" :src="avatar" />
<button v-else @click="chooseAvatar">选择图片</button>
<img-crop
v-if="chooseAvatarUrl"
:url="chooseAvatarUrl"
:width="200"
:height="200"
@cancel="chooseAvatarUrl = ''"
@success="updateAvatar"
/>
</view>
</template>
<script lang="ts" setup>
import ImgCrop from '@/components/img-crop/img-crop.vue';
import { ref } from 'vue';
const avatar = ref('');
const chooseAvatarUrl = ref('');
async function chooseAvatar() {
// #ifdef MP-WEIXIN
uni.chooseMedia({
count: 1,
mediaType: ['image'],
success({ tempFiles }) {
chooseAvatarUrl.value = tempFiles[0].tempFilePath;
},
});
// #endif
// #ifndef MP-WEIXIN
const { tempFilePaths } = await uni.chooseImage({ count: 1 });
chooseAvatarUrl.value = tempFilePaths[0];
// #endif
}
async function updateAvatar(filePath: string) {
chooseAvatarUrl.value = '';
const { data } = await uni.uploadFile({
url: `${import.meta.env.VITE_BASE_URL}/api/resources/upload`,
header: {
Authorization: `Bearer Token`,
},
formData: { type: 'avatar' },
name: 'file',
filePath,
});
const res = JSON.parse(data);
avatar.value = res.data.url;
}
</script>