283e6651aa
Co-authored-by: Cursor <cursoragent@cursor.com>
31 lines
822 B
TypeScript
31 lines
822 B
TypeScript
import { weixinSdk } from './weixin';
|
|
|
|
export type ClientGpsLocation = {
|
|
province?: string;
|
|
city?: string;
|
|
district?: string;
|
|
latitude: number;
|
|
longitude: number;
|
|
address?: string;
|
|
};
|
|
|
|
/** 尝试获取客户端 GPS(微信 JSSDK / 浏览器 Geolocation),失败返回 null 不阻塞下单 */
|
|
export async function tryGetClientGpsLocation(): Promise<ClientGpsLocation | null> {
|
|
if (process.env.TARO_ENV === 'weapp') {
|
|
try {
|
|
const Taro = (await import('@tarojs/taro')).default;
|
|
const loc = await Taro.getLocation({ type: 'gcj02' });
|
|
return { latitude: loc.latitude, longitude: loc.longitude };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const loc = await weixinSdk.getLocation();
|
|
if (!loc) return null;
|
|
return {
|
|
latitude: loc.latitude,
|
|
longitude: loc.longitude,
|
|
};
|
|
}
|