62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
import { pathToBase64 } from "@/common/image-tools/index.js"
|
|
import configService from "../config.service";
|
|
|
|
let clientPath = '';// 临时上传路径
|
|
let resultPath = '';// 上传返回路径
|
|
|
|
// 先转base64再转arrayBuffer再上传
|
|
const getImageBase64 = async(vm,filePath,success) => {
|
|
await pathToBase64(filePath).then(path => {
|
|
uni.request({
|
|
url: clientPath,
|
|
method: 'PUT',
|
|
header: {"Content-Type": "image/png",},
|
|
data: uni.base64ToArrayBuffer(path.replace(/^data:image\/\w+;base64,/, "")),
|
|
}).then((res)=>{
|
|
console.log(res);
|
|
if(res[1]?.statusCode===200){
|
|
success(resultPath);
|
|
}else{
|
|
vm.$refs.uToast.show({type: 'error',title: "图片上传失败!"});
|
|
vm.dotLoading = false;
|
|
return;
|
|
}
|
|
})
|
|
}).catch(error => {
|
|
vm.$refs.uToast.show({type: 'error',title: "处理失败!"});
|
|
vm.dotLoading = false;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @name 上传方法
|
|
* @param vm 当前-相当于this
|
|
* @param filePath 文件临时路径
|
|
* @param success 成功回调方法
|
|
*
|
|
*/
|
|
const ossUpload = async(vm,filePath,success) => {
|
|
// 文生图类无需上传
|
|
if(!filePath){
|
|
success("error");
|
|
return;
|
|
}
|
|
let res = await vm.$api.getOssSignal();
|
|
if(res?.success){
|
|
resultPath = res.data.substring(0, res.data.indexOf("?"));
|
|
// #ifdef H5
|
|
clientPath = res.data.replace(configService.aliUrl, "/ossUpload");
|
|
clientPath = clientPath.replace(configService.otherAliUrl, "/otherOssUpload");
|
|
clientPath = clientPath.replace(configService.anotherAliUrl, "/anotherOssUpload");
|
|
// #endif
|
|
// #ifndef H5
|
|
clientPath = res.data;
|
|
// #endif
|
|
// clientPath = res.data.replace(configService.aliAsyncUrl, "/asyncOssUpload");
|
|
}
|
|
getImageBase64(vm,filePath,success);
|
|
}
|
|
|
|
export {
|
|
ossUpload
|
|
} |