237 lines
6.1 KiB
JavaScript
237 lines
6.1 KiB
JavaScript
const MENU_FILE = "./menu-seed.json";
|
|
const STORAGE_FILE = "./storage-seed.json";
|
|
|
|
let menuPromise = null;
|
|
let storagePromise = null;
|
|
|
|
self.addEventListener("install", (event) => {
|
|
self.skipWaiting();
|
|
event.waitUntil(Promise.all([loadMenu(), loadStorage()]));
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(self.clients.claim());
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const url = new URL(event.request.url);
|
|
if (!(url.pathname.startsWith("/hakocompany/") || url.pathname.startsWith("/company/") || url.hostname.includes("app-company-be.sqygj.cn"))) {
|
|
return;
|
|
}
|
|
event.respondWith(handleApiRequest(event.request));
|
|
});
|
|
|
|
async function loadMenu() {
|
|
if (menuPromise) {
|
|
return menuPromise;
|
|
}
|
|
menuPromise = fetch(MENU_FILE, { cache: "no-store" })
|
|
.then((response) => (response.ok ? response.json() : []))
|
|
.catch(() => []);
|
|
return menuPromise;
|
|
}
|
|
|
|
async function loadStorage() {
|
|
if (storagePromise) {
|
|
return storagePromise;
|
|
}
|
|
storagePromise = fetch(STORAGE_FILE, { cache: "no-store" })
|
|
.then((response) => (response.ok ? response.json() : {}))
|
|
.catch(() => ({}));
|
|
return storagePromise;
|
|
}
|
|
|
|
function ok(data) {
|
|
return {
|
|
data,
|
|
code: 0,
|
|
message: "mock success.",
|
|
ok: true
|
|
};
|
|
}
|
|
|
|
function paged(records = []) {
|
|
return ok({
|
|
rowsCount: records.length,
|
|
pageNumber: 1,
|
|
pageSize: 10,
|
|
pageCount: records.length ? 1 : 0,
|
|
records
|
|
});
|
|
}
|
|
|
|
function parseJsonMaybe(value, fallback = {}) {
|
|
try {
|
|
return JSON.parse(value || "{}");
|
|
} catch (_error) {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
async function handleApiRequest(request) {
|
|
if (request.method === "OPTIONS") {
|
|
return new Response("", {
|
|
status: 200,
|
|
headers: corsHeaders("text/plain;charset=utf-8")
|
|
});
|
|
}
|
|
|
|
const storage = await loadStorage();
|
|
const menu = await loadMenu();
|
|
const companyInfo = parseJsonMaybe(storage.companyInfo);
|
|
const userInfo = parseJsonMaybe(storage.userInfo);
|
|
const boardInfo = parseJsonMaybe(storage.boardInfo);
|
|
const memberInfo = parseJsonMaybe(storage.memberInfor);
|
|
|
|
const url = new URL(request.url);
|
|
const path = url.pathname;
|
|
|
|
if (path.includes("/hakocompany/account/")) {
|
|
return json(ok({
|
|
companyAuthApplyOrderRespDTOS: [companyInfo],
|
|
menuSelectAllResponseDTOS: menu,
|
|
companyAuthApplyOrderRespDTO: companyInfo
|
|
}));
|
|
}
|
|
|
|
if (path.includes("/company/member/findById")) {
|
|
return json(ok(memberInfo));
|
|
}
|
|
|
|
if (path.includes("/company/organizationalStructure/statusBoard")) {
|
|
return json(ok({
|
|
login: boardInfo.login || 1,
|
|
org: boardInfo.org || 1,
|
|
company: boardInfo.company || 1,
|
|
activation: boardInfo.activation || 1,
|
|
cockpit: boardInfo.cockpit || 1,
|
|
projectCount: boardInfo.projectCount || 94,
|
|
houseCount: boardInfo.houseCount || 4686,
|
|
floorageCount: boardInfo.floorageCount || 2888556.18
|
|
}));
|
|
}
|
|
|
|
if (path.includes("/company/organizationalStructure/dueNum")) {
|
|
return json(ok({
|
|
projectGoodsDueNum: 3,
|
|
smsSurplusNum: 11042,
|
|
contractDueNum: 1,
|
|
memberCertificateDueNum: 0
|
|
}));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/RightsProject/expireSchemeDetail")) {
|
|
return json(ok({
|
|
projectGoodsDueNum: 3,
|
|
smsSurplusNum: 11042,
|
|
contractDueNum: 1,
|
|
memberCertificateDueNum: 0
|
|
}));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/PlatformMessage/noticePageList")) {
|
|
return json(paged([
|
|
{ id: 1, title: "企业平台通知", createTime: "2026-04-01 22:00:00", content: "这是企业服务平台的本地 mock 通知。" }
|
|
]));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/PlatformMessage/checkMessage")) {
|
|
return json(ok(false));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/AgentConfig/getAgentUrl")) {
|
|
return json(ok("https://example.com/mock-etms-agent"));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/SatisfactionEvaluate/checkEvaluateTime")) {
|
|
return json(ok(false));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/OperationalGuidance/pageList")) {
|
|
return json(paged([
|
|
{ id: 1, title: "合同管理", tag: "操作指引" },
|
|
{ id: 2, title: "事务报表", tag: "操作指引" },
|
|
{ id: 3, title: "财务—常规配置", tag: "操作指引" }
|
|
]));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/TrainingInfo/pageList")) {
|
|
return json(paged([]));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/Company/") || path.includes("/company/Company/")) {
|
|
return json(ok(companyInfo));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/FsSubject/")) {
|
|
return json(ok([]));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/OrderContract/pageList")) {
|
|
return json(paged([]));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/WeightConfig") || path.includes("/hakocompany/Satisfaction")) {
|
|
return json(paged([]));
|
|
}
|
|
|
|
if (path.includes("/hakocompany/Board") || path.includes("/hakocompany/Cockpit")) {
|
|
return json(ok(boardInfo));
|
|
}
|
|
|
|
if (path.includes("/pageList")) {
|
|
return json(paged([]));
|
|
}
|
|
|
|
if (path.includes("/listBy") || path.includes("/selectAll") || path.endsWith("/list")) {
|
|
return json(ok([]));
|
|
}
|
|
|
|
if (
|
|
path.includes("/create") ||
|
|
path.includes("/update") ||
|
|
path.includes("/delete") ||
|
|
path.includes("/save") ||
|
|
path.includes("/edit") ||
|
|
path.includes("/sync") ||
|
|
path.includes("/batch")
|
|
) {
|
|
return json(ok(true));
|
|
}
|
|
|
|
if (path.includes("/findBy") || path.includes("/detail")) {
|
|
return json(ok({
|
|
companyInfo,
|
|
userInfo,
|
|
memberInfo,
|
|
boardInfo
|
|
}));
|
|
}
|
|
|
|
if (path.includes("/report") || path.includes("/statistics") || path.includes("/count") || path.includes("/total")) {
|
|
return json(ok({
|
|
projectCount: boardInfo.projectCount || 0,
|
|
houseCount: boardInfo.houseCount || 0,
|
|
floorageCount: boardInfo.floorageCount || 0
|
|
}));
|
|
}
|
|
|
|
return json(ok({}));
|
|
}
|
|
|
|
function json(payload) {
|
|
return new Response(JSON.stringify(payload), {
|
|
status: 200,
|
|
headers: corsHeaders("application/json;charset=utf-8")
|
|
});
|
|
}
|
|
|
|
function corsHeaders(contentType) {
|
|
return {
|
|
"Content-Type": contentType,
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Methods": "GET,POST,PUT,PATCH,DELETE,OPTIONS",
|
|
"Access-Control-Allow-Headers": "*"
|
|
};
|
|
}
|