123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- import { Base64 } from "js-base64";
- import { positionTransform } from "./mt4";
- import { alert } from "@/helper/message";
- export const asyncTimeout = (mis: number = 0) => new Promise(resolve => setTimeout(resolve, mis))
- export const dateFormat = (date: Date, fmt: string) => {
- var o: any = {
- "M+": date.getMonth() + 1, //月份
- "d+": date.getDate(), //日
- "h+": date.getHours(), //小时
- "m+": date.getMinutes(), //分
- "s+": date.getSeconds(), //秒
- "q+": Math.floor((date.getMonth() + 3) / 3), //季度
- S: date.getMilliseconds(), //毫秒
- };
- if (/(y+)/.test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- (date.getFullYear() + "").substr(4 - RegExp.$1.length)
- );
- }
- for (var k in o) {
- if (new RegExp("(" + k + ")").test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
- );
- }
- }
- return fmt;
- };
- export const copyText = (text: string) => {
- const input = document.createElement("input");
- document.body.appendChild(input);
- input.setAttribute("value", text);
- input.select();
- document.execCommand("copy");
- document.body.removeChild(input);
- };
- // 防抖
- export const debounce = <T extends (...args: any) => any>(
- fn: T,
- delay: number = 160
- ) => {
- let timeout: any;
- return function <This>(this: This, ...args: Parameters<T>) {
- clearTimeout(timeout);
- timeout = setTimeout(() => {
- fn.apply(this, args);
- }, delay);
- };
- };
- export const throttle = <Args extends any[]>(
- fn: (...args: Args) => void,
- deley: number
- ) => {
- let valib = false;
- let lastCtx: { self: any; args: Args } | null = null;
- return function (this: any, ...args: Args) {
- lastCtx = {
- args,
- self: this,
- };
- if (valib) {
- return;
- }
- const currentCtx = lastCtx;
- valib = true;
- setTimeout(() => {
- fn.apply(currentCtx.self, currentCtx.args);
- valib = false;
- }, deley);
- };
- };
- function randomWord(randomFlag: boolean, min: number, max?: number) {
- let str = "";
- let range = min;
- const arr = [
- "0",
- "1",
- "2",
- "3",
- "4",
- "5",
- "6",
- "7",
- "8",
- "9",
- "a",
- "b",
- "c",
- "d",
- "e",
- "f",
- "g",
- "h",
- "i",
- "j",
- "k",
- "l",
- "m",
- "n",
- "o",
- "p",
- "q",
- "r",
- "s",
- "t",
- "u",
- "v",
- "w",
- "x",
- "y",
- "z",
- "A",
- "B",
- "C",
- "D",
- "E",
- "F",
- "G",
- "H",
- "I",
- "J",
- "K",
- "L",
- "M",
- "N",
- "O",
- "P",
- "Q",
- "R",
- "S",
- "T",
- "U",
- "V",
- "W",
- "X",
- "Y",
- "Z",
- ];
- // 随机产生
- if (randomFlag && max) {
- range = Math.round(Math.random() * (max - min)) + min;
- }
- for (let i = 0; i < range; i++) {
- const pos = Math.round(Math.random() * (arr.length - 1));
- str += arr[pos];
- }
- return str;
- }
- /**
- * 密码加密
- * @param {String} pwd
- */
- export function encodePwd(str: string, strv: string): string[];
- export function encodePwd(str: string): string;
- export function encodePwd(str: string, strv = "") {
- str = Base64.encode(str);
- const NUM = 2;
- const front = randomWord(false, 8);
- const middle = randomWord(false, 8);
- const end = randomWord(false, 8);
- const str1 = str.substring(0, NUM);
- const str2 = str.substring(NUM);
- if (strv) {
- const strv1 = strv.substring(0, NUM);
- const strv2 = strv.substring(NUM);
- return [
- front + str2 + middle + str1 + end,
- front + strv2 + middle + strv1 + end,
- ];
- }
- return front + str2 + middle + str1 + end;
- }
- /**
- *获取id
- */
- export const guid = () => {
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
- let r = (Math.random() * 16) | 0,
- v = c == "x" ? r : (r & 0x3) | 0x8;
- return v.toString(16);
- });
- };
- export const mix = (origin: any, clear = false, ...appends: any[]) => {
- if (clear) {
- for (const key in origin) {
- delete origin[key];
- }
- }
- for (const append of appends) {
- console.log(origin, append);
- for (const key in append) {
- origin[key] = append[key];
- }
- }
- return origin;
- };
- export const urlToBlob = async (url: string) => {
- try {
- const response = await fetch(url, { mode: 'no-cors' }); // 获取文件内容
- const blob = await response.blob(); // 转成 blob
- console.log('urlToBlob', blob);
- if(blob.type == 'text/html'){
- throw "暂未获取到平面图,请前往三维场景制作并保存";
- return
- }
- return blob;
- } catch (error) {
- // 请求出错,说明图片不存在
- alert("暂未获取到平面图,请前往三维场景制作并保存");
- }
- }
- export const base64ToBlob = (base64Data: string) => {
- let arr = base64Data.split(",");
- let matchs = arr[0].match(/:(.*?);/);
- if (!matchs) {
- return null;
- }
- let fileType = matchs[1];
- let bstr = atob(arr[1]),
- l = bstr.length,
- u8Arr = new Uint8Array(l);
- while (l--) {
- u8Arr[l] = bstr.charCodeAt(l);
- }
- return new Blob([u8Arr], {
- type: fileType,
- });
- };
- export const drawImage = (
- ctx: CanvasRenderingContext2D,
- bg_w: number,
- bg_h: number,
- imgPath: CanvasImageSource,
- imgWidth: number,
- imgHeight: number,
- x: number,
- y: number
- ) => {
- let dWidth = bg_w / imgWidth; // canvas与图片的宽度比例
- let dHeight = bg_h / imgHeight; // canvas与图片的高度比例
- if (
- (imgWidth > bg_w && imgHeight > bg_h) ||
- (imgWidth < bg_w && imgHeight < bg_h)
- ) {
- if (dWidth > dHeight) {
- ctx.drawImage(
- imgPath,
- 0,
- (imgHeight - bg_h / dWidth) / 2,
- imgWidth,
- bg_h / dWidth,
- x,
- y,
- bg_w,
- bg_h
- );
- } else {
- ctx.drawImage(
- imgPath,
- (imgWidth - bg_w / dHeight) / 2,
- 0,
- bg_w / dHeight,
- imgHeight,
- x,
- y,
- bg_w,
- bg_h
- );
- }
- } else {
- if (imgWidth < bg_w) {
- ctx.drawImage(
- imgPath,
- 0,
- (imgHeight - bg_h / dWidth) / 2,
- imgWidth,
- bg_h / dWidth,
- x,
- y,
- bg_w,
- bg_h
- );
- } else {
- ctx.drawImage(
- imgPath,
- (imgWidth - bg_w / dHeight) / 2,
- 0,
- bg_w / dHeight,
- imgHeight,
- x,
- y,
- bg_w,
- bg_h
- );
- }
- }
- };
- export const mixEnum = <T, K>(enum1: T, enum2: K): T | K => {
- return {
- ...enum1,
- ...enum2,
- };
- };
- // 字符串转params对象
- export const strToParams = (str: string) => {
- if (str[0] === "?") {
- str = str.substr(1);
- }
- const result: { [key: string]: string } = {};
- const splitRG = /([^=&]+)(?:=([^&]*))?&?/;
- let rgRet;
- while ((rgRet = str.match(splitRG))) {
- result[rgRet[1]] = rgRet[2] === undefined ? "" : rgRet[2];
- str = str.substr(rgRet[0].length);
- }
- return result;
- };
- export const getDomMatrix = (dom: HTMLElement) => {
- const str = getComputedStyle(dom, null).getPropertyValue("transform");
- const matrix2d = str
- .substring(7, str.length - 2)
- .split(", ")
- .map(Number);
- return [
- matrix2d[0],
- matrix2d[1],
- 0,
- 0,
- matrix2d[2],
- matrix2d[3],
- 0,
- 0,
- 0,
- 0,
- 1,
- 0,
- matrix2d[4] + dom.offsetWidth / 2,
- matrix2d[5] + dom.offsetHeight / 2,
- 0,
- 1,
- ];
- };
- export function copyTextToClipboard(input: string, { target = document.body }: Options = {}) {
- const element = document.createElement('textarea');
- const previouslyFocusedElement = document.activeElement;
- element.value = input;
- element.setAttribute('readonly', '');
- (element.style as any).contain = 'strict';
- element.style.position = 'absolute';
- element.style.left = '-9999px';
- element.style.fontSize = '12pt';
- const selection = document.getSelection();
- let originalRange;
- if (selection && selection.rangeCount > 0) {
- originalRange = selection.getRangeAt(0);
- }
- target.append(element);
- element.select();
- element.selectionStart = 0;
- element.selectionEnd = input.length;
- let isSuccess = false;
- try {
- isSuccess = document.execCommand('copy');
- } catch (e: any) {
- throw new Error(e);
- }
- element.remove();
- if (originalRange && selection) {
- selection.removeAllRanges();
- selection.addRange(originalRange);
- }
- if (previouslyFocusedElement) {
- (previouslyFocusedElement as HTMLElement).focus();
- }
- return isSuccess;
- }
|