123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- import type { TabCover } from "./store";
- import type { Scene } from "../../example/platform/platform-resource";
- import type { StoreData } from "@/core/store/store";
- import { token, params, urlUpdateQuery, urlGetQuery } from "../env";
- import { genLoading } from "../loadding";
- import { tempStrFill } from "@/utils/shared";
- import { ElMessage } from "element-plus";
- const SCENE_TYPE = {
- fuse: "fuse",
- mesh: "mesh",
- cloud: "cloud",
- } as const;
- const resourceURLS = {
- oss: import.meta.env.VITE_MESH_OSS,
- ossRoot: import.meta.env.VITE_OSS_ROOT,
- [SCENE_TYPE.mesh]: import.meta.env.VITE_MESH_API,
- [SCENE_TYPE.cloud]: import.meta.env.VITE_CLOUD_API,
- [SCENE_TYPE.fuse]: import.meta.env.VITE_FUSE_API,
- };
- const viewURLS = {
- [SCENE_TYPE.mesh]: import.meta.env.VITE_MESH_VIEW,
- [SCENE_TYPE.cloud]: import.meta.env.VITE_CLOUD_VIEW,
- [SCENE_TYPE.fuse]: import.meta.env.VITE_FUSE_VIEW,
- };
- const getHeaders = () => ({
- token: token.value || localStorage.getItem("token") || "",
- caseId: params.value.caseId || "",
- "page-type": "edit",
- });
- export const get = (url: string, params: Record<string, any>) => {
- const p = new URLSearchParams();
- for (const key in params) {
- p.append(key, params[key]);
- }
- const l = `${resourceURLS[SCENE_TYPE.fuse]}${url}?${p.toString()}`;
- return after(fetch(l, { method: "get", headers: getHeaders() }));
- };
- export const post = (url: string, data: Record<string, any>) => {
- const l = `${resourceURLS[SCENE_TYPE.fuse]}${url}`;
- return after(
- fetch(l, {
- headers: {
- "Content-Type": "application/json;charset=UTF-8",
- ...getHeaders(),
- },
- method: "post",
- body: JSON.stringify(data),
- })
- );
- };
- const postFile = (url: string, data: Record<string, any>) => {
- const formData = new FormData();
- for (const key in data) {
- formData.append(key, data[key]);
- }
- const l = `${resourceURLS[SCENE_TYPE.fuse]}${url}`;
- return after(
- fetch(l, {
- headers: getHeaders(),
- method: "post",
- body: formData,
- })
- );
- };
- let isLoging = false;
- const login = (isBack = true) => {
- if (isLoging) {
- throw "登录中";
- }
- isLoging = true;
- if (import.meta.env.DEV && params.value.caseId) {
- post("/service/manage/login", {
- password: "JwiuK95dExMjM0NTY=7nHGf5ySQWSuC4G1An",
- username: "super-admin",
- userName: "super-admin",
- }).then((res) => {
- params.value.token = res.token;
- console.error(res.token);
- // console.log(res.token, {...params.value})
- setTimeout(() => location.reload(), 1000);
- isLoging = false;
- });
- return;
- }
- if (import.meta.env.VITE_LOGIN_VIEW) {
- const p: any = { ...params.value };
- delete p.token;
- const cur = urlUpdateQuery(location.href, p, true);
- let link = tempStrFill(import.meta.env.VITE_LOGIN_VIEW, {
- redirect: escape(cur),
- });
- if (!isBack) {
- let url: URL;
- try {
- url = new URL(link);
- } catch {
- url = new URL(link, location.origin);
- }
- url.searchParams.delete("redirect");
- const query = urlGetQuery(url.toString());
- delete query["redirect"];
- link = urlUpdateQuery(url.toString(), query, true);
- }
- location.replace(link);
- isLoging = false;
- }
- };
- const after = async (fet: Promise<Response>) => {
- const res = await fet.then((res) => res.json());
- if (res.code === 8034) {
- setTimeout(() => {
- history.back()
- }, 1000)
- throw `${res.message},即将退出`;
- }
- if ([4008, 4010, 7012].includes(res.code)) {
- setTimeout(() => {
- window.platform.login(res.code !== 7012);
- }, 1000);
- throw res.message;
- } else if (res.code !== 0) {
- throw res.message;
- } else {
- return res.data;
- }
- };
- const getSceneList = genLoading(async (keyword: string): Promise<Scene[]> => {
- const list = await post(`fusion/case/sceneListPost`, {
- caseId: params.value.caseId,
- isMesh: 1,
- sceneName: keyword,
- });
- return list.map((item: any) => ({
- type: SCENE_TYPE.mesh,
- m: item.num,
- title: item.name,
- id: item.id.toString(),
- token,
- }));
- });
- const getOverviewData = genLoading(async (id: string) => {
- if (!id) {
- return {
- store: {
- layers: {
- default: {},
- },
- },
- viewport: null,
- };
- }
- const data = await get("fusion/caseOverview/info", { overviewId: id });
- return {
- ...data,
- store: JSON.parse(data.store) || {
- layers: {
- default: {},
- },
- },
- viewport: JSON.parse(data.viewport),
- };
- });
- const saveOverviewData = genLoading(
- async (
- id: string,
- data: {
- store: StoreData;
- viewport: number[] | null;
- caseTabulation: {
- id: string;
- store: StoreData;
- viewport: number[] | null;
- isAutoGen: boolean;
- cover: TabCover | null;
- paperKey?: string;
- overviewId: string;
- };
- }
- ) => {
- const item = await post(`fusion/caseOverview/addOrUpdate`, {
- ...params.value,
- ...data,
- id,
- store: JSON.stringify(data.store),
- viewport: JSON.stringify(data.viewport),
- caseTabulation: {
- ...data.caseTabulation,
- store: JSON.stringify(data.caseTabulation.store),
- viewport: JSON.stringify(data.caseTabulation.viewport),
- cover: JSON.stringify(data.caseTabulation.cover),
- isAutoGen: Number(data.caseTabulation.isAutoGen),
- paperKey: data.caseTabulation.paperKey,
- overviewId: data.caseTabulation.overviewId,
- },
- });
- return item.id;
- }
- );
- const getTabulationId = async (id: string) => {
- const list = await get("fusion/caseTabulation/getByOverviewId", {
- overviewId: id,
- });
- return list[0]?.id;
- };
- const getTabulationData = genLoading(async (id: string) => {
- if (!id) {
- return {
- store: {
- layers: {
- default: {},
- },
- },
- cover: null,
- isAutoGen: true,
- viewport: null,
- paperKey: "a4",
- };
- }
- const data = await get(`fusion/caseTabulation/info`, { tabulationId: id });
- return {
- ...data,
- store: JSON.parse(data.store) || {
- layers: {
- default: {},
- },
- },
- viewport: JSON.parse(data.viewport),
- cover: JSON.parse(data.cover),
- isAutoGen: Number(data.isAutoGen),
- paperKey: data.paperKey || "a4",
- };
- });
- setTimeout(() => {
- if (!params.value.caseId || !token) {
- ElMessage.error("当前项目号不存在!");
- window.platform.login(!!params.value.caseId);
- } else {
- getSceneList("");
- }
- }, 500);
- const saveTabulationData = genLoading(
- async (
- id: string,
- data: {
- store: StoreData;
- viewport: number[] | null;
- isAutoGen: boolean;
- cover: TabCover | null;
- paperKey?: string;
- overviewId: string;
- }
- ) => {
- const item = await post("fusion/caseTabulation/addOrUpdate", {
- ...params.value,
- ...data,
- id,
- store: JSON.stringify(data.store),
- viewport: JSON.stringify(data.viewport),
- cover: JSON.stringify(data.cover),
- isAutoGen: Number(data.isAutoGen),
- paperKey: data.paperKey,
- overviewId: data.overviewId,
- });
- return item.id;
- }
- );
- // caseId=6&overviewId=195
- const uploadResourse = genLoading(async (file: File) => {
- const url = await postFile(`fusion/upload/file`, { file });
- if (url.includes("//")) {
- return url;
- }
- if (import.meta.env.DEV && import.meta.env.VITE_STATIC) {
- return `${import.meta.env.VITE_STATIC}${url}`;
- } else {
- return url;
- }
- });
- const getTableTemp = () => {
- let table: Record<string, string>;
- let title: string = params.value.title;
- if (params.value.table) {
- try {
- table = JSON.parse(params.value.table);
- } catch (e) {
- console.log("tableTemp模板解析失败");
- }
- }
- if (!table!) {
- table = {
- 案发时间: "",
- 案发地点: "",
- 绘图单位: "",
- 绘图人: "",
- 绘图时间: "",
- };
- }
- if (!title) {
- title = "默认标题";
- }
- console.log(table, title)
- return { table, title };
- };
- window.platform = {
- login,
- resourceURLS,
- viewURLS,
- getOverviewData,
- getSceneList,
- saveOverviewData,
- getTabulationData,
- saveTabulationData,
- uploadResourse,
- getTabulationId,
- getTableTemp,
- };
- /* @vite-ignore */
- import(import.meta.env.VITE_ENTRY_EXAMPLE);
|