123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import { defineStore } from "pinia";
- import { getSTSInfo } from "@/api/path/system.api";
- import { setLocalStorage, getLocalStorage } from "@/utils";
- const INITIALIZE = {
- local_loading: false,
- layout: "menu",
- menus: "",
- routeItem: "",
- menuTabSate: "",
- menuSecondLongShow: false,
- user_login_information: "",
- stsClientInfo: {},
- USER_IP:{}
- };
- try {
- const SYSTEM_STORE = getLocalStorage("SYSTEM_STORE")
- for (const key in SYSTEM_STORE) {
- const value = getLocalStorage(key)
- if (value) INITIALIZE[key] = value
- }
- } catch (err) {
- window.console.log('无存储数据', err)
- }
- export const useSystemStore = defineStore({
- id: "useSystemStore",
- state: () => ({
- ...INITIALIZE,
- }),
- getters: {
- getUserIp(){
- return this.USER_IP
- },
- getLocalLoading() {
- return this.local_loading
- },
- getToken() {
- if (this.token) return this.token;
- this.token = window.localStorage?.token || "";
- return this.token;
- },
- getLayout() {
- return this.layout;
- },
- getRouteItem() {
- return this.routeItem || getLocalStorage("routeItem")
- },
- getMenus() {
- if (this.menus) return this.menus;
- this.menus = window.localStorage?.menus || "";
- return this.menus;
- },
- getMenuTabSate() {
- return this.menuTabSate || getLocalStorage("menuTabSate");
- },
- getSTSClient() {
- const stsFn = async () => {
- const windowVar = window
- let longTime = 0
- if (windowVar.stsOSSClient && windowVar.stsOSSClient.stsTokenFreshTime) {
- const oldTime = windowVar.stsOSSClient.stsTokenFreshTime.getTime()
- const newTime = new Date().getTime()
- longTime = newTime - oldTime
- }
- // STS信息
- if (!windowVar.stsOSSClient || longTime > 10 * 60 * 1000) {
- const res = await getSTSInfo({})
- const info = res.data
- const systemStore = useSystemStore()
- const getInfoData = info => {
- const infoData = {
- endpoint: 'https://cardiot.oss-cn-beijing.aliyuncs.com',
- region: 'oss-cn-beijing',
- accessKeyId: info.AccessKeyId,
- accessKeySecret: info.AccessKeySecret,
- stsToken: info.SecurityToken,
- bucket: 'cardiot',
- cname: true
- }
- this.stsClientInfo = infoData
- systemStore.setStateValue('stsClientInfo', infoData)
- return infoData
- }
- getInfoData(info)
- const client = new windowVar.OSS({
- ...this.stsClientInfo,
- refreshSTSToken: async () => {
- const res = await getSTSInfo({})
- getInfoData(res.data)
- return {
- accessKeyId: info.AccessKeyId,
- accessKeySecret: info.AccessKeySecret,
- stsToken: info.SecurityToken
- }
- },
- refreshSTSTokenInterval: 30 * 60 * 1000
- })
- client.putObject = async data => {
- const putRes = await client.put(data.key, data.body)
- putRes.statusCode = 200
- return putRes
- }
- windowVar.stsOSSClient = client
- return client
- }
- return windowVar.stsOSSClient
- };
- return stsFn
- },
- getRole() {
- // if (this.role) return ;
- // this.role = window.localStorage?.role || "";
- // return this.role;
- return JSON.parse(localStorage.getItem("user_login_information"))?.userType
- },
- },
- actions: {
- localLoading(value = false) {
- this.local_loading = value
- },
- // 储存
- setStateValue(res) {
- this[res.key] = res.value;
- const value =
- typeof res.value === "string" ? res.value : JSON.stringify(res.value);
- if (res.localStorage) {
- // 此方法只处理了简单数据类型 如字符串(token)、布尔之类的
- window.localStorage.setItem(res.key, value);
- }
- }
- },
- });
|