admin_web/apps/web-antd/src/store/util.ts
hahwu dbfb3c4bd9
Some checks failed
CI / Test (ubuntu-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Lint (ubuntu-latest) (push) Has been cancelled
CI / Lint (windows-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CodeQL / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Has been cancelled
Deploy Website on push / Deploy Push Playground Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Docs Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Antd Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Element Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Naive Ftp (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
CI / CI OK (push) Has been cancelled
版本更新
2026-04-21 16:03:45 +08:00

142 lines
3.8 KiB
TypeScript

import MergeData from './MergeData.json';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);
/**
* 将 Unix 时间戳(秒)格式化为 UTC+8 时区的时间字符串
*/
export function formatUTC8Time(timestamp: number): string {
if (!timestamp) return '';
return dayjs.unix(timestamp).utcOffset(8).format('YYYY-MM-DD HH:mm:ss');
}
export function getImageUrl(key: string): string {
if (!key) return '';
// 1. 判断 key 是否为 UI_MergeData_<number> 格式,取出数字;同时支持直接传入数字字符串或数字
let id: string | null = null;
const m = /^UI_MergeData_(\d+)$/.exec(key);
if (m) id = m[1] ?? '';
else if (/^\d+$/.test(key)) id = key;
else id = String(key);
// 2. 从 MergeData 中取出 id 对应的 Icon
const item = (MergeData as any)[id];
const icon = item && typeof item.Icon === 'string' ? item.Icon : '';
if (!icon) return '';
// 3. 在 Node 环境中遍历指定文件夹寻找包含 Icon 名称的文件并返回绝对路径
// 如果不在 Node 环境(例如浏览器),则回退返回 icon 字符串
try {
// @ts-ignore
const fs = require('fs');
// @ts-ignore
const path = require('path');
const SEARCH_DIR =
'D:\\Github\\AplusB_Pet_nation\\Assets\\GameMain\\UI\\UISprites\\MergeObj';
function findFile(dir: string): string | null {
let entries: string[] = [];
try {
entries = fs.readdirSync(dir);
} catch (e) {
return null;
}
for (const name of entries) {
const full = path.join(dir, name);
let stat;
try {
stat = fs.statSync(full);
} catch (e) {
continue;
}
if (stat.isDirectory()) {
const res = findFile(full);
if (res) return res;
} else {
if (name.indexOf(icon) !== -1) return path.resolve(full);
}
}
return null;
}
const found = findFile(SEARCH_DIR);
if (found) return found;
} catch (e) {
// 非 Node 环境或访问失败,忽略并回退
}
// 回退:返回 icon 名称(供前端拼接资源路径使用)
return icon;
}
export const getUnixTime = (date: any): number => {
if (date) {
if (typeof date === 'string') {
return dayjs(date).unix();
} else if (dayjs.isDayjs(date)) {
return date.unix();
} else if (typeof (date as any).unix === 'function') {
return (date as any).unix();
} else {
return dayjs(date).unix();
}
}
return 0;
};
export const getItemUrl = (itemId: number): string => {
switch (itemId) {
case 100001:
return './Assets/Art_SubModule/Art_Resource/Art_UISprites/Shop/Big/shop_energy_LV1.png';
case 100002:
return './Assets/GameMain/UI/UISprites/MergeObj/Packed/Other/Production_star_LV1.png';
case 100003:
return './Assets/Art_SubModule/Art_Resource/Art_UISprites/Shop/Big/shop_diamond_LV2.png';
default:
return '';
}
};
export const formatItems = (items: string) => {
try {
const itemList = JSON.parse(items);
const r = itemList.map((item: { Id: number; Num: number }) => ({
...item,
url: getItemUrl(item.Id),
}));
return r;
} catch (e) {
//console.error('Failed to parse items:', e);
return [];
}
}
export const parseNumber = (param: any) => {
try{
switch(typeof(param)){
case 'string':
return parseInt(param, 10);
case 'number':
return param;
default:
return 0;
}
}catch(e){
return 0;
}
}
export const formatJsonStr = (jsonStr: string): string => {
try {
return JSON.stringify(JSON.parse(jsonStr))
.replace(/\\r?\\n/g, '')
.replace(/\r?\n/g, '');
} catch (e) {
console.error('Failed to format JSON string:', e);
return jsonStr;
}
};