feat(project): base smatec's frontend
This commit is contained in:
4
src/utils/format.ts
Normal file
4
src/utils/format.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
// 示例方法,没有实际意义
|
||||
export function trim(str: string) {
|
||||
return str.trim();
|
||||
}
|
||||
25
src/utils/jwt.ts
Normal file
25
src/utils/jwt.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export function parseJwt(token: string) {
|
||||
if (!token) return null;
|
||||
|
||||
const base64Url = token.split('.')[1];
|
||||
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
||||
const jsonPayload = decodeURIComponent(
|
||||
atob(base64)
|
||||
.split('')
|
||||
.map((c) => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
|
||||
.join(''),
|
||||
);
|
||||
return JSON.parse(jsonPayload);
|
||||
}
|
||||
|
||||
export function checkTokenExpired(token: string): boolean {
|
||||
const parsed = parseJwt(token);
|
||||
|
||||
const { exp } = parsed;
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const oneHour = 60 * 60;
|
||||
if (exp - now < oneHour) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
16
src/utils/logo.ts
Normal file
16
src/utils/logo.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import gmsLogo from '../../public/gms-logo.png';
|
||||
import sgwLogo from '../../public/sgw-logo.png';
|
||||
import smatecLogo from '../../public/smatec-logo.png';
|
||||
import spoleLogo from '../../public/spole-logo.png';
|
||||
export const getLogoImage = () => {
|
||||
switch (process.env.DOMAIN_ENV) {
|
||||
case 'gms':
|
||||
return gmsLogo;
|
||||
case 'sgw':
|
||||
return sgwLogo;
|
||||
case 'spole':
|
||||
return spoleLogo;
|
||||
default:
|
||||
return smatecLogo;
|
||||
}
|
||||
};
|
||||
48
src/utils/storage.ts
Normal file
48
src/utils/storage.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { TOKEN } from '@/constants';
|
||||
|
||||
export function getToken(): string {
|
||||
return localStorage.getItem(TOKEN) || '';
|
||||
}
|
||||
|
||||
export function setToken(token: string) {
|
||||
localStorage.setItem(TOKEN, token);
|
||||
}
|
||||
|
||||
export function removeToken() {
|
||||
localStorage.removeItem(TOKEN);
|
||||
}
|
||||
|
||||
export function getBrowserId() {
|
||||
const id = localStorage.getItem('sip-browserid');
|
||||
if (!id) {
|
||||
const { navigator, screen } = window;
|
||||
let uid = '';
|
||||
uid += navigator?.mimeTypes?.length || '';
|
||||
uid += navigator?.userAgent.replace(/\D+/g, '') || '';
|
||||
uid += navigator?.plugins?.length || '';
|
||||
uid += screen?.height || '';
|
||||
uid += screen?.width || '';
|
||||
uid += screen?.pixelDepth || '';
|
||||
localStorage.setItem('sip-browserid', uid);
|
||||
return uid;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all localStorage data except browserId
|
||||
*/
|
||||
export function clearAllData() {
|
||||
const browserId = localStorage.getItem('sip-browserid');
|
||||
localStorage.clear();
|
||||
if (browserId) {
|
||||
localStorage.setItem('sip-browserid', browserId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all sessionStorage data
|
||||
*/
|
||||
export function clearSessionData() {
|
||||
sessionStorage.clear();
|
||||
}
|
||||
Reference in New Issue
Block a user