index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import drawView from "./core/libs/drawView";
  2. import logReport from "./core/libs/logReport";
  3. import errorReport from "./core/libs/errorReport";
  4. import devOptions from "./core/libs/devOptions";
  5. import createH5Bubble from "./core/libs/createH5Bubble";
  6. import vueMixin from "./core/proxy/vueMixin";
  7. import devToolsProxyInstall from "./core/proxy/index";
  8. import pageLinkList from "./core/libs/pageLinkList";
  9. /**
  10. * @type {Vue}
  11. */
  12. let that;
  13. const devTools = {
  14. options: null,
  15. /**
  16. * 挂载安装APP页面
  17. */
  18. install(vm, options) {
  19. try {
  20. that = vm;
  21. let _this = this;
  22. if (vm && vm.config && vm.config.globalProperties) {
  23. vm.config.globalProperties.$logReport = logReport;
  24. } else {
  25. vm.prototype.$logReport = logReport;
  26. }
  27. //! 初始化配置项
  28. devOptions.setOptions(options)
  29. options = devOptions.getOptions()
  30. _this.options = options;
  31. if (!options || !options.status) {
  32. return console.log("%c devTools 调试工具未运行!", 'padding: 4px;background-color: red;color: #fff;font-size: 15px;');
  33. }
  34. //! 挂载dev工具
  35. if (vm && vm.config && vm.config.globalProperties) {
  36. vm.config.globalProperties.$devTools = devTools;
  37. } else {
  38. vm.prototype.$devTools = devTools;
  39. }
  40. if (options.error.status) {
  41. //! 挂载vue报错
  42. vm.config.errorHandler = (err, vm, trace) => {
  43. errorReport(err, trace, "ve")
  44. };
  45. //! 挂载vue警告
  46. vm.config.warnHandler = (err, vm, trace) => {
  47. errorReport(err, trace, "vw")
  48. }
  49. }
  50. //!混入生命周期监听器
  51. vm.mixin(vueMixin)
  52. //!绘制环境变量小标签
  53. // #ifdef APP-PLUS
  54. drawView(options, devTools)
  55. // #endif
  56. // #ifdef H5
  57. createH5Bubble(options, devTools)
  58. // #endif
  59. //!调试工具全局拦截器挂载
  60. devToolsProxyInstall(options)
  61. //! 注册dev弹窗打开事件
  62. uni.$on("devTools_showDialog", () => {
  63. _this.show()
  64. })
  65. //! 注册dev弹窗关闭事件
  66. uni.$on("devTools_closeDialog", (options) => {
  67. _this.hide(options)
  68. })
  69. //! 挂载uni对象
  70. uni.$dev = {
  71. show() {
  72. _this.show()
  73. },
  74. hide() {
  75. _this.hide()
  76. },
  77. errorReport,
  78. logReport,
  79. }
  80. //! 注册jsRunner执行事件
  81. uni.$on("devTools_jsRunner", (code) => {
  82. let result = undefined;
  83. try {
  84. let fun = (("ev" + "__混淆__" + "al").replace("__混淆__", ""));
  85. result = globalThis[fun](code);
  86. // result = eval(code);
  87. } catch (error) {
  88. if (error && error.message) {
  89. result = error.message;
  90. }
  91. }
  92. uni.$emit("devTools_jsRunnerCallback", result)
  93. })
  94. // ! 页面路由列表
  95. pageLinkList.install()
  96. } catch (error) {
  97. console.log("devTools install error", error);
  98. }
  99. },
  100. /**
  101. * 打开调试弹窗
  102. */
  103. show() {
  104. let pages = getCurrentPages();
  105. //! 已经打开了调试工具,不要重复显示
  106. if (pages[pages.length - 1].route == this.options.devRoute) {
  107. return false;
  108. }
  109. uni.navigateTo({
  110. url: this.options.route,
  111. animationType: 'none',
  112. animationDuration: 0,
  113. })
  114. },
  115. /**
  116. * 隐藏调试弹窗
  117. */
  118. hide(options) {
  119. // #ifdef APP-PLUS
  120. uni.$emit("devTools_closeDevToolsPanel")
  121. let isBack = false;
  122. uni.$once("devTools_panelHideSuccess", () => {
  123. if (!isBack) {
  124. isBack = true;
  125. uni.navigateBack();
  126. }
  127. })
  128. setTimeout(() => {
  129. if (!isBack) {
  130. isBack = true;
  131. uni.navigateBack();
  132. }
  133. }, 500);
  134. // #endif
  135. // #ifndef APP-PLUS
  136. uni.navigateBack()
  137. // #endif
  138. if (options && options.navigateToUrl) {
  139. let t = 600;
  140. // #ifndef APP-PLUS
  141. t = 200;
  142. // #endif
  143. setTimeout(() => {
  144. uni.navigateTo({
  145. url: options.navigateToUrl,
  146. })
  147. }, t);
  148. }
  149. },
  150. errorReport,
  151. logReport,
  152. }
  153. export default devTools;