-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: editorInfo migrate to globalService (#702)
* feat: editorInfo migrate to globalService * feat: change to signal event * refactor: useApp migrate to globalService * refactor: optimize globalService state structure * feat: metaservice provide default apis: getState and setState * feat: poc refactor * feat: service poc improve * feat: improve service definition * feat: reduce the props exposed by service * fix: rebase to latest code * fix: change by review comment * fix: change import name * fix: use constant var instead of string * fix: adjust import order * fix: add unsubscribe logic * change by review * fix: change by review comment * fix: add initTimeout check --------- Co-authored-by: gene9831 <[email protected]>
- Loading branch information
1 parent
4697f07
commit 48687e8
Showing
48 changed files
with
515 additions
and
435 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { useHttp } from '@opentiny/tiny-engine-http' | ||
import { useMessage, useModal, defineService, META_SERVICE } from '@opentiny/tiny-engine-meta-register' | ||
import { watch } from 'vue' | ||
|
||
const getBaseInfo = () => { | ||
const paramsMap = new URLSearchParams(location.search) | ||
const id = paramsMap.get('id') | ||
const blockId = paramsMap.get('blockid') | ||
const pageId = paramsMap.get('pageid') | ||
const type = paramsMap.get('type') | ||
const version = paramsMap.get('version') | ||
|
||
return { | ||
type: type || 'app', | ||
id, | ||
pageId, | ||
blockId, | ||
version | ||
} | ||
} | ||
|
||
const initialState = { | ||
userInfo: null, | ||
// 当前应用 | ||
appInfo: { | ||
id: '', | ||
name: '', | ||
app_desc: '', | ||
app_website: '', | ||
obs_url: null, | ||
published_at: '', | ||
created_at: '', | ||
updated_at: '', | ||
platform: '', | ||
state: null, | ||
published: false, | ||
tenant: null, | ||
editor_url: '' | ||
}, | ||
// 应用列表 | ||
appList: [] | ||
} | ||
|
||
const getUserInfo = () => { | ||
// 获取登录用户信息 | ||
return useHttp() | ||
.get('/platform-center/api/user/me') | ||
.catch((error) => { | ||
useModal().message({ message: error.message, status: 'error' }) | ||
}) | ||
} | ||
|
||
// 获取当前应用的信息 | ||
const fetchAppInfo = (appId) => useHttp().get(`/app-center/api/apps/detail/${appId}`) | ||
|
||
// 获取应用列表 | ||
const fetchAppList = (platformId) => useHttp().get(`/app-center/api/apps/list/${platformId}`) | ||
|
||
const { subscribe, publish } = useMessage() | ||
|
||
export default defineService({ | ||
id: META_SERVICE.GlobalService, | ||
type: 'MetaService', | ||
options: {}, | ||
initialState, | ||
init: ({ state }) => { | ||
watch( | ||
() => state.appInfo, | ||
(appInfo) => { | ||
publish({ topic: 'app_info_changed', data: appInfo }) | ||
} | ||
) | ||
|
||
watch( | ||
() => state.appList, | ||
(appList) => { | ||
publish({ topic: 'app_list_changed', data: appList }) | ||
} | ||
) | ||
|
||
subscribe({ | ||
topic: 'app_id_changed', | ||
callback: (appId) => { | ||
if (!appId) { | ||
// eslint-disable-next-line no-console | ||
console.error('Invalid appId received in app_id_changed event') | ||
|
||
return | ||
} | ||
|
||
fetchAppInfo(appId).then((app) => { | ||
state.appInfo = app | ||
// 监听应用 ID 变化,根据应用名称设置网页 title | ||
document.title = `${app.name} —— TinyEditor 前端可视化设计器` | ||
}) | ||
} | ||
}) | ||
|
||
subscribe({ | ||
topic: 'platform_id_changed', | ||
callback: (platformId) => { | ||
if (!platformId) { | ||
// eslint-disable-next-line no-console | ||
console.error('Received platform_id_changed event with no platformId') | ||
|
||
return | ||
} | ||
fetchAppList(platformId).then((list) => { | ||
state.appList = list | ||
}) | ||
} | ||
}) | ||
|
||
getUserInfo().then((data) => { | ||
if (data) { | ||
state.userInfo = data | ||
} | ||
publish({ topic: 'global_service_init_finish' }) | ||
}) | ||
}, | ||
apis: ({ state }) => ({ | ||
getBaseInfo, | ||
isAdmin: () => state.userInfo.resetPasswordToken === 'p_webcenter' | ||
}) | ||
}) |
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,24 +1,2 @@ | ||
import { HOOK_NAME } from '@opentiny/tiny-engine-meta-register' | ||
|
||
import useApp from './useApp' | ||
import useEditorInfo from './useEditorInfo' | ||
|
||
export { GenerateCodeService } from './generateCode' | ||
|
||
export const AppService = { | ||
id: 'engine.service.app', | ||
type: 'MetaService', | ||
apis: useApp(), | ||
composable: { | ||
name: HOOK_NAME.useApp | ||
} | ||
} | ||
|
||
export const EditorInfoService = { | ||
id: 'engine.service.editorInfo', | ||
type: 'MetaService', | ||
apis: useEditorInfo(), | ||
composable: { | ||
name: HOOK_NAME.useEditorInfo | ||
} | ||
} | ||
export { default as GlobalService } from './defaultGlobalService' |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.