diff --git a/designer-demo/registry.js b/designer-demo/registry.js index 72bcf8296..88a463480 100644 --- a/designer-demo/registry.js +++ b/designer-demo/registry.js @@ -41,16 +41,15 @@ import { Styles, Layout, Canvas, - EditorInfoService, - AppService, - GenerateCodeService + GenerateCodeService, + GlobalService } from '@opentiny/tiny-engine' import engineConfig from './engine.config' export default { root: { id: 'engine.root', - metas: [EditorInfoService, AppService, GenerateCodeService] + metas: [GenerateCodeService, GlobalService] }, config: engineConfig, layout: { diff --git a/designer-demo/src/main.js b/designer-demo/src/main.js index 19d563cb0..6f29317b2 100644 --- a/designer-demo/src/main.js +++ b/designer-demo/src/main.js @@ -17,4 +17,8 @@ import { configurators } from './configurators/' import 'virtual:svg-icons-register' import '@opentiny/tiny-engine-theme' -init({ registry, configurators }) +init({ + registry, + configurators, + createAppSignal: ['global_service_init_finish'] +}) diff --git a/packages/common/composable/defaultGlobalService.js b/packages/common/composable/defaultGlobalService.js new file mode 100644 index 000000000..f62f05635 --- /dev/null +++ b/packages/common/composable/defaultGlobalService.js @@ -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' + }) +}) diff --git a/packages/common/composable/index.js b/packages/common/composable/index.js index 75268a70d..7abca1078 100644 --- a/packages/common/composable/index.js +++ b/packages/common/composable/index.js @@ -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' diff --git a/packages/common/composable/useApp.js b/packages/common/composable/useApp.js deleted file mode 100644 index 88a97773a..000000000 --- a/packages/common/composable/useApp.js +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright (c) 2023 - present TinyEngine Authors. - * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. - * - * Use of this source code is governed by an MIT-style license. - * - * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, - * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR - * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. - * - */ - -import { reactive, watch } from 'vue' -import { useHttp } from '@opentiny/tiny-engine-http' - -const defaultState = { - // 应用列表 - list: [], - // 当前选中的应用 - selectedApp: { - id: '', - name: '', - app_desc: '', - app_website: '', - obs_url: null, - published_at: '', - created_at: '', - updated_at: '', - platform: '', - state: null, - published: false, - tenant: null, - editor_url: '' - }, - // 当前选中的appId - selectedId: '' -} - -const appInfoState = reactive({ ...defaultState }) - -// 获取当前应用的信息 -const fetchAppInfo = (appId) => useHttp().get(`/app-center/api/apps/detail/${appId}`) - -watch( - () => appInfoState.selectedId, - (id) => { - fetchAppInfo(id).then((app) => { - appInfoState.selectedApp = app - // 监听应用 ID 变化,根据应用名称设置网页 title - document.title = `${app.name} —— TinyEditor 前端可视化设计器` - }) - } -) - -// 获取应用列表 -const fetchAppList = (platformId) => useHttp().get(`/app-center/api/apps/list/${platformId}`) - -const updateApp = async (id) => { - const appInfo = await fetchAppInfo(id) - appInfoState.selectedApp = appInfo - appInfoState.selectedId = appInfo.id -} - -export default () => { - return { - appInfoState, - fetchAppInfo, - fetchAppList, - updateApp - } -} diff --git a/packages/common/composable/useEditorInfo.js b/packages/common/composable/useEditorInfo.js deleted file mode 100644 index 250642b24..000000000 --- a/packages/common/composable/useEditorInfo.js +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Copyright (c) 2023 - present TinyEngine Authors. - * Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd. - * - * Use of this source code is governed by an MIT-style license. - * - * THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, - * BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR - * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. - * - */ - -import { ref } from 'vue' -import { useHttp } from '@opentiny/tiny-engine-http' -import { useModal } from '@opentiny/tiny-engine-meta-register' - -// web版获取配置信息: 从url中获取 -const _getWebData = () => { - 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 userInfo = ref({}) -const getUserInfo = () => { - // 获取登录用户信息 - useHttp() - .get('/platform-center/api/user/me') - .then((data) => { - if (data) { - userInfo.value = data - } - }) - .catch((error) => { - useModal().message({ message: error.message, status: 'error' }) - }) -} - -const isAdmin = () => userInfo.value.resetPasswordToken === 'p_webcenter' -/** - * 1、是否是VSCode插件: 通过是否有全局变量window.vscodeBridge判断 - * - * 2、vscode中类型和id - * window.vscodeInjectData - * type: app 应用管理 block 区块管理 - * id: 应用id/blockid - * ...其他详细信息 - * - * 3、web版中,通过url参数判断 - * type: app 应用管理 block 区块管理 - * id: 应用id/blockid - * - */ -export default () => { - return { - useInfo: _getWebData, - getUserInfo, - userInfo, - isAdmin - } -} diff --git a/packages/common/js/canvas.js b/packages/common/js/canvas.js index bdf88ae3c..50d526b0d 100644 --- a/packages/common/js/canvas.js +++ b/packages/common/js/canvas.js @@ -11,12 +11,13 @@ */ import { PAGE_STATUS } from './constants' -import { useEditorInfo, useResource } from '@opentiny/tiny-engine-meta-register' +import { useResource, getMetaApi, META_SERVICE } from '@opentiny/tiny-engine-meta-register' export const getCanvasStatus = (data) => { + const globalState = getMetaApi(META_SERVICE.GlobalService).getState() // 写死ID 待删除 let isDemo = useResource().resState.isDemo - const { resetPasswordToken } = useEditorInfo().userInfo.value + const { resetPasswordToken } = globalState.userInfo if (isDemo && [PAGE_STATUS.Developer, PAGE_STATUS.SuperAdmin].includes(resetPasswordToken)) { isDemo = false @@ -29,7 +30,7 @@ export const getCanvasStatus = (data) => { } else if (!data) { state = PAGE_STATUS.Release } else { - state = useEditorInfo().userInfo.value.id === data.id ? PAGE_STATUS.Occupy : PAGE_STATUS.Lock + state = globalState.userInfo.id === data.id ? PAGE_STATUS.Occupy : PAGE_STATUS.Lock } return { diff --git a/packages/configurator/src/collection-configurator/CollectionConfigurator.vue b/packages/configurator/src/collection-configurator/CollectionConfigurator.vue index cc4c695ff..9b4448bbf 100644 --- a/packages/configurator/src/collection-configurator/CollectionConfigurator.vue +++ b/packages/configurator/src/collection-configurator/CollectionConfigurator.vue @@ -10,7 +10,7 @@