systemStore.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { defineStore } from "pinia";
  2. import { getSTSInfo } from "@/api/path/system.api";
  3. import { setLocalStorage, getLocalStorage } from "@/utils";
  4. const INITIALIZE = {
  5. local_loading: false,
  6. routeSate: "",
  7. user_login_information: "",
  8. stsClientInfo: {},
  9. wallet: {
  10. address: "",
  11. privateKey: "",
  12. },
  13. ISCREATE: true
  14. };
  15. try {
  16. const SYSTEM_STORE = getLocalStorage("SYSTEM_STORE");
  17. for (const key in SYSTEM_STORE) {
  18. const value = getLocalStorage(key);
  19. if (value) INITIALIZE[key] = value;
  20. }
  21. } catch (err) {
  22. window.console.log("无存储数据", err);
  23. }
  24. export const useSystemStore = defineStore("useSystemStore", {
  25. state: () => ({
  26. ...INITIALIZE,
  27. }),
  28. getters: {
  29. getLocalLoading() {
  30. return this.local_loading;
  31. },
  32. getToken() {
  33. if (this.token) return this.token;
  34. this.token = window.localStorage?.token || "";
  35. return this.token;
  36. },
  37. getWallet() {
  38. if (this.wallet.address) return this.wallet;
  39. this.wallet = window.localStorage?.wallet || "";
  40. return this.wallet;
  41. },
  42. getRouteSate() {
  43. return this.routeSate || getLocalStorage("routeSate");
  44. },
  45. getSTSClient() {
  46. const stsFn = async () => {
  47. const windowVar = window;
  48. let longTime = 0;
  49. if (
  50. windowVar.stsOSSClient &&
  51. windowVar.stsOSSClient.stsTokenFreshTime
  52. ) {
  53. const oldTime = windowVar.stsOSSClient.stsTokenFreshTime.getTime();
  54. const newTime = new Date().getTime();
  55. longTime = newTime - oldTime;
  56. }
  57. // STS信息
  58. if (!windowVar.stsOSSClient || longTime > 10 * 60 * 1000) {
  59. const res = await getSTSInfo({});
  60. const info = res.data;
  61. const systemStore = useSystemStore();
  62. const getInfoData = (info) => {
  63. const infoData = {
  64. endpoint: "https://flexi.oss-cn-hongkong.aliyuncs.com",
  65. region: "oss-cn-hongkong",
  66. accessKeyId: info.AccessKeyId,
  67. accessKeySecret: info.AccessKeySecret,
  68. stsToken: info.SecurityToken,
  69. bucket: "flexi",
  70. cname: true,
  71. };
  72. this.stsClientInfo = infoData;
  73. systemStore.setStateValue("stsClientInfo", infoData);
  74. return infoData;
  75. };
  76. getInfoData(info);
  77. const client = new windowVar.OSS({
  78. ...this.stsClientInfo,
  79. refreshSTSToken: async () => {
  80. const res = await getSTSInfo({});
  81. getInfoData(res.data);
  82. return {
  83. accessKeyId: info.AccessKeyId,
  84. accessKeySecret: info.AccessKeySecret,
  85. stsToken: info.SecurityToken,
  86. };
  87. },
  88. refreshSTSTokenInterval: 30 * 60 * 1000,
  89. });
  90. client.putObject = async (data) => {
  91. const putRes = await client.put(data.key, data.body);
  92. putRes.statusCode = 200;
  93. return putRes;
  94. };
  95. windowVar.stsOSSClient = client;
  96. return client;
  97. }
  98. return windowVar.stsOSSClient;
  99. };
  100. return stsFn;
  101. },
  102. getISCREATE(){
  103. return this.ISCREATE || getLocalStorage("ISCREATE");
  104. }
  105. },
  106. actions: {
  107. localLoading(value = false) {
  108. this.local_loading = value;
  109. },
  110. // 储存
  111. setStateValue(res) {
  112. this[res.key] = res.value;
  113. const value =
  114. typeof res.value === "string" ? res.value : JSON.stringify(res.value);
  115. if (res.localStorage) {
  116. // 此方法只处理了简单数据类型 如字符串(token)、布尔之类的
  117. window.localStorage.setItem(res.key, value);
  118. }
  119. },
  120. },
  121. });