| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- import { readFileSync, writeFileSync } from "fs";
- import { join, dirname } from "path";
- import { fileURLToPath } from "url";
- // 获取当前文件路径
- const __filename = fileURLToPath(import.meta.url);
- const __dirname = dirname(__filename);
- // 权重配置
- const weightConfig = {
- 1: [1, 72, 43, 49, 11, 15, 7, 5, 80, 39],
- 2: [42, 2, 112, 90, 95, 8, 61, 51, 53],
- 3: [74, 25, 21, 114, 67, 35, 10, 94, 27],
- 4: [149, 136, 132, 126],
- 5: [117, 121, 123, 116],
- 6: [102],
- "3d": [1, 157, 159, 14, 13, 15, 153, 2, 11, 158],
- };
- function transformJsonData(originalData) {
- const newImages = [];
- let imageIdCounter = 1;
- // 首先处理权重分配
- const weightedItems = originalData.map((item) => {
- const newItem = { ...item };
- // 遍历权重配置
- for (const [type, ids] of Object.entries(weightConfig)) {
- if (String(newItem.type) === type) {
- const index = ids.findIndex((id) => id === newItem.id);
- if (index !== -1) {
- // 计算权重:100 - (index * 10)
- newItem.weight = 100 - index * 10;
- break; // 找到匹配后跳出循环
- }
- }
- }
- return newItem;
- });
- // 然后进行其他转换
- const transformedData = weightedItems.map((item) => {
- const newItem = { ...item };
- // 重命名字段
- if (newItem.title !== undefined) {
- newItem.type = newItem.title;
- delete newItem.title;
- }
- if (newItem.desc !== undefined) {
- newItem.intro = newItem.desc;
- delete newItem.desc;
- }
- if (newItem.vrLink !== undefined) {
- newItem.vrUrl = newItem.vrLink;
- delete newItem.vrLink;
- }
- if (newItem.modelLink !== undefined) {
- newItem.modelUrl = newItem.modelLink;
- delete newItem.modelLink;
- }
- if (newItem.audio !== undefined) {
- newItem.audioPath = newItem.audio;
- delete newItem.audio;
- }
- // 拆分location
- if (newItem.location !== undefined) {
- const [lon, lat] = newItem.location.split(",");
- newItem.lat = parseFloat(lat);
- newItem.lon = parseFloat(lon);
- delete newItem.location;
- }
- // 处理images
- if (newItem.images && Array.isArray(newItem.images)) {
- const imageIds = [];
- newItem.images.forEach((imagePath) => {
- newImages.push({
- id: imageIdCounter,
- goodsId: newItem.id,
- path: imagePath,
- });
- imageIds.push(imageIdCounter.toString());
- imageIdCounter++;
- });
- newItem.imageIds = imageIds.join(",");
- delete newItem.images;
- }
- return newItem;
- });
- return {
- transformedData,
- newImages,
- };
- }
- // 主函数
- async function main() {
- try {
- // 读取原始JSON文件
- const inputFilePath = join(__dirname, "./map.json");
- const originalData = JSON.parse(readFileSync(inputFilePath, "utf-8"));
- // 转换数据
- const result = transformJsonData(originalData);
- // 写入转换后的主数据
- const outputMainPath = join(__dirname, "transformed_main.json");
- writeFileSync(
- outputMainPath,
- JSON.stringify(result.transformedData, null, 2)
- );
- // 写入新的图片数据
- const outputImagesPath = join(__dirname, "transformed_images.json");
- writeFileSync(outputImagesPath, JSON.stringify(result.newImages, null, 2));
- console.log("转换完成!");
- console.log(`主数据已保存到: ${outputMainPath}`);
- console.log(`图片数据已保存到: ${outputImagesPath}`);
- } catch (error) {
- console.error("处理过程中发生错误:", error);
- }
- }
- // 执行主函数
- main();
|