123
dengchaochao
2024-06-10 99f91d00f05d502bd51e2fbe16d2485610cd67f7
1
2
3
4
5
6
7
8
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
156
157
158
159
160
export const getImageUrl = (path) => {
  const modules = import.meta.glob("/src/assets/forexImages/**/*.png", {
    eager: true,
  });
  return modules[path].default;
};
 
export const getImages = (path) => {
  return new URL(`/src/assets/images/${path}`, import.meta.url).href;
};
 
export const handleSymbolImg = (name) => {
  if (!name) {
    return;
  }
 
  // usdthb
 
  let url;
  const newName = name.toLowerCase();
  if (
    newName === "usdt" ||
    newName === "usd" ||
    newName === "usdcusdt" ||
    newName === "usdthb" ||
    newName === "usdtwd"
  ) {
    url = "/symbol/" + newName + ".png"; //不用过滤
  } else {
    const regex = /(\/){0,1}usd(t){0,1}(\/){0,1}/i;
    const filterUSDName = newName.replace(regex, "");
    url = "/symbol/" + filterUSDName + ".png";
  }
  return new URL(url, import.meta.url).href;
};
 
// 设置localStorage
export const setStorage = function (key, obj) {
  let json = JSON.stringify(obj);
  window.localStorage.setItem(key, json);
};
 
// 获取localStorage
export const getStorage = function (key) {
  const str = window.localStorage.getItem(key);
  if (!str) {
    return null;
  }
  return JSON.parse(str);
};
 
// 移除
export const removeStorage = function (key) {
  window.localStorage.removeItem(key);
};
// 获取浏览器默认语言
export const getBrowserLang = function () {
  let browserLang = navigator.language
    ? navigator.language
    : navigator.browserLanguage;
 
  let defaultBrowserLang = "en";
  if (
    browserLang.toLowerCase() === "cn" ||
    browserLang.toLowerCase() === "zh" ||
    browserLang.toLowerCase() === "zh-cn"
  ) {
    defaultBrowserLang = "zh";
  }
  return defaultBrowserLang;
};
 
export const dataTime = (data, isTrue) => {
  var date = new Date(data);
  let Y = date.getFullYear() + "-";
  let M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  let D = date.getDate() + " ";
  let h = date.getHours() + ":";
  let m = date.getMinutes() + ":";
  let s = date.getSeconds();
  let str = Y + M + D;
  if (isTrue) {
    str = Y + M + D + h + m + s;
  } else {
    str = Y + M + D;
  }
  return str;
};
 
export const debounce = (fn, delay = 500) => {
  // timer 是在闭包中的
  let timer = null;
  return function () {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.apply(this, arguments);
      timer = null;
    }, delay);
  };
};
 
export function filterArrayEmpty(val) {
  let isEmpty = val.every((val) => {
    return val != "";
  });
  return isEmpty;
}
 
export function strFirstBit(val) {
  let arr = val.map((val, index, arr) => {
    return val.currency;
  });
  return arr;
}
 
export function addAndSubtr(val) {
  if (val >= 0) {
    return "+" + val + "%";
  } else if (val < 0) {
    return val + "%";
  } else {
    return "--";
  }
}
 
export function mergeSort(arr) {
  if (arr.length < 2) {
    return arr;
  }
  var middle = parseInt(arr.length / 2);
  var left = arr.slice(0, middle);
  var right = arr.slice(middle);
  return merge(mergeSort(left), mergeSort(right));
}
 
function merge(left, right) {
  var result = [];
  var i = 0,
    j = 0;
  while (i < left.length && j < right.length) {
    if (left[i].create_time_ts > right[j].create_time_ts) {
      result.push(right[j++]);
    } else {
      result.push(left[i++]);
    }
  }
  while (i < left.length) {
    result.push(left[i++]);
  }
  while (j < right.length) {
    result.push(right[j++]);
  }
 
  return result;
}