|
| 1 | +import { defineService, META_SERVICE } from '@opentiny/tiny-engine'; |
| 2 | +import axios from 'axios'; |
| 3 | +import AxiosMockAdapter from 'axios-mock-adapter'; |
| 4 | + |
| 5 | +let http = null; |
| 6 | +let mock = null; |
| 7 | + |
| 8 | +const createInterceptorHandler = |
| 9 | + (http) => |
| 10 | + ({ data, type }) => { |
| 11 | + if (typeof data === 'function') { |
| 12 | + http.interceptors[type].use(data); |
| 13 | + |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + if (Array.isArray(data)) { |
| 18 | + data.forEach((item) => { |
| 19 | + if (!item) return; |
| 20 | + |
| 21 | + if (Array.isArray(item)) { |
| 22 | + http.interceptors[type].use(...item); |
| 23 | + |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + if (typeof item === 'function') { |
| 28 | + http.interceptors[type].use(item); |
| 29 | + } |
| 30 | + }); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | +export default defineService({ |
| 35 | + id: META_SERVICE.Http, |
| 36 | + type: 'MetaService', |
| 37 | + options: { |
| 38 | + axiosConfig: { |
| 39 | + // axios 配置 |
| 40 | + baseURL: '', |
| 41 | + withCredentials: false, // 跨域请求时是否需要使用凭证 |
| 42 | + headers: {}, // 请求头 |
| 43 | + }, |
| 44 | + interceptors: { |
| 45 | + // 拦截器 |
| 46 | + request: [], // 支持配置多个请求拦截器,先注册后执行 |
| 47 | + response: [], // 支持配置多个响应拦截器,先注册先执行 |
| 48 | + }, |
| 49 | + mockConfig: [], |
| 50 | + }, |
| 51 | + init: ({ options = {} }) => { |
| 52 | + const { axiosConfig = {}, interceptors = {}, enableMock } = options; |
| 53 | + const { request = [], response = [] } = interceptors; |
| 54 | + |
| 55 | + http = axios.create(axiosConfig); |
| 56 | + if (enableMock) { |
| 57 | + mock = new AxiosMockAdapter(http); |
| 58 | + mock.onGet(/\/mock\/bundle\.json$/).passThrough(); |
| 59 | + mock.onPost(/\/app-center\/api\/ai\/chat/).passThrough(); |
| 60 | + |
| 61 | + http.interceptors.request.use((config) => { |
| 62 | + const AI_PATH = '/app-center/api/ai/chat'; |
| 63 | + |
| 64 | + if (config.url === AI_PATH) { |
| 65 | + config.url = `/tiny-engine${AI_PATH}`; // 修改路径 |
| 66 | + } |
| 67 | + return config; |
| 68 | + }); |
| 69 | + |
| 70 | + mock.onAny().reply(async (config) => { |
| 71 | + const { mockConfig = [] } = options; |
| 72 | + // 构建完整 URL(包含 baseURL) |
| 73 | + const fullUrl = (config.baseURL || '') + (config.url || ''); |
| 74 | + const method = (config.method || 'GET').toUpperCase(); |
| 75 | + |
| 76 | + const mockItem = mockConfig.find((item) => { |
| 77 | + // 方法匹配 |
| 78 | + if (item.method && method !== item.method.toUpperCase()) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + // URL 匹配 - 同时检查 config.url 和 fullUrl |
| 83 | + if (typeof item.url === 'string') { |
| 84 | + return ( |
| 85 | + item.url === config.url || |
| 86 | + item.url === fullUrl || |
| 87 | + config.url?.includes(item.url) || |
| 88 | + fullUrl.includes(item.url) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + if (item.url instanceof RegExp) { |
| 93 | + return item.url.test(config.url) || item.url.test(fullUrl); |
| 94 | + } |
| 95 | + |
| 96 | + return false; |
| 97 | + }); |
| 98 | + |
| 99 | + if (mockItem) { |
| 100 | + if (typeof mockItem.response === 'function') { |
| 101 | + return mockItem.response(config); |
| 102 | + } |
| 103 | + return mockItem.response; |
| 104 | + } |
| 105 | + |
| 106 | + // 如果没有匹配到,输出调试信息 |
| 107 | + console.warn( |
| 108 | + `[Mock] 未匹配到接口: ${method} ${fullUrl || config.url}`, |
| 109 | + '可用路由:', |
| 110 | + mockConfig.map((item) => `${item.method || 'ANY'} ${item.url}`) |
| 111 | + ); |
| 112 | + |
| 113 | + return [ |
| 114 | + 200, |
| 115 | + { |
| 116 | + code: 200, |
| 117 | + errMsg: |
| 118 | + '当前 demo 暂未支持该接口,请前往GitHub 或者 Gitee 克隆项目完整体验', |
| 119 | + error: |
| 120 | + '当前 demo 暂未支持该接口,请前往GitHub 或者 Gitee 克隆项目完整体验', |
| 121 | + }, |
| 122 | + ]; |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + const addInterceptors = createInterceptorHandler(http); |
| 127 | + addInterceptors({ data: request, type: 'request' }); |
| 128 | + addInterceptors({ data: response, type: 'response' }); |
| 129 | + }, |
| 130 | + apis: () => ({ |
| 131 | + getHttp: () => http, |
| 132 | + getMock: () => mock, |
| 133 | + get: (...args) => http?.get(...args), |
| 134 | + post: (...args) => http?.post(...args), |
| 135 | + request: (...args) => http?.request(...args), |
| 136 | + put: (...args) => http?.put(...args), |
| 137 | + delete: (...args) => http?.delete(...args), |
| 138 | + setOptions: (options) => { |
| 139 | + // 支持动态设置选项 |
| 140 | + if (options.axiosConfig) { |
| 141 | + Object.assign(http.defaults, options.axiosConfig); |
| 142 | + } |
| 143 | + if (options.interceptors) { |
| 144 | + const addInterceptors = createInterceptorHandler(http); |
| 145 | + if (options.interceptors.request) { |
| 146 | + addInterceptors({ |
| 147 | + data: options.interceptors.request, |
| 148 | + type: 'request', |
| 149 | + }); |
| 150 | + } |
| 151 | + if (options.interceptors.response) { |
| 152 | + addInterceptors({ |
| 153 | + data: options.interceptors.response, |
| 154 | + type: 'response', |
| 155 | + }); |
| 156 | + } |
| 157 | + } |
| 158 | + // 重新初始化 mock(如果启用) |
| 159 | + if (options.enableMock && options.mockConfig) { |
| 160 | + // 如果 mock 已存在,先恢复 |
| 161 | + if (mock) { |
| 162 | + mock.restore(); |
| 163 | + } |
| 164 | + mock = new AxiosMockAdapter(http); |
| 165 | + mock.onGet(/\/mock\/bundle\.json$/).passThrough(); |
| 166 | + mock.onPost(/\/app-center\/api\/ai\/chat/).passThrough(); |
| 167 | + |
| 168 | + http.interceptors.request.use((config) => { |
| 169 | + const AI_PATH = '/app-center/api/ai/chat'; |
| 170 | + |
| 171 | + if (config.url === AI_PATH) { |
| 172 | + config.url = `/tiny-engine${AI_PATH}`; |
| 173 | + } |
| 174 | + return config; |
| 175 | + }); |
| 176 | + |
| 177 | + mock.onAny().reply(async (config) => { |
| 178 | + // 构建完整 URL(包含 baseURL) |
| 179 | + const fullUrl = (config.baseURL || '') + (config.url || ''); |
| 180 | + const method = (config.method || 'GET').toUpperCase(); |
| 181 | + |
| 182 | + const mockItem = options.mockConfig.find((item) => { |
| 183 | + // 方法匹配 |
| 184 | + if (item.method && method !== item.method.toUpperCase()) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + |
| 188 | + // URL 匹配 - 同时检查 config.url 和 fullUrl |
| 189 | + if (typeof item.url === 'string') { |
| 190 | + return ( |
| 191 | + item.url === config.url || |
| 192 | + item.url === fullUrl || |
| 193 | + config.url?.includes(item.url) || |
| 194 | + fullUrl.includes(item.url) |
| 195 | + ); |
| 196 | + } |
| 197 | + |
| 198 | + if (item.url instanceof RegExp) { |
| 199 | + return item.url.test(config.url) || item.url.test(fullUrl); |
| 200 | + } |
| 201 | + |
| 202 | + return false; |
| 203 | + }); |
| 204 | + |
| 205 | + if (mockItem) { |
| 206 | + if (typeof mockItem.response === 'function') { |
| 207 | + return mockItem.response(config); |
| 208 | + } |
| 209 | + return mockItem.response; |
| 210 | + } |
| 211 | + |
| 212 | + // 如果没有匹配到,输出调试信息 |
| 213 | + console.warn( |
| 214 | + `[Mock] 未匹配到接口: ${method} ${fullUrl || config.url}`, |
| 215 | + '可用路由:', |
| 216 | + options.mockConfig.map( |
| 217 | + (item) => `${item.method || 'ANY'} ${item.url}` |
| 218 | + ) |
| 219 | + ); |
| 220 | + |
| 221 | + return [ |
| 222 | + 200, |
| 223 | + { |
| 224 | + code: 200, |
| 225 | + errMsg: |
| 226 | + '当前 demo 暂未支持该接口,请前往GitHub 或者 Gitee 克隆项目完整体验', |
| 227 | + error: |
| 228 | + '当前 demo 暂未支持该接口,请前往GitHub 或者 Gitee 克隆项目完整体验', |
| 229 | + }, |
| 230 | + ]; |
| 231 | + }); |
| 232 | + } |
| 233 | + }, |
| 234 | + }), |
| 235 | +}); |
0 commit comments