chore: update auth and portal

This commit is contained in:
2026-01-14 11:29:17 +08:00
parent fb0a1c2f84
commit 3bcee7efc2
42 changed files with 5969 additions and 3014 deletions

View File

@@ -1,144 +1,144 @@
const hexChars = '0123456789abcdef';
const hexChars = "0123456789abcdef";
function add(x, y) {
return (x + y) >>> 0;
return (x + y) >>> 0;
}
function rol(x, n) {
return (x << n) | (x >>> (32 - n));
return (x << n) | (x >>> (32 - n));
}
function cmn(q, a, b, x, s, t) {
return add(rol(add(add(a, q), add(x, t)), s), b);
return add(rol(add(add(a, q), add(x, t)), s), b);
}
function ff(a, b, c, d, x, s, t) {
return cmn((b & c) | (~b & d), a, b, x, s, t);
return cmn((b & c) | (~b & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t) {
return cmn((b & d) | (c & ~d), a, b, x, s, t);
return cmn((b & d) | (c & ~d), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t) {
return cmn(b ^ c ^ d, a, b, x, s, t);
return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t) {
return cmn(c ^ (b | ~d), a, b, x, s, t);
return cmn(c ^ (b | ~d), a, b, x, s, t);
}
function toHex(n) {
let out = '';
for (let i = 0; i < 4; i++) {
const byte = (n >>> (i * 8)) & 0xff;
out += hexChars.charAt((byte >>> 4) & 0x0f) + hexChars.charAt(byte & 0x0f);
}
return out;
let out = "";
for (let i = 0; i < 4; i++) {
const byte = (n >>> (i * 8)) & 0xff;
out += hexChars.charAt((byte >>> 4) & 0x0f) + hexChars.charAt(byte & 0x0f);
}
return out;
}
export function md5ArrayBuffer(buffer) {
const bytes = new Uint8Array(buffer);
const words = [];
for (let i = 0; i < bytes.length; i++) {
words[i >> 2] |= bytes[i] << ((i % 4) * 8);
const bytes = new Uint8Array(buffer);
const words = [];
for (let i = 0; i < bytes.length; i++) {
words[i >> 2] |= bytes[i] << ((i % 4) * 8);
}
const bitLen = bytes.length * 8;
words[bitLen >> 5] |= 0x80 << (bitLen % 32);
words[(((bitLen + 64) >>> 9) << 4) + 14] = bitLen;
const totalLen = (((bitLen + 64) >>> 9) << 4) + 16;
for (let i = 0; i < totalLen; i++) {
if (words[i] === undefined) {
words[i] = 0;
}
}
const bitLen = bytes.length * 8;
words[bitLen >> 5] |= 0x80 << (bitLen % 32);
words[(((bitLen + 64) >>> 9) << 4) + 14] = bitLen;
let a = 0x67452301;
let b = 0xefcdab89;
let c = 0x98badcfe;
let d = 0x10325476;
const totalLen = (((bitLen + 64) >>> 9) << 4) + 16;
for (let i = 0; i < totalLen; i++) {
if (words[i] === undefined) {
words[i] = 0;
}
}
for (let i = 0; i < words.length; i += 16) {
const oa = a;
const ob = b;
const oc = c;
const od = d;
let a = 0x67452301;
let b = 0xefcdab89;
let c = 0x98badcfe;
let d = 0x10325476;
a = ff(a, b, c, d, words[i + 0], 7, 0xd76aa478);
d = ff(d, a, b, c, words[i + 1], 12, 0xe8c7b756);
c = ff(c, d, a, b, words[i + 2], 17, 0x242070db);
b = ff(b, c, d, a, words[i + 3], 22, 0xc1bdceee);
a = ff(a, b, c, d, words[i + 4], 7, 0xf57c0faf);
d = ff(d, a, b, c, words[i + 5], 12, 0x4787c62a);
c = ff(c, d, a, b, words[i + 6], 17, 0xa8304613);
b = ff(b, c, d, a, words[i + 7], 22, 0xfd469501);
a = ff(a, b, c, d, words[i + 8], 7, 0x698098d8);
d = ff(d, a, b, c, words[i + 9], 12, 0x8b44f7af);
c = ff(c, d, a, b, words[i + 10], 17, 0xffff5bb1);
b = ff(b, c, d, a, words[i + 11], 22, 0x895cd7be);
a = ff(a, b, c, d, words[i + 12], 7, 0x6b901122);
d = ff(d, a, b, c, words[i + 13], 12, 0xfd987193);
c = ff(c, d, a, b, words[i + 14], 17, 0xa679438e);
b = ff(b, c, d, a, words[i + 15], 22, 0x49b40821);
for (let i = 0; i < words.length; i += 16) {
const oa = a;
const ob = b;
const oc = c;
const od = d;
a = gg(a, b, c, d, words[i + 1], 5, 0xf61e2562);
d = gg(d, a, b, c, words[i + 6], 9, 0xc040b340);
c = gg(c, d, a, b, words[i + 11], 14, 0x265e5a51);
b = gg(b, c, d, a, words[i + 0], 20, 0xe9b6c7aa);
a = gg(a, b, c, d, words[i + 5], 5, 0xd62f105d);
d = gg(d, a, b, c, words[i + 10], 9, 0x02441453);
c = gg(c, d, a, b, words[i + 15], 14, 0xd8a1e681);
b = gg(b, c, d, a, words[i + 4], 20, 0xe7d3fbc8);
a = gg(a, b, c, d, words[i + 9], 5, 0x21e1cde6);
d = gg(d, a, b, c, words[i + 14], 9, 0xc33707d6);
c = gg(c, d, a, b, words[i + 3], 14, 0xf4d50d87);
b = gg(b, c, d, a, words[i + 8], 20, 0x455a14ed);
a = gg(a, b, c, d, words[i + 13], 5, 0xa9e3e905);
d = gg(d, a, b, c, words[i + 2], 9, 0xfcefa3f8);
c = gg(c, d, a, b, words[i + 7], 14, 0x676f02d9);
b = gg(b, c, d, a, words[i + 12], 20, 0x8d2a4c8a);
a = ff(a, b, c, d, words[i + 0], 7, 0xd76aa478);
d = ff(d, a, b, c, words[i + 1], 12, 0xe8c7b756);
c = ff(c, d, a, b, words[i + 2], 17, 0x242070db);
b = ff(b, c, d, a, words[i + 3], 22, 0xc1bdceee);
a = ff(a, b, c, d, words[i + 4], 7, 0xf57c0faf);
d = ff(d, a, b, c, words[i + 5], 12, 0x4787c62a);
c = ff(c, d, a, b, words[i + 6], 17, 0xa8304613);
b = ff(b, c, d, a, words[i + 7], 22, 0xfd469501);
a = ff(a, b, c, d, words[i + 8], 7, 0x698098d8);
d = ff(d, a, b, c, words[i + 9], 12, 0x8b44f7af);
c = ff(c, d, a, b, words[i + 10], 17, 0xffff5bb1);
b = ff(b, c, d, a, words[i + 11], 22, 0x895cd7be);
a = ff(a, b, c, d, words[i + 12], 7, 0x6b901122);
d = ff(d, a, b, c, words[i + 13], 12, 0xfd987193);
c = ff(c, d, a, b, words[i + 14], 17, 0xa679438e);
b = ff(b, c, d, a, words[i + 15], 22, 0x49b40821);
a = hh(a, b, c, d, words[i + 5], 4, 0xfffa3942);
d = hh(d, a, b, c, words[i + 8], 11, 0x8771f681);
c = hh(c, d, a, b, words[i + 11], 16, 0x6d9d6122);
b = hh(b, c, d, a, words[i + 14], 23, 0xfde5380c);
a = hh(a, b, c, d, words[i + 1], 4, 0xa4beea44);
d = hh(d, a, b, c, words[i + 4], 11, 0x4bdecfa9);
c = hh(c, d, a, b, words[i + 7], 16, 0xf6bb4b60);
b = hh(b, c, d, a, words[i + 10], 23, 0xbebfbc70);
a = hh(a, b, c, d, words[i + 13], 4, 0x289b7ec6);
d = hh(d, a, b, c, words[i + 0], 11, 0xeaa127fa);
c = hh(c, d, a, b, words[i + 3], 16, 0xd4ef3085);
b = hh(b, c, d, a, words[i + 6], 23, 0x04881d05);
a = hh(a, b, c, d, words[i + 9], 4, 0xd9d4d039);
d = hh(d, a, b, c, words[i + 12], 11, 0xe6db99e5);
c = hh(c, d, a, b, words[i + 15], 16, 0x1fa27cf8);
b = hh(b, c, d, a, words[i + 2], 23, 0xc4ac5665);
a = gg(a, b, c, d, words[i + 1], 5, 0xf61e2562);
d = gg(d, a, b, c, words[i + 6], 9, 0xc040b340);
c = gg(c, d, a, b, words[i + 11], 14, 0x265e5a51);
b = gg(b, c, d, a, words[i + 0], 20, 0xe9b6c7aa);
a = gg(a, b, c, d, words[i + 5], 5, 0xd62f105d);
d = gg(d, a, b, c, words[i + 10], 9, 0x02441453);
c = gg(c, d, a, b, words[i + 15], 14, 0xd8a1e681);
b = gg(b, c, d, a, words[i + 4], 20, 0xe7d3fbc8);
a = gg(a, b, c, d, words[i + 9], 5, 0x21e1cde6);
d = gg(d, a, b, c, words[i + 14], 9, 0xc33707d6);
c = gg(c, d, a, b, words[i + 3], 14, 0xf4d50d87);
b = gg(b, c, d, a, words[i + 8], 20, 0x455a14ed);
a = gg(a, b, c, d, words[i + 13], 5, 0xa9e3e905);
d = gg(d, a, b, c, words[i + 2], 9, 0xfcefa3f8);
c = gg(c, d, a, b, words[i + 7], 14, 0x676f02d9);
b = gg(b, c, d, a, words[i + 12], 20, 0x8d2a4c8a);
a = ii(a, b, c, d, words[i + 0], 6, 0xf4292244);
d = ii(d, a, b, c, words[i + 7], 10, 0x432aff97);
c = ii(c, d, a, b, words[i + 14], 15, 0xab9423a7);
b = ii(b, c, d, a, words[i + 5], 21, 0xfc93a039);
a = ii(a, b, c, d, words[i + 12], 6, 0x655b59c3);
d = ii(d, a, b, c, words[i + 3], 10, 0x8f0ccc92);
c = ii(c, d, a, b, words[i + 10], 15, 0xffeff47d);
b = ii(b, c, d, a, words[i + 1], 21, 0x85845dd1);
a = ii(a, b, c, d, words[i + 8], 6, 0x6fa87e4f);
d = ii(d, a, b, c, words[i + 15], 10, 0xfe2ce6e0);
c = ii(c, d, a, b, words[i + 6], 15, 0xa3014314);
b = ii(b, c, d, a, words[i + 13], 21, 0x4e0811a1);
a = ii(a, b, c, d, words[i + 4], 6, 0xf7537e82);
d = ii(d, a, b, c, words[i + 11], 10, 0xbd3af235);
c = ii(c, d, a, b, words[i + 2], 15, 0x2ad7d2bb);
b = ii(b, c, d, a, words[i + 9], 21, 0xeb86d391);
a = hh(a, b, c, d, words[i + 5], 4, 0xfffa3942);
d = hh(d, a, b, c, words[i + 8], 11, 0x8771f681);
c = hh(c, d, a, b, words[i + 11], 16, 0x6d9d6122);
b = hh(b, c, d, a, words[i + 14], 23, 0xfde5380c);
a = hh(a, b, c, d, words[i + 1], 4, 0xa4beea44);
d = hh(d, a, b, c, words[i + 4], 11, 0x4bdecfa9);
c = hh(c, d, a, b, words[i + 7], 16, 0xf6bb4b60);
b = hh(b, c, d, a, words[i + 10], 23, 0xbebfbc70);
a = hh(a, b, c, d, words[i + 13], 4, 0x289b7ec6);
d = hh(d, a, b, c, words[i + 0], 11, 0xeaa127fa);
c = hh(c, d, a, b, words[i + 3], 16, 0xd4ef3085);
b = hh(b, c, d, a, words[i + 6], 23, 0x04881d05);
a = hh(a, b, c, d, words[i + 9], 4, 0xd9d4d039);
d = hh(d, a, b, c, words[i + 12], 11, 0xe6db99e5);
c = hh(c, d, a, b, words[i + 15], 16, 0x1fa27cf8);
b = hh(b, c, d, a, words[i + 2], 23, 0xc4ac5665);
a = add(a, oa);
b = add(b, ob);
c = add(c, oc);
d = add(d, od);
}
a = ii(a, b, c, d, words[i + 0], 6, 0xf4292244);
d = ii(d, a, b, c, words[i + 7], 10, 0x432aff97);
c = ii(c, d, a, b, words[i + 14], 15, 0xab9423a7);
b = ii(b, c, d, a, words[i + 5], 21, 0xfc93a039);
a = ii(a, b, c, d, words[i + 12], 6, 0x655b59c3);
d = ii(d, a, b, c, words[i + 3], 10, 0x8f0ccc92);
c = ii(c, d, a, b, words[i + 10], 15, 0xffeff47d);
b = ii(b, c, d, a, words[i + 1], 21, 0x85845dd1);
a = ii(a, b, c, d, words[i + 8], 6, 0x6fa87e4f);
d = ii(d, a, b, c, words[i + 15], 10, 0xfe2ce6e0);
c = ii(c, d, a, b, words[i + 6], 15, 0xa3014314);
b = ii(b, c, d, a, words[i + 13], 21, 0x4e0811a1);
a = ii(a, b, c, d, words[i + 4], 6, 0xf7537e82);
d = ii(d, a, b, c, words[i + 11], 10, 0xbd3af235);
c = ii(c, d, a, b, words[i + 2], 15, 0x2ad7d2bb);
b = ii(b, c, d, a, words[i + 9], 21, 0xeb86d391);
a = add(a, oa);
b = add(b, ob);
c = add(c, oc);
d = add(d, od);
}
return toHex(a) + toHex(b) + toHex(c) + toHex(d);
return toHex(a) + toHex(b) + toHex(c) + toHex(d);
}

View File

@@ -1,66 +1,66 @@
// Simple Fetch Wrapper
import { getTenantCode } from './tenant';
import { getTenantCode } from "./tenant";
export async function request(endpoint, options = {}) {
const tenantCode = getTenantCode();
if (!tenantCode) {
throw new Error('Tenant code missing in URL');
}
const baseUrl = `/t/${tenantCode}/v1`;
const token = localStorage.getItem('token');
const headers = {
'Content-Type': 'application/json',
...options.headers,
};
const tenantCode = getTenantCode();
if (!tenantCode) {
throw new Error("Tenant code missing in URL");
}
const baseUrl = `/t/${tenantCode}/v1`;
const token = localStorage.getItem("token");
if (token) {
headers['Authorization'] = `Bearer ${token}`;
const headers = {
"Content-Type": "application/json",
...options.headers,
};
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
if (options.body && !(options.body instanceof FormData)) {
options.body = JSON.stringify(options.body);
}
if (options.body instanceof FormData) {
delete headers["Content-Type"]; // Let browser set boundary
}
try {
const res = await fetch(`${baseUrl}${endpoint}`, {
...options,
headers,
});
const contentType = res.headers.get("content-type");
let data;
if (contentType && contentType.indexOf("application/json") !== -1) {
data = await res.json();
} else {
data = await res.text();
}
if (options.body && !(options.body instanceof FormData)) {
options.body = JSON.stringify(options.body);
}
if (options.body instanceof FormData) {
delete headers['Content-Type']; // Let browser set boundary
}
try {
const res = await fetch(`${baseUrl}${endpoint}`, {
...options,
headers
});
const contentType = res.headers.get("content-type");
let data;
if (contentType && contentType.indexOf("application/json") !== -1) {
data = await res.json();
} else {
data = await res.text();
if (!res.ok) {
// Handle 401 Unauthorized
if (res.status === 401) {
localStorage.removeItem("token");
localStorage.removeItem("user");
const loginPath = `/t/${tenantCode}/auth/login`;
// Redirect to login if not already there
if (!window.location.pathname.includes("/auth/login")) {
window.location.href = loginPath;
}
}
if (!res.ok) {
// Handle 401 Unauthorized
if (res.status === 401) {
localStorage.removeItem('token');
localStorage.removeItem('user');
const loginPath = `/t/${tenantCode}/auth/login`;
// Redirect to login if not already there
if (!window.location.pathname.includes('/auth/login')) {
window.location.href = loginPath;
}
}
// Handle errorx response { code, message, error_id }
const errorMsg = data.message || `Request failed with status ${res.status}`;
throw new Error(errorMsg);
}
return data;
} catch (err) {
console.error('API Request Error:', err);
throw err;
// Handle errorx response { code, message, error_id }
const errorMsg =
data.message || `Request failed with status ${res.status}`;
throw new Error(errorMsg);
}
return data;
} catch (err) {
console.error("API Request Error:", err);
throw err;
}
}

View File

@@ -1,18 +1,18 @@
export function getTenantCode() {
const match = window.location.pathname.match(/^\/t\/([^/]+)(?:\/|$)/);
return match ? match[1] : '';
const match = window.location.pathname.match(/^\/t\/([^/]+)(?:\/|$)/);
return match ? match[1] : "";
}
export function resolveTenantCode(route) {
if (route && route.params && route.params.tenantCode) {
return String(route.params.tenantCode);
}
return getTenantCode();
if (route && route.params && route.params.tenantCode) {
return String(route.params.tenantCode);
}
return getTenantCode();
}
export function tenantPath(path, route) {
const tenantCode = resolveTenantCode(route);
const base = tenantCode ? `/t/${tenantCode}` : '';
const normalized = path.startsWith('/') ? path : `/${path}`;
return `${base}${normalized}`;
const tenantCode = resolveTenantCode(route);
const base = tenantCode ? `/t/${tenantCode}` : "";
const normalized = path.startsWith("/") ? path : `/${path}`;
return `${base}${normalized}`;
}