utils.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
  73. class RandomUtil {
  74. static randomIntRange(min, max) {
  75. return Math.floor(Math.random() * (max - min) + min);
  76. }
  77. static randomInt(n) {
  78. return this.randomIntRange(0, n);
  79. }
  80. static randomSeq(count) {
  81. let str = '';
  82. for (let i = 0; i < count; ++i) {
  83. str += seq[this.randomInt(62)];
  84. }
  85. return str;
  86. }
  87. static randomShortId(count) {
  88. let str = '';
  89. for (let i = 0; i < count; ++i) {
  90. str += seq[this.randomInt(16)];
  91. }
  92. return str;
  93. }
  94. static randomText(len) {
  95. let str = '';
  96. for (let i = 0; i < len; i++) {
  97. str += seq[this.randomInt(36)];
  98. }
  99. return str;
  100. }
  101. static randomMTSecret() {
  102. let str = '';
  103. for (let i = 0; i < 32; ++i) {
  104. let index = this.randomInt(16);
  105. if (index <= 9) {
  106. str += index;
  107. } else {
  108. str += seq[index - 10];
  109. }
  110. }
  111. return str;
  112. }
  113. static randomUUID() {
  114. let d = new Date().getTime();
  115. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  116. let r = (d + Math.random() * 16) % 16 | 0;
  117. d = Math.floor(d / 16);
  118. return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16);
  119. });
  120. }
  121. static randomShadowsocksPassword() {
  122. let array = new Uint8Array(32);
  123. window.crypto.getRandomValues(array);
  124. return btoa(String.fromCharCode.apply(null, array));
  125. }
  126. }
  127. class ObjectUtil {
  128. static getPropIgnoreCase(obj, prop) {
  129. for (const name in obj) {
  130. if (!obj.hasOwnProperty(name)) {
  131. continue;
  132. }
  133. if (name.toLowerCase() === prop.toLowerCase()) {
  134. return obj[name];
  135. }
  136. }
  137. return undefined;
  138. }
  139. static deepSearch(obj, key) {
  140. if (obj instanceof Array) {
  141. for (let i = 0; i < obj.length; ++i) {
  142. if (this.deepSearch(obj[i], key)) {
  143. return true;
  144. }
  145. }
  146. } else if (obj instanceof Object) {
  147. for (let name in obj) {
  148. if (!obj.hasOwnProperty(name)) {
  149. continue;
  150. }
  151. if (this.deepSearch(obj[name], key)) {
  152. return true;
  153. }
  154. }
  155. } else {
  156. return obj.toString().toLowerCase().indexOf(key.toLowerCase()) >= 0;
  157. }
  158. return false;
  159. }
  160. static isEmpty(obj) {
  161. return obj === null || obj === undefined || obj === '';
  162. }
  163. static isArrEmpty(arr) {
  164. return !this.isEmpty(arr) && arr.length === 0;
  165. }
  166. static copyArr(dest, src) {
  167. dest.splice(0);
  168. for (const item of src) {
  169. dest.push(item);
  170. }
  171. }
  172. static clone(obj) {
  173. let newObj;
  174. if (obj instanceof Array) {
  175. newObj = [];
  176. this.copyArr(newObj, obj);
  177. } else if (obj instanceof Object) {
  178. newObj = {};
  179. for (const key of Object.keys(obj)) {
  180. newObj[key] = obj[key];
  181. }
  182. } else {
  183. newObj = obj;
  184. }
  185. return newObj;
  186. }
  187. static deepClone(obj) {
  188. let newObj;
  189. if (obj instanceof Array) {
  190. newObj = [];
  191. for (const item of obj) {
  192. newObj.push(this.deepClone(item));
  193. }
  194. } else if (obj instanceof Object) {
  195. newObj = {};
  196. for (const key of Object.keys(obj)) {
  197. newObj[key] = this.deepClone(obj[key]);
  198. }
  199. } else {
  200. newObj = obj;
  201. }
  202. return newObj;
  203. }
  204. static cloneProps(dest, src, ...ignoreProps) {
  205. if (dest == null || src == null) {
  206. return;
  207. }
  208. const ignoreEmpty = this.isArrEmpty(ignoreProps);
  209. for (const key of Object.keys(src)) {
  210. if (!src.hasOwnProperty(key)) {
  211. continue;
  212. } else if (!dest.hasOwnProperty(key)) {
  213. continue;
  214. } else if (src[key] === undefined) {
  215. continue;
  216. }
  217. if (ignoreEmpty) {
  218. dest[key] = src[key];
  219. } else {
  220. let ignore = false;
  221. for (let i = 0; i < ignoreProps.length; ++i) {
  222. if (key === ignoreProps[i]) {
  223. ignore = true;
  224. break;
  225. }
  226. }
  227. if (!ignore) {
  228. dest[key] = src[key];
  229. }
  230. }
  231. }
  232. }
  233. static delProps(obj, ...props) {
  234. for (const prop of props) {
  235. if (prop in obj) {
  236. delete obj[prop];
  237. }
  238. }
  239. }
  240. static execute(func, ...args) {
  241. if (!this.isEmpty(func) && typeof func === 'function') {
  242. func(...args);
  243. }
  244. }
  245. static orDefault(obj, defaultValue) {
  246. if (obj == null) {
  247. return defaultValue;
  248. }
  249. return obj;
  250. }
  251. static equals(a, b) {
  252. for (const key in a) {
  253. if (!a.hasOwnProperty(key)) {
  254. continue;
  255. }
  256. if (!b.hasOwnProperty(key)) {
  257. return false;
  258. } else if (a[key] !== b[key]) {
  259. return false;
  260. }
  261. }
  262. return true;
  263. }
  264. }