Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
104 lines (94 loc) · 4.01 KB
/
Copy pathindex.js
File metadata and controls
104 lines (94 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Hono } from "hono/tiny";
import { getPageHtml } from "./page.js";
import { parseCoords, gcj02ToWgs84, toWgs84, round6, inRange } from "./parse.js";
const app = new Hono();
app.get("/", (c) => {
return c.html(getPageHtml());
});
// 地图链接解析: 供快捷指令调用。
// GET /api/parse?u=<链接>&format=json&cs=<gcj|none>
// 返回 {lat, lon, name}; 高德/苹果地图(中国大陆均为 GCJ-02)自动转 WGS84; 境外坐标自动跳过(out_of_china)。cs=none 可强制不转换。
// 不带 format=json 时返回纯文本 "lat=..&lon=.." 片段。
app.get("/api/parse", async (c) => {
const raw = c.req.query("u") || "";
const cs = (c.req.query("cs") || "").toLowerCase();
const fmt = (c.req.query("format") || "").toLowerCase();
try {
let { lat, lon, name, src } = await parseCoords(raw);
// 默认按来源自动换算; cs=none 强制不转换, cs=gcj/bd 强制按指定坐标系转换。
if (cs === "gcj") ({ lat, lon } = gcj02ToWgs84(lat, lon));
else if (cs === "bd") ({ lat, lon } = toWgs84(lat, lon, "baidu"));
else if (cs !== "none") ({ lat, lon } = toWgs84(lat, lon, src));
// 出口再校验一次: cs= 是调用方指定的, 强行按错误坐标系换算也可能把值推出值域。
// 宁可报错也不要返回一个能被当成坐标写进设备的数字。
if (!inRange(lat, lon)) throw new Error("解析出的坐标超出合法范围");
lat = round6(lat);
lon = round6(lon);
name = name || "";
c.header("Access-Control-Allow-Origin", "*");
if (fmt === "json") return c.json({ lat, lon, name });
return c.text(`lat=${lat}&lon=${lon}`);
} catch (e) {
c.header("Access-Control-Allow-Origin", "*");
return c.json({ error: String(e && e.message ? e.message : e) }, 422);
}
});
// 全球地点搜索: 供 app 的「全球」模式调用。
// GET /api/search?q=<关键词>
// 转发 OpenStreetMap Nominatim, 返回 {results:[{name, detail, lat, lon}]} (均为 WGS-84)。
// 按关键词边缘缓存 7 天 —— 地名基本不变, 重复搜索不回源, 既快又避开 Nominatim 的限速。
app.get("/api/search", async (c) => {
c.header("Access-Control-Allow-Origin", "*");
const q = (c.req.query("q") || "").trim();
if (q.length < 2) return c.json({ results: [] });
const cache = caches.default;
const cacheKey = new Request(
`https://wloc.search.cache/?q=${encodeURIComponent(q.toLowerCase())}`
);
const cached = await cache.match(cacheKey);
if (cached) return c.json(await cached.json());
const api =
`https://nominatim.openstreetmap.org/search?format=jsonv2&limit=8&q=${encodeURIComponent(q)}`;
let raw;
try {
const resp = await fetch(api, {
headers: {
// Nominatim 使用政策要求标识调用方, 不带会被拒。
"User-Agent": "wloc-ios-place-search/1.0",
"Accept-Language": "zh-CN,zh,en",
},
});
if (!resp.ok) return c.json({ results: [] });
raw = await resp.json();
} catch {
return c.json({ results: [] });
}
const results = (Array.isArray(raw) ? raw : [])
.map((r) => ({
name: r.name || String(r.display_name || "").split(",")[0].trim(),
detail: r.display_name || "",
lat: Number(r.lat),
lon: Number(r.lon),
}))
.filter((r) => Number.isFinite(r.lat) && Number.isFinite(r.lon));
const payload = { results };
// 存一份带 TTL 的副本; waitUntil 不阻塞本次返回。
c.executionCtx.waitUntil(
cache.put(
cacheKey,
new Response(JSON.stringify(payload), {
headers: {
"Content-Type": "application/json",
"Cache-Control": "max-age=604800",
},
})
)
);
return c.json(payload);
});
// 兜底 500 也要带 CORS —— 否则快捷指令那边看到的是跨域错误, 而不是真正的原因。
app.onError((e, c) => {
c.header("Access-Control-Allow-Origin", "*");
return c.text(`${e && e.message ? e.message : e}`, 500);
});
export default app;