import { ActionTree } from 'vuex' ; import { PassportState , RootState , mutations } from '../types' ; import { $http } from '../../utils/http' ; import { api } from '../../utils/api' ; export const actions = { param param param data returns { Promise < R > } login ( { dispatch , commit } , data ) { const url = data . url ?? api . api . login ; if ( data . url ) delete data . url ; return new Promise ( ( resolve , reject ) => { $http . post ( url , data ) . then ( ( res ) => { if ( res . ret . code === 1 ) { commit ( mutations . passport . auto , data . remember ?? false ) ; dispatch ( 'user' , res . data ) ; } resolve ( res ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } ) ; } , param param param data logout ( { commit } , data ) { const url = data . url ?? api . api . logout ; return new Promise ( ( resolve , reject ) => { $http . get ( url ) . then ( ( res ) => { if ( res . ret . code === 1 ) commit ( mutations . passport . reset ) ; resolve ( res ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } ) ; } , param param param data register ( { } , data ) { const url = data . url ?? api . api . register ; if ( data . url ) delete data . url ; return new Promise ( ( resolve , reject ) => { $http . post ( url , data ) . then ( ( res ) => { resolve ( res ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } ) ; } , param param param data binding ( { dispatch , commit } , data ) { const url = `${ api . api . register }/${ data . type }` ; return new Promise ( ( resolve , reject ) => { $http . post ( url , data ) . then ( ( res ) => { if ( res . ret . code === 1 ) { commit ( mutations . passport . auto , true ) ; dispatch ( 'user' , res . data ) ; } resolve ( res ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } ) ; } , param param0 param data authorize ( { dispatch , commit } , data ) { return new Promise ( ( resolve , reject ) => { $http . post ( data . url , { token : data . token } ) . then ( ( res ) => { if ( res . ret . code === 1 ) { commit ( mutations . passport . auto , true ) ; dispatch ( 'user' , res . data ) ; } resolve ( res ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } ) ; } , param commit param data returns { Promise < R > } refresh ( { commit } , data ) { const url = data . url ?? api . api . refresh ; data . url && delete data . url ; return new Promise ( ( resolve , reject ) => { $http . post ( url , data ) . then ( ( res ) => { if ( res . ret . code === 1 ) { commit ( mutations . passport . token . access , res . data . access_token ) ; commit ( mutations . passport . token . refresh , res . data . refresh_token ) ; } resolve ( res ) ; } ) . catch ( ( err ) => { reject ( err ) ; } ) ; } ) ; } , param commit param data user ( { commit } , data ) { commit ( mutations . passport . user , data . user ) ; commit ( mutations . passport . token . access , data . tokens . access_token ) ; commit ( mutations . passport . token . refresh , data . tokens . refresh_token ) ; } } ;