systemStore.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. layout: "blendLeft",
  7. menus: "",
  8. routeItem: "",
  9. menuTabSate: "",
  10. menuSecondLongShow: false,
  11. user_login_information: "",
  12. stsClientInfo: {},
  13. };
  14. try {
  15. const SYSTEM_STORE = getLocalStorage("SYSTEM_STORE")
  16. for (const key in SYSTEM_STORE) {
  17. const value = getLocalStorage(key)
  18. if (value) INITIALIZE[key] = value
  19. }
  20. } catch (err) {
  21. window.console.log('无存储数据', err)
  22. }
  23. export const useSystemStore = defineStore({
  24. id: "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. getLayout() {
  38. return this.layout;
  39. },
  40. getRouteItem() {
  41. return this.routeItem || getLocalStorage("routeItem")
  42. },
  43. getMenus() {
  44. if (this.menus) return this.menus;
  45. this.menus = window.localStorage?.menus || "";
  46. return this.menus;
  47. },
  48. getMenuTabSate() {
  49. return this.menuTabSate || getLocalStorage("menuTabSate");
  50. },
  51. getSTSClient() {
  52. const stsFn = async () => {
  53. const windowVar = window
  54. let longTime = 0
  55. if (windowVar.stsOSSClient && windowVar.stsOSSClient.stsTokenFreshTime) {
  56. const oldTime = windowVar.stsOSSClient.stsTokenFreshTime.getTime()
  57. const newTime = new Date().getTime()
  58. longTime = newTime - oldTime
  59. }
  60. // STS信息
  61. if (!windowVar.stsOSSClient || longTime > 10 * 60 * 1000) {
  62. const res = await getSTSInfo({})
  63. const info = res.data
  64. const systemStore = useSystemStore()
  65. const getInfoData = info => {
  66. const infoData = {
  67. endpoint: 'https://cardiot.oss-cn-beijing.aliyuncs.com',
  68. region: 'oss-cn-beijing',
  69. accessKeyId: info.AccessKeyId,
  70. accessKeySecret: info.AccessKeySecret,
  71. stsToken: info.SecurityToken,
  72. bucket: 'cardiot',
  73. cname: true
  74. }
  75. this.stsClientInfo = infoData
  76. systemStore.setStateValue('stsClientInfo', infoData)
  77. return infoData
  78. }
  79. getInfoData(info)
  80. const client = new windowVar.OSS({
  81. ...this.stsClientInfo,
  82. refreshSTSToken: async () => {
  83. const res = await getSTSInfo({})
  84. getInfoData(res.data)
  85. return {
  86. accessKeyId: info.AccessKeyId,
  87. accessKeySecret: info.AccessKeySecret,
  88. stsToken: info.SecurityToken
  89. }
  90. },
  91. refreshSTSTokenInterval: 30 * 60 * 1000
  92. })
  93. client.putObject = async data => {
  94. const putRes = await client.put(data.key, data.body)
  95. putRes.statusCode = 200
  96. return putRes
  97. }
  98. windowVar.stsOSSClient = client
  99. return client
  100. }
  101. return windowVar.stsOSSClient
  102. };
  103. return stsFn
  104. },
  105. getRole() {
  106. // if (this.role) return ;
  107. // this.role = window.localStorage?.role || "";
  108. // return this.role;
  109. return JSON.parse(localStorage.getItem("user_login_information"))?.userType
  110. },
  111. },
  112. actions: {
  113. localLoading(value = false) {
  114. this.local_loading = value
  115. },
  116. // 储存
  117. setStateValue(res) {
  118. this[res.key] = res.value;
  119. const value =
  120. typeof res.value === "string" ? res.value : JSON.stringify(res.value);
  121. if (res.localStorage) {
  122. // 此方法只处理了简单数据类型 如字符串(token)、布尔之类的
  123. window.localStorage.setItem(res.key, value);
  124. }
  125. }
  126. },
  127. });