ae7c63c08d
CI / verify (pull_request) Has been cancelled
Share PullToRefresh for partner/shop H5; enable Taro pull-down on mini-user lists. Co-authored-by: Cursor <cursoragent@cursor.com>
140 lines
4.3 KiB
TypeScript
140 lines
4.3 KiB
TypeScript
import { useCallback, useState } from 'react';
|
|
import { View, Text } from '@tarojs/components';
|
|
import Taro, { useDidShow, usePullDownRefresh, useRouter } from '@tarojs/taro';
|
|
import PageShell from '../../components/PageShell';
|
|
import SubPageHeader from '../../components/SubPageHeader';
|
|
import {
|
|
buildAddressEditUrl,
|
|
buildOrderConfirmUrl,
|
|
readCheckoutContext,
|
|
} from '../../lib/checkout-nav';
|
|
import { request, toast } from '../../lib/api';
|
|
|
|
type Address = {
|
|
id: string;
|
|
receiverName: string;
|
|
phone: string;
|
|
province: string;
|
|
city: string;
|
|
district: string;
|
|
detail: string;
|
|
isDefault?: number | boolean;
|
|
};
|
|
|
|
function formatAddress(a: Address) {
|
|
return `${a.province}${a.city}${a.district}${a.detail}`;
|
|
}
|
|
|
|
export default function AddressesPage() {
|
|
const router = useRouter();
|
|
const checkoutCtx = readCheckoutContext(router.params);
|
|
const selectMode = checkoutCtx.select === true;
|
|
const [list, setList] = useState<Address[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const loadList = useCallback(() => {
|
|
setLoading(true);
|
|
request<Address[]>('/user/addresses')
|
|
.then((data) => setList(Array.isArray(data) ? data : []))
|
|
.catch(() => setList([]))
|
|
.finally(() => setLoading(false));
|
|
}, []);
|
|
|
|
useDidShow(() => {
|
|
loadList();
|
|
});
|
|
|
|
usePullDownRefresh(() => {
|
|
void Promise.resolve(loadList()).finally(() => Taro.stopPullDownRefresh());
|
|
});
|
|
|
|
function selectAddress(addr: Address) {
|
|
if (!selectMode) return;
|
|
Taro.redirectTo({
|
|
url: buildOrderConfirmUrl({
|
|
productId: checkoutCtx.productId,
|
|
qty: checkoutCtx.qty,
|
|
addressId: addr.id,
|
|
cross: checkoutCtx.cross,
|
|
}),
|
|
});
|
|
}
|
|
|
|
async function removeAddress(id: string) {
|
|
const res = await Taro.showModal({
|
|
title: '删除地址',
|
|
content: '确定删除该收货地址吗?',
|
|
});
|
|
if (!res.confirm) return;
|
|
try {
|
|
await request(`/user/addresses/${id}`, { method: 'DELETE' });
|
|
toast('已删除', 'success');
|
|
loadList();
|
|
} catch (e) {
|
|
toast(e instanceof Error ? e.message : '删除失败');
|
|
}
|
|
}
|
|
|
|
return (
|
|
<PageShell variant="sub" className="address-page" hasFixedFooter>
|
|
<SubPageHeader title={selectMode ? '选择收货地址' : '地址管理'} />
|
|
<View className="sub-page-body" style={{ paddingBottom: 80 }}>
|
|
{loading ? <View className="u-empty">加载中…</View> : null}
|
|
{!loading && list.length === 0 ? (
|
|
<View className="u-empty">暂无收货地址</View>
|
|
) : null}
|
|
{list.map((a) => (
|
|
<View
|
|
key={a.id}
|
|
className="address-item"
|
|
onClick={() => selectAddress(a)}
|
|
>
|
|
<View className="address-item-head">
|
|
<Text className="address-item-name">{a.receiverName}</Text>
|
|
<Text className="address-item-phone">{a.phone}</Text>
|
|
{a.isDefault === 1 || a.isDefault === true ? (
|
|
<Text className="address-default-tag">默认</Text>
|
|
) : null}
|
|
</View>
|
|
<Text className="address-item-detail">{formatAddress(a)}</Text>
|
|
{!selectMode ? (
|
|
<View className="address-item-actions">
|
|
<Text
|
|
className="address-action"
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
Taro.navigateTo({ url: buildAddressEditUrl(a.id, checkoutCtx) }).catch((err) => {
|
|
toast(err instanceof Error ? err.message : '无法打开编辑页');
|
|
});
|
|
}}
|
|
>
|
|
编辑
|
|
</Text>
|
|
<Text
|
|
className="address-action"
|
|
onClick={(e) => {
|
|
e.stopPropagation();
|
|
void removeAddress(a.id);
|
|
}}
|
|
>
|
|
删除
|
|
</Text>
|
|
</View>
|
|
) : null}
|
|
</View>
|
|
))}
|
|
</View>
|
|
<View
|
|
className="address-fab"
|
|
onClick={() => {
|
|
Taro.navigateTo({ url: buildAddressEditUrl(undefined, checkoutCtx) }).catch((e) => {
|
|
toast(e instanceof Error ? e.message : '无法打开新增地址页');
|
|
});
|
|
}}
|
|
>
|
|
<Text>新增地址</Text>
|
|
</View>
|
|
</PageShell>
|
|
);
|
|
}
|