用户端和服务端增加地址功能(三个地址共存)
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 },
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import AppImage from '@dukang/shared-ui/AppImage';
|
||||
import { request } from '../lib/api';
|
||||
import { buildProductDetailUrl } from '../lib/navigation';
|
||||
import { STITCH_ORDER_PRODUCT_IMAGE } from '../lib/order-images';
|
||||
import { getProductMainImage } from '../lib/product-images';
|
||||
import { tryGetClientGpsLocation } from '../lib/client-location';
|
||||
|
||||
type Address = {
|
||||
id: string;
|
||||
@@ -119,9 +119,15 @@ export default function OrderConfirmPage() {
|
||||
setLoading(true);
|
||||
setMsg('');
|
||||
try {
|
||||
const clientLocation = await tryGetClientGpsLocation();
|
||||
const order = await request<{ id: string }>('USER_H5', '/trade/orders', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ productId, quantity, addressId }),
|
||||
body: JSON.stringify({
|
||||
productId,
|
||||
quantity,
|
||||
addressId,
|
||||
...(clientLocation ? { clientLocation } : {}),
|
||||
}),
|
||||
});
|
||||
const qs = new URLSearchParams();
|
||||
qs.set('orderId', order.id);
|
||||
|
||||
Reference in New Issue
Block a user