fix(mini-user): locate before store list fetch; prefer masked phone on mine
Resolve city once then fetch stores once; skip refetch when city unchanged. Show masked phone under nickname when available. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -351,11 +351,14 @@ export default function MinePage() {
|
|||||||
const needProfileFill = isWeapp && needsWxProfileFill(display);
|
const needProfileFill = isWeapp && needsWxProfileFill(display);
|
||||||
const avatarProfileReady = isWeapp ? !needProfileFill : hasWechat;
|
const avatarProfileReady = isWeapp ? !needProfileFill : hasWechat;
|
||||||
const maskedPhone = profile?.phone ? maskPhone(String(profile.phone)) : '';
|
const maskedPhone = profile?.phone ? maskPhone(String(profile.phone)) : '';
|
||||||
const memberLabel = needProfileFill
|
// 昵称下优先展示脱敏手机号;无手机号时再提示完善资料/授权
|
||||||
? '点击头像完善资料'
|
const memberLabel =
|
||||||
: !isWeapp && !hasWechat && canWxAuth
|
maskedPhone ||
|
||||||
? '点击头像授权'
|
(needProfileFill
|
||||||
: maskedPhone || '未绑定手机';
|
? '点击头像完善资料'
|
||||||
|
: !isWeapp && !hasWechat && canWxAuth
|
||||||
|
? '点击头像授权'
|
||||||
|
: '未绑定手机');
|
||||||
const avatarClickable = isWeapp || (!hasWechat && canWxAuth);
|
const avatarClickable = isWeapp || (!hasWechat && canWxAuth);
|
||||||
const previewAvatar = draftAvatarUrl || display.avatarUrl;
|
const previewAvatar = draftAvatarUrl || display.avatarUrl;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||||
import { View, Text, Image, Input } from '@tarojs/components';
|
import { View, Text, Image, Input } from '@tarojs/components';
|
||||||
import Taro, { useDidShow, usePullDownRefresh, useShareAppMessage, useShareTimeline } from '@tarojs/taro';
|
import Taro, { useDidShow, usePullDownRefresh, useShareAppMessage, useShareTimeline } from '@tarojs/taro';
|
||||||
import PageShell from '../../components/PageShell';
|
import PageShell from '../../components/PageShell';
|
||||||
@@ -25,7 +25,6 @@ import {
|
|||||||
toCityWideRegion,
|
toCityWideRegion,
|
||||||
type UserCoords,
|
type UserCoords,
|
||||||
} from '../../lib/user-location';
|
} from '../../lib/user-location';
|
||||||
import { FALLBACK_CITY_CODE } from '../../lib/product-images';
|
|
||||||
import { formatDistanceMeters } from '../../lib/geo';
|
import { formatDistanceMeters } from '../../lib/geo';
|
||||||
import { request, toast } from '../../lib/api';
|
import { request, toast } from '../../lib/api';
|
||||||
import {
|
import {
|
||||||
@@ -55,6 +54,10 @@ type Store = {
|
|||||||
distanceMeters?: number | null;
|
distanceMeters?: number | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function regionEqual(a: RegionSelection, b: RegionSelection): boolean {
|
||||||
|
return a.province === b.province && a.city === b.city && a.district === b.district;
|
||||||
|
}
|
||||||
|
|
||||||
export default function StoresPage() {
|
export default function StoresPage() {
|
||||||
const [stores, setStores] = useState<Store[]>([]);
|
const [stores, setStores] = useState<Store[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
@@ -65,8 +68,9 @@ export default function StoresPage() {
|
|||||||
const [category, setCategory] = useState<CategorySelection>(EMPTY_CATEGORY);
|
const [category, setCategory] = useState<CategorySelection>(EMPTY_CATEGORY);
|
||||||
const [categoryOpen, setCategoryOpen] = useState(false);
|
const [categoryOpen, setCategoryOpen] = useState(false);
|
||||||
const [categoryTree, setCategoryTree] = useState<StoreCategoryNode[]>([]);
|
const [categoryTree, setCategoryTree] = useState<StoreCategoryNode[]>([]);
|
||||||
const [cityCode, setCityCode] = useState<string>(FALLBACK_CITY_CODE);
|
/** 上次已用于请求的城市编码;仅换城才再拉列表 */
|
||||||
const [userCoords, setUserCoords] = useState<UserCoords | null>(() => readCachedUserCoords());
|
const cityCodeRef = useRef<string | null>(null);
|
||||||
|
const fetchSeqRef = useRef(0);
|
||||||
const regionLabel = formatRegionLabel(region);
|
const regionLabel = formatRegionLabel(region);
|
||||||
const categoryLabel = formatCategoryLabel(category);
|
const categoryLabel = formatCategoryLabel(category);
|
||||||
|
|
||||||
@@ -81,13 +85,51 @@ export default function StoresPage() {
|
|||||||
return map;
|
return map;
|
||||||
}, [categoryTree]);
|
}, [categoryTree]);
|
||||||
|
|
||||||
|
/** 按城市拉门店;coords 仅用于排序距离,不单独触发二次请求 */
|
||||||
|
async function fetchStores(nextCode: string, coords: UserCoords | null) {
|
||||||
|
const seq = ++fetchSeqRef.current;
|
||||||
|
setLoading(true);
|
||||||
|
const qs = new URLSearchParams();
|
||||||
|
if (nextCode) qs.set('cityCode', nextCode);
|
||||||
|
if (coords) {
|
||||||
|
qs.set('lat', String(coords.latitude));
|
||||||
|
qs.set('lng', String(coords.longitude));
|
||||||
|
}
|
||||||
|
const path = qs.toString() ? `/stores?${qs}` : '/stores';
|
||||||
|
try {
|
||||||
|
const list = await request<Store[]>(path);
|
||||||
|
if (seq !== fetchSeqRef.current) return;
|
||||||
|
setStores(Array.isArray(list) ? list : []);
|
||||||
|
} catch (e) {
|
||||||
|
if (seq !== fetchSeqRef.current) return;
|
||||||
|
toast(e instanceof Error ? e.message : '加载失败');
|
||||||
|
} finally {
|
||||||
|
if (seq === fetchSeqRef.current) setLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 流程:先定位 → 再请求一次列表(成功用定位城市,失败用默认城市)。
|
||||||
|
* 再次进入页且城市未变:不重复请求,避免列表闪烁。
|
||||||
|
*/
|
||||||
useDidShow(() => {
|
useDidShow(() => {
|
||||||
syncTabBarSelected(1);
|
syncTabBarSelected(1);
|
||||||
void resolveUserCity().then((resolved) => {
|
void (async () => {
|
||||||
setRegion(toCityWideRegion(resolved.region));
|
const resolved = await resolveUserCity();
|
||||||
setCityCode(getCityCodeForCatalog(resolved));
|
const nextCode = getCityCodeForCatalog(resolved);
|
||||||
setUserCoords(readCachedUserCoords());
|
const nextCoords = readCachedUserCoords();
|
||||||
});
|
const nextRegion = toCityWideRegion(resolved.region);
|
||||||
|
const cityChanged = cityCodeRef.current !== nextCode;
|
||||||
|
|
||||||
|
setRegion((prev) => (regionEqual(prev, nextRegion) ? prev : nextRegion));
|
||||||
|
|
||||||
|
if (cityCodeRef.current == null || cityChanged) {
|
||||||
|
cityCodeRef.current = nextCode;
|
||||||
|
await fetchStores(nextCode, nextCoords);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setLoading(false);
|
||||||
|
})();
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -96,49 +138,20 @@ export default function StoresPage() {
|
|||||||
.catch(() => setCategoryTree([]));
|
.catch(() => setCategoryTree([]));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const loadStores = useCallback(() => {
|
|
||||||
setLoading(true);
|
|
||||||
const qs = new URLSearchParams();
|
|
||||||
if (cityCode) qs.set('cityCode', cityCode);
|
|
||||||
const coords = userCoords ?? readCachedUserCoords();
|
|
||||||
if (coords) {
|
|
||||||
qs.set('lat', String(coords.latitude));
|
|
||||||
qs.set('lng', String(coords.longitude));
|
|
||||||
}
|
|
||||||
const path = qs.toString() ? `/stores?${qs}` : '/stores';
|
|
||||||
return request<Store[]>(path)
|
|
||||||
.then((list) => setStores(Array.isArray(list) ? list : []))
|
|
||||||
.catch((e) => toast(e instanceof Error ? e.message : '加载失败'))
|
|
||||||
.finally(() => setLoading(false));
|
|
||||||
}, [cityCode, userCoords]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
void loadStores();
|
|
||||||
}, [loadStores]);
|
|
||||||
|
|
||||||
usePullDownRefresh(() => {
|
usePullDownRefresh(() => {
|
||||||
void (async () => {
|
void (async () => {
|
||||||
try {
|
try {
|
||||||
const resolved = await resolveUserCity(true);
|
const resolved = await resolveUserCity(true);
|
||||||
setRegion(toCityWideRegion(resolved.region));
|
|
||||||
const nextCode = getCityCodeForCatalog(resolved);
|
const nextCode = getCityCodeForCatalog(resolved);
|
||||||
setCityCode(nextCode);
|
const nextRegion = toCityWideRegion(resolved.region);
|
||||||
const coords = readCachedUserCoords();
|
const coords = readCachedUserCoords();
|
||||||
setUserCoords(coords);
|
cityCodeRef.current = nextCode;
|
||||||
setLoading(true);
|
setRegion(nextRegion);
|
||||||
const qs = new URLSearchParams();
|
await fetchStores(nextCode, coords);
|
||||||
if (nextCode) qs.set('cityCode', nextCode);
|
|
||||||
if (coords) {
|
|
||||||
qs.set('lat', String(coords.latitude));
|
|
||||||
qs.set('lng', String(coords.longitude));
|
|
||||||
}
|
|
||||||
const path = qs.toString() ? `/stores?${qs}` : '/stores';
|
|
||||||
const list = await request<Store[]>(path);
|
|
||||||
setStores(Array.isArray(list) ? list : []);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
toast(e instanceof Error ? e.message : '加载失败');
|
toast(e instanceof Error ? e.message : '加载失败');
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
} finally {
|
||||||
Taro.stopPullDownRefresh();
|
Taro.stopPullDownRefresh();
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user