Compare commits

..

No commits in common. "1aeea5f498be294c23a0e1ea01ca50293d4ea452" and "a13db5dd39be14751929eff7e55b7d5a1ca346ff" have entirely different histories.

2 changed files with 27 additions and 64 deletions

View File

@ -1,5 +1,5 @@
import { create } from 'zustand' import { create } from 'zustand'
import {persist,createJSONStorage} from 'zustand/middleware' import {persist} from 'zustand/middleware'
const CACHE_EXPIRY = 10 * 60 * 1000; const CACHE_EXPIRY = 10 * 60 * 1000;
export interface Location { export interface Location {
@ -71,68 +71,23 @@ export interface WeatherStore {
} }
export const useWeatherStore = create<WeatherStore>()( export const useWeatherStore = create<WeatherStore>()(persist((set,get)=>({currentWeather:null,weatherCache:new Map(),isLoading:false,error:null,
persist( setCurrentWeather:(weather)=>set({currentWeather:weather}),
(set,get)=>({ getWeatherFromCache:(city)=>{
currentWeather:null,weatherCache:new Map(),isLoading:false,error:null, const cashe = get().weatherCache.get(city.toLowerCase());
setCurrentWeather:(weather)=>set({currentWeather:weather}), if(!cashe)return null;
getWeatherFromCache:(city)=>{ const isExpired = Date.now()-cashe.timestamp>CACHE_EXPIRY;
const cashe = get().weatherCache.get(city.toLowerCase()); return isExpired?null:cashe.data;
if(!cashe)return null; },
const isExpired = Date.now()-cashe.timestamp>CACHE_EXPIRY; cacheWeather:(city,weather)=>set((state)=>{
return isExpired?null:cashe.data; const newCache = new Map(state.weatherCache);
}, newCache.set(city.toLowerCase(),{data:weather,timestamp:Date.now()});
cacheWeather:(city,weather)=>set((state)=>{ return{weatherCache:newCache}
const newCache = new Map(state.weatherCache); }),
newCache.set(city.toLowerCase(),{data:weather,timestamp:Date.now()}); setLoading:(loading)=>set({error})
return{weatherCache:newCache} })
}),
setLoading:(loading)=>set({isLoading:loading}), ))
setError:(error)=>set({error}),
reset:()=>set({currentWeather:null,weatherCache:new Map(),isLoading:false,error:null}),
searchWeather:async(city:string)=>{
try{
set({isLoading:true,error:null});
const cachedWeather = get().getWeatherFromCache(city);
if(cachedWeather){
set({currentWeather:cachedWeather,isLoading:false});
return;
}
const WeatherData = await WeatherAPI.getcurrentWeather(city);
set({currentWeather:WeatherData,isLoading:false});
get().cacheWeather(city,WeatherData);
}catch(error){
set({error:'获取失败',isLoading:false});
}
},
refreshWeather:async()=>{
const currentWeather = get().currentWeather;
if(!currentWeather)return;
try{
set({isLoading:true,error:null});
const city = currentWeather.location.name;
const WeatherData = await WeatherAPI.getcurrentWeather(city);
set({currentWeather:WeatherData,isLoading:false});
get().cacheWeather(city,WeatherData);
}catch(error){
set({error:'刷新失败',isLoading:false});
}
}
}),
{
name:'weather-strorage',
storage:createJSONStorage(()=>localStorage),
partialize:(state)=>({
weatherCache:Array.from(state.weatherCache.entries())
}),
onRehydrateStorage:(state)=>{
if(state && Array.isArray(state.weatherCache)){
state.weatherCache = new Map(state.weatherCache as any);
}
}
}
)
)

View File

@ -43,4 +43,12 @@
"vite": "^7.1.7", "vite": "^7.1.7",
"vite-tsconfig-paths": "^5.1.4" "vite-tsconfig-paths": "^5.1.4"
} }
<<<<<<< HEAD
<<<<<<< HEAD
} }
=======
}
>>>>>>> 34ee14ecd423bb649a4c45f54ab6e3f37ad0f35f
=======
}
>>>>>>> 12e66397d84bd55cde0c1b209d3e943e5e20bcb9