Skip to content

Commit 3f3bd79

Browse files
committed
feat:拦截me接口返回mock
1 parent 08da3ec commit 3f3bd79

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"scripts": {
4747
"dev": "pnpm -F tiny-pro-vue start",
4848
"dev:backend": "pnpm -F tinyui-nestjs-server start",
49+
"dev:designer": "pnpm -F template/lowcode-designer dev",
4950
"describe": "npm-scripts-info",
5051
"build": "run-s build:*",
5152
"build:main": "tsc -p tsconfig.json",

template/lowcode-designer/src/composable/http/index.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { HttpService } from '@opentiny/tiny-engine'
33
import { useBroadcastChannel } from '@vueuse/core'
44
import { constants } from '@opentiny/tiny-engine-utils'
55
import Login from './Login.vue'
6+
import mockConfig from './mockConfig'
67

78
const LOGIN_EXPIRED_CODE = 401
89
const { BROADCAST_CHANNEL } = constants
@@ -36,6 +37,72 @@ const preRequest = (config) => {
3637
config.baseURL = ''
3738
}
3839

40+
// 检查是否需要 mock - 纯前端项目,拦截 /api/user/me 接口
41+
const fullUrl = (config.baseURL || '') + (config.url || '')
42+
const urlToCheck = config.url || ''
43+
44+
// 检查是否是 /api/user/me 或 /user/me 请求
45+
const isMeRequest = /\/api\/user\/me$|\/user\/me$/.test(fullUrl) ||
46+
/\/api\/user\/me$|\/user\/me$/.test(urlToCheck)
47+
48+
if (isMeRequest && (config.method || 'GET').toUpperCase() === 'GET') {
49+
50+
// 找到对应的 mock 配置
51+
const mockRoute = mockConfig.find((route) => {
52+
const urlMatch = typeof route.url === 'string'
53+
? fullUrl.includes(route.url) || urlToCheck.includes(route.url)
54+
: route.url.test(fullUrl) || route.url.test(urlToCheck)
55+
const methodMatch = !route.method ||
56+
route.method.toUpperCase() === (config.method || 'GET').toUpperCase()
57+
return urlMatch && methodMatch
58+
})
59+
60+
if (mockRoute) {
61+
// 直接调用 mock 的 response 函数
62+
const mockResult = mockRoute.response(config)
63+
64+
// 如果返回的是 Promise,等待 resolve
65+
if (mockResult && typeof mockResult.then === 'function') {
66+
return mockResult.then(([status, data]) => {
67+
console.info('222222222222222222222222', data)
68+
// 返回一个模拟的响应对象
69+
// 注意:axios 请求拦截器返回的应该是 config,但我们可以通过返回一个特殊对象来阻止请求
70+
// 实际上,我们需要修改 config,让它不会发送真实请求
71+
config.adapter = () => {
72+
// 返回一个 Promise,resolve 一个模拟的响应
73+
return Promise.resolve({
74+
data: {
75+
data,
76+
error: null
77+
},
78+
status,
79+
statusText: 'OK',
80+
headers: {},
81+
config
82+
})
83+
}
84+
return config
85+
})
86+
} else {
87+
// 如果 response 不是异步的,直接处理
88+
const [status, data] = mockResult
89+
config.adapter = () => {
90+
return Promise.resolve({
91+
data: {
92+
data,
93+
error: null
94+
},
95+
status,
96+
statusText: 'OK',
97+
headers: {},
98+
config
99+
})
100+
}
101+
return config
102+
}
103+
}
104+
}
105+
39106
return config
40107
}
41108

@@ -124,7 +191,9 @@ const customizeHttpService = () => {
124191
interceptors: {
125192
request: [preRequest],
126193
response: [[preResponse, errorResponse]]
127-
}
194+
},
195+
mockConfig,
196+
enableMock: true
128197
}
129198

130199
HttpService.apis.setOptions(options)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Mock 路由配置
3+
* 用于纯前端项目的接口 mock
4+
*/
5+
6+
// Mock 用户数据(真实接口返回格式)
7+
const mockUserData = {
8+
data: {
9+
id: 1,
10+
username: '开发者',
11+
email: 'developer@lowcode.com',
12+
provider: null,
13+
password: null,
14+
confirmationToken: 'uuid~dfafasdfasdfa',
15+
confirmed: true,
16+
blocked: false,
17+
created_by: null,
18+
updated_by: null,
19+
created_at: '2021-11-11T13:52:21.000Z',
20+
updated_at: '2022-11-01T01:39:30.000Z',
21+
block: false,
22+
is_admin: true,
23+
is_public: false,
24+
},
25+
locale: 'zh-cn',
26+
};
27+
28+
// 格式化响应数据
29+
const getResponseData = (data) => {
30+
return {
31+
data,
32+
error: null,
33+
};
34+
};
35+
36+
export default [
37+
// 获取用户信息
38+
{
39+
url: /\/api\/user\/me$|\/user\/me$/, // 使用正则表达式匹配
40+
method: 'GET',
41+
response: async () => {
42+
console.log('[Lowcode Designer] Mock /api/user/me 接口,返回 mock 数据');
43+
return [200, getResponseData(mockUserData)];
44+
},
45+
},
46+
];

0 commit comments

Comments
 (0)