forked from opentiny/tiny-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring useHttp to httpService (opentiny#886)
* refactor/http-service
- Loading branch information
Showing
86 changed files
with
510 additions
and
1,636 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { createApp } from 'vue' | ||
import { HttpService } from '@opentiny/tiny-engine' | ||
import { useBroadcastChannel } from '@vueuse/core' | ||
import { constants } from '@opentiny/tiny-engine-utils' | ||
import Login from './Login.vue' | ||
|
||
const LOGIN_EXPIRED_CODE = 401 | ||
const { BROADCAST_CHANNEL } = constants | ||
|
||
const { post: globalNotify } = useBroadcastChannel({ name: BROADCAST_CHANNEL.Notify }) | ||
|
||
const procession = { | ||
promiseLogin: null, | ||
mePromise: {} | ||
} | ||
let loginVM = null | ||
|
||
const showError = (url, message) => { | ||
globalNotify({ | ||
type: 'error', | ||
title: '接口报错', | ||
message: `报错接口: ${url} \n报错信息: ${message ?? ''}` | ||
}) | ||
} | ||
|
||
const preRequest = (config) => { | ||
const isDevelopEnv = import.meta.env.MODE?.includes('dev') | ||
|
||
if (isDevelopEnv && config.url.match(/\/generate\//)) { | ||
config.baseURL = '' | ||
} | ||
|
||
const isVsCodeEnv = window.vscodeBridge | ||
|
||
if (isVsCodeEnv) { | ||
config.baseURL = '' | ||
} | ||
|
||
return config | ||
} | ||
|
||
const preResponse = (res) => { | ||
if (res.data?.error) { | ||
showError(res.config?.url, res?.data?.error?.message) | ||
|
||
return Promise.reject(res.data.error) | ||
} | ||
|
||
return res.data?.data | ||
} | ||
|
||
const openLogin = () => { | ||
if (!window.lowcode) { | ||
const loginDom = document.createElement('div') | ||
document.body.appendChild(loginDom) | ||
loginVM = createApp(Login).mount(loginDom) | ||
|
||
window.lowcode = { | ||
platformCenter: { | ||
Session: { | ||
rebuiltCallback: function () { | ||
loginVM.closeLogin() | ||
|
||
procession.mePromise.resolve('login ok') | ||
procession.promiseLogin = null | ||
procession.mePromise = {} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
if (!procession.promiseLogin) { | ||
procession.promiseLogin = loginVM.openLogin(procession, '/api/rebuildSession') | ||
procession.promiseLogin.then((response) => { | ||
HttpService.apis.request(response.config).then(resolve, reject) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
const errorResponse = (error) => { | ||
// 用户信息失效时,弹窗提示登录 | ||
const { response } = error | ||
|
||
if (response?.status === LOGIN_EXPIRED_CODE) { | ||
// vscode 插件环境弹出输入框提示登录 | ||
if (window.vscodeBridge) { | ||
return Promise.resolve(true) | ||
} | ||
|
||
// 浏览器环境弹出小窗登录 | ||
if (response?.headers['x-login-url']) { | ||
return openLogin() | ||
} | ||
} | ||
|
||
showError(error.config?.url, error?.message) | ||
|
||
return response?.data.error ? Promise.reject(response.data.error) : Promise.reject(error.message) | ||
} | ||
|
||
const getConfig = (env = import.meta.env) => { | ||
const baseURL = env.VITE_ORIGIN | ||
// 仅在本地开发时,启用 withCredentials | ||
const dev = env.MODE?.includes('dev') | ||
// 获取租户 id | ||
const getTenant = () => new URLSearchParams(location.search).get('tenant') | ||
|
||
return { | ||
baseURL, | ||
withCredentials: dev, | ||
headers: { | ||
...(dev && { 'x-lowcode-mode': 'develop' }), | ||
'x-lowcode-org': getTenant() | ||
} | ||
} | ||
} | ||
|
||
const customizeHttpService = () => { | ||
const options = { | ||
axiosConfig: getConfig(), | ||
interceptors: { | ||
request: [preRequest], | ||
response: [[preResponse, errorResponse]] | ||
} | ||
} | ||
|
||
HttpService.apis.setOptions(options) | ||
|
||
return HttpService | ||
} | ||
|
||
export default customizeHttpService() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as HttpService } from './http' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { defineService, META_SERVICE } from '@opentiny/tiny-engine-meta-register' | ||
import axios from 'axios' | ||
|
||
let http = null | ||
|
||
const createInterceptorHandler = | ||
(http) => | ||
({ data, type }) => { | ||
if (typeof data === 'function') { | ||
http.interceptors[type].use(data) | ||
|
||
return | ||
} | ||
|
||
if (Array.isArray(data)) { | ||
data.forEach((item) => { | ||
if (!item) return | ||
|
||
if (Array.isArray(item)) { | ||
http.interceptors[type].use(...item) | ||
|
||
return | ||
} | ||
|
||
if (typeof item === 'function') { | ||
http.interceptors[type].use(item) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
export default defineService({ | ||
id: META_SERVICE.Http, | ||
type: 'MetaService', | ||
options: { | ||
axiosConfig: { | ||
// axios 配置 | ||
baseURL: '', | ||
withCredentials: false, // 跨域请求时是否需要使用凭证 | ||
headers: {} // 请求头 | ||
}, | ||
interceptors: { | ||
// 拦截器 | ||
request: [], // 支持配置多个请求拦截器,先注册后执行 | ||
response: [] // 支持配置多个响应拦截器,先注册先执行 | ||
} | ||
}, | ||
init: ({ options = {} }) => { | ||
const { axiosConfig = {}, interceptors = {} } = options | ||
const { request = [], response = [] } = interceptors | ||
|
||
http = axios.create(axiosConfig) | ||
const addInterceptors = createInterceptorHandler(http) | ||
addInterceptors({ data: request, type: 'request' }) | ||
addInterceptors({ data: response, type: 'response' }) | ||
}, | ||
apis: () => ({ | ||
getHttp: () => http, | ||
get: (...args) => http?.get(...args), | ||
post: (...args) => http?.post(...args), | ||
request: (...args) => http?.request(...args), | ||
put: (...args) => http?.put(...args), | ||
delete: (...args) => http?.delete(...args) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { GenerateCodeService } from './generateCode' | ||
export { default as GlobalService } from './defaultGlobalService' | ||
export { default as HttpService } from './http' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.