utils.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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() {
  88. let str = '';
  89. for (let i = 0; i < 8; ++i) {
  90. str += seq[this.randomInt(16)];
  91. }
  92. return str;
  93. }
  94. static randomLowerAndNum(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 randomUUID() {
  102. const template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
  103. return template.replace(/[xy]/g, function (c) {
  104. const randomValues = new Uint8Array(1);
  105. crypto.getRandomValues(randomValues);
  106. let randomValue = randomValues[0] % 16;
  107. let calculatedValue = (c === 'x') ? randomValue : (randomValue & 0x3 | 0x8);
  108. return calculatedValue.toString(16);
  109. });
  110. }
  111. static randomShadowsocksPassword() {
  112. let array = new Uint8Array(32);
  113. window.crypto.getRandomValues(array);
  114. return btoa(String.fromCharCode.apply(null, array));
  115. }
  116. }
  117. class ObjectUtil {
  118. static getPropIgnoreCase(obj, prop) {
  119. for (const name in obj) {
  120. if (!obj.hasOwnProperty(name)) {
  121. continue;
  122. }
  123. if (name.toLowerCase() === prop.toLowerCase()) {
  124. return obj[name];
  125. }
  126. }
  127. return undefined;
  128. }
  129. static deepSearch(obj, key) {
  130. if (obj instanceof Array) {
  131. for (let i = 0; i < obj.length; ++i) {
  132. if (this.deepSearch(obj[i], key)) {
  133. return true;
  134. }
  135. }
  136. } else if (obj instanceof Object) {
  137. for (let name in obj) {
  138. if (!obj.hasOwnProperty(name)) {
  139. continue;
  140. }
  141. if (this.deepSearch(obj[name], key)) {
  142. return true;
  143. }
  144. }
  145. } else {
  146. return obj.toString().toLowerCase().indexOf(key.toLowerCase()) >= 0;
  147. }
  148. return false;
  149. }
  150. static isEmpty(obj) {
  151. return obj === null || obj === undefined || obj === '';
  152. }
  153. static isArrEmpty(arr) {
  154. return !this.isEmpty(arr) && arr.length === 0;
  155. }
  156. static copyArr(dest, src) {
  157. dest.splice(0);
  158. for (const item of src) {
  159. dest.push(item);
  160. }
  161. }
  162. static clone(obj) {
  163. let newObj;
  164. if (obj instanceof Array) {
  165. newObj = [];
  166. this.copyArr(newObj, obj);
  167. } else if (obj instanceof Object) {
  168. newObj = {};
  169. for (const key of Object.keys(obj)) {
  170. newObj[key] = obj[key];
  171. }
  172. } else {
  173. newObj = obj;
  174. }
  175. return newObj;
  176. }
  177. static deepClone(obj) {
  178. let newObj;
  179. if (obj instanceof Array) {
  180. newObj = [];
  181. for (const item of obj) {
  182. newObj.push(this.deepClone(item));
  183. }
  184. } else if (obj instanceof Object) {
  185. newObj = {};
  186. for (const key of Object.keys(obj)) {
  187. newObj[key] = this.deepClone(obj[key]);
  188. }
  189. } else {
  190. newObj = obj;
  191. }
  192. return newObj;
  193. }
  194. static cloneProps(dest, src, ...ignoreProps) {
  195. if (dest == null || src == null) {
  196. return;
  197. }
  198. const ignoreEmpty = this.isArrEmpty(ignoreProps);
  199. for (const key of Object.keys(src)) {
  200. if (!src.hasOwnProperty(key)) {
  201. continue;
  202. } else if (!dest.hasOwnProperty(key)) {
  203. continue;
  204. } else if (src[key] === undefined) {
  205. continue;
  206. }
  207. if (ignoreEmpty) {
  208. dest[key] = src[key];
  209. } else {
  210. let ignore = false;
  211. for (let i = 0; i < ignoreProps.length; ++i) {
  212. if (key === ignoreProps[i]) {
  213. ignore = true;
  214. break;
  215. }
  216. }
  217. if (!ignore) {
  218. dest[key] = src[key];
  219. }
  220. }
  221. }
  222. }
  223. static delProps(obj, ...props) {
  224. for (const prop of props) {
  225. if (prop in obj) {
  226. delete obj[prop];
  227. }
  228. }
  229. }
  230. static execute(func, ...args) {
  231. if (!this.isEmpty(func) && typeof func === 'function') {
  232. func(...args);
  233. }
  234. }
  235. static orDefault(obj, defaultValue) {
  236. if (obj == null) {
  237. return defaultValue;
  238. }
  239. return obj;
  240. }
  241. static equals(a, b) {
  242. for (const key in a) {
  243. if (!a.hasOwnProperty(key)) {
  244. continue;
  245. }
  246. if (!b.hasOwnProperty(key)) {
  247. return false;
  248. } else if (a[key] !== b[key]) {
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. }