vconsole.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* eslint-disable */
  2. /**
  3. * @description 打开 vConsole 面板
  4. * 支持url带参数唤起 vconsole=show
  5. * 测试环境下自动打开 vConsole 面板
  6. */
  7. import { getDevicePlatform, getEnv } from '@/utils';
  8. if (show()) {
  9. let storeList = [];
  10. let methodList = ['log', 'info', 'warn', 'debug', 'error'];
  11. methodList.forEach(function (item) {
  12. let method = console[item];
  13. console[item] = function () {
  14. storeList.push({
  15. logType: item,
  16. logs: arguments,
  17. });
  18. method.apply(console, arguments);
  19. };
  20. });
  21. loadScript('https://fastly.jsdelivr.net/npm/vconsole@3/dist/vconsole.min.js', function () {
  22. if (typeof vConsole === 'undefined') {
  23. // eslint-disable-next-line no-undef
  24. window.vConsole = new VConsole({
  25. maxLogNumber: 5000,
  26. });
  27. for (let i = 0; i < storeList.length; i++) {
  28. const item = storeList[i];
  29. window.vConsole.log.log(...item.logs);
  30. }
  31. }
  32. });
  33. }
  34. /**
  35. * 是否展示 vconsole
  36. */
  37. function show() {
  38. if (window.location.href.includes('vconsole=show')) {
  39. return true;
  40. }
  41. if (getEnv().ENV === 'test') {
  42. if (getDevicePlatform().isInWeChatDevTools && !getDevicePlatform().isInMiniProgram) {
  43. return false;
  44. }
  45. return true;
  46. }
  47. return false;
  48. }
  49. /**
  50. * 加载动态脚本
  51. * @param {*} src
  52. * @param {*} callback
  53. */
  54. function loadScript(src, callback) {
  55. let s;
  56. let t;
  57. s = document.createElement('script');
  58. s.type = 'text/javascript';
  59. s.src = src;
  60. t = document.getElementsByTagName('script')[0];
  61. t.parentNode.insertBefore(s, t);
  62. s.onload = function () {
  63. callback();
  64. };
  65. }