From b0e4623aac5167c7ce4c5a00e835e0d8eb821084 Mon Sep 17 00:00:00 2001 From: buddy Date: Sat, 23 Dec 2023 13:12:02 +0800 Subject: [PATCH] refactor: will throw http error and re-fetch --- src/pages/news/children/recommend/sage.ts | 7 +++-- src/utils/http.ts | 34 +++++++++++------------ src/utils/index.ts | 15 ++++++++++ 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/pages/news/children/recommend/sage.ts b/src/pages/news/children/recommend/sage.ts index 178d02f8..8e8edc25 100644 --- a/src/pages/news/children/recommend/sage.ts +++ b/src/pages/news/children/recommend/sage.ts @@ -8,18 +8,19 @@ import { import { RootState } from '@/store/index' import { getSongList, getBanner, getRecommendSongList } from './api/index' import { Song } from '@/interface/index' +import { wrapperReFetch } from '@/utils' export const actions: ActionTree = { async [RecommendActions.SET_ACTION_BANNERS]({ commit }) { - const banners = await getBanner(0) + const banners = await wrapperReFetch(() => getBanner(0)) commit(RecommendMutations.SET_BANNERS, banners) }, async [RecommendActions.SET_ACTION_SONG_LIST]({ commit }) { - const result = await getSongList(14) + const result = await wrapperReFetch(() => getSongList(14)) commit(RecommendMutations.SET_SONG_LIST, result) }, async [RecommendActions.SET_ACTION_RECOMMEND_SONG_LIST]({ commit }) { - const result = await getRecommendSongList() + const result = await wrapperReFetch(getRecommendSongList) commit(RecommendMutations.SET_SONG_LIST, result) } } diff --git a/src/utils/http.ts b/src/utils/http.ts index 777705f1..476a3989 100644 --- a/src/utils/http.ts +++ b/src/utils/http.ts @@ -46,15 +46,14 @@ export function get( params?: unknown, options?: HttpConfig ): Promise { - return http - .get(url, { - params, - ...options - }) - .catch(e => { - console.error(e) - return e - }) + return http.get(url, { + params, + ...options + }) + // .catch(e => { + // console.error(e) + // return e + // }) } export function post( @@ -62,15 +61,14 @@ export function post( data?: unknown, options?: HttpConfig ): Promise { - return http - .post(url, { - data, - ...options - }) - .catch(e => { - console.error(e) - return e - }) + return http.post(url, { + data, + ...options + }) + // .catch(e => { + // console.error(e) + // return e + // }) } export default http diff --git a/src/utils/index.ts b/src/utils/index.ts index eedb4480..543697cb 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -324,3 +324,18 @@ export const measureImg = (source: string) => { export const overNum = (num: number) => { return formatCount(num) } + +export const wrapperReFetch = async ( + asyncGet: (...args: any) => Promise +) => { + try { + const res = await asyncGet() + return res + } catch (error) { + return new Promise(resolve => { + setTimeout(async () => { + resolve(await wrapperReFetch(asyncGet)) + }, 1000) + }) + } +}