import Module from '../../module' ; import InspireCloud from '../../inspirecloud' ; import { PLATFORM_ENUM , UserResponse } from '../../types/constant' ; declare const wx ; declare const tt ; export default class UserModule extends Module { constructor ( inspirecloud ) { super ( inspirecloud ) ; this . name = 'user' ; } private login ( client , allowAnonymousLogin = false ) { return new Promise < any > ( ( resolve , reject ) => { let options = { success ( res ) { resolve ( res ) ; } , fail ( e ) { reject ( e ) ; } } ; if ( allowAnonymousLogin ) { options . force = false ; } client . login ( options ) ; } ) ; } private async getUserInfo ( client , platform ) { return new Promise ( ( resolve , reject ) => { client . getSetting ( { success ( res ) { if ( platform === PLATFORM_ENUM . FS_PROGRAM || res . authSetting [ 'scope.userInfo' ] ) { client . getUserInfo ( { success ( res2 ) { resolve ( res2 ) ; } , fail ( ) { resolve ( { } ) ; } } ) ; } else { resolve ( { } ) ; } } , fail ( e ) { reject ( e ) ; } } ) ; } ) ; } param platform param allowAnonymousLogin returns async logInByOAuth ( opts ) { const { allowAnonymousLogin = false , platform } = opts ; try { let adapter = wx ; if ( platform === PLATFORM_ENUM . FS_PROGRAM || platform === PLATFORM_ENUM . TT_PROGRAM ) { adapter = tt ; } const res = await this . login ( adapter , allowAnonymousLogin ) ; let postData = { } ; if ( res . code ) { const userInfo = await this . getUserInfo ( adapter , platform ) ; postData . userInfo = userInfo ; postData . code = res . code ; } if ( res . anonymousCode && res . isLogin === false ) { postData . anonymousCode = res . anonymousCode ; } if ( ! res . code && ! res . anonymousCode ) { return res ; } const resp = await this . inspirecloud . httpInstance . request ( { url : `/--platform?platform=${ platform }` , data : postData , method : 'post' } ) ; const data = resp . data ; return data ; } catch ( e ) { if ( e . response && e . response . data && e . response . data . error ) { throw new Error ( e . response . data . error ) ; } throw e ; } } loginByOAuth = this . logInByOAuth ; }