systemStore.js 3.9 KB

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