FloatLogConfig.js
2.31 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
import { index as envIndex } from '@/env'
const listeners = new Set()
let configLoaded = false
let serverAllows = false
function isNonProduction() {
return envIndex !== 3
}
/**
* app-tape 配置 isShowLogBall:仅显式 true 时展示,未配置/false 均隐藏
*/
export function parseIsShowLogBall(mockData) {
if (!mockData || mockData.isShowLogBall == null) return false
const v = mockData.isShowLogBall
if (v === true || v === 1) return true
if (typeof v === 'string') return v.toLowerCase() === 'true'
return false
}
function computeVisible() {
return isNonProduction() && serverAllows
}
function notify() {
const visible = computeVisible()
listeners.forEach(fn => {
try {
fn(visible)
} catch (e) {
/* ignore */
}
})
return visible
}
const FloatLogConfig = {
isNonProduction,
isLogBallVisible() {
return computeVisible()
},
isConfigLoaded() {
return configLoaded
},
applyFromMockData(mockData) {
if (!isNonProduction()) {
serverAllows = false
} else {
serverAllows = parseIsShowLogBall(mockData)
}
configLoaded = true
const visible = notify()
if (visible) {
const FloatLogBus = require('./FloatLogBus.js').default
FloatLogBus.install()
}
return visible
},
/**
* 拉取配置中心 app-tape 并更新悬浮球(uni.$api.systemApi.getMockData)
* 例: https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=app-tape&env=test
* 字段 isShowLogBall;生产环境不展示
*/
async refreshFromServer(immediate = false) {
if (!isNonProduction()) {
serverAllows = false
configLoaded = true
return notify()
}
const cached = uni.$storage?.get?.('mock_data')
if (cached) {
this.applyFromMockData(cached)
}
try {
if (uni.$api?.systemApi?.getMockData) {
const data = await uni.$api.systemApi.getMockData(immediate)
return this.applyFromMockData(data)
}
serverAllows = false
} catch (e) {
uni.$logger?.local?.warn?.(`[FloatLogConfig] 读取 app-tape 配置失败: ${e?.message || e}`)
serverAllows = false
}
configLoaded = true
return notify()
},
subscribe(fn) {
listeners.add(fn)
fn(computeVisible())
return () => listeners.delete(fn)
}
}
export default FloatLogConfig