utils.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. class HttpUtil {
  2. static _handleMsg(msg) {
  3. if (!(msg instanceof Msg)) {
  4. return;
  5. }
  6. if (msg.msg === "") {
  7. return;
  8. }
  9. if (msg.success) {
  10. Vue.prototype.$message.success(msg.msg);
  11. } else {
  12. Vue.prototype.$message.error(msg.msg);
  13. }
  14. }
  15. static _respToMsg(resp) {
  16. const data = resp.data;
  17. if (data == null) {
  18. return new Msg(true);
  19. } else if (typeof data === 'object') {
  20. if (data.hasOwnProperty('success')) {
  21. return new Msg(data.success, data.msg, data.obj);
  22. } else {
  23. return data;
  24. }
  25. } else {
  26. return new Msg(false, 'unknown data:', data);
  27. }
  28. }
  29. static async get(url, data, options) {
  30. let msg;
  31. try {
  32. const resp = await axios.get(url, data, options);
  33. msg = this._respToMsg(resp);
  34. } catch (e) {
  35. msg = new Msg(false, e.toString());
  36. }
  37. this._handleMsg(msg);
  38. return msg;
  39. }
  40. static async post(url, data, options) {
  41. let msg;
  42. try {
  43. const resp = await axios.post(url, data, options);
  44. msg = this._respToMsg(resp);
  45. } catch (e) {
  46. msg = new Msg(false, e.toString());
  47. }
  48. this._handleMsg(msg);
  49. return msg;
  50. }
  51. static async postWithModal(url, data, modal) {
  52. if (modal) {
  53. modal.loading(true);
  54. }
  55. const msg = await this.post(url, data);
  56. if (modal) {
  57. modal.loading(false);
  58. if (msg instanceof Msg && msg.success) {
  59. modal.close();
  60. }
  61. }
  62. return msg;
  63. }
  64. }
  65. class PromiseUtil {
  66. static async sleep(timeout) {
  67. await new Promise(resolve => {
  68. setTimeout(resolve, timeout)
  69. });
  70. }
  71. }
  72. const seq = [
  73. 'a', 'b', 'c', 'd', 'e', 'f', 'g',
  74. 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  75. 'o', 'p', 'q', 'r', 's', 't',
  76. 'u', 'v', 'w', 'x', 'y', 'z',
  77. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  78. 'A', 'B', 'C', 'D', 'E', 'F', 'G',
  79. 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  80. 'O', 'P', 'Q', 'R', 'S', 'T',
  81. 'U', 'V', 'W', 'X', 'Y', 'Z'
  82. ];
  83. const shortIdSeq = [
  84. 'a', 'b', 'c', 'd', 'e', 'f',
  85. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  86. ];
  87. class RandomUtil {
  88. static randomIntRange(min, max) {
  89. return parseInt(Math.random() * (max - min) + min, 10);
  90. }
  91. static randomInt(n) {
  92. return this.randomIntRange(0, n);
  93. }
  94. static randomSeq(count) {
  95. let str = '';
  96. for (let i = 0; i < count; ++i) {
  97. str += seq[this.randomInt(62)];
  98. }
  99. return str;
  100. }
  101. static randomShortIdSeq(count) {
  102. let str = '';
  103. for (let i = 0; i < count; ++i) {
  104. str += shortIdSeq[this.randomInt(16)];
  105. }
  106. return str;
  107. }
  108. static randomLowerAndNum(count) {
  109. let str = '';
  110. for (let i = 0; i < count; ++i) {
  111. str += seq[this.randomInt(36)];
  112. }
  113. return str;
  114. }
  115. static randomMTSecret() {
  116. let str = '';
  117. for (let i = 0; i < 32; ++i) {
  118. let index = this.randomInt(16);
  119. if (index <= 9) {
  120. str += index;
  121. } else {
  122. str += seq[index - 10];
  123. }
  124. }
  125. return str;
  126. }
  127. static randomUUID() {
  128. let d = new Date().getTime();
  129. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  130. let r = (d + Math.random() * 16) % 16 | 0;
  131. d = Math.floor(d / 16);
  132. return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16);
  133. });
  134. }
  135. static randomText() {
  136. var chars = 'abcdefghijklmnopqrstuvwxyz1234567890';
  137. var string = '';
  138. var len = 6 + Math.floor(Math.random() * 5);
  139. for (var ii = 0; ii < len; ii++) {
  140. string += chars[Math.floor(Math.random() * chars.length)];
  141. }
  142. return string;
  143. }
  144. static randowShortId() {
  145. let str = '';
  146. str += this.randomShortIdSeq(8);
  147. return str;
  148. }
  149. static randomShadowsocksPassword() {
  150. let array = new Uint8Array(32);
  151. window.crypto.getRandomValues(array);
  152. return btoa(String.fromCharCode.apply(null, array));
  153. }
  154. }
  155. class ObjectUtil {
  156. static getPropIgnoreCase(obj, prop) {
  157. for (const name in obj) {
  158. if (!obj.hasOwnProperty(name)) {
  159. continue;
  160. }
  161. if (name.toLowerCase() === prop.toLowerCase()) {
  162. return obj[name];
  163. }
  164. }
  165. return undefined;
  166. }
  167. static deepSearch(obj, key) {
  168. if (obj instanceof Array) {
  169. for (let i = 0; i < obj.length; ++i) {
  170. if (this.deepSearch(obj[i], key)) {
  171. return true;
  172. }
  173. }
  174. } else if (obj instanceof Object) {
  175. for (let name in obj) {
  176. if (!obj.hasOwnProperty(name)) {
  177. continue;
  178. }
  179. if (this.deepSearch(obj[name], key)) {
  180. return true;
  181. }
  182. }
  183. } else {
  184. return obj.toString().toLowerCase().indexOf(key.toLowerCase()) >= 0;
  185. }
  186. return false;
  187. }
  188. static isEmpty(obj) {
  189. return obj === null || obj === undefined || obj === '';
  190. }
  191. static isArrEmpty(arr) {
  192. return !this.isEmpty(arr) && arr.length === 0;
  193. }
  194. static copyArr(dest, src) {
  195. dest.splice(0);
  196. for (const item of src) {
  197. dest.push(item);
  198. }
  199. }
  200. static clone(obj) {
  201. let newObj;
  202. if (obj instanceof Array) {
  203. newObj = [];
  204. this.copyArr(newObj, obj);
  205. } else if (obj instanceof Object) {
  206. newObj = {};
  207. for (const key of Object.keys(obj)) {
  208. newObj[key] = obj[key];
  209. }
  210. } else {
  211. newObj = obj;
  212. }
  213. return newObj;
  214. }
  215. static deepClone(obj) {
  216. let newObj;
  217. if (obj instanceof Array) {
  218. newObj = [];
  219. for (const item of obj) {
  220. newObj.push(this.deepClone(item));
  221. }
  222. } else if (obj instanceof Object) {
  223. newObj = {};
  224. for (const key of Object.keys(obj)) {
  225. newObj[key] = this.deepClone(obj[key]);
  226. }
  227. } else {
  228. newObj = obj;
  229. }
  230. return newObj;
  231. }
  232. static cloneProps(dest, src, ...ignoreProps) {
  233. if (dest == null || src == null) {
  234. return;
  235. }
  236. const ignoreEmpty = this.isArrEmpty(ignoreProps);
  237. for (const key of Object.keys(src)) {
  238. if (!src.hasOwnProperty(key)) {
  239. continue;
  240. } else if (!dest.hasOwnProperty(key)) {
  241. continue;
  242. } else if (src[key] === undefined) {
  243. continue;
  244. }
  245. if (ignoreEmpty) {
  246. dest[key] = src[key];
  247. } else {
  248. let ignore = false;
  249. for (let i = 0; i < ignoreProps.length; ++i) {
  250. if (key === ignoreProps[i]) {
  251. ignore = true;
  252. break;
  253. }
  254. }
  255. if (!ignore) {
  256. dest[key] = src[key];
  257. }
  258. }
  259. }
  260. }
  261. static delProps(obj, ...props) {
  262. for (const prop of props) {
  263. if (prop in obj) {
  264. delete obj[prop];
  265. }
  266. }
  267. }
  268. static execute(func, ...args) {
  269. if (!this.isEmpty(func) && typeof func === 'function') {
  270. func(...args);
  271. }
  272. }
  273. static orDefault(obj, defaultValue) {
  274. if (obj == null) {
  275. return defaultValue;
  276. }
  277. return obj;
  278. }
  279. static equals(a, b) {
  280. for (const key in a) {
  281. if (!a.hasOwnProperty(key)) {
  282. continue;
  283. }
  284. if (!b.hasOwnProperty(key)) {
  285. return false;
  286. } else if (a[key] !== b[key]) {
  287. return false;
  288. }
  289. }
  290. return true;
  291. }
  292. }