From df4b0d97bddff3e6fbb8e3d73620015b03e3e2af Mon Sep 17 00:00:00 2001 From: lu17301156525 Date: Mon, 24 Mar 2025 00:18:56 -0700 Subject: [PATCH 1/2] fix: page API modification --- app/controller/app-center/pages.ts | 6 +- app/lib/interface.ts | 7 ++- app/service/app-center/pages.ts | 99 +++++++++++++++++++++--------- app/validate/app-center/pages.ts | 4 ++ config/locale/en-US.json | 2 +- config/locale/zh-CN.json | 2 +- 6 files changed, 83 insertions(+), 37 deletions(-) diff --git a/app/controller/app-center/pages.ts b/app/controller/app-center/pages.ts index 4e05095..bf30732 100644 --- a/app/controller/app-center/pages.ts +++ b/app/controller/app-center/pages.ts @@ -11,7 +11,7 @@ */ import { Controller } from 'egg'; import { E_Schema2CodeType } from '../../lib/enum'; -import { +import { dslCodeRule, schema2codeRule, createPageRule, @@ -120,7 +120,7 @@ class PagesController extends Controller { // 更新页面 const updatePageParam = this.ctx.helper.pickObject( { id, ...body }, - ['id', 'isBody', 'isHome', 'message', 'name', 'parentId', 'route', 'page_content'] + ['id', 'isBody', 'isHome', 'message', 'name', 'parentId', 'route', 'page_content', 'isDefault'] ); this.ctx.validate(updatePageRule, updatePageParam); this.ctx.body = await pages.updatePage(updatePageParam); @@ -150,7 +150,7 @@ class PagesController extends Controller { * @router GET /api/code * @summary 获取页面或者区块的代码 */ - async dslCode() { + async dslCode() { const { type, id, history, app } = this.ctx.query; const { appCenter } = this.ctx.service; this.ctx.validate(dslCodeRule, this.ctx.query); diff --git a/app/lib/interface.ts b/app/lib/interface.ts index e422040..f56a51a 100644 --- a/app/lib/interface.ts +++ b/app/lib/interface.ts @@ -9,8 +9,8 @@ * A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS. * */ -import { RequestOptions2 as RequestOptions, HttpMethod } from 'urllib'; -import { E_TaskStatus, E_TaskType, E_SchemaFormatFunc, E_ProjectState, E_Framework, E_Schema2CodeType } from './enum'; +import { HttpMethod, RequestOptions2 as RequestOptions } from 'urllib'; +import { E_Framework, E_ProjectState, E_Schema2CodeType, E_SchemaFormatFunc, E_TaskStatus, E_TaskType } from './enum'; /** **************系统接口*******************/ // service 数据中心通用参数 @@ -148,6 +148,7 @@ export interface I_UpdatePageParam { depth?: number; message?: string; path_name?: string; + isDefault?: boolean; } // 页面历史 @@ -462,7 +463,7 @@ export interface I_CreateComponentLibrary { export interface I_CreateComponentLibraryParam { component: Array, componentLibrary: I_CreateComponentLibrary -} +} // AI聊天消息 export interface IAiMessage { role: string; // 角色 diff --git a/app/service/app-center/pages.ts b/app/service/app-center/pages.ts index 7dda570..61731b0 100644 --- a/app/service/app-center/pages.ts +++ b/app/service/app-center/pages.ts @@ -1,19 +1,25 @@ /** -* 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. -* -*/ + * 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 { StatusCodes } from 'http-status-codes'; import * as qs from 'querystring'; import { ApiError, throwApiError } from '../../lib/ApiError'; import { E_AppErrorCode, E_ErrorCode, E_Framework, E_Method, E_Schema2CodeType, E_TYPES } from '../../lib/enum'; -import { I_CreatePageParam, I_GetOutcomeParam, I_Response, I_TranslateSchemaParam, I_UpdatePageParam } from '../../lib/interface'; +import { + I_CreatePageParam, + I_GetOutcomeParam, + I_Response, + I_TranslateSchemaParam, + I_UpdatePageParam +} from '../../lib/interface'; import DataService from '../dataService'; class Pages extends DataService { @@ -53,7 +59,7 @@ class Pages extends DataService { }); const res: Array = []; - pageList.data.forEach(item => { + pageList.data.forEach((item) => { if (item.isHome === '1') { item.isHome = true; res.unshift(item); @@ -76,9 +82,6 @@ class Pages extends DataService { return await this.service.appCenter.folders.del(id); } - // 保护默认页面 - await this.protectDefaultPage(pageInfo, pageInfo.data.app); - return await this.fQuery({ url: `pages/${id}`, method: E_Method.Delete @@ -96,8 +99,10 @@ class Pages extends DataService { const { app } = pageInfo.data; - // 保护默认页面 - await this.protectDefaultPage(pageInfo, app); + // 默认页面 + if (param.isDefault) { + await this.protectDefaultPage(pageInfo); + } let res: any = {}; // 针对参数中isHome的传值进行isHome字段的判定 @@ -131,16 +136,52 @@ class Pages extends DataService { return updatePageInfo; } - async protectDefaultPage(pageInfo: I_Response, id: number) { - if (pageInfo.data.isDefault) { - // 查询是否是模板应用,不是的话不能删除或修改 - const app = await this.service.appCenter.apps.findOne({ id }); - if (!app.data?.template_type) { - throw (new ApiError('', E_ErrorCode.BadRequest, E_AppErrorCode.CM301)); - } + async protectDefaultPage(pageInfo: I_Response) { + const id = pageInfo.data.parentId; + const parentId = await this.getParentPage(id); + const subPageId = await this.getSubPage(parentId); + if (subPageId === false) { + return true; + } + const param = { + isDefault: false + }; + const updatePageInfo = await this.fQuery({ + url: `pages/${subPageId}`, + method: E_Method.Put, + data: param + }); + if (!updatePageInfo.data.id) { + throw new ApiError('', E_ErrorCode.BadRequest, E_AppErrorCode.CM301); + } + } + + protected async getParentPage(parentId: string) { + const page = await this.getPageById(Number(parentId)); + if (page.data.isPage || page.data.parentId === '0') { + return parentId; } + return this.getParentPage(page.data.parentId); } + protected async getSubPage(parentId: string) { + if (parentId === '0') { + return false; + } + const pageList = await this.find({ parent_id: parentId }); + // 查找默认子页面 + for (const item of pageList.data) { + if (item.isPage && item.isDefault) { + return item.id; + } else if (!item.isPage) { + const subPageId = await this.getSubPage(item.id); + if (subPageId !== false) { + return subPageId; + } + } + } + return false; + } async createPage(param: I_CreatePageParam) { const { folderTree } = this.ctx.service.appCenter; // 判断isHome 为true时,parentId 不为0,禁止创建 @@ -189,7 +230,7 @@ class Pages extends DataService { /** * 调用dsl转换页面或区块的schema为代码 - * @param { I_GetOutcomeParam } params + * @param { I_GetOutcomeParam } params * @returns { Promise } dsl库返回解析代码的对象 */ async getOutcome(params: I_GetOutcomeParam): Promise { @@ -223,7 +264,7 @@ class Pages extends DataService { * @param { any } pageInfo 页面内容数据 * @param { E_Schema2CodeType } type 处理数据类型, 默认 Page * @return { Promise } dsl函数返回数据 - */ + */ schema2code(appId: string | number, pageInfo: any, type: E_Schema2CodeType = E_Schema2CodeType.PAGE): Promise { const { schema, name } = pageInfo; return this.translateSchema({ @@ -240,7 +281,7 @@ class Pages extends DataService { * @param { number|string } id 页面或区块id * @param { number|string } app 应用id * @return { Promise } - */ + */ getPreviewMetaData(type: E_Schema2CodeType, id: number | string, app: number | string): Promise { const { appCenter, materialCenter } = this.service; if (type === E_Schema2CodeType.BLOCK) { @@ -261,7 +302,7 @@ class Pages extends DataService { * 通过appId 获取 dsl 必须的 components 和 blocksData数据 * @param { string|number } appId 应用id * @return {any} 返回对应参数 - */ + */ protected async getDslParamByAppId(appId: string | number) { const { appCenter } = this.service; // 获取应用下的物料资产包 @@ -283,7 +324,7 @@ class Pages extends DataService { * 通过dsl 将页面/区块schema数据生成对应代码 * @param { I_TranslateSchemaParam } params * @return {Promise} dsl函数返回数据 - */ + */ protected async translateSchema(params: I_TranslateSchemaParam): Promise { const { appCenter } = this.service; const { schema, name, type = E_Schema2CodeType.PAGE, appId, framework: customFramework } = params; diff --git a/app/validate/app-center/pages.ts b/app/validate/app-center/pages.ts index faedbdb..e8d859a 100644 --- a/app/validate/app-center/pages.ts +++ b/app/validate/app-center/pages.ts @@ -78,6 +78,10 @@ export const updatePageRule = { type: 'object', rule: { componentName: ['Page'] }, required: false + }, + isDefault: { + type: 'boolean', + required: false } }; diff --git a/config/locale/en-US.json b/config/locale/en-US.json index cb12486..73b4eaf 100644 --- a/config/locale/en-US.json +++ b/config/locale/en-US.json @@ -29,7 +29,7 @@ "CM207": "Do not have permission to build this material", "CM208": "Parameter error. The associated template type does not match the group template type.", - "CM301": "default page can not be deleted or modified", + "CM301": "failed to modify the default page", "CM302": "fetch repository failed", "CM303": "checkout to the branch failed, make sure it exists?", "CM304": "checkout to new branch failed", diff --git a/config/locale/zh-CN.json b/config/locale/zh-CN.json index c54d0af..a231e97 100644 --- a/config/locale/zh-CN.json +++ b/config/locale/zh-CN.json @@ -29,7 +29,7 @@ "CM207": "无权限构建该物料", "CM208": "参数错误,关联的模板类型与分组模板类型不符", - "CM301": "默认页面不能删除和修改", + "CM301": "默认页面修改失败", "CM302": "clone仓库到本地失败", "CM303": "切换到分支失败,请确定分支是否存在", "CM304": "创建并切换到新分支失败", From 124ae1894052a586c430eb6055fce0d2a38501fe Mon Sep 17 00:00:00 2001 From: lu17301156525 Date: Mon, 24 Mar 2025 03:02:03 -0700 Subject: [PATCH 2/2] fix: page API modification --- app/service/app-center/pages.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/service/app-center/pages.ts b/app/service/app-center/pages.ts index 61731b0..9d0d926 100644 --- a/app/service/app-center/pages.ts +++ b/app/service/app-center/pages.ts @@ -174,10 +174,7 @@ class Pages extends DataService { if (item.isPage && item.isDefault) { return item.id; } else if (!item.isPage) { - const subPageId = await this.getSubPage(item.id); - if (subPageId !== false) { - return subPageId; - } + return this.getSubPage(item.id); } } return false;