utils.js 7.7 KB

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