setting.vue 19.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
<template>
  <view class="settingView">
    <view
      v-if="loading"
      class="loading"
    >
      <text class="loadingText">加载中</text>
    </view>
    <template v-else>
      <subTitleBar
        :isOpen="exportIsShow"
        @click="exportIsShow = !exportIsShow"
        title="导出全部日志"
      />
      <template v-if="exportIsShow">
        <view
          class="delBtn"
          @click="exportJsonFile"
        >
          <text class="delBtnText">导出日志文件(.json)</text>
        </view>
      </template>
      <view class="divisionLine"></view>

      <subTitleBar
        :isOpen="cacheListIsShow"
        title="清空全部缓存"
        @click="cacheListIsShow = !cacheListIsShow"
      />
      <template v-if="cacheListIsShow">
        <view
          v-for="(item, index) in cacheSelectList"
          :key="index"
          @click.stop="doSelectCache(index)"
          class="checkboxItem"
        >
          <checkbox
            :value="item.check ? '1' : '0'"
            :checked="item.check"
            color="#ff2d55"
          />
          <text
            class="name"
            :style="{
              color: item.count ? '#333' : '#888',
            }"
          >
            {{ item.name }}
          </text>

          <text v-if="item.key == 'file'"></text>
          <text
            v-else-if="item.count"
            class="count"
          >
            ({{ item.count }})
          </text>
          <text
            v-else
            class="empty"
          >
            (空)
          </text>
        </view>
        <view
          class="delBtn"
          @click="delCache"
        >
          <text class="delBtnText">清空选中</text>
        </view>
      </template>
      <view class="divisionLine"></view>

      <subTitleBar
        :isOpen="configIsShow"
        title="DevTools当前配置参数"
        @click="configIsShow = !configIsShow"
      />
      <view
        v-if="configIsShow"
        class="objectAnalysisView"
      >
        <objectAnalysis
          :isOpenFirst="true"
          :data="config"
          :width="710"
        />
      </view>
      <view class="divisionLine"></view>

      <subTitleBar
        :showArrow="false"
        title="关于"
      />

      <view class="about">
        <view>
          <text class="row">Copyright©2024 福州重塑网络科技有限公司 前端团队</text>
        </view>
        <view
          @click="goUrl('https://dev.api0.cn')"
          style="display: flex; flex-direction: row"
        >
          <text class="row">在线文档:</text>
          <text
            class="row"
            style="color: #ff2d55"
            >https://dev.api0.cn</text
          >
        </view>
        <view>
          <text class="row">当前版本:v{{ config.version }}</text>
        </view>
      </view>

      <view style="height: 100rpx"></view>
    </template>
  </view>
</template>
<script>
import devCache from "../../../core/libs/devCache";
import devOptions from "../../../core/libs/devOptions";
import jsonCompress from "../../../core/libs/jsonCompress";
import appDelDir from "../libs/appDelDir";
import subTitleBar from "../ui/subTitleBar.vue";
import objectAnalysis from "./objectAnalysis.vue";
import getRuntimeInfo from "../libs/getRuntimeInfo";
export default {
  components: {
    subTitleBar,
    objectAnalysis,
  },
  data() {
    return {
      /**
       * 是否加载中
       */
      loading: false,
      /**
       * 缓存列表是否展示
       */
      cacheListIsShow: false,
      /**
       * 缓存列表
       */
      cacheSelectList: [],
      /**
       * 配置文件是否显示
       */
      configIsShow: false,
      /**
       * 当前配置
       */
      config: devOptions.getOptions(),
      /**
       * 是否显示导出日志按钮
       */
      exportIsShow: false,
    };
  },
  methods: {
    /**
     * 加载页面
     */
    async getPage() {
      let that = this;
      that.loading = true;
      that.cacheSelectList = await that.countCache();
      that.loading = false;
    },
    /**
     * 统计缓存信息
     */
    countCache() {
      let that = this;
      return new Promise(async (yes) => {
        let cacheSelectList = [];

        // dev 工具日志
        let keys = {
          errorReport: "Error错误日志",
          console: "Console打印日志",
          request: "Request请求日志",
          logReport: "Logs日志",
          uniBus: "UniBus函数日志",
        };
        Object.keys(keys).map((key) => {
          let logs = devCache.get(key);
          cacheSelectList.push({
            name: keys[key],
            check: logs.length > 0,
            count: logs.length,
            key,
          });
        });
        // #ifdef H5
        let indexDBList = await this.getIndexDBList();
        let cookieLength = document.cookie.split(";").length;
        if (document.cookie == "") {
          cookieLength = 0;
        }
        // #endif

        cacheSelectList = cacheSelectList.concat([
          that.countStorageCache(),
          // #ifdef H5
          {
            key: "sessionStorage",
            name: "SessionStorage临时缓存",
            check: sessionStorage.length > 0,
            count: sessionStorage.length,
          },
          // #endif
          // #ifdef APP-PLUS
          {
            key: "file",
            name: "FileSys本地文件(_doc)",
            check: false,
            count: "未知 // TODO",
          },
          // #endif
          // #ifdef MP-WEIXIN
          {
            name: "FileSys本地文件(FileSystemManager)",
            check: false,
            key: "file",
            count: "未知 // TODO",
          },
          // #endif
          {
            key: "pageCount",
            name: "Pages页面停留统计",
            check: devCache.get("pageCount").length > 0,
            count: devCache.get("pageCount").length,
          },
          {
            key: "dayOnline",
            name: "Pages日活时间统计",
            check: devCache.get("dayOnline").length > 0,
            count: devCache.get("dayOnline").length,
          },
          // #ifdef H5
          {
            key: "cookie",
            name: "Cookie",
            check: cookieLength > 0,
            count: cookieLength,
          },
          {
            key: "IndexDB",
            name: "IndexDB",
            check: indexDBList.length > 0,
            count: indexDBList.length,
          },
          // #endif
        ]);

        yes(cacheSelectList);
      });
    },
    /**
     * 统计本地缓存
     */
    countStorageCache() {
      let n = 0;
      // #ifdef APP-PLUS
      let keys = plus.storage.getAllKeys();
      for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        if (key.indexOf("devTools_") == 0) {
          // 忽略以 devTools_ 开头的key
          continue;
        }
        n++;
      }
      // #endif
      // #ifdef H5
      for (let i = 0; i < localStorage.length; i++) {
        let key = localStorage.key(i);
        if (key.indexOf("devTools_") == 0) {
          continue;
        }
        n++;
      }
      // #endif
      // #ifdef MP
      let keyList = devCache.get("storage");
      if (!keyList) keyList = [];
      for (let i = 0; i < keyList.length; i++) {
        const key = keyList[i];
        if (key.indexOf("devTools_") == 0) {
          continue;
        }
        n++;
      }
      // #endif
      return {
        key: "localStorage",
        name: "localStorage本地缓存",
        check: n > 0,
        count: n,
      };
    },
    /**
     * 获取indexDB列表
     */
    getIndexDBList() {
      return new Promise((yes) => {
        try {
          indexedDB.databases().then((list) => {
            yes(list);
          });
        } catch (error) {
          console.log("getIndexDBList error", error);
          yes([]);
        }
      });
    },
    /**
     * 选择清空的缓存项目
     */
    doSelectCache(index) {
      this.cacheSelectList[index].check = !this.cacheSelectList[index].check;
    },
    /**
     * 清空缓存
     */
    delCache() {
      let that = this;

      let selectedKey = [];
      that.cacheSelectList.map((item) => {
        if (item.check) {
          selectedKey.push(item.key);
        }
      });

      let keyDelFun = {
        errorReport() {
          devCache.set("errorReport", []);
        },
        console() {
          uni.$emit("devTools_delConsoleAll");
        },
        request() {
          uni.$emit("devTools_delNetworkAll");
        },
        logReport() {
          devCache.set("logReport", []);
        },
        uniBus() {
          uni.$emit("devTools_delUniBusAll");
        },
        localStorage() {
          // #ifdef APP-PLUS
          let keys = plus.storage.getAllKeys();
          for (let i = 0; i < keys.length; i++) {
            const key = String(keys[i]);
            if (key.indexOf("devTools_") == 0) {
              continue;
            }
            uni.removeStorageSync(key);
          }
          // #endif

          // #ifdef H5
          for (let i = 0; i < localStorage.length; i++) {
            let key = String(localStorage.key(i));
            if (key.indexOf("devTools_") == 0) {
              continue;
            }
            setTimeout(() => {
              localStorage.removeItem(key);
            }, i * 2 + 1);
          }
          // #endif

          // #ifdef MP
          let keyList = devCache.get("storage");
          if (!keyList) keyList = [];
          for (let i = 0; i < keyList.length; i++) {
            const key = keyList[i];
            if (key.indexOf("devTools_") == 0) {
              continue;
            }
            uni.removeStorageSync(key);
          }
          // #endif
        },
        sessionStorage() {
          for (let i = 0; i < sessionStorage.length; i++) {
            let key = String(sessionStorage.key(i));
            if (key.indexOf("devTools_") == 0) {
              continue;
            }
            sessionStorage.removeItem(key);
          }
        },
        file() {
          // #ifdef APP-PLUS
          appDelDir("_doc/");
          // #endif
          // #ifdef MP-WEIXIN
          let fs = wx.getFileSystemManager();
          fs.rmdir({
            dirPath: wx.env.USER_DATA_PATH + "/",
            recursive: true,
          });
          // #endif
        },
        pageCount() {
          devCache.set("pageCount", []);
        },
        dayOnline() {
          devCache.set("dayOnline", []);
        },
        cookie() {
          let keys = [];
          document.cookie.split(";").forEach((cookieStr) => {
            const [name, value] = cookieStr.trim().split("=");
            keys.push(name);
          });
          keys.map((k) => {
            document.cookie = `${k}=;expires=` + new Date(new Date().getTime() + 200).toGMTString() + ";path=/";
          });
        },
        IndexDB() {
          indexedDB.databases().then((list) => {
            list.map((item) => {
              indexedDB.deleteDatabase(item.name);
            });
          });
        },
      };

      if (selectedKey.length == 0) {
        return uni.showToast({
          title: "请先勾选需要清空的项目!",
          icon: "none",
        });
      }
      uni.showLoading({
        title: "清空中...",
        mask: true,
      });

      setTimeout(() => {
        uni.hideLoading();
        uni.showToast({
          title: "清空成功!",
          icon: "success",
        });
        that.getPage();
      }, 5100);

      selectedKey.map((key) => {
        keyDelFun[key]();
      });
    },
    /**
     * 导出日志文件到json
     */
    async exportJsonFile() {
      let that = this;

      // #ifdef MP
      if (1) {
        uni.showToast({
          title: "小程序平台不支持导出日志,建议直接上传至服务器!",
          icon: "none",
        });
        return;
      }
      // #endif

      uni.showLoading({
        title: "打包中...",
      });

      try {
        let devOp = devOptions.getOptions();
        let waitExportObject = {
          exportOptions: {
            version: devOp.version,
            config: devOp,
            exportTime: new Date().getTime(),
            // #ifdef APP-PLUS
            platform: "app",
            // #endif
            // #ifdef H5
            platform: "h5",
            // #endif
            // #ifdef MP
            platform: "mp",
            // #endif
            // #ifdef MP-WEIXIN
            platform: "wx",
            // #endif
            // #ifdef MP-QQ
            platform: "qq",
            // #endif
          },
          error: devCache.get("errorReport"),
          console: devCache.get("console"),
          network: devCache.get("request"),
          pageCount: devCache.get("pageCount"),
          dayOnline: devCache.get("dayOnline"),
          logs: devCache.get("logReport"), // ! 运行日志
          info: await getRuntimeInfo(), // ! 当前运行的系统信息
          uniBus: devCache.get("uniBus"),
          busCount: devCache.get("busCount"),
          pageRouteMap: devCache.get("pageRouteMap"),
          pageRouteKeyMap: devCache.get("pageRouteKeyMap"),
          storage: {},
          sessionStorage: {},
          cookie: {},
          ...that.getCache(),
        };

        try {
          if (that.$store.state) {
            waitExportObject.vuex = that.$store.state;
          }
        } catch (error) {}
        try {
          if (uni.Pinia) {
            waitExportObject.pinia = uni.Pinia.getActivePinia().state.value;
          } else if (that.$pinia.state.value) {
            waitExportObject.pinia = that.$pinia.state.value;
          }
        } catch (error) {}
        try {
          if (getApp().globalData) {
            waitExportObject.globalData = getApp().globalData;
          }
        } catch (error) {}

        let data = jsonCompress.safeJsonStringify(waitExportObject);
        data = JSON.parse(data);
        data = JSON.stringify(data, null, 2);
        let t = new Date().getTime();
        let exportFileName = `export_devtools_log_${t}.json`;

        // #ifdef H5
        const blob = new Blob([data], { type: "application/json" });
        const url = URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.style = "display: none";
        a.download = exportFileName;
        a.href = url;
        document.body.appendChild(a);
        a.click();
        uni.showToast({
          title: "导出成功!",
          icon: "success",
        });
        // #endif

        // #ifdef APP-PLUS
        plus.io.resolveLocalFileSystemURL(
          "_downloads/",
          (entry) => {
            entry.getFile(
              exportFileName,
              {
                create: true,
              },
              (fileEntry) => {
                fileEntry.createWriter((writer) => {
                  writer.onwrite = (e) => {
                    uni.hideLoading();
                    uni.showModal({
                      title: "导出成功",
                      content: "文件导出成功!已保存至公共下载路径,文件名称:" + exportFileName,
                    });
                  };
                  writer.onerror = () => {
                    uni.hideLoading();
                    uni.showToast({
                      title: "日志导出失败!_写入文件失败",
                      icon: "none",
                    });
                  };
                  writer.write(data);
                });
              }
            );
          },
          (err) => {
            console.log("err", err);
            uni.hideLoading();
            uni.showToast({
              title: "文件保存失败!_打开目录失败",
              icon: "none",
            });
          }
        );
        // #endif

        uni.hideLoading();
      } catch (error) {
        if (error && error.message) {
          console.log("导出失败!", error.message);
        } else {
          console.log("导出失败!", error);
        }

        uni.hideLoading();
        uni.showToast({
          title: "导出失败!",
          icon: "error",
        });
      }
    },
    /**
     * 获取缓存数据
     */
    getCache() {
      let data = {
        storage: {},
        sessionStorage: {},
        cookie: {},
      };

      // #ifdef APP-PLUS
      let keys = plus.storage.getAllKeys();
      for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        if (key.indexOf("devTools_") == 0) {
          // 忽略以 devTools_ 开头的key
          continue;
        }
        data.storage[key] = uni.getStorageSync(key);
      }
      // #endif

      // #ifdef H5

      for (let i = 0; i < localStorage.length; i++) {
        let key = localStorage.key(i);
        if (key.indexOf("devTools_") == 0) {
          continue;
        }
        let value = uni.getStorageSync(key);
        data.storage[key] = value;
      }

      for (let i = 0; i < sessionStorage.length; i++) {
        let key = sessionStorage.key(i);
        if (key.indexOf("devTools_") == 0) {
          continue;
        }
        let value = sessionStorage.getItem(key);
        data.sessionStorage[key] = value;
      }

      document.cookie.split(";").forEach((cookieStr) => {
        const [name, value] = cookieStr.trim().split("=");
        data.cookie[name] = value;
      });
      // #endif

      // #ifdef MP
      let keyList = devCache.get("storage");
      if (!keyList) keyList = [];
      for (let i = 0; i < keyList.length; i++) {
        const key = keyList[i];
        if (key.indexOf("devTools_") == 0) {
          continue;
        }
        let value = uni.getStorageSync(key);
        if (value) {
          data.storage[key] = value;
        }
      }
      // #endif

      return data;
    },
    /**
     * 跳转指定URL
     */
    goUrl(url) {
      // #ifdef H5
      window.open(url);
      // #endif
      // #ifdef MP
      uni.setClipboardData({
        data: url,
      });
      // #endif
      // #ifdef APP-PLUS
      plus.runtime.openURL(url);
      // #endif
    },
  },
};
</script>
<style lang="scss" scoped>
.settingView {
  display: flex;
  flex-direction: column;
  width: 750rpx;
  .loading {
    width: 750rpx;
    height: 300rpx;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    .loadingText {
      font-size: 24rpx;
      color: #888;
    }
  }
  .divisionLine {
    width: 750rpx;
    height: 1px;
    background-color: rgba(0, 0, 0, 0.1);
  }
  .checkboxItem {
    display: flex;
    flex-direction: row;
    padding: 10rpx 20rpx;
    width: 750rpx;
    align-items: center;
    &:active {
      background-color: rgba(0, 0, 0, 0.05);
    }
    .name {
      font-size: 24rpx;
      margin-left: 5rpx;
    }
    .count {
      font-size: 20rpx;
      margin-left: 10rpx;
      color: #ff2d55;
    }
    .empty {
      font-size: 20rpx;
      margin-left: 10rpx;
      color: #999;
    }
  }
  .delBtn {
    width: 710rpx;
    margin-left: 20rpx;
    border-radius: 20rpx;
    height: 70rpx;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    background-color: rgb(255, 45, 85);
    margin-top: 20rpx;
    margin-bottom: 30rpx;
    &:active {
      background-color: rgba(255, 45, 85, 0.8);
    }
    .delBtnText {
      font-size: 24rpx;
      color: #fff;
    }
  }
}
.objectAnalysisView {
  width: 710rpx;
  margin-left: 20rpx;
  margin-bottom: 20rpx;
}
.about {
  width: 710rpx;
  margin-left: 20rpx;
  display: flex;
  flex-direction: column;
  .row {
    margin-bottom: 10rpx;
    font-size: 24rpx;
    color: #888;
  }
}
</style>