import { defineStore } from "pinia"; import type { globalSettingType } from "@/store/types"; import { store } from "@/store"; import {getGlobalConfig, updateGlobalSetting, updateServer} from "@/api/server"; import {state} from "vue-tsc/out/shared"; export const useGlobalSettingStore = defineStore({ id: "pure-globalSetting", state: (): globalSettingType => ({ endpointAddress: "", dnsServer: [], MTU: 0, persistentKeepalive: 0, firewallMark: "", table: "", configFilePath: "" }), getters: { getGlobalSetting(state) { return state; }, getEndpointAddress(state) { return state.endpointAddress; } }, actions: { SET_ENDPOINT_ADDRESS(endpointAddress: string) { this.endpointAddress = endpointAddress; }, SET_DNS_SERVER(dnsServer: []) { this.dnsServer = dnsServer; }, SET_MTU(MTU: number) { this.MTU = MTU; }, SET_PERSISTENT_KEEPALIVE(persistentKeepalive: string) { this.persistentKeepalive = persistentKeepalive; }, SET_FIREWALL_MARK(firewallMark: string) { this.firewallMark = firewallMark; }, SET_TABLE(table: number) { this.table = table; }, SET_CONFIG_FILE_PATH(configFilePath: number) { this.configFilePath = configFilePath; }, async getGlobalSettingApi() { return new Promise((resolve, reject) => { getGlobalConfig() .then(data => { if (data.code === 200) { this.SET_ENDPOINT_ADDRESS(data.data.endpointAddress); this.SET_DNS_SERVER(data.data.dnsServer); this.SET_MTU(data.data.MTU); this.SET_PERSISTENT_KEEPALIVE(data.data.persistentKeepalive); this.SET_FIREWALL_MARK(data.data.firewallMark); this.SET_TABLE(data.data.table); this.SET_CONFIG_FILE_PATH(data.data.configFilePath); } resolve(data); }) .catch(error => { reject(error); }); }); }, async updateGlobalSettingApi(data?: object) { return new Promise((resolve, reject) => { updateGlobalSetting(data) .then(res => { resolve(res); }) .catch(error => { reject(error); }); }); } } }); export function useGlobalSettingStoreHook() { return useGlobalSettingStore(store); }