Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

12 changes: 12 additions & 0 deletions mockServer/src/routes/main-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,16 @@ router.post('block-history/create', async (ctx) => {
ctx.body = await mockService.blockHistoryService.create(ctx.request.body)
})

router.post('/app-center/api/i18n/entries/bulk/delete', async (ctx) => {
ctx.body = await mockService.i18nService.delete(ctx.request.body)
})

router.post('/app-center/api/i18n/entries/update', async (ctx) => {
ctx.body = await mockService.i18nService.update(ctx.request.body)
})

router.post('/app-center/api/i18n/entries/create', async (ctx) => {
ctx.body = await mockService.i18nService.create(ctx.request.body)
})

export default router
10 changes: 8 additions & 2 deletions mockServer/src/services/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
*
*/

import fs from 'fs-extra'
import path from 'path'
import { mockService } from '../routes/main-routes'
import { getResponseData } from '../tool/Common'
import { getResponseData, transformI18nMock } from '../tool/Common'
export default class AppService {
async lock(query) {
const { id, state } = query
Expand All @@ -27,7 +29,11 @@ export default class AppService {
getAppPreviewMetaData() {
const appMetaData = require('../assets/json/appinfo.json')

const { i18n: i18nEntries, source = [], extension = [], app } = appMetaData
const schemaFilePath = path.resolve(process.cwd(), './src/mock/get/app-center/v1/apps/schema/1.json')
const schemaData = fs.readJSONSync(schemaFilePath)
const i18nEntries = transformI18nMock(schemaData?.data || {})

const { source = [], extension = [], app } = appMetaData
// 拼装数据源
const dataSource = {
list: source,
Expand Down
42 changes: 42 additions & 0 deletions mockServer/src/services/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import path from 'path'
import fs from 'fs-extra'
import { getResponseData } from '../tool/Common'

const schemaFilePath = path.resolve(process.cwd(), './src/mock/get/app-center/v1/apps/schema/1.json')
export default class PageService {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里类名需要重命名一下,比如: I18nService

constructor() {

}

async create(params) {
return this.update(params)
}

async update(params) {
const schemaData = fs.readJSONSync(schemaFilePath)

Object.entries(schemaData?.data?.i18n || {}).forEach(([lang, translations]) => {
translations[params.key] = params.contents[lang] || ''
})

fs.writeJSONSync(schemaFilePath, schemaData, { spaces: 2 })
return getResponseData({ "data": [], "locale": "zh-cn" ,"mock": true})
}

async delete(params) {
//读取schema文件
const schemaData = fs.readJSONSync(schemaFilePath)

Object.entries(schemaData?.data?.i18n || {}).forEach(([lang, translations]) => {
params.key_in.forEach((key) => {
if (translations.hasOwnProperty(key)) {
delete translations[key]
}
})
})

//写回schema文件
fs.writeJSONSync(schemaFilePath, schemaData, { spaces: 2 })
return getResponseData({ "data": [], "locale": "zh-cn" ,"mock": true})
}
}
3 changes: 3 additions & 0 deletions mockServer/src/services/mockService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import SourceService from './source'
import BlockGroupService from './blockGroup'
import BlockCategoryService from './blockCategory'
import Schema2CodeServcice from './schema2code'
import I18nService from './i18n'
export default class MockService {
schema2codeService
pageService
Expand All @@ -24,6 +25,7 @@ export default class MockService {
sourceService
blockGroupService
blockCategoryService
i18nService

constructor() {
this.schema2codeService = new Schema2CodeServcice()
Expand All @@ -33,5 +35,6 @@ export default class MockService {
this.sourceService = new SourceService()
this.blockGroupService = new BlockGroupService()
this.blockCategoryService = new BlockCategoryService()
this.i18nService = new I18nService()
}
}
73 changes: 73 additions & 0 deletions mockServer/src/tool/Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,76 @@ export const getDatabasePath = (fileName) => {
const databasePath = process.env.DATABASE_PATH || path.resolve(__dirname, '../database')
return path.resolve(databasePath, fileName)
}

export const transformI18nMock = (sourceData, startId = 123) => {
/**
* 生成随机日期字符串 (ISO 格式)
* @returns {string} e.g., "2023-05-15T00:48:10.000Z"
*/
const getRandomDate = () => {
const start = new Date(2022, 0, 1)
const end = new Date()
const date = new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()))
return date.toISOString()
}

/**
* 转换 i18n 数据为 Mock 列表格式
* @param {Object} sourceData - 原始 i18n 对象
* @param {number} startId - 起始 ID
* @returns {Array}
*/
const transform = (sourceData, startId = 123) => {
// 1. 语言基础配置 (用于填充 lang 对象中的静态字段)
const langConfig = {
zh_HK: { id: 1, label: '繁體中文' },
en_US: { id: 2, label: '美式英文' },
zh_CN: { id: 3, label: '简体中文' }
// 可以根据需要补充更多
}

let currentId = startId
const result = []

// 2. 遍历语言 (zh_HK, en_US...)
if (sourceData && sourceData.i18n) {
Object.entries(sourceData.i18n).forEach(([langCode, translations]) => {
// 获取语言元数据,如果没有配置则给默认值
const langMeta = langConfig[langCode] || { id: 99, label: langCode }

// 生成语言对象的通用时间戳 (假设同一种语言创建时间一致,也可以移到下方循环内随机)
const langTimestamp = getRandomDate()

// 3. 遍历该语言下的所有 Key (lowcode.xxx)
Object.entries(translations).forEach(([key, content]) => {
const record = {
id: currentId++,
key: key,
content: content,
host: 1, // 固定值
host_type: 'app', // 固定值
lang: {
id: langMeta.id,
lang: langCode,
label: langMeta.label,
created_by: null,
updated_by: null,
created_at: langTimestamp,
updated_at: langTimestamp
},
created_by: null,
updated_by: null,
created_at: getRandomDate(),
updated_at: getRandomDate()
Comment on lines +142 to +143
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么要随机日期时间呢?可以按照实际的日期时间?

}

result.push(record)
})
})
}

return result
}

return transform(sourceData, startId)
}