upLoadingFile.js 996 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import {
  2. getSts
  3. } from "@/api/common.js";
  4. import tools from "./tools";
  5. import AliOss from 'uploading-oss'
  6. let ossData = null;
  7. let sysTime = 0;
  8. let setAliSts = async function () {
  9. let res = await getSts({});
  10. if (res.code === 1) {
  11. ossData = res.data;
  12. sysTime = setTime();
  13. } else {
  14. tools.error(res.msg);
  15. }
  16. }
  17. let setTime = function () {
  18. return (new Date()).getTime();
  19. }
  20. let getFilPathName = function () {
  21. let date = new Date();
  22. return "image/" + date.getYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
  23. }
  24. export const upLoadingFileOss = async function (file) {
  25. if (ossData === null || (sysTime + 900000 <= setTime())) {
  26. await setAliSts();
  27. }
  28. return new Promise((resolve, reject) => {
  29. AliOss.uploadOss(file, {
  30. savePath: getFilPathName(),
  31. AccessKeySecret: ossData.AccessKeySecret,
  32. SecurityToken: ossData.SecurityToken,
  33. AccessKeyId: ossData.AccessKeyId,
  34. bucket: "jhnewshop",
  35. area: "oss-cn-chengdu",
  36. }, function (res) {
  37. resolve(res);
  38. });
  39. });
  40. }