util.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // 引用 https://web.tianyunperfect.cn/simple/js/util.js
  2. function lt(obj) {
  3. console.table(JSON.parse(JSON.stringify(obj)));
  4. }
  5. function deepClone(obj) {
  6. return JSON.parse(JSON.stringify(obj))
  7. }
  8. Date.prototype.format = function (fmt) {
  9. const o = {
  10. "M+": this.getMonth() + 1, //月份
  11. "d+": this.getDate(), //日
  12. "h+": this.getHours(), //小时
  13. "m+": this.getMinutes(), //分
  14. "s+": this.getSeconds(), //秒
  15. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  16. "S": this.getMilliseconds() //毫秒
  17. };
  18. if (/(y+)/.test(fmt)) {
  19. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  20. }
  21. for (const k in o) {
  22. if (new RegExp("(" + k + ")").test(fmt)) {
  23. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  24. }
  25. }
  26. return fmt;
  27. };
  28. // 添加响应拦截器
  29. axios.interceptors.response.use(function (response) {
  30. // 对响应数据做点什么
  31. return response.data;
  32. }, function (error) {
  33. // 对响应错误做点什么
  34. return Promise.reject(error);
  35. });
  36. // 提醒
  37. function my_tip(string) {
  38. const div = document.createElement("div");
  39. div.setAttribute("id", "my_alert_danger");
  40. div.setAttribute("style", "position: fixed; width: 30%; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 2000; background-color:green");
  41. div.innerHTML = string;
  42. document.body.prepend(div);
  43. setTimeout(() => {
  44. const element = document.getElementById("my_alert_danger");
  45. if (element) {
  46. element.parentNode.removeChild(element);
  47. }
  48. }, 2000)
  49. }
  50. // 发送同步请求 let a = sendSyncRequest("https://httpbin.tianyunperfect.cn/ip","GET",1)
  51. function sendSyncRequest(url, method, data) {
  52. const xhr = new XMLHttpRequest();
  53. xhr.open(method, url, false);
  54. if (method.match(/^(POST|PUT)$/i)) {
  55. xhr.setRequestHeader('Content-Type', 'application/json');
  56. xhr.send(JSON.stringify(data));
  57. } else {
  58. xhr.send();
  59. }
  60. return JSON.parse(xhr.responseText);
  61. }