import { IStore } from '@client/store' ; import api from '@client/api' ; import config from '@client/config' ; import { setBlogState } from '@client/store/blog/action' ; import { setAppState } from '@client/store/app/action' ; import { setUserState } from '@client/store/user/action' ; const getBlogData = async ( store ) => { const { app , blog } = store . getState ( ) ; const [ { data : blogTagsList } , { data : latestBlogSubData } , { data : hotBlogSubData } , { data : visitorsCount } ] = await Promise . all ( [ api . blogTag . getBlogTags ( ) , api . blog . getLatestBlogSubPage ( ) , api . blog . getHotBlogSubPage ( ) , api . common . getVisitorsCount ( ) ] ) ; const list = [ latestBlogSubData , hotBlogSubData ] ; const index = config . blogMenuList . findIndex ( ( item ) => item . name === blog . blogTag ) ; let total = 0 ; if ( index >= 0 ) { total = list [ index ] . total ; } store . dispatch ( setBlogState ( { blogTags : blogTagsList , hotBlogSubList : hotBlogSubData . list , latestBlogSubList : latestBlogSubData . list , blogTotal : total } ) ) ; store . dispatch ( setAppState ( { visitorsCount } ) ) ; return ; } ; const getUserInfo = async ( store ) => { const { data } = await api . user . getUserInfo ( ) ; if ( data ) { store . dispatch ( setUserState ( data ) ) ; } return ; } ; export default ( store ) => { return new Promise ( async ( resolve , reject ) => { try { const { app } = store . getState ( ) ; if ( app . loadedInitData ) { return resolve ( null ) ; } await Promise . all ( [ getBlogData ( store ) , getUserInfo ( store ) ] ) ; store . dispatch ( setAppState ( { loadedInitData : true } ) ) ; resolve ( null ) ; } catch ( e ) { store . dispatch ( setAppState ( { loadedInitData : false } ) ) ; reject ( e ) ; } } ) ; } ;