utils.js 7.5 KB

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