-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
119 lines (115 loc) · 3.01 KB
/
index.js
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
// JSON actions
const fetchJSON = (filePath) => {
return fetch(`/veact-admin/__demo__/mock/${filePath}.json`).then((response) => response.json())
}
// first fetch cache data map
const _cacheData = new Map()
const ensureJSON = async (filePath) => {
if (_cacheData.has(filePath)) {
return _cacheData.get(filePath)
} else {
const data = await fetchJSON(filePath)
_cacheData.set(filePath, data)
return data
}
}
// mock token
console.info('mock token')
window.localStorage.setItem('id_token', 'veact_admin.mock.token')
// mock by axios adapter
console.info('mock axios')
window.__axiosAdapter = (config) => {
console.debug('mock request:', config)
// 1. 仅处理所有 Get 请求
// 2. Auth 相关的必要 Post 请求除外
// 3. 所有请求返回错误,提示:Demo 站点无法操作数据
const handlers = {
'/auth/login': {
post: () => ensureJSON('auth/login')
},
'/auth/check': {
post: () => ensureJSON('auth/check')
},
'/auth/admin': {
get: () => ensureJSON('auth/admin')
},
'/expansion/statistic': {
get: () => ensureJSON('expansion/statistic')
},
'/expansion/google-token': {
get: () => ensureJSON('expansion/google-token')
},
'/expansion/uptoken': {
get: () => ensureJSON('expansion/uptoken')
},
'/vote': {
get: () => ensureJSON('vote')
},
'/feedback': {
get: () => ensureJSON('feedback')
},
'/announcement': {
get: () => ensureJSON('announcement')
},
'/category': {
get: () => ensureJSON('category')
},
'/tag': {
get: () => ensureJSON('tag')
},
'/comment': {
get: () => ensureJSON('comment/list')
},
'/comment/calendar': {
get: () => ensureJSON('comment/calendar')
},
'/disqus/config': {
get: () => ensureJSON('disqus/config')
},
'/option': {
get: () => ensureJSON('option')
},
'/article': {
get: () => ensureJSON('article/list')
},
'/article/calendar': {
get: () => ensureJSON('article/calendar')
},
'/article/612c81321a53290533a7b782': {
get: () => ensureJSON('article/612c81321a53290533a7b782')
},
'/article/610c29438a907384c63fef00': {
get: () => ensureJSON('article/610c29438a907384c63fef00')
},
'/article/61030f5fcf1faa098ee126b2': {
get: () => ensureJSON('article/61030f5fcf1faa098ee126b2')
}
}
return new Promise(async (resolve) => {
const target = handlers?.[config.url]?.[config.method]
if (target) {
return resolve({
status: 200,
statusText: 'OK',
headers: {
'content-type': 'application/json'
},
data: await target()
})
} else {
return resolve({
status: 400,
statusText: 'ERROR',
headers: {
'content-type': 'application/json'
},
data: {
status: 'error',
message: '操作失败',
error: 'Demo 站点不支持此数据操作',
result: null
}
})
}
})
}