import { Controller } from 'egg' ; import { OpenComponentAccessTokenApi , OpenMpApi , OpenAuthorizerAccessTokenApi , AccessToken , ApiConfigKit , ICache , AccessTokenType , CallbackApi } from 'tnwx' ; export default class OpenMpApiController extends Controller { /**
     * 授权回调
     */ public async authRedirect ( ) { const { ctx } = this ; let appId = ctx . app . config . openMpConfig . appId ; let authCode = ctx . query . auth_code ; let expires = ctx . query . expires_in ; console . log ( `authCode:${ authCode } expiresIn:${ expires }` ) ; let queryAuth = await OpenMpApi . queryAuth ( appId , authCode ) ; let authorizationInfo = queryAuth . authorization_info ; console . log ( 'queryAuth.authorization_info:' ) ; console . log ( authorizationInfo ) ; let authorizerAppId = authorizationInfo . authorizer_appid ; let authorizerAccessToken = authorizationInfo . authorizer_access_token ; let expiresIn = authorizationInfo . expires_in ; let authorizerRefreshToken = authorizationInfo . authorizer_refresh_token ; let cache = ApiConfigKit . getCache ; let accessToken = new AccessToken ( JSON . stringify ( { authorizer_access_token : authorizerAccessToken , expires_in : expiresIn , authorizer_refresh_token : authorizerRefreshToken } ) , AccessTokenType . AUTHORIZER_TOKEN ) ; cache . set ( appId . concat ( '_' ) . concat ( authorizerAppId ) , accessToken . getCacheJson ) ; let authorizerAccessTokenCache = await OpenAuthorizerAccessTokenApi . getAccessToken ( authorizerAppId ) ; if ( authorizerAccessTokenCache ) { console . log ( `缓存中的 authorizerAccessToken: ${ authorizerAccessTokenCache . getAccessToken }` ) ; } else { console . log ( `缓存中暂无 authorizerAccessToken` ) ; } } public async api ( ) { const { ctx } = this ; let type = parseInt ( ctx . query . type ) ; let refreshToken = ctx . query . refreshToken ; console . log ( `type:${ type }` ) ; console . log ( `refreshToken:${ refreshToken }` ) ; let appId = ApiConfigKit . getApiConfig . getAppId ; switch ( type ) { case 1 : try { let data = await OpenComponentAccessTokenApi . getAccessToken ( ) ; ctx . body = data ; } catch ( error ) { console . log ( error ) ; } break ; case 2 : try { let data = await OpenMpApi . getPreAuthCode ( appId ) ; let preAuthCode = data . pre_auth_code ; console . log ( `preAuthCode:${ preAuthCode }` ) ; let componentLoginPageUrl = OpenMpApi . getComponentLoginPage ( appId , preAuthCode , ctx . app . config . openMpConfig . domain . concat ( '/openmpauth' ) ) ; await this . ctx . render ( 'openmp' , { url : componentLoginPageUrl } ) ; } catch ( error ) { console . log ( error ) ; } break ; case 3 : try { let data = await OpenMpApi . getPreAuthCode ( appId ) ; let preAuthCode = data . pre_auth_code ; console . log ( `preAuthCode:${ preAuthCode }` ) ; let bindComponentUrl = OpenMpApi . bindComponent ( appId , preAuthCode , ctx . app . config . openMpConfig . domain . concat ( '/openmpauth' ) , 3 ) ; await this . ctx . render ( 'openmp' , { url : bindComponentUrl } ) ; } catch ( error ) { console . log ( error ) ; } break ; case 4 : try { let data = await OpenMpApi . getAuthorizerList ( appId , 0 , 50 ) ; ctx . body = data ; } catch ( error ) { console . log ( error ) ; } break ; case 5 : try { let data = await OpenMpApi . getAuthorizerInfo ( appId , ctx . app . config . openMpConfig . authAppId ) ; ctx . body = data ; } catch ( error ) { console . log ( error ) ; } break ; case 6 : try { let data = await OpenMpApi . getAuthorizerOption ( appId , ctx . app . config . openMpConfig . authAppId , 'location_report' ) ; ctx . body = data ; } catch ( error ) { console . log ( error ) ; } break ; case 7 : try { let componentAccessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let authAccessToken = await OpenAuthorizerAccessTokenApi . getAccessToken ( ctx . app . config . openMpConfig . authAppId , componentAccessToken . getAccessToken , refreshToken ) ; console . log ( authAccessToken ) ; console . log ( `获取到的 authAccessToken: ${ authAccessToken . getAccessToken }` ) ; ctx . body = authAccessToken ; } catch ( error ) { console . log ( error ) ; } break ; case 8 : try { let authAccessToken = await OpenAuthorizerAccessTokenApi . getAccessToken ( ctx . app . config . openMpConfig . authAppId ) ; console . log ( '获取到的 authAccessToken' ) ; console . log ( authAccessToken ) ; let data = await CallbackApi . getCallbackIp ( authAccessToken ) ; ctx . body = data ; } catch ( error ) { console . log ( error ) ; } break ; default : break ; } } public async jssdk ( ) { let { ctx } = this ; await ctx . render ( 'jssdk' , ctx . request . body ) ; } }