import { AddMenu , DeleteMenu , GetLoadMenu , GetMenuFunctionList , GetPage , UpdateMenu } from '../services' ; import { useCallback , useState } from 'react' ; import { MenuDto } from '@/typings/menudto' ; const useMenuModel = ( ) => { const [ loadMenuForm , setLoadMenuForm ] = useState < MenuDto . MenuOutputLoadDto > ( ) ; const [ itemList , setItemList ] = useState < Array < MenuDto . MenuTable > > ( [ ] ) ; const [ loading , setLoading ] = useState < boolean > ( false ) ; const [ menuFunctionItemList , setMenuFunctionItemList ] = useState < Array < MenuDto . MenuFunctionTable > > ( [ ] ) ; const [ menuFunctionLoading , setMnuFunctionLoading ] = useState < boolean > ( false ) ; const getMenuTable = useCallback ( async ( ) => await new Promise < Array < MenuDto . MenuTable > > ( ( resolve , reject ) => { setLoading ( true ) ; GetPage ( ) . then ( ( response ) => { let data = response . itemList ; setItemList ( data ) ; resolve ( data ) ; } ) . catch ( error => reject ( error ) ) . finally ( ( ) => setLoading ( false ) ) ; } ) , [ ] ) ; const addMenu = useCallback ( async ( param ) => await new Promise < boolean > ( ( resolve , reject ) => { setLoading ( true ) ; AddMenu ( param ) . then ( ( ) => resolve ( true ) ) . catch ( error => reject ( error ) ) . finally ( ( ) => setLoading ( false ) ) ; } ) , [ ] ) ; const delMenu = useCallback ( async ( id ) => await new Promise < boolean > ( ( resolve , reject ) => { setLoading ( true ) ; DeleteMenu ( { id } ) . then ( ( ) => resolve ( true ) ) . catch ( error => reject ( error ) ) . finally ( ( ) => setLoading ( false ) ) ; } ) , [ ] ) ; const getLoadMenu = useCallback ( async ( { payload , callback } ) => await GetLoadMenu ( payload ) . then ( ( response ) => { let menuData = response . data as MenuDto . MenuOutputLoadDto ; setLoadMenuForm ( menuData ) ; if ( callback ) callback ( { state : true , msg : response . msg , data : menuData } ) ; } ) . catch ( error => { if ( callback ) callback ( { state : false , msg : error } ) ; else console . log ( error ) ; } ) , [ ] ) ; /**
     * 加载一个菜单
     */ const editMenu = useCallback ( async ( param ) => await new Promise < boolean > ( ( resolve , reject ) => { setLoading ( true ) ; UpdateMenu ( param ) . then ( ( ) => resolve ( true ) ) . catch ( error => reject ( error ) ) . finally ( ( ) => setLoading ( false ) ) ; } ) , [ ] ) ; const getMenuFunctionTable = useCallback ( async ( id ) => await new Promise < Array < MenuDto . MenuFunctionTable > > ( ( resolve , reject ) => { setMnuFunctionLoading ( true ) ; GetMenuFunctionList ( { id } ) . then ( ( response ) => { let data = response . itemList ; setMenuFunctionItemList ( data ) ; resolve ( data ) ; } ) . catch ( error => reject ( error ) ) . finally ( ( ) => setMnuFunctionLoading ( false ) ) ; } ) , [ ] ) ; return { itemList , loading , getMenuTable , addMenu , delMenu , editMenu , getLoadMenu , loadMenuForm , getMenuFunctionTable , menuFunctionItemList , menuFunctionLoading } ; } ; export default useMenuModel ;