enter.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import type { TabCover } from "./store";
  2. import type { Scene } from "../../example/platform/platform-resource";
  3. import type { StoreData } from "@/core/store/store";
  4. import { token, params, urlUpdateQuery, urlGetQuery } from "../env";
  5. import { genLoading } from "../loadding";
  6. import { tempStrFill } from "@/utils/shared";
  7. import { ElMessage } from "element-plus";
  8. const SCENE_TYPE = {
  9. fuse: "fuse",
  10. mesh: "mesh",
  11. cloud: "cloud",
  12. } as const;
  13. const resourceURLS = {
  14. oss: import.meta.env.VITE_MESH_OSS,
  15. ossRoot: import.meta.env.VITE_OSS_ROOT,
  16. [SCENE_TYPE.mesh]: import.meta.env.VITE_MESH_API,
  17. [SCENE_TYPE.cloud]: import.meta.env.VITE_CLOUD_API,
  18. [SCENE_TYPE.fuse]: import.meta.env.VITE_FUSE_API,
  19. };
  20. const viewURLS = {
  21. [SCENE_TYPE.mesh]: import.meta.env.VITE_MESH_VIEW,
  22. [SCENE_TYPE.cloud]: import.meta.env.VITE_CLOUD_VIEW,
  23. [SCENE_TYPE.fuse]: import.meta.env.VITE_FUSE_VIEW,
  24. };
  25. const getHeaders = () => ({
  26. token: token.value || localStorage.getItem("token") || "",
  27. caseId: params.value.caseId || "",
  28. "page-type": "edit",
  29. });
  30. export const get = (url: string, params: Record<string, any>) => {
  31. const p = new URLSearchParams();
  32. for (const key in params) {
  33. p.append(key, params[key]);
  34. }
  35. const l = `${resourceURLS[SCENE_TYPE.fuse]}${url}?${p.toString()}`;
  36. return after(fetch(l, { method: "get", headers: getHeaders() }));
  37. };
  38. export const post = (url: string, data: Record<string, any>) => {
  39. const l = `${resourceURLS[SCENE_TYPE.fuse]}${url}`;
  40. return after(
  41. fetch(l, {
  42. headers: {
  43. "Content-Type": "application/json;charset=UTF-8",
  44. ...getHeaders(),
  45. },
  46. method: "post",
  47. body: JSON.stringify(data),
  48. })
  49. );
  50. };
  51. const postFile = (url: string, data: Record<string, any>) => {
  52. const formData = new FormData();
  53. for (const key in data) {
  54. formData.append(key, data[key]);
  55. }
  56. const l = `${resourceURLS[SCENE_TYPE.fuse]}${url}`;
  57. return after(
  58. fetch(l, {
  59. headers: getHeaders(),
  60. method: "post",
  61. body: formData,
  62. })
  63. );
  64. };
  65. let isLoging = false;
  66. const login = (isBack = true) => {
  67. if (isLoging) {
  68. throw "登录中";
  69. }
  70. isLoging = true;
  71. if (import.meta.env.DEV && params.value.caseId) {
  72. post("/service/manage/login", {
  73. password: "JwiuK95dExMjM0NTY=7nHGf5ySQWSuC4G1An",
  74. username: "super-admin",
  75. userName: "super-admin",
  76. }).then((res) => {
  77. params.value.token = res.token;
  78. console.error(res.token);
  79. // console.log(res.token, {...params.value})
  80. setTimeout(() => location.reload(), 1000);
  81. isLoging = false;
  82. });
  83. return;
  84. }
  85. if (import.meta.env.VITE_LOGIN_VIEW) {
  86. const p: any = { ...params.value };
  87. delete p.token;
  88. const cur = urlUpdateQuery(location.href, p, true);
  89. let link = tempStrFill(import.meta.env.VITE_LOGIN_VIEW, {
  90. redirect: escape(cur),
  91. });
  92. if (!isBack) {
  93. let url: URL;
  94. try {
  95. url = new URL(link);
  96. } catch {
  97. url = new URL(link, location.origin);
  98. }
  99. url.searchParams.delete("redirect");
  100. const query = urlGetQuery(url.toString());
  101. delete query["redirect"];
  102. link = urlUpdateQuery(url.toString(), query, true);
  103. }
  104. location.replace(link);
  105. isLoging = false;
  106. }
  107. };
  108. const after = async (fet: Promise<Response>) => {
  109. const res = await fet.then((res) => res.json());
  110. if (res.code === 8034) {
  111. setTimeout(() => {
  112. history.back()
  113. }, 1000)
  114. throw `${res.message},即将退出`;
  115. }
  116. if ([4008, 4010, 7012].includes(res.code)) {
  117. setTimeout(() => {
  118. window.platform.login(res.code !== 7012);
  119. }, 1000);
  120. throw res.message;
  121. } else if (res.code !== 0) {
  122. throw res.message;
  123. } else {
  124. return res.data;
  125. }
  126. };
  127. const getSceneList = genLoading(async (keyword: string): Promise<Scene[]> => {
  128. const list = await post(`fusion/case/sceneListPost`, {
  129. caseId: params.value.caseId,
  130. isMesh: 1,
  131. sceneName: keyword,
  132. });
  133. return list.map((item: any) => ({
  134. type: SCENE_TYPE.mesh,
  135. m: item.num,
  136. title: item.name,
  137. id: item.id.toString(),
  138. token,
  139. }));
  140. });
  141. const getOverviewData = genLoading(async (id: string) => {
  142. if (!id) {
  143. return {
  144. store: {
  145. layers: {
  146. default: {},
  147. },
  148. },
  149. viewport: null,
  150. };
  151. }
  152. const data = await get("fusion/caseOverview/info", { overviewId: id });
  153. return {
  154. ...data,
  155. store: JSON.parse(data.store) || {
  156. layers: {
  157. default: {},
  158. },
  159. },
  160. viewport: JSON.parse(data.viewport),
  161. };
  162. });
  163. const saveOverviewData = genLoading(
  164. async (
  165. id: string,
  166. data: {
  167. store: StoreData;
  168. viewport: number[] | null;
  169. caseTabulation: {
  170. id: string;
  171. store: StoreData;
  172. viewport: number[] | null;
  173. isAutoGen: boolean;
  174. cover: TabCover | null;
  175. paperKey?: string;
  176. overviewId: string;
  177. };
  178. }
  179. ) => {
  180. const item = await post(`fusion/caseOverview/addOrUpdate`, {
  181. ...params.value,
  182. ...data,
  183. id,
  184. store: JSON.stringify(data.store),
  185. viewport: JSON.stringify(data.viewport),
  186. caseTabulation: {
  187. ...data.caseTabulation,
  188. store: JSON.stringify(data.caseTabulation.store),
  189. viewport: JSON.stringify(data.caseTabulation.viewport),
  190. cover: JSON.stringify(data.caseTabulation.cover),
  191. isAutoGen: Number(data.caseTabulation.isAutoGen),
  192. paperKey: data.caseTabulation.paperKey,
  193. overviewId: data.caseTabulation.overviewId,
  194. },
  195. });
  196. return item.id;
  197. }
  198. );
  199. const getTabulationId = async (id: string) => {
  200. const list = await get("fusion/caseTabulation/getByOverviewId", {
  201. overviewId: id,
  202. });
  203. return list[0]?.id;
  204. };
  205. const getTabulationData = genLoading(async (id: string) => {
  206. if (!id) {
  207. return {
  208. store: {
  209. layers: {
  210. default: {},
  211. },
  212. },
  213. cover: null,
  214. isAutoGen: true,
  215. viewport: null,
  216. paperKey: "a4",
  217. };
  218. }
  219. const data = await get(`fusion/caseTabulation/info`, { tabulationId: id });
  220. return {
  221. ...data,
  222. store: JSON.parse(data.store) || {
  223. layers: {
  224. default: {},
  225. },
  226. },
  227. viewport: JSON.parse(data.viewport),
  228. cover: JSON.parse(data.cover),
  229. isAutoGen: Number(data.isAutoGen),
  230. paperKey: data.paperKey || "a4",
  231. };
  232. });
  233. setTimeout(() => {
  234. if (!params.value.caseId || !token) {
  235. ElMessage.error("当前项目号不存在!");
  236. window.platform.login(!!params.value.caseId);
  237. } else {
  238. getSceneList("");
  239. }
  240. }, 500);
  241. const saveTabulationData = genLoading(
  242. async (
  243. id: string,
  244. data: {
  245. store: StoreData;
  246. viewport: number[] | null;
  247. isAutoGen: boolean;
  248. cover: TabCover | null;
  249. paperKey?: string;
  250. overviewId: string;
  251. }
  252. ) => {
  253. const item = await post("fusion/caseTabulation/addOrUpdate", {
  254. ...params.value,
  255. ...data,
  256. id,
  257. store: JSON.stringify(data.store),
  258. viewport: JSON.stringify(data.viewport),
  259. cover: JSON.stringify(data.cover),
  260. isAutoGen: Number(data.isAutoGen),
  261. paperKey: data.paperKey,
  262. overviewId: data.overviewId,
  263. });
  264. return item.id;
  265. }
  266. );
  267. // caseId=6&overviewId=195
  268. const uploadResourse = genLoading(async (file: File) => {
  269. const url = await postFile(`fusion/upload/file`, { file });
  270. if (url.includes("//")) {
  271. return url;
  272. }
  273. if (import.meta.env.DEV && import.meta.env.VITE_STATIC) {
  274. return `${import.meta.env.VITE_STATIC}${url}`;
  275. } else {
  276. return url;
  277. }
  278. });
  279. const getTableTemp = () => {
  280. let table: Record<string, string>;
  281. let title: string = params.value.title;
  282. if (params.value.table) {
  283. try {
  284. table = JSON.parse(params.value.table);
  285. } catch (e) {
  286. console.log("tableTemp模板解析失败");
  287. }
  288. }
  289. if (!table!) {
  290. table = {
  291. 案发时间: "",
  292. 案发地点: "",
  293. 绘图单位: "",
  294. 绘图人: "",
  295. 绘图时间: "",
  296. };
  297. }
  298. if (!title) {
  299. title = "默认标题";
  300. }
  301. console.log(table, title)
  302. return { table, title };
  303. };
  304. window.platform = {
  305. login,
  306. resourceURLS,
  307. viewURLS,
  308. getOverviewData,
  309. getSceneList,
  310. saveOverviewData,
  311. getTabulationData,
  312. saveTabulationData,
  313. uploadResourse,
  314. getTabulationId,
  315. getTableTemp,
  316. };
  317. /* @vite-ignore */
  318. import(import.meta.env.VITE_ENTRY_EXAMPLE);