mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
geoip: fix service register
This commit is contained in:
1 parent
d94e3e650a
commit
41e10e49f5
1 file changed
+19
-17
+19
-17
@@ -13,22 +13,24 @@ export interface Result {
|
|||||||
display: string
|
display: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export function apply() {
|
class GeoIPService extends Service {
|
||||||
Context.service('geoip', class GeoIPService extends Service {
|
provider = '<a href="http://www.maxmind.com" target="_blank">MaxMind</a>';
|
||||||
provider = '<a href="http://www.maxmind.com" target="_blank">MaxMind</a>';
|
lookup(ip: string, locale: string): Result {
|
||||||
lookup(ip: string, locale: string): Result {
|
const res: any = reader.get(ip);
|
||||||
const res: any = reader.get(ip);
|
if (!res) return { display: 'Unknown address'.translate(locale) };
|
||||||
if (!res) return { display: 'Unknown address'.translate(locale) };
|
const ret: Result = { display: '' };
|
||||||
const ret: Result = { display: '' };
|
if (res.location) ret.location = res.location;
|
||||||
if (res.location) ret.location = res.location;
|
if (res.continent) ret.continent = res.continent.names[locale] || res.continent.names.en;
|
||||||
if (res.continent) ret.continent = res.continent.names[locale] || res.continent.names.en;
|
if (res.country || res.registered_country) {
|
||||||
if (res.country || res.registered_country) {
|
ret.country = (res.country || res.registered_country).names[locale]
|
||||||
ret.country = (res.country || res.registered_country).names[locale]
|
|| (res.country || res.registered_country).names.en;
|
||||||
|| (res.country || res.registered_country).names.en;
|
|
||||||
}
|
|
||||||
if (res.city) ret.city = res.city.names[locale] || res.city.names.en;
|
|
||||||
ret.display = `${ret.continent} ${ret.country}${ret.city ? ` ${ret.city}` : ''}`;
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
});
|
if (res.city) ret.city = res.city.names[locale] || res.city.names.en;
|
||||||
|
ret.display = `${ret.continent} ${ret.country}${ret.city ? ` ${ret.city}` : ''}`;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export function apply(ctx: Context) {
|
||||||
|
Context.service('geoip', GeoIPService);
|
||||||
|
ctx.geoip = new GeoIPService(ctx, 'geoip', true);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user