From d38baa9c5bcd1994df9c27a7dae4a9bf10f94f53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=88=90?= Date: Wed, 10 Sep 2025 11:11:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=A5=E5=8F=A3=20?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=20=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E6=9B=B4=E6=96=B0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/list/index.tsx | 9 +++++++++ src/store/listStore.ts | 15 +++++++-------- types/list/types.ts | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/pages/list/index.tsx b/src/pages/list/index.tsx index 0138f87..502a3ce 100644 --- a/src/pages/list/index.tsx +++ b/src/pages/list/index.tsx @@ -62,6 +62,15 @@ const ListPage = () => { getLocation() }, []); + // 监听距离和排序方式变化,自动调用接口 + useEffect(() => { + // 只有当 distanceQuickFilter 有值时才调用接口 + if (distanceQuickFilter?.distance !== undefined || distanceQuickFilter?.quick !== undefined) { + console.log('距离或排序方式发生变化,重新获取数据:', distanceQuickFilter); + getMatchesData(); + } + }, [distanceQuickFilter?.distance, distanceQuickFilter?.quick]); + // 获取位置信息 const getLocation = async () => { const location = await getCurrentLocationInfo() diff --git a/src/store/listStore.ts b/src/store/listStore.ts index 840ff74..c7f3896 100644 --- a/src/store/listStore.ts +++ b/src/store/listStore.ts @@ -154,7 +154,7 @@ export const useListStore = create()((set, get) => ({ }, // 获取列表数据(常规搜索) - fetchMatches: async (params) => { + fetchMatches: async (params, isFirstLoad = false) => { set({ loading: true, error: null }); const { getSearchParams, setListData, distanceQuickFilter } = get(); @@ -172,6 +172,11 @@ export const useListStore = create()((set, get) => ({ if (isIntegrate) { reqParams.order = ""; fetchFn = getGamesIntegrateList; + + // 第一次进入页面时传入 isRefresh 参数 + if (isFirstLoad) { + reqParams.isRefresh = true; + } } console.log("===fetchMatches 获取列表数据参数:", reqParams); @@ -187,7 +192,6 @@ export const useListStore = create()((set, get) => ({ } const { count, rows } = data; setListData({ - // recommendList: rows || [], error: '', data: rows || [], loading: false, @@ -246,12 +250,7 @@ export const useListStore = create()((set, get) => ({ // 获取列表数据 getMatchesData: () => { const { fetchMatches } = get(); - fetchMatches(); - // if (distanceQuickFilter?.quick === "0") { - // getIntegrateListData(); - // } else { - // fetchMatches(); - // } + fetchMatches({}, true); // 第一次进入页面,传入 isFirstLoad = true }, // 获取历史搜索数据 diff --git a/types/list/types.ts b/types/list/types.ts index 63e6787..ede3572 100644 --- a/types/list/types.ts +++ b/types/list/types.ts @@ -61,7 +61,7 @@ export interface ListState { } export interface ListActions { - fetchMatches: (params?: Record) => Promise + fetchMatches: (params?: Record,isFirstLoad?: Boolean) => Promise // getIntegrateListData: (params?: Record) => Promise getMatchesData: () => void clearError: () => void From 5fdee20d456afdd6d9d62280de414439f9c7d8c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=88=90?= Date: Wed, 10 Sep 2025 21:43:36 +0800 Subject: [PATCH 2/2] 1 --- src/app.ts | 1 - src/components/Auth/index.tsx | 1 - src/config/env.ts | 5 +- src/container/listCustomNavbar/index.tsx | 22 ++-- src/pages/detail/index.tsx | 5 + src/pages/home/index.tsx | 41 ++++++-- src/pages/list/index.tsx | 20 +++- src/services/loginService.ts | 80 +++++--------- src/services/userService.ts | 126 ++++++++++++++--------- src/store/global.ts | 15 ++- src/store/listStore.ts | 2 +- src/store/userStore.ts | 115 +++++++++++++++++++-- 12 files changed, 281 insertions(+), 152 deletions(-) diff --git a/src/app.ts b/src/app.ts index baff6c5..89d3e97 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,7 +3,6 @@ import './nutui-theme.scss' import './app.scss' import { useDictionaryStore } from './store/dictionaryStore' import { useGlobalStore } from './store/global' -import { check_login_status } from './services/loginService'; // import { getNavbarHeight } from "@/utils/getNavbarHeight"; diff --git a/src/components/Auth/index.tsx b/src/components/Auth/index.tsx index 2ec4f42..5bfc65d 100644 --- a/src/components/Auth/index.tsx +++ b/src/components/Auth/index.tsx @@ -1,6 +1,5 @@ import React, { useEffect, useState } from 'react' import Taro from '@tarojs/taro' -import { View } from '@tarojs/components' import { check_login_status } from '@/services/loginService' diff --git a/src/config/env.ts b/src/config/env.ts index 2830e77..150c81c 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -17,15 +17,14 @@ const envConfigs: Record = { // 开发环境 development: { name: '开发环境', - apiBaseURL: 'https://sit.light120.com', - // apiBaseURL: 'http://localhost:9098', + //apiBaseURL: 'https://sit.light120.com', + apiBaseURL: 'http://localhost:9098', timeout: 15000, enableLog: true, enableMock: true }, - // 生产环境 production: { name: '生产环境', diff --git a/src/container/listCustomNavbar/index.tsx b/src/container/listCustomNavbar/index.tsx index 7f07eb3..279f363 100644 --- a/src/container/listCustomNavbar/index.tsx +++ b/src/container/listCustomNavbar/index.tsx @@ -1,6 +1,8 @@ import { View, Text, Image } from "@tarojs/components"; import img from "@/config/images"; import { useGlobalState } from "@/store/global"; +import { useUserInfo, } from '@/store/userStore' + import { useListState } from "@/store/listStore"; import CustomNavbar from "@/components/CustomNavbar"; import { Input } from "@nutui/nutui-react-taro"; @@ -32,9 +34,11 @@ const ListHeader = (props: IProps) => { const { gamesNum, searchValue, isShowInputCustomerNavBar } = useListState(); const { statusBarHeight, navbarHeight } = statusNavbarHeightInfo; - const currentAddress = getLocationLoading - ? getLocationText - : location?.address; + const { city,district } = useUserInfo() + + console.log("useUserInfo",city,district ) + + const currentAddress = city + district const handleInputClick = () => { const pages = Taro.getCurrentPages(); @@ -69,9 +73,8 @@ const ListHeader = (props: IProps) => { {/* 首页logo 导航*/} - 附近${gamesNum}场球局 + 附近{gamesNum}场球局 {/* 搜索导航 */} + ); } export default HomePage; \ No newline at end of file diff --git a/src/pages/list/index.tsx b/src/pages/list/index.tsx index 502a3ce..cb53331 100644 --- a/src/pages/list/index.tsx +++ b/src/pages/list/index.tsx @@ -11,6 +11,7 @@ import GuideBar from "@/components/GuideBar"; import ListContainer from "@/container/listContainer"; import DistanceQuickFilter from "@/components/DistanceQuickFilter"; import { withAuth } from "@/components"; +import { updateUserLocation } from "@/services/userService"; // import img from "@/config/images"; // import ShareCardCanvas from "@/components/ShareCardCanvas/example"; @@ -66,16 +67,29 @@ const ListPage = () => { useEffect(() => { // 只有当 distanceQuickFilter 有值时才调用接口 if (distanceQuickFilter?.distance !== undefined || distanceQuickFilter?.quick !== undefined) { - console.log('距离或排序方式发生变化,重新获取数据:', distanceQuickFilter); - getMatchesData(); + + if (distanceQuickFilter?.quick !== "0") { + getMatchesData(); + } } }, [distanceQuickFilter?.distance, distanceQuickFilter?.quick]); // 获取位置信息 const getLocation = async () => { const location = await getCurrentLocationInfo() - // 保存位置 + + // 保存位置到全局状态 updateState({ location }); + + // 同时更新用户位置到后端和 store + if (location && location.latitude && location.longitude) { + try { + await updateUserLocation(location.latitude, location.longitude); + } catch (error) { + console.error('更新用户位置失败:', error); + } + } + // 页面加载时获取数据 getMatchesData(); return location; diff --git a/src/services/loginService.ts b/src/services/loginService.ts index 25c7d3f..b849cd0 100644 --- a/src/services/loginService.ts +++ b/src/services/loginService.ts @@ -1,6 +1,7 @@ import Taro from '@tarojs/taro'; -import httpService, { ApiResponse } from './httpService'; +import httpService from './httpService'; import tokenManager from '../utils/tokenManager'; +import { useUser } from '@/store/userStore'; // 微信用户信息接口 export interface WechatUserInfo { @@ -34,25 +35,12 @@ export interface VerifyCodeResponse { token?: string; user_info?: WechatUserInfo; } -// 用户详细信息 -export interface UserInfoType { - id: number - openid: string - unionid: string - session_key: string - nickname: string - avatar_url: string - gender: string - country: string - province: string - city: string - language: string - phone: string - is_subscribed: string - latitude: number - longitude: number - subscribe_time: string - last_login_time: string + +// 手机号验证码登录接口参数 +export interface PhoneLoginParams { + phone: string; + verification_code: string; + user_code: string } @@ -76,6 +64,13 @@ export const wechat_auth_login = async (phone_code?: string): Promise { } }; -// 获取用户信息 -export const get_user_info = (): WechatUserInfo | null => { - try { - let userinfo = Taro.getStorageSync('user_info') - if (userinfo) { - return JSON.parse(userinfo) - } - return null; - } catch (error) { - return null; - } -}; - // 获取用户token export const get_user_token = (): string | null => { try { @@ -330,24 +319,9 @@ export const refresh_login_status = async (): Promise => { } }; -// 获取用户详细信息 -export const fetchUserProfile = async (): Promise> => { - try { - const response = await httpService.post('user/detail'); - return response; - } catch (error) { - console.error('获取用户信息失败:', error); - throw error; - } -}; -// 更新用户信息 -export const updateUserProfile = async (payload: Partial) => { - try { - const response = await httpService.post('/user/update', payload); - return response; - } catch (error) { - console.error('更新用户信息失败:', error); - throw error; - } -}; \ No newline at end of file + + + + + diff --git a/src/services/userService.ts b/src/services/userService.ts index 31fb90b..825bfa5 100644 --- a/src/services/userService.ts +++ b/src/services/userService.ts @@ -1,8 +1,8 @@ import { UserInfo } from '@/components/UserInfo'; -import Taro from '@tarojs/taro'; import { API_CONFIG } from '@/config/api'; -import httpService from './httpService'; +import httpService, { ApiResponse } from './httpService'; import uploadFiles from './uploadFiles'; +import Taro from '@tarojs/taro'; // 用户详情接口 @@ -35,55 +35,28 @@ interface UserDetailData { }; } -// 更新用户信息参数接口 -interface UpdateUserParams { - nickname: string; - avatar_url: string; - gender: string; - phone: string; - latitude?: string; - longitude?: string; - city: string; - province: string; - country: string; - +// 用户详细信息接口(从 loginService 移过来) +export interface UserInfoType { + id: number + openid: string + unionid: string + session_key: string + nickname: string + avatar_url: string + gender: string + country: string + province: string + city: string + district:string + language: string + phone: string + is_subscribed: string + latitude: number + longitude: number + subscribe_time: string + last_login_time: string } -// 上传响应接口 -interface UploadResponseData { - create_time: string; - last_modify_time: string; - duration: string; - thumbnail_url: string; - view_count: string; - download_count: string; - is_delete: number; - id: number; - user_id: number; - resource_type: string; - file_name: string; - original_name: string; - file_path: string; - file_url: string; - file_size: number; - mime_type: string; - description: string; - tags: string; - is_public: string; - width: number; - height: number; - uploadInfo: { - success: boolean; - name: string; - path: string; - ossPath: string; - fileType: string; - fileSize: number; - originalName: string; - suffix: string; - storagePath: string; - }; -} // 后端球局数据接口 interface BackendGameData { @@ -284,7 +257,7 @@ export class UserService { } } - // 更新用户信息 + // 更新用户信息(简化版本,具体逻辑在 userStore 中处理) static async update_user_info(update_data: Partial): Promise { try { // 过滤掉空字段 @@ -490,4 +463,55 @@ export class UserService { return require('../static/userInfo/default_avatar.svg'); } } -} \ No newline at end of file +} + +// 从 loginService 移过来的用户相关方法 + +// 获取用户详细信息 +export const fetchUserProfile = async (): Promise> => { + try { + const response = await httpService.post('user/detail'); + return response; + } catch (error) { + console.error('获取用户信息失败:', error); + throw error; + } +}; + +// 更新用户信息 +export const updateUserProfile = async (payload: Partial) => { + try { + const response = await httpService.post('/user/update', payload); + return response; + } catch (error) { + console.error('更新用户信息失败:', error); + throw error; + } +}; + +// 更新用户坐标位置 +export const updateUserLocation = async (latitude: number, longitude: number) => { + try { + const response = await httpService.post('/user/update_location', { + latitude, + longitude, + }); + return response; + } catch (error) { + console.error('更新用户坐标位置失败:', error); + throw error; + } +}; + +// 获取用户信息(从本地存储) +export const get_user_info = (): any | null => { + try { + let userinfo = Taro.getStorageSync('user_info') + if (userinfo) { + return JSON.parse(userinfo) + } + return null; + } catch (error) { + return null; + } +}; \ No newline at end of file diff --git a/src/store/global.ts b/src/store/global.ts index 05623a9..7aa8e70 100644 --- a/src/store/global.ts +++ b/src/store/global.ts @@ -16,7 +16,7 @@ interface GlobalState { interface GlobalActions { updateState: (payload: Record) => void; getNavbarHeightInfo: () => void; - getCurrentLocationInfo: () => void; + getCurrentLocationInfo: () => any; } // 完整的 Store 类型 type GlobalStore = GlobalState & GlobalActions; @@ -50,14 +50,13 @@ export const useGlobalStore = create()((set, get) => ({ }, // 获取位置信息 - getCurrentLocationInfo: () => { - return getCurrentLocation().then((res) => { - set({ - getLocationLoading: false, - location: res || {}, - }); - return res; + getCurrentLocationInfo: async () => { + const res = await getCurrentLocation() + set({ + getLocationLoading: false, + location: res || {}, }); + return res; }, // 更新store数据 diff --git a/src/store/listStore.ts b/src/store/listStore.ts index c7f3896..d554f9f 100644 --- a/src/store/listStore.ts +++ b/src/store/listStore.ts @@ -175,7 +175,7 @@ export const useListStore = create()((set, get) => ({ // 第一次进入页面时传入 isRefresh 参数 if (isFirstLoad) { - reqParams.isRefresh = true; + reqParams.seachOption.isRefresh = true; } } diff --git a/src/store/userStore.ts b/src/store/userStore.ts index 16831ab..97fc548 100644 --- a/src/store/userStore.ts +++ b/src/store/userStore.ts @@ -1,13 +1,25 @@ import { create } from 'zustand' -import { fetchUserProfile, updateUserProfile, UserInfoType } from '@/services/loginService' +import { fetchUserProfile, updateUserProfile, UserInfoType } from '@/services/userService' export interface UserState { user: UserInfoType + loading: boolean + error: string | null + + // 获取用户信息 fetchUserInfo: () => Promise - updateUserInfo: (userInfo: Partial) => void + + // 更新用户信息(先调用接口更新,再重新获取最新数据) + updateUserInfo: (userInfo: Partial) => Promise + + // 更新用户位置(特殊处理,需要重新获取详情数据) + updateUserLocation: (latitude: number, longitude: number) => Promise + + // 清除用户数据 + clearUserData: () => void } -export const useUser = create()((set) => ({ +export const useUser = create()((set, get) => ({ user: { id: 0, "openid": "", @@ -19,6 +31,7 @@ export const useUser = create()((set) => ({ "country": "", "province": "", "city": "", + "district":"", "language": "", "phone": "13800138000", "is_subscribed": "0", @@ -27,20 +40,102 @@ export const useUser = create()((set) => ({ "subscribe_time": "2024-06-15 14:00:00", "last_login_time": "2024-06-15 14:00:00" }, + loading: false, + error: null, + + // 获取用户信息 fetchUserInfo: async () => { - const res = await fetchUserProfile() - console.log(res) - set({ user: res.data }) + try { + set({ loading: true, error: null }) + const res = await fetchUserProfile() + if (res.code === 0) { + set({ user: res.data, loading: false }) + } else { + set({ error: res.message || '获取用户信息失败', loading: false }) + } + } catch (error) { + console.error('获取用户信息失败:', error) + set({ error: '获取用户信息失败', loading: false }) + } }, - updateUserInfo: async(userInfo: Partial) => { - const res = await updateUserProfile(userInfo) - set({ user: res.data }) + + // 更新用户信息 + updateUserInfo: async (userInfo: Partial) => { + try { + set({ loading: true, error: null }) + + // 先调用更新接口 + const updateRes = await updateUserProfile(userInfo) + if (updateRes.code !== 0) { + throw new Error(updateRes.message || '更新用户信息失败') + } + + // 更新成功后,重新获取最新的用户信息 + await get().fetchUserInfo() + + } catch (error) { + console.error('更新用户信息失败:', error) + set({ error: '更新用户信息失败', loading: false }) + throw error + } + }, + + // 更新用户位置 + updateUserLocation: async (latitude: number, longitude: number) => { + try { + set({ loading: true, error: null }) + + // 调用位置更新接口 + const locationRes = await updateUserProfile({ latitude, longitude }) + if (locationRes.code !== 0) { + throw new Error(locationRes.message || '更新位置失败') + } + + // 位置更新成功后,重新获取最新的用户信息 + await get().fetchUserInfo() + + } catch (error) { + console.error('更新用户位置失败:', error) + set({ error: '更新用户位置失败', loading: false }) + throw error + } + }, + + // 清除用户数据 + clearUserData: () => { + set({ + user: { + id: 0, + "openid": "", + "unionid": "", + "session_key": "", + "nickname": "", + "avatar_url": "", + "gender": "", + "country": "", + "province": "", + "city": "", + "language": "", + "phone": "", + "is_subscribed": "0", + "latitude": 0, + "longitude": 0, + "subscribe_time": "", + "last_login_time": "" + }, + loading: false, + error: null + }) } })) export const useUserInfo = () => useUser((state) => state.user) +export const useUserLoading = () => useUser((state) => state.loading) +export const useUserError = () => useUser((state) => state.error) export const useUserActions = () => useUser((state) => ({ fetchUserInfo: state.fetchUserInfo, - updateUserInfo: state.updateUserInfo + updateUserInfo: state.updateUserInfo, + updateUserLocation: state.updateUserLocation, + clearUserData: state.clearUserData }))