utils.js 4.69 KB
Newer Older
李纪文's avatar
李纪文 committed
1
import { authorizationToken } from '../apis';
李纪文's avatar
李纪文 committed
2 3
import sha1 from 'sha1';
import { sha256, sha224 } from 'js-sha256';
李纪文's avatar
李纪文 committed
4

5 6 7
// 判断是否是数字
export const isNumber = (val) => {
  const regPos = /^\d+(\.\d+)?$/; // 非负浮点数
8 9
  const regNeg =
    /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; // 负浮点数
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  return regPos.test(val) || regNeg.test(val);
};

// 创造guid
export const createGuid = () => {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
    .replace(/[xy]/g, (c) => {
      // eslint-disable-next-line no-bitwise
      const r = (Math.random() * 16) | 0;
      // eslint-disable-next-line no-bitwise
      const v = c === 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    })
    .toUpperCase();
};

// 深拷贝
export const deepCopy = (p, c) => {
  // eslint-disable-next-line no-param-reassign
  c = c || {};
  // eslint-disable-next-line no-restricted-syntax
  for (const i in p) {
    if (typeof p[i] === 'object') {
      // eslint-disable-next-line no-param-reassign
      c[i] = p[i].constructor === Array ? [] : {};
      deepCopy(p[i], c[i]);
    } else {
      // eslint-disable-next-line no-param-reassign
      c[i] = p[i];
    }
  }
  return c;
};

// rgba颜色转换
export const hexToRgba = (hex, opacity) => {
  // eslint-disable-next-line no-param-reassign
  hex = hex || '#808080';
  // eslint-disable-next-line no-param-reassign
  opacity = opacity !== undefined ? opacity : 1;
  // eslint-disable-next-line radix
  return `rgba(${parseInt(`0x${hex.slice(1, 3)}`)}, ${parseInt(`0x${hex.slice(3, 5)}`)}, ${parseInt(
    `0x${hex.slice(5, 7)}`,
  )}, ${opacity})`;
};

// 加法运算
export const addNumMethod = (num1, num2) => {
  let sq1;
  let sq2;
  let m;
  try {
    sq1 = num1.toString().split('.')[1].length;
  } catch (e) {
    sq1 = 0;
  }
  try {
    sq2 = num2.toString().split('.')[1].length;
  } catch (e) {
    sq2 = 0;
  }
  // eslint-disable-next-line prefer-const
  m = Math.max(sq1, sq2);
  return (num1 + num2).toFixed(m);
};

// 进制转换
export const hexSwitch = (num, m, n) => {
  const s = `${num}`;
  const result = parseInt(s, m).toString(n);
  return result;
};

// 文本样式
export const textStyle = () => {
  return {
    font: 'bold 11pt Helvetica, Arial, sans-serif',
    stroke: 'whitesmoke',
  };
};
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

// 菜单处理
export const querySkipUrl = (data, id) => {
  let res = null;
  for (let i = 0; i < data.length; i++) {
    if (data[i].widgetId === id) {
      res = data[i];
      return data[i];
    } else if (data[i].widgets && data[i].widgets.length > 0) {
      res = querySkipUrl(data[i].widgets, id);
    }
    if (res) break;
  }
  return res;
};
李纪文's avatar
李纪文 committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

// 判断是否是json
export const isJson = (text) => {
  if (
    /^[\],:{}\s]*$/.test(
      text
        .replace(/\\["\\/bfnrtu]/g, '@')
        .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?/g, ']')
        .replace(/(?:^|:|,)(?:\s*\[)+/g, ''),
    )
  ) {
    try {
      JSON.parse(text);
    } catch (e) {
      return false;
    }
    return true;
  } else {
    return false;
  }
};
李纪文's avatar
李纪文 committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

// 站点字母
export const stationData = [
  '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',
];
李纪文's avatar
李纪文 committed
156 157 158

export const getVideoUrl = () => {
  const protocol = window.location.protocol;
李纪文's avatar
李纪文 committed
159 160 161 162
  const address =
    protocol === 'https:'
      ? window.location.origin.replace(protocol, 'wss:')
      : window.location.origin.replace(protocol, 'ws:');
163
  return `${address}/`;
李纪文's avatar
李纪文 committed
164
};
李纪文's avatar
李纪文 committed
165 166 167 168

export const loginauthen = () => {
  if (!window.globalConfig?.hasGateWay) return false;
  authorizationToken({
169 170
    loginName: window.globalConfig.token || '',
    type: 'token',
李纪文's avatar
李纪文 committed
171 172 173 174 175 176 177 178 179
  })
    .then((tokenRes) => {
      if (tokenRes.code === 0) {
        window.globalConfig.access_token = tokenRes.data?.access_token ?? '';
        localStorage.setItem('access_token', tokenRes.data?.access_token ?? '');
      }
    })
    .catch((err) => {});
};
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

// 菜单查找Product
export const queryProduct = (data, url) => {
  let res = null;
  for (let i = 0; i < data.length; i++) {
    if (url.indexOf(data[i].url) > -1) {
      res = data[i];
      return data[i];
    } else if (data[i].widgets && data[i].widgets.length > 0) {
      res = queryProduct(data[i].widgets, url);
    }
    if (res) break;
  }
  return res;
};
李纪文's avatar
李纪文 committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

export const encipher = (password, type) => {
  let code = '';
  if (!password) return code;
  const wordtype = type || window?.globalConfig?.encrypt || 'SHA1';
  switch (wordtype) {
    case 'SHA1':
      code = sha1(password);
      break;
    case 'SHA256':
      code = sha256(password);
      break;
    default:
      code = sha1(password);
      break;
  }
  return code;
};