feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import cronstrue from 'cronstrue/i18n';
|
||||
import { I18n } from '@coze-arch/i18n';
|
||||
|
||||
const langMap = {
|
||||
// 简体中文
|
||||
'zh-CN': 'zh_CN',
|
||||
zh: 'zh-CN',
|
||||
// 繁体中文
|
||||
zh_TW: 'zh_TW',
|
||||
// 英语
|
||||
'en-US': 'en',
|
||||
en: 'en',
|
||||
// 日语
|
||||
'ja-JP': 'ja',
|
||||
ja: 'ja',
|
||||
// 韩语
|
||||
'ko-KR': 'ko',
|
||||
ko: 'ko',
|
||||
// 法语
|
||||
'fr-FR': 'fr',
|
||||
fr: 'fr',
|
||||
// 德语
|
||||
'de-DE': 'de',
|
||||
de: 'de',
|
||||
// 意大利语
|
||||
'it-IT': 'it',
|
||||
it: 'it',
|
||||
// 西班牙语
|
||||
'es-ES': 'es',
|
||||
es: 'es',
|
||||
};
|
||||
|
||||
// 校验使用 cronjob 翻译结果
|
||||
export const isCronJobVerify = cronJob => {
|
||||
// 仅支持 6 位 cronjob(后端限制)
|
||||
const parts = cronJob?.split(' ');
|
||||
if (parts?.length !== 6) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
const rs = cronstrue.toString(cronJob, {
|
||||
locale: langMap['zh-CN'],
|
||||
throwExceptionOnParseError: true,
|
||||
});
|
||||
|
||||
// 额外校验一下字符串是否包含 null undefined
|
||||
// 1 2 3 31 1- 1 在上午 03:02:01, 限每月 31 号, 或者为星期一, 一月至undefined
|
||||
// 1 2 3 31 1 1#6 在上午 03:02:01, 限每月 31 号, 限每月的null 星期一, 仅于一月份
|
||||
if (rs.includes('null') || rs.includes('undefined')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const cronJobTranslator = (
|
||||
cronJob?: string,
|
||||
errorMsg?: string,
|
||||
): string => {
|
||||
if (!cronJob) {
|
||||
return '';
|
||||
}
|
||||
const lang = I18n.getLanguages();
|
||||
|
||||
if (isCronJobVerify(cronJob)) {
|
||||
return cronstrue.toString(cronJob, {
|
||||
locale: langMap[lang[0]] ?? langMap['zh-CN'],
|
||||
use24HourTimeFormat: true,
|
||||
});
|
||||
}
|
||||
return (
|
||||
errorMsg ??
|
||||
I18n.t('workflow_trigger_param_unvalid_cron', {}, '参数为非法 cron 表达式')
|
||||
);
|
||||
};
|
||||
20
frontend/packages/workflow/components/src/utils/index.ts
Normal file
20
frontend/packages/workflow/components/src/utils/index.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* eslint-disable @coze-arch/no-batch-import-or-export */
|
||||
export * from './utils';
|
||||
|
||||
export { cronJobTranslator, isCronJobVerify } from './cronjob-translator';
|
||||
75
frontend/packages/workflow/components/src/utils/utils.ts
Normal file
75
frontend/packages/workflow/components/src/utils/utils.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
let getIsIPadCache: boolean | undefined;
|
||||
/**
|
||||
* gpt-4 提供的代码
|
||||
*/
|
||||
export const getIsIPad = () => {
|
||||
if (typeof getIsIPadCache === 'undefined') {
|
||||
const { userAgent } = navigator;
|
||||
const isIPadDevice = /iPad/.test(userAgent);
|
||||
const isIPadOS =
|
||||
userAgent.includes('Macintosh') &&
|
||||
'ontouchstart' in document.documentElement;
|
||||
|
||||
getIsIPadCache = isIPadDevice || isIPadOS;
|
||||
}
|
||||
|
||||
return getIsIPadCache;
|
||||
};
|
||||
|
||||
/* 时间戳转文本,并省略年份或日期*/
|
||||
export const formatOmittedDateTime = time => {
|
||||
if (!time) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const day = dayjs(time);
|
||||
const today = dayjs();
|
||||
|
||||
let formatStr: string;
|
||||
|
||||
if (!today.isSame(day, 'year')) {
|
||||
// 不是当年,展示年份
|
||||
formatStr = 'YYYY-MM-DD HH:mm';
|
||||
} else if (!today.isSame(day, 'day')) {
|
||||
// 不是当天, 展示日期
|
||||
formatStr = 'MM-DD HH:mm';
|
||||
} else {
|
||||
// 当天只展示时间
|
||||
formatStr = 'HH:mm';
|
||||
}
|
||||
|
||||
return day.format(formatStr);
|
||||
};
|
||||
|
||||
/** 等待 */
|
||||
export const wait = (ms: number) =>
|
||||
new Promise(r => {
|
||||
setTimeout(r, ms);
|
||||
});
|
||||
|
||||
import { reporter as infraReporter } from '@coze-arch/logger';
|
||||
|
||||
/**
|
||||
* 流程使用的 slardar 上报实例
|
||||
*/
|
||||
export const reporter = infraReporter.createReporterWithPreset({
|
||||
namespace: 'workflow',
|
||||
});
|
||||
Reference in New Issue
Block a user