index.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. import { Base64 } from "js-base64";
  2. import { positionTransform } from "./mt4";
  3. import { alert } from "@/helper/message";
  4. export const asyncTimeout = (mis: number = 0) => new Promise(resolve => setTimeout(resolve, mis))
  5. export const dateFormat = (date: Date, fmt: string) => {
  6. var o: any = {
  7. "M+": date.getMonth() + 1, //月份
  8. "d+": date.getDate(), //日
  9. "h+": date.getHours(), //小时
  10. "m+": date.getMinutes(), //分
  11. "s+": date.getSeconds(), //秒
  12. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  13. S: date.getMilliseconds(), //毫秒
  14. };
  15. if (/(y+)/.test(fmt)) {
  16. fmt = fmt.replace(
  17. RegExp.$1,
  18. (date.getFullYear() + "").substr(4 - RegExp.$1.length)
  19. );
  20. }
  21. for (var k in o) {
  22. if (new RegExp("(" + k + ")").test(fmt)) {
  23. fmt = fmt.replace(
  24. RegExp.$1,
  25. RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
  26. );
  27. }
  28. }
  29. return fmt;
  30. };
  31. export const copyText = (text: string) => {
  32. const input = document.createElement("input");
  33. document.body.appendChild(input);
  34. input.setAttribute("value", text);
  35. input.select();
  36. document.execCommand("copy");
  37. document.body.removeChild(input);
  38. };
  39. // 防抖
  40. export const debounce = <T extends (...args: any) => any>(
  41. fn: T,
  42. delay: number = 160
  43. ) => {
  44. let timeout: any;
  45. return function <This>(this: This, ...args: Parameters<T>) {
  46. clearTimeout(timeout);
  47. timeout = setTimeout(() => {
  48. fn.apply(this, args);
  49. }, delay);
  50. };
  51. };
  52. export const throttle = <Args extends any[]>(
  53. fn: (...args: Args) => void,
  54. deley: number
  55. ) => {
  56. let valib = false;
  57. let lastCtx: { self: any; args: Args } | null = null;
  58. return function (this: any, ...args: Args) {
  59. lastCtx = {
  60. args,
  61. self: this,
  62. };
  63. if (valib) {
  64. return;
  65. }
  66. const currentCtx = lastCtx;
  67. valib = true;
  68. setTimeout(() => {
  69. fn.apply(currentCtx.self, currentCtx.args);
  70. valib = false;
  71. }, deley);
  72. };
  73. };
  74. function randomWord(randomFlag: boolean, min: number, max?: number) {
  75. let str = "";
  76. let range = min;
  77. const arr = [
  78. "0",
  79. "1",
  80. "2",
  81. "3",
  82. "4",
  83. "5",
  84. "6",
  85. "7",
  86. "8",
  87. "9",
  88. "a",
  89. "b",
  90. "c",
  91. "d",
  92. "e",
  93. "f",
  94. "g",
  95. "h",
  96. "i",
  97. "j",
  98. "k",
  99. "l",
  100. "m",
  101. "n",
  102. "o",
  103. "p",
  104. "q",
  105. "r",
  106. "s",
  107. "t",
  108. "u",
  109. "v",
  110. "w",
  111. "x",
  112. "y",
  113. "z",
  114. "A",
  115. "B",
  116. "C",
  117. "D",
  118. "E",
  119. "F",
  120. "G",
  121. "H",
  122. "I",
  123. "J",
  124. "K",
  125. "L",
  126. "M",
  127. "N",
  128. "O",
  129. "P",
  130. "Q",
  131. "R",
  132. "S",
  133. "T",
  134. "U",
  135. "V",
  136. "W",
  137. "X",
  138. "Y",
  139. "Z",
  140. ];
  141. // 随机产生
  142. if (randomFlag && max) {
  143. range = Math.round(Math.random() * (max - min)) + min;
  144. }
  145. for (let i = 0; i < range; i++) {
  146. const pos = Math.round(Math.random() * (arr.length - 1));
  147. str += arr[pos];
  148. }
  149. return str;
  150. }
  151. /**
  152. * 密码加密
  153. * @param {String} pwd
  154. */
  155. export function encodePwd(str: string, strv: string): string[];
  156. export function encodePwd(str: string): string;
  157. export function encodePwd(str: string, strv = "") {
  158. str = Base64.encode(str);
  159. const NUM = 2;
  160. const front = randomWord(false, 8);
  161. const middle = randomWord(false, 8);
  162. const end = randomWord(false, 8);
  163. const str1 = str.substring(0, NUM);
  164. const str2 = str.substring(NUM);
  165. if (strv) {
  166. const strv1 = strv.substring(0, NUM);
  167. const strv2 = strv.substring(NUM);
  168. return [
  169. front + str2 + middle + str1 + end,
  170. front + strv2 + middle + strv1 + end,
  171. ];
  172. }
  173. return front + str2 + middle + str1 + end;
  174. }
  175. /**
  176. *获取id
  177. */
  178. export const guid = () => {
  179. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
  180. let r = (Math.random() * 16) | 0,
  181. v = c == "x" ? r : (r & 0x3) | 0x8;
  182. return v.toString(16);
  183. });
  184. };
  185. export const mix = (origin: any, clear = false, ...appends: any[]) => {
  186. if (clear) {
  187. for (const key in origin) {
  188. delete origin[key];
  189. }
  190. }
  191. for (const append of appends) {
  192. console.log(origin, append);
  193. for (const key in append) {
  194. origin[key] = append[key];
  195. }
  196. }
  197. return origin;
  198. };
  199. export const urlToBlob = async (url: string) => {
  200. try {
  201. const response = await fetch(url, { mode: 'no-cors' }); // 获取文件内容
  202. const blob = await response.blob(); // 转成 blob
  203. console.log('urlToBlob', blob);
  204. if(blob.type == 'text/html'){
  205. throw "暂未获取到平面图,请前往三维场景制作并保存";
  206. return
  207. }
  208. return blob;
  209. } catch (error) {
  210. // 请求出错,说明图片不存在
  211. alert("暂未获取到平面图,请前往三维场景制作并保存");
  212. }
  213. }
  214. export const base64ToBlob = (base64Data: string) => {
  215. let arr = base64Data.split(",");
  216. let matchs = arr[0].match(/:(.*?);/);
  217. if (!matchs) {
  218. return null;
  219. }
  220. let fileType = matchs[1];
  221. let bstr = atob(arr[1]),
  222. l = bstr.length,
  223. u8Arr = new Uint8Array(l);
  224. while (l--) {
  225. u8Arr[l] = bstr.charCodeAt(l);
  226. }
  227. return new Blob([u8Arr], {
  228. type: fileType,
  229. });
  230. };
  231. export const drawImage = (
  232. ctx: CanvasRenderingContext2D,
  233. bg_w: number,
  234. bg_h: number,
  235. imgPath: CanvasImageSource,
  236. imgWidth: number,
  237. imgHeight: number,
  238. x: number,
  239. y: number
  240. ) => {
  241. let dWidth = bg_w / imgWidth; // canvas与图片的宽度比例
  242. let dHeight = bg_h / imgHeight; // canvas与图片的高度比例
  243. if (
  244. (imgWidth > bg_w && imgHeight > bg_h) ||
  245. (imgWidth < bg_w && imgHeight < bg_h)
  246. ) {
  247. if (dWidth > dHeight) {
  248. ctx.drawImage(
  249. imgPath,
  250. 0,
  251. (imgHeight - bg_h / dWidth) / 2,
  252. imgWidth,
  253. bg_h / dWidth,
  254. x,
  255. y,
  256. bg_w,
  257. bg_h
  258. );
  259. } else {
  260. ctx.drawImage(
  261. imgPath,
  262. (imgWidth - bg_w / dHeight) / 2,
  263. 0,
  264. bg_w / dHeight,
  265. imgHeight,
  266. x,
  267. y,
  268. bg_w,
  269. bg_h
  270. );
  271. }
  272. } else {
  273. if (imgWidth < bg_w) {
  274. ctx.drawImage(
  275. imgPath,
  276. 0,
  277. (imgHeight - bg_h / dWidth) / 2,
  278. imgWidth,
  279. bg_h / dWidth,
  280. x,
  281. y,
  282. bg_w,
  283. bg_h
  284. );
  285. } else {
  286. ctx.drawImage(
  287. imgPath,
  288. (imgWidth - bg_w / dHeight) / 2,
  289. 0,
  290. bg_w / dHeight,
  291. imgHeight,
  292. x,
  293. y,
  294. bg_w,
  295. bg_h
  296. );
  297. }
  298. }
  299. };
  300. export const mixEnum = <T, K>(enum1: T, enum2: K): T | K => {
  301. return {
  302. ...enum1,
  303. ...enum2,
  304. };
  305. };
  306. // 字符串转params对象
  307. export const strToParams = (str: string) => {
  308. if (str[0] === "?") {
  309. str = str.substr(1);
  310. }
  311. const result: { [key: string]: string } = {};
  312. const splitRG = /([^=&]+)(?:=([^&]*))?&?/;
  313. let rgRet;
  314. while ((rgRet = str.match(splitRG))) {
  315. result[rgRet[1]] = rgRet[2] === undefined ? "" : rgRet[2];
  316. str = str.substr(rgRet[0].length);
  317. }
  318. return result;
  319. };
  320. export const getDomMatrix = (dom: HTMLElement) => {
  321. const str = getComputedStyle(dom, null).getPropertyValue("transform");
  322. const matrix2d = str
  323. .substring(7, str.length - 2)
  324. .split(", ")
  325. .map(Number);
  326. return [
  327. matrix2d[0],
  328. matrix2d[1],
  329. 0,
  330. 0,
  331. matrix2d[2],
  332. matrix2d[3],
  333. 0,
  334. 0,
  335. 0,
  336. 0,
  337. 1,
  338. 0,
  339. matrix2d[4] + dom.offsetWidth / 2,
  340. matrix2d[5] + dom.offsetHeight / 2,
  341. 0,
  342. 1,
  343. ];
  344. };
  345. export function copyTextToClipboard(input: string, { target = document.body }: Options = {}) {
  346. const element = document.createElement('textarea');
  347. const previouslyFocusedElement = document.activeElement;
  348. element.value = input;
  349. element.setAttribute('readonly', '');
  350. (element.style as any).contain = 'strict';
  351. element.style.position = 'absolute';
  352. element.style.left = '-9999px';
  353. element.style.fontSize = '12pt';
  354. const selection = document.getSelection();
  355. let originalRange;
  356. if (selection && selection.rangeCount > 0) {
  357. originalRange = selection.getRangeAt(0);
  358. }
  359. target.append(element);
  360. element.select();
  361. element.selectionStart = 0;
  362. element.selectionEnd = input.length;
  363. let isSuccess = false;
  364. try {
  365. isSuccess = document.execCommand('copy');
  366. } catch (e: any) {
  367. throw new Error(e);
  368. }
  369. element.remove();
  370. if (originalRange && selection) {
  371. selection.removeAllRanges();
  372. selection.addRange(originalRange);
  373. }
  374. if (previouslyFocusedElement) {
  375. (previouslyFocusedElement as HTMLElement).focus();
  376. }
  377. return isSuccess;
  378. }