import api from "../helpers/api" ; import appStore from "../stores/appStore" ; class UserService { public getState ( ) { return appStore . getState ( ) . userState ; } public async doSignIn ( ) { const { data : user } = await api . getUserInfo ( ) ; if ( user ) { appStore . dispatch ( { type : "SIGN_IN" , payload : { user } , } ) ; } else { userService . doSignOut ( ) ; } return user ; } public async doSignOut ( ) { appStore . dispatch ( { type : "SIGN_OUT" , payload : null , } ) ; api . signout ( ) . catch ( ( ) => { } ) ; } public async checkUsernameUsable ( username ) { const { data : isUsable } = await api . checkUsernameUsable ( username ) ; return isUsable ; } public async updateUsername ( username ) { await api . updateUserinfo ( username ) ; } public async removeGithubName ( ) { await api . updateUserinfo ( undefined , undefined , "" ) ; } public async checkPasswordValid ( password ) { const { data : isValid } = await api . checkPasswordValid ( password ) ; return isValid ; } public async updatePassword ( password ) { await api . updateUserinfo ( undefined , password ) ; } public async updateWxUserId ( wxUserId ) { await api . updateUserinfo ( undefined , undefined , undefined , wxUserId ) ; } } const userService = new UserService ( ) ; export default userService ;