-
Notifications
You must be signed in to change notification settings - Fork 452
feat:支持了i18n的mock的CRUD持久化,支持了预览时i18n正确使用修改后内容,移除不必要的mock json文件 #1720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
timtian
wants to merge
1
commit into
opentiny:develop
Choose a base branch
from
timtian:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−4
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
mockServer/src/mock/post/app-center/i18n/entries/bulk/delete.json
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,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 { | ||
| 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}) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里为什么要随机日期时间呢?可以按照实际的日期时间? |
||
| } | ||
|
|
||
| result.push(record) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| return transform(sourceData, startId) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里类名需要重命名一下,比如:
I18nService