transformMap.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { readFileSync, writeFileSync } from "fs";
  2. import { join, dirname } from "path";
  3. import { fileURLToPath } from "url";
  4. // 获取当前文件路径
  5. const __filename = fileURLToPath(import.meta.url);
  6. const __dirname = dirname(__filename);
  7. // 权重配置
  8. const weightConfig = {
  9. 1: [1, 72, 43, 49, 11, 15, 7, 5, 80, 39],
  10. 2: [42, 2, 112, 90, 95, 8, 61, 51, 53],
  11. 3: [74, 25, 21, 114, 67, 35, 10, 94, 27],
  12. 4: [149, 136, 132, 126],
  13. 5: [117, 121, 123, 116],
  14. 6: [102],
  15. "3d": [1, 157, 159, 14, 13, 15, 153, 2, 11, 158],
  16. };
  17. function transformJsonData(originalData) {
  18. const newImages = [];
  19. let imageIdCounter = 1;
  20. // 首先处理权重分配
  21. const weightedItems = originalData.map((item) => {
  22. const newItem = { ...item };
  23. // 遍历权重配置
  24. for (const [type, ids] of Object.entries(weightConfig)) {
  25. if (String(newItem.type) === type) {
  26. const index = ids.findIndex((id) => id === newItem.id);
  27. if (index !== -1) {
  28. // 计算权重:100 - (index * 10)
  29. newItem.weight = 100 - index * 10;
  30. break; // 找到匹配后跳出循环
  31. }
  32. }
  33. }
  34. return newItem;
  35. });
  36. // 然后进行其他转换
  37. const transformedData = weightedItems.map((item) => {
  38. const newItem = { ...item };
  39. // 重命名字段
  40. if (newItem.title !== undefined) {
  41. newItem.type = newItem.title;
  42. delete newItem.title;
  43. }
  44. if (newItem.desc !== undefined) {
  45. newItem.intro = newItem.desc;
  46. delete newItem.desc;
  47. }
  48. if (newItem.vrLink !== undefined) {
  49. newItem.vrUrl = newItem.vrLink;
  50. delete newItem.vrLink;
  51. }
  52. if (newItem.modelLink !== undefined) {
  53. newItem.modelUrl = newItem.modelLink;
  54. delete newItem.modelLink;
  55. }
  56. if (newItem.audio !== undefined) {
  57. newItem.audioPath = newItem.audio;
  58. delete newItem.audio;
  59. }
  60. // 拆分location
  61. if (newItem.location !== undefined) {
  62. const [lon, lat] = newItem.location.split(",");
  63. newItem.lat = parseFloat(lat);
  64. newItem.lon = parseFloat(lon);
  65. delete newItem.location;
  66. }
  67. // 处理images
  68. if (newItem.images && Array.isArray(newItem.images)) {
  69. const imageIds = [];
  70. newItem.images.forEach((imagePath) => {
  71. newImages.push({
  72. id: imageIdCounter,
  73. goodsId: newItem.id,
  74. path: imagePath,
  75. });
  76. imageIds.push(imageIdCounter.toString());
  77. imageIdCounter++;
  78. });
  79. newItem.imageIds = imageIds.join(",");
  80. delete newItem.images;
  81. }
  82. return newItem;
  83. });
  84. return {
  85. transformedData,
  86. newImages,
  87. };
  88. }
  89. // 主函数
  90. async function main() {
  91. try {
  92. // 读取原始JSON文件
  93. const inputFilePath = join(__dirname, "./map.json");
  94. const originalData = JSON.parse(readFileSync(inputFilePath, "utf-8"));
  95. // 转换数据
  96. const result = transformJsonData(originalData);
  97. // 写入转换后的主数据
  98. const outputMainPath = join(__dirname, "transformed_main.json");
  99. writeFileSync(
  100. outputMainPath,
  101. JSON.stringify(result.transformedData, null, 2)
  102. );
  103. // 写入新的图片数据
  104. const outputImagesPath = join(__dirname, "transformed_images.json");
  105. writeFileSync(outputImagesPath, JSON.stringify(result.newImages, null, 2));
  106. console.log("转换完成!");
  107. console.log(`主数据已保存到: ${outputMainPath}`);
  108. console.log(`图片数据已保存到: ${outputImagesPath}`);
  109. } catch (error) {
  110. console.error("处理过程中发生错误:", error);
  111. }
  112. }
  113. // 执行主函数
  114. main();