This commit is contained in:
2026-01-23 02:35:52 +08:00
parent 9849f93b1e
commit 2db3868fcf
39 changed files with 3381 additions and 164 deletions

View File

@@ -0,0 +1,104 @@
'use strict';
import { query, queryLogin } from '@/modules/query';
import { create } from 'zustand';
import { toast as message } from 'sonner';
export const getIsMac = async () => {
// @ts-ignore
const userAgentData = navigator.userAgentData;
if (userAgentData) {
const ua = await userAgentData.getHighEntropyValues(['platform']);
if (ua.platform === 'macOS') {
return true;
}
}
return false;
};
export const getIsElectron = () => {
// 检查 window.process 和 navigator.userAgent 中是否包含 Electron 信息
return (
// @ts-ignore
(typeof window !== 'undefined' && typeof window.process !== 'undefined' && window.process.type === 'renderer') ||
(typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('Electron') >= 0)
);
};
type PlatfromStore = {
isMac: boolean;
setIsMac: (mac: boolean) => void;
mount: boolean;
isElectron: boolean;
init: () => Promise<void>;
};
export const usePlatformStore = create<PlatfromStore>((set) => {
return {
isMac: false,
mount: false,
isElectron: false,
setIsMac: (mac) => set({ isMac: mac }),
init: async () => {
const mac = await getIsMac();
// @ts-ignore
const isElectron = getIsElectron();
set({ isMac: isElectron && mac, isElectron: isElectron, mount: true });
},
};
});
type Me = {
id?: string;
username?: string;
needChangePassword?: boolean;
role?: string;
description?: string;
type?: 'user' | 'org';
orgs?: string[];
avatar?: string;
};
export type LayoutStore = {
open: boolean;
setOpen: (open: boolean) => void;
me: Me;
setMe: (me: Me) => void;
getMe: () => Promise<void>;
openUser: boolean;
setOpenUser: (openUser: boolean) => void;
switchOrg: (username?: string, type?: 'user' | 'org') => Promise<void>;
isAdmin: boolean;
setIsAdmin: (isAdmin: boolean) => void;
checkHasOrg: () => boolean;
};
export const useLayoutStore = create<LayoutStore>((set, get) => ({
open: false,
setOpen: (open) => set({ open }),
me: {},
setMe: (me) => set({ me }),
getMe: async () => {
const res = await queryLogin.getMe();
if (res.code === 200) {
set({ me: res.data });
set({ isAdmin: res.data.orgs?.includes('admin') });
}
},
openUser: false,
setOpenUser: (openUser) => set({ openUser }),
switchOrg: async (username?: string, type?: string) => {
const res = await queryLogin.switchUser(username || '');
if (res.code === 200) {
message.success('Switch success');
setTimeout(() => {
window.location.reload();
}, 1000);
} else {
message.error(res.message || 'Request failed');
}
},
isAdmin: false,
setIsAdmin: (isAdmin) => set({ isAdmin }),
checkHasOrg: () => {
const user = get().me || {};
if (!user.orgs) {
return false;
}
return user?.orgs?.length > 0;
},
}));