150 lines
4.3 KiB
JavaScript
150 lines
4.3 KiB
JavaScript
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
|
||
})
|
||
})
|
||
})
|
||
}
|
||
|
||
const taroChooseImg = () => {
|
||
Taro.chooseImage({
|
||
count: 1,
|
||
sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
|
||
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
|
||
success: (res) => {
|
||
console.log('res:', res)
|
||
Taro.chooseMessageFile({
|
||
count: 1,
|
||
|
||
})
|
||
},
|
||
fail: (err) => {
|
||
console.log('图片选择失败:', err)
|
||
}
|
||
})
|
||
}
|
||
|
||
export default uploadCDNImg |