用户端和服务端增加地址功能(三个地址共存)
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
export type ClientGpsLocation = {
|
||||
province?: string;
|
||||
city?: string;
|
||||
district?: string;
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
address?: string;
|
||||
};
|
||||
|
||||
type WxLocationResult = {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
};
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
wx?: {
|
||||
getLocation?: (options: {
|
||||
type?: string;
|
||||
success?: (res: WxLocationResult) => void;
|
||||
fail?: () => void;
|
||||
}) => void;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/** 尝试获取客户端 GPS(微信优先,其次 H5 Geolocation),失败返回 null 不阻塞下单 */
|
||||
export async function tryGetClientGpsLocation(): Promise<ClientGpsLocation | null> {
|
||||
if (typeof window !== 'undefined' && window.wx?.getLocation) {
|
||||
const wxResult = await new Promise<WxLocationResult | null>((resolve) => {
|
||||
window.wx!.getLocation!({
|
||||
type: 'gcj02',
|
||||
success: (res) => resolve(res),
|
||||
fail: () => resolve(null),
|
||||
});
|
||||
});
|
||||
if (wxResult) {
|
||||
return {
|
||||
latitude: wxResult.latitude,
|
||||
longitude: wxResult.longitude,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof navigator === 'undefined' || !navigator.geolocation) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
(pos) => {
|
||||
resolve({
|
||||
latitude: pos.coords.latitude,
|
||||
longitude: pos.coords.longitude,
|
||||
});
|
||||
},
|
||||
() => resolve(null),
|
||||
{ enableHighAccuracy: false, timeout: 5000, maximumAge: 60_000 },
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user