const fs = require('fs'); const path = require('path'); const basieTilesPath = "F:/"; //获取气象层级 exports.getMap = async (req, res) => { const { z, x, y } = req.params; // 支持多种图片格式,优先尝试jpg // const tilePaths = [ // path.join('F:', (z > 10 ? '地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/' : '地图瓦片全球1-10级_瓦片:谷歌/tiles/'), z, x, `${y}.jpg`), // path.join('F:', (z > 10 ? '地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/' : '地图瓦片全球1-10级_瓦片:谷歌/tiles/'), z, x, `${y}.png`), // path.join('F:', (z > 10 ? '地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/' : '地图瓦片全球1-10级_瓦片:谷歌/tiles/'), z, x, `${y}.webp`) // ]; const path = z > 10 ? `${basieTilesPath}地图瓦片/河南-安阳-滑县11-18级_瓦片:谷歌/tiles/${z}/${x}/${y}` : `${basieTilesPath}地图瓦片/全球1-10级_瓦片:谷歌/tiles/${z}/${x}/${y}`; const tilePaths = [ `${path}.jpg`, `${path}.png`, `${path}.webp`, ]; // 查找存在的瓦片文件 let foundTile = null; for (const tilePath of tilePaths) { if (fs.existsSync(tilePath)) { foundTile = tilePath; break; } } if (foundTile) { // 设置缓存头 res.setHeader('Cache-Control', 'public, max-age=604800'); // 缓存一周 res.setHeader('Expires', new Date(Date.now() + 604800000).toUTCString()); // 发送文件 res.sendFile(foundTile); } else { res.status(404).send('Tile not found'); } }