index.ts 7.7 KB

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