封装图片组件

This commit is contained in:
2022-06-10 15:41:33 +08:00
parent 8197140027
commit 38a3b348bd
57 changed files with 2899 additions and 181 deletions
-132
View File
@@ -1,132 +0,0 @@
import Taro from '@tarojs/taro';
import { GET_UPLOAD_SIGN, CDN_UPLOAD_IMG, UPLOAD_CDN_URL } from './constant'
import { GetSignApi } from '@/api/cdn'
const { fetchData: GetSign, success, data: resData, msg, code } = GetSignApi()
// 上传图片 获取auth,Policy
/*
scene 场景值,区分上传文件的根路径
type 类型值,区分上传业务bucket
*/
const getSecret = (scene, type) => {
return new Promise(async (resolve, reject) => {
const SAVE_PATH = `/${scene}/{filemd5}{day}{hour}{min}{sec}{.suffix}`;
let params = {
'method': 'post',
'save_key': SAVE_PATH
}
// 获取签名
await GetSign(params)
if (success.value) {
// console.log('返回签名',resData.value);
resolve(resData.value)
} else {
reject({
code: code.value || '9999',
msg: msg.value
});
}
})
}
const getFileType = (name) => {
if (!name) return false;
var imgType = ["gif", "jpeg", "jpg", "bmp", "png"];
var videoType = ["avi", "wmv", "mkv", "mp4", "mov", "rm", "3gp", "flv", "mpg", "rmvb", "quicktime"];
if (RegExp("\.?(" + imgType.join("|") + ")$", "i").test(name.toLowerCase())) {
return 'image';
} else if (RegExp("\.(" + videoType.join("|") + ")$", "i").test(name.toLowerCase())) {
return 'video';
} else {
return false;
}
}
const upYunbucket = (type) => {
var bucket = ""
switch (type) {
case "product":
bucket = "testzzfzyc"
break;
}
return bucket
}
/**
*
* @param {*} file 传入文件
* @param {String} secene 传入 'product'
* @param {String} type 传入 'product'
* @returns
*/
const uploadCDNImg = (file, secene, type) => {
// var file = event.target.files[0];
// var filetype = file.type
let filetype = file.tempFilePath
if (!getFileType(filetype)) {
Taro.showToast({
title: "上传文件类型错误",
icon: "none",
duration: 3800
})
return false
}
return new Promise((resolve, reject, race) => {
getSecret(secene, type)
.then(result => {
console.log('bucket', result.bucket);
var formdata = {
'authorization': result.authorization,
'policy': result.policy,
// "file": file.tempFilePath,
}
const uploadTask = Taro.uploadFile({
url: `${UPLOAD_CDN_URL}${result.bucket}`,
formData: formdata,
filePath: file.tempFilePath,
name: 'file',
success: res => {
resolve(JSON.parse(`${res.data}`))
},
fail: err => {
console.log(err)
reject(err)
}
})
uploadTask.progress(res => {
console.log('上传进度', res.progress);
if (res.progress < 100) {
Taro.showLoading({
title: '上传中...'
})
} else {
Taro.hideLoading()
}
})
})
.catch(result => {
reject(result)
Taro.showToast({
title: "获取密钥失败!",
icon: "none",
duration: 3800
})
})
})
}
export default uploadCDNImg