|
|
@@ -12,12 +12,12 @@
|
|
|
* @param {area} 地区
|
|
|
* @return {string|boolean} 成功返回文件地址,失败返回false
|
|
|
*/
|
|
|
-import COS from "./cos-wx-sdk-v5.min"
|
|
|
-
|
|
|
-console.log(COS)
|
|
|
+import COS from "cos-js-sdk-v5"
|
|
|
import tools from "./tools";
|
|
|
import {getTxySts} from "@/api/common";
|
|
|
|
|
|
+
|
|
|
+
|
|
|
// 存储桶名称,由bucketname-appid 组成,appid必须填入,可以在COS控制台查看存储桶名称。 https://console.cloud.tencent.com/cos5/bucket
|
|
|
let Bucket = 'icoco-1317650740';
|
|
|
// 存储桶Region可以在COS控制台指定存储桶的概览页查看 https://console.cloud.tencent.com/cos5/bucket/
|
|
|
@@ -26,32 +26,52 @@ let Region = 'ap-guangzhou';
|
|
|
|
|
|
let cos=null;
|
|
|
function startCos(){
|
|
|
+ console.log('startCos-----------------')
|
|
|
cos = new COS({
|
|
|
getAuthorization: function (options, callback) {
|
|
|
console.log('---------------获取初始化信息')
|
|
|
- getTxySts().then((res)=>{
|
|
|
- if(res.code===0){
|
|
|
- callback({
|
|
|
- TmpSecretId: res.data.credentials.tmpSecretId,
|
|
|
- TmpSecretKey: res.data.credentials.tmpSecretKey,
|
|
|
- XCosSecurityToken: res.data.credentials.sessionToken,
|
|
|
- StartTime: res.data.startTime, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
|
|
|
- ExpiredTime: res.data.expiredTime, // 时间戳,单位秒,如:1580000900
|
|
|
- })
|
|
|
- }
|
|
|
- })
|
|
|
+ let cosToken=tools.getCosToken()
|
|
|
+ if(cosToken){
|
|
|
+ callback({
|
|
|
+ TmpSecretId: cosToken.credentials.tmpSecretId,
|
|
|
+ TmpSecretKey: cosToken.credentials.tmpSecretKey,
|
|
|
+ XCosSecurityToken: cosToken.credentials.sessionToken,
|
|
|
+ StartTime: cosToken.startTime*1, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
|
|
|
+ ExpiredTime: cosToken.expiredTime*1, // 时间戳,单位秒,如:1580000900
|
|
|
+ })
|
|
|
+ }else {
|
|
|
+ getTxySts().then((res)=>{
|
|
|
+
|
|
|
+ console.log('---------------获取初始化信息')
|
|
|
+ if(res.code===0){
|
|
|
+ tools.setCosToken(res.data)
|
|
|
+ callback({
|
|
|
+ TmpSecretId: res.data.credentials.tmpSecretId,
|
|
|
+ TmpSecretKey: res.data.credentials.tmpSecretKey,
|
|
|
+ XCosSecurityToken: res.data.credentials.sessionToken,
|
|
|
+ StartTime: res.data.startTime*1, // 时间戳,单位秒,如:1580000000,建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
|
|
|
+ ExpiredTime: res.data.expiredTime*1, // 时间戳,单位秒,如:1580000900
|
|
|
+ })
|
|
|
+ setTimeout(()=>{
|
|
|
+ cos=null
|
|
|
+ tools.delCosToken()
|
|
|
+ },(res.data.expiredTime-res.data.startTime))
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
+let cosServe={}
|
|
|
/**
|
|
|
* COS文件上传
|
|
|
* @param file
|
|
|
* @param folder 上传目录
|
|
|
* @returns {Promise<unknown>}
|
|
|
*/
|
|
|
-function txUploadFile(file,folder){
|
|
|
+cosServe.txUploadFile=function (file,folder){
|
|
|
if(cos===null){
|
|
|
console.log('对象初始化')
|
|
|
//初始化对象
|
|
|
@@ -63,13 +83,13 @@ function txUploadFile(file,folder){
|
|
|
return new Promise((resolve, reject) => {
|
|
|
console.log('cos-*-------------------------',cos)
|
|
|
console.log(file)
|
|
|
- console.log(folder+'/'+tools.getDate()+tools.getRandFileName(file))
|
|
|
+ console.log(folder+'/'+tools.getDate()+tools.getRandFileName(file.name))
|
|
|
// 分片上传文件
|
|
|
- cos.postObject({
|
|
|
+ cos.putObject({
|
|
|
Bucket: Bucket,
|
|
|
Region: Region,
|
|
|
- Key: folder+'/'+tools.getDate()+'/'+tools.getRandFileName(file),
|
|
|
- FilePath: file,
|
|
|
+ Key: folder+'/'+tools.getDate()+'/'+tools.getRandFileName(file.name),
|
|
|
+ Body: file,
|
|
|
onHashProgress: function (progressData) {
|
|
|
console.log('校验中', JSON.stringify(progressData));
|
|
|
},
|
|
|
@@ -78,7 +98,8 @@ function txUploadFile(file,folder){
|
|
|
},
|
|
|
}, function (err, data) {
|
|
|
tools.hideLoading()
|
|
|
- console.log(err, data);
|
|
|
+ console.log(err);
|
|
|
+ console.log(data);
|
|
|
if(data.Location!==undefined){
|
|
|
data.Location='https://'+data.Location;
|
|
|
resolve( data)
|
|
|
@@ -92,5 +113,26 @@ function txUploadFile(file,folder){
|
|
|
}
|
|
|
|
|
|
|
|
|
+cosServe.getSignUrl=function (url){
|
|
|
+ if(cos===null){
|
|
|
+ startCos();
|
|
|
+ }
|
|
|
+ console.log(url)
|
|
|
+ url= url.replace('https://','')
|
|
|
+ url= url.replace('http://','')
|
|
|
+ url= url.replace('icoco-1317650740.cos.ap-guangzhou.myqcloud.com','')
|
|
|
+ console.log(url)
|
|
|
+ console.log('开始签名----------------------------'+url)
|
|
|
+ let newUrl= cos.getObjectUrl({
|
|
|
+ Bucket: Bucket,
|
|
|
+ Region: Region,
|
|
|
+ Key: url, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),支持中文,必须字段 */
|
|
|
+ Sign: true,
|
|
|
+ })
|
|
|
+ console.log('签名完成----------------------------'+newUrl)
|
|
|
+ console.log(newUrl)
|
|
|
+ return newUrl
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
-export default txUploadFile
|
|
|
+export default cosServe
|