FloatLogPanel.vue 12 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
<template>
  <view v-if="panelEnabled" class="float-log-root">
    <view v-if="!expanded" class="float-log-fab" @tap.stop="togglePanel">
      <view class="fab-inner">
        <text class="fab-text">日志</text>
        <text v-if="lineCount > 0" class="fab-badge">{{ displayCount }}</text>
      </view>
    </view>

    <view v-if="expanded" class="float-log-mask" @tap="closePanel">
      <view class="float-log-panel" @tap.stop>
        <view class="panel-header">
          <text class="panel-title">实时日志 {{ displayLines.length }}/{{ lineCount }}{{ truncatedHint }}</text>
          <view class="panel-actions">
            <text class="action-btn" @tap="onCopy">复制</text>
            <text class="action-btn" @tap="onClear">清空</text>
            <!-- #ifdef APP-PLUS -->
            <text class="action-btn accent" @tap="onRunStorageMerge">归并</text>
            <!-- #endif -->
            <text class="action-btn danger" @tap="closePanel">关闭</text>
          </view>
        </view>

        <view class="filter-row">
          <input class="filter-input" v-model="keyword" placeholder="关键字过滤,如 StoragePathMerge / PathUtils" confirm-type="search" />
        </view>

        <view class="level-row">
          <text v-for="lv in levelOptions" :key="lv.value" :class="['level-chip', { active: levelFilter === lv.value }]" @tap="levelFilter = lv.value">{{ lv.label }}</text>
        </view>

        <scroll-view class="log-scroll" scroll-y :scroll-into-view="scrollIntoView" :scroll-with-animation="false">
          <view v-for="item in displayLines" :key="item.id" :id="'log-' + item.id" :class="['log-line', 'level-' + item.level]">
            <text class="log-time">{{ item.time }}</text>
            <text class="log-tag">[{{ item.level }}]</text>
            <text class="log-text">{{ item.text }}</text>
          </view>
          <view v-if="displayLines.length === 0" class="log-empty">暂无匹配日志</view>
        </scroll-view>

        <view class="tag-row">
          <text v-for="tag in quickTags" :key="tag.label" :class="['tag-chip', { active: keyword === tag.value }]" @tap="setKeyword(tag.value)">{{ tag.label }}</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import FloatLogBus from '@/utils/logger/FloatLogBus.js'
import FloatLogConfig from '@/utils/logger/FloatLogConfig.js'
import { loadPersistedMergeLogs } from '@/utils/storageMerge/log.js'
// #ifdef APP-PLUS
import StoragePathMerge from '@/utils/storageMerge/StoragePathMerge.js'
// #endif

const DISPLAY_LIMIT = 200
const REFRESH_THROTTLE_MS = 150

export default {
  name: 'FloatLogPanel',
  data() {
    return {
      expanded: false,
      panelEnabled: true,
      keyword: '',
      levelFilter: 'all',
      lines: [],
      lineCount: 0,
      scrollIntoView: '',
      levelOptions: [
        { label: '全部', value: 'all' },
        { label: 'INFO', value: 'info' },
        { label: 'WARN', value: 'warn' },
        { label: 'ERROR', value: 'error' },
        { label: 'DEBUG', value: 'debug' }
      ],
      quickTags: [
        { label: '全部', value: '' },
        { label: 'StoragePathMerge', value: 'StoragePathMerge' },
        { label: 'PathUtils', value: 'PathUtils' },
        { label: 'SQLite', value: 'SQLite' },
        { label: '接待', value: '接待' },
        { label: 'FloatLogBus', value: 'FloatLogBus' }
      ]
    }
  },
  computed: {
    displayCount() {
      return this.lineCount > 99 ? '99+' : String(this.lineCount)
    },
    filteredLines() {
      let list = this.lines
      if (this.levelFilter !== 'all') {
        list = list.filter(l => l.level === this.levelFilter)
      }
      const kw = (this.keyword || '').trim().toLowerCase()
      if (kw) {
        list = list.filter(l => l.text.toLowerCase().includes(kw))
      }
      return list
    },
    displayLines() {
      const list = this.filteredLines
      if (list.length <= DISPLAY_LIMIT) return list
      return list.slice(list.length - DISPLAY_LIMIT)
    },
    truncatedHint() {
      if (this.filteredLines.length > DISPLAY_LIMIT) {
        return ` (仅显示最近${DISPLAY_LIMIT}条)`
      }
      return ''
    }
  },
  watch: {
    keyword() {
      if (this.expanded) this._scheduleRefresh(true)
    },
    levelFilter() {
      if (this.expanded) this._scheduleRefresh(true)
    }
  },
  mounted() {
    this._refreshTimer = null
    this._syncPanelEnabled()
    this._syncLineCount()
    this._unsub = FloatLogBus.subscribe(() => this._onBusLine())
    this._unsubConfig = FloatLogConfig.subscribe(() => {
      this._syncPanelEnabled()
    })
  },
  beforeDestroy() {
    this._clearRefreshTimer()
    this._unsub && this._unsub()
    this._unsubConfig && this._unsubConfig()
  },
  methods: {
    _syncPanelEnabled() {
      this.panelEnabled = FloatLogConfig.isLogBallVisible()
    },
    _syncLineCount() {
      this.lineCount = FloatLogBus.getLineCount()
    },
    _clearRefreshTimer() {
      if (this._refreshTimer) {
        clearTimeout(this._refreshTimer)
        this._refreshTimer = null
      }
    },
    _onBusLine() {
      this._syncLineCount()
      if (!this.expanded) return
      this._scheduleRefresh()
    },
    _scheduleRefresh(scrollBottom = false) {
      this._clearRefreshTimer()
      this._refreshTimer = setTimeout(() => {
        this._refreshTimer = null
        this.refresh()
        if (scrollBottom) {
          this.$nextTick(() => this.scrollToBottom())
        }
      }, REFRESH_THROTTLE_MS)
    },
    _isMergeKeyword(kw) {
      const k = (kw || '').trim().toLowerCase()
      return k.includes('storagepathmerge')
    },
    refresh() {
      const kw = (this.keyword || '').trim()
      if (this._isMergeKeyword(kw)) {
        const byId = new Map()
        loadPersistedMergeLogs().forEach(item => {
          if (item && item.id) byId.set(item.id, item)
        })
        FloatLogBus.getMergeLines().forEach(item => {
          if (item && item.id) byId.set(item.id, item)
        })
        this.lines = [...byId.values()]
      } else if (kw) {
        this.lines = FloatLogBus.getLines()
      } else {
        this.lines = FloatLogBus.getRecentLines(DISPLAY_LIMIT + 50)
      }
      this._syncLineCount()
    },
    togglePanel() {
      if (this.expanded) {
        this.closePanel()
        return
      }
      this.expanded = true
      this.refresh()
      this.$nextTick(() => this.scrollToBottom())
    },
    closePanel() {
      this.expanded = false
      this._clearRefreshTimer()
      this.lines = []
    },
    setKeyword(value) {
      this.keyword = value
      if (this.expanded) this.refresh()
      this.$nextTick(() => this.scrollToBottom())
    },
    scrollToBottom() {
      const list = this.displayLines
      if (list.length === 0) {
        this.scrollIntoView = ''
        return
      }
      const last = list[list.length - 1]
      const nextId = 'log-' + last.id
      if (this.scrollIntoView === nextId) {
        this.scrollIntoView = ''
        this.$nextTick(() => {
          this.scrollIntoView = nextId
        })
        return
      }
      this.scrollIntoView = nextId
    },
    onClear() {
      FloatLogBus.clearLines()
      this.refresh()
      uni.showToast({ title: '已清空', icon: 'none', duration: 800 })
    },
    onCopy() {
      const kw = (this.keyword || '').trim()
      let list = this._isMergeKeyword(kw) ? [...loadPersistedMergeLogs(), ...FloatLogBus.getMergeLines()] : FloatLogBus.getLines()
      if (this.levelFilter !== 'all') {
        list = list.filter(l => l.level === this.levelFilter)
      }
      const kwLower = kw.toLowerCase()
      if (kw && !this._isMergeKeyword(kw)) {
        list = list.filter(l => l.text.toLowerCase().includes(kwLower))
      }
      const text = list.map(l => `${l.time} [${l.level}] ${l.text}`).join('\n')
      if (!text) {
        uni.showToast({ title: '无内容', icon: 'none' })
        return
      }
      uni.setClipboardData({
        data: text,
        success: () => {
          uni.showToast({ title: '已复制', icon: 'none' })
        }
      })
    },
    async onRunStorageMerge() {
      // #ifdef APP-PLUS
      uni.showLoading({ title: '归并中', mask: true })
      try {
        await StoragePathMerge.run({ force: true })
        this.keyword = 'StoragePathMerge'
        this.refresh()
        this.$nextTick(() => this.scrollToBottom())
        uni.showToast({ title: '归并已执行', icon: 'none' })
      } catch (e) {
        uni.showToast({ title: e?.message || '归并失败', icon: 'none' })
      } finally {
        uni.hideLoading()
      }
      // #endif
    }
  }
}
</script>

<style lang="scss" scoped>
.float-log-root {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 99999;
  pointer-events: none;
}

.float-log-fab {
  position: fixed;
  right: 24rpx;
  bottom: 20rpx;
  width: 120rpx;
  height: 120rpx;
  z-index: 99999;
  pointer-events: auto;
}

.fab-inner {
  width: 120rpx;
  height: 120rpx;
  border-radius: 60rpx;
  background: linear-gradient(135deg, #1565c0, #42a5f5);
  box-shadow: 0 8rpx 24rpx rgba(21, 101, 192, 0.45);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
}

.fab-text {
  color: #fff;
  font-size: 26rpx;
  font-weight: 600;
}

.fab-badge {
  position: absolute;
  top: 4rpx;
  right: 4rpx;
  min-width: 36rpx;
  height: 36rpx;
  line-height: 36rpx;
  padding: 0 8rpx;
  border-radius: 18rpx;
  background: #e53935;
  color: #fff;
  font-size: 20rpx;
  text-align: center;
}

.float-log-mask {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 100000;
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.float-log-panel {
  width: 90%;
  height: 90%;
  max-width: 100%;
  max-height: 100%;
  background: #1e1e2e;
  border-radius: 16rpx;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-sizing: border-box;
  box-shadow: 0 16rpx 48rpx rgba(0, 0, 0, 0.45);
}

.panel-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20rpx 24rpx;
  background: #283593;
}

.panel-title {
  color: #fff;
  font-size: 30rpx;
  font-weight: 600;
  flex: 1;
  margin-right: 16rpx;
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12rpx;
  justify-content: flex-end;
}

.action-btn {
  color: #90caf9;
  font-size: 26rpx;
  padding: 8rpx 16rpx;
}

.action-btn.accent {
  color: #a5d6a7;
}

.action-btn.danger {
  color: #ffab91;
}

.filter-row {
  flex-shrink: 0;
  padding: 16rpx 20rpx 8rpx;
  background: #2d2d44;
}

.filter-input {
  width: 100%;
  height: 64rpx;
  background: #3d3d5c;
  border-radius: 8rpx;
  padding: 0 20rpx;
  color: #e0e0e0;
  font-size: 26rpx;
}

.level-row {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 12rpx;
  padding: 8rpx 20rpx 12rpx;
  background: #2d2d44;
  border-bottom: 1rpx solid #444;
}

.level-chip {
  padding: 6rpx 18rpx;
  border-radius: 20rpx;
  background: #3d3d5c;
  color: #b0bec5;
  font-size: 22rpx;
}

.level-chip.active {
  background: #5c6bc0;
  color: #fff;
}

.log-scroll {
  flex: 1;
  height: 0;
  padding: 12rpx 16rpx;
}

.log-line {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  margin-bottom: 10rpx;
  font-size: 22rpx;
  line-height: 1.45;
  word-break: break-all;
}

.log-time {
  color: #78909c;
  margin-right: 8rpx;
  flex-shrink: 0;
}

.log-tag {
  margin-right: 8rpx;
  flex-shrink: 0;
  font-weight: 600;
}

.log-text {
  color: #eceff1;
  flex: 1;
}

.level-info .log-tag {
  color: #81d4fa;
}
.level-warn .log-tag {
  color: #ffd54f;
}
.level-error .log-tag {
  color: #ef9a9a;
}
.level-debug .log-tag {
  color: #b39ddb;
}

.log-empty {
  color: #78909c;
  text-align: center;
  padding: 40rpx;
  font-size: 26rpx;
}

.tag-row {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 12rpx;
  padding: 16rpx 20rpx;
  background: #2d2d44;
  border-top: 1rpx solid #444;
}

.tag-chip {
  padding: 8rpx 20rpx;
  border-radius: 24rpx;
  background: #3d3d5c;
  color: #b0bec5;
  font-size: 22rpx;
}

.tag-chip.active {
  background: #1565c0;
  color: #fff;
}
</style>