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.
fix: update cli template resolve preview error
- Loading branch information
Showing
6 changed files
with
317 additions
and
24 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
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
83 changes: 83 additions & 0 deletions
83
packages/engine-cli/template/designer/src/composable/http/Login.vue
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,83 @@ | ||
<template> | ||
<div v-if="visible" class="tiny-popup__wrapper"> | ||
<div class="tiny-sso__box"> | ||
<div class="tiny-sso__body"> | ||
<iframe :src="url" class="tiny-sso__body-iframe" frameBorder="0" scrolling="no"></iframe> | ||
</div> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { ref } from 'vue' | ||
export default { | ||
setup() { | ||
const visible = ref(false) | ||
const url = ref('') | ||
const openLogin = (procession, newUrl) => { | ||
visible.value = true | ||
url.value = newUrl | ||
return new Promise((resolve, reject) => { | ||
procession.mePromise.resolve = resolve | ||
procession.mePromise.reject = reject | ||
}) | ||
} | ||
const closeLogin = () => { | ||
visible.value = false | ||
} | ||
return { | ||
openLogin, | ||
closeLogin, | ||
visible, | ||
url | ||
} | ||
} | ||
} | ||
</script> | ||
|
||
<style scoped lang="less"> | ||
.tiny-popup__wrapper { | ||
z-index: 9999; | ||
background: rgba(0, 0, 0, 0.5); | ||
position: fixed; | ||
top: 0; | ||
right: 0; | ||
bottom: 0; | ||
left: 0; | ||
overflow: auto; | ||
margin: 0; | ||
.tiny-sso__box { | ||
position: absolute; | ||
background: #fff; | ||
border: 1px solid transparent; | ||
box-shadow: 2px 2px 2px 0 rgba(0, 0, 0, 0.2); | ||
left: 50%; | ||
top: 50%; | ||
transform: translate(-50%, -50%); | ||
.tiny-sso__body { | ||
text-align: initial; | ||
padding: 20px; | ||
color: #5a5e66; | ||
line-height: 32px; | ||
font-size: 14px; | ||
.tiny-sso__body-iframe { | ||
width: 450px; | ||
height: 450px; | ||
overflow: hidden; | ||
//兼容edge | ||
@supports (-ms-ime-align: auto) { | ||
height: 460px; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
</style> |
125 changes: 112 additions & 13 deletions
125
packages/engine-cli/template/designer/src/composable/http/index.js
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,36 +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) => { | ||
return Promise.reject(error.message) | ||
// 用户信息失效时,弹窗提示登录 | ||
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: env.VITE_ORIGIN, | ||
baseURL, | ||
withCredentials: dev, | ||
headers: { | ||
'x-lowcode-mode': env.MODE | ||
...(dev && { 'x-lowcode-mode': 'develop' }), | ||
'x-lowcode-org': getTenant() | ||
} | ||
} | ||
} | ||
|
||
const options = { | ||
axiosConfig: getConfig(), | ||
enableMock: false, | ||
mockData: {}, | ||
interceptors: { | ||
request: [preRequest], | ||
response: [[preResponse, errorResponse]] | ||
const customizeHttpService = () => { | ||
const options = { | ||
axiosConfig: getConfig(), | ||
interceptors: { | ||
request: [preRequest], | ||
response: [[preResponse, errorResponse]] | ||
} | ||
} | ||
} | ||
|
||
HttpService.apis.setOptions(options) | ||
HttpService.apis.setOptions(options) | ||
|
||
return HttpService | ||
} | ||
|
||
export default HttpService | ||
export default customizeHttpService() |
Oops, something went wrong.