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 595c3aa..e2d75ba 100644 --- a/src/components/Auth/index.tsx +++ b/src/components/Auth/index.tsx @@ -1,7 +1,8 @@ -import React, { useEffect, useState } from "react"; -import Taro from "@tarojs/taro"; -import { View } from "@tarojs/components"; -import { check_login_status } from "@/services/loginService"; +import React, { useEffect, useState } from 'react' +import Taro from '@tarojs/taro' +import { check_login_status } from '@/services/loginService' + + export function getCurrentFullPath(): string { const pages = Taro.getCurrentPages(); 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 0138f87..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"; @@ -62,11 +63,33 @@ const ListPage = () => { getLocation() }, []); + // 监听距离和排序方式变化,自动调用接口 + useEffect(() => { + // 只有当 distanceQuickFilter 有值时才调用接口 + if (distanceQuickFilter?.distance !== undefined || distanceQuickFilter?.quick !== undefined) { + + 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 a59f6f6..81efa61 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 tokenManager from "../utils/tokenManager"; +import tokenManager from '../utils/tokenManager'; +import { useUser } from '@/store/userStore'; // 微信用户信息接口 export interface WechatUserInfo { @@ -42,6 +43,13 @@ export interface UserStats { participated_games_count: number; } +// 手机号验证码登录接口参数 +export interface PhoneLoginParams { + phone: string; + verification_code: string; + user_code: string +} + export interface UserInfoType { subscribe_time: string; last_login_time: string; @@ -91,6 +99,13 @@ export const wechat_auth_login = async ( }); if (auth_response.code === 0) { + // 登录成功后,更新用户信息到 store + try { + await useUser.getState().fetchUserInfo(); + } catch (error) { + console.error('更新用户信息到 store 失败:', error); + } + return { success: true, message: "微信登录成功", @@ -132,6 +147,13 @@ export const phone_auth_login = async ( }); if (verify_response.code === 0) { + // 登录成功后,更新用户信息到 store + try { + await useUser.getState().fetchUserInfo(); + } catch (error) { + console.error('更新用户信息到 store 失败:', error); + } + return { success: true, message: "登录成功", 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 840ff74..d554f9f 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.seachOption.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