import {
	getSts
} from "@/api/common.js";
import tools from "./tools";
import AliOss from 'uploading-oss'
let ossData = null;
let sysTime = 0;
let setAliSts = async function () {
	let res = await getSts({});
	if (res.code === 1) {
		ossData = res.data;
		sysTime = setTime();
	} else {
		tools.error(res.msg);
	}
}

let setTime = function () {
	return (new Date()).getTime();
}
let getFilPathName = function () {
	let date = new Date();
	return "image/" + date.getYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
}

export const upLoadingFileOss = async function (file) {
	if (ossData === null || (sysTime + 900000 <= setTime())) {
		await setAliSts();
	}
	return new Promise((resolve, reject) => {
		AliOss.uploadOss(file, {
			savePath: getFilPathName(),
			AccessKeySecret: ossData.AccessKeySecret,
			SecurityToken: ossData.SecurityToken,
			AccessKeyId: ossData.AccessKeyId,
			bucket: "jhnewshop",
			area: "oss-cn-chengdu",
		}, function (res) {
			resolve(res);
		});
	});
}
