import * as util from 'util' ; import { HttpKit } from '@tnwx/kits' ; import { AccessToken , OpenCpAccessTokenApi , AccessTokenType , OpenComponentAccessTokenApi } from '@tnwx/accesstoken' ; author Javen
 *  copyright description export class OpenMpApi { private static componentLoginPageUrl = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s&auth_type=%s' ; param appId param preAuthCode param redirectUrl param authType param bizAppId public static getComponentLoginPage ( appId , preAuthCode , redirectUrl , authType ? , bizAppId ? ) { let url = util . format ( this . componentLoginPageUrl , appId , preAuthCode , redirectUrl ) ; if ( authType ) { url . concat ( '&auth_type=' ) . concat ( authType . toString ( ) ) ; } if ( bizAppId ) { url . concat ( '&biz_appid=' ) . concat ( bizAppId ) ; } return url ; } private static bindComponentUrl = 'https://mp.weixin.qq.com/safe/bindcomponent?action=bindcomponent&no_scan=1&component_appid=%s&pre_auth_code=%s&redirect_uri=%s' ; param appId param preAuthCode param redirectUrl param authType param bizAppId public static bindComponent ( appId , preAuthCode , redirectUrl , authType ? , bizAppId ? ) { let url = util . format ( this . bindComponentUrl , appId , preAuthCode , redirectUrl ) ; if ( ! authType ) authType = 3 ; url . concat ( '&auth_type=' ) . concat ( authType . toString ( ) ) ; if ( bizAppId ) { url . concat ( '&biz_appid=' ) . concat ( bizAppId ) ; } url . concat ( '#wechat_redirect' ) ; return url ; } private static getPreAuthCodeUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=%s' ; param appId public static async getPreAuthCode ( appId ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . getPreAuthCodeUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { component_appid : appId } ) ) ; } private static queryAuthUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token=%s' ; param appId param authorizationCode public static async queryAuth ( appId , authorizationCode ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . queryAuthUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { component_appid : appId , authorization_code : authorizationCode } ) ) ; } private static getAuthorizerInfoUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s' ; param appId param authorizerAppId public static async getAuthorizerInfo ( appId , authorizerAppId ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . getAuthorizerInfoUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { component_appid : appId , authorizer_appid : authorizerAppId } ) ) ; } private static getAuthorizerOptionUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=%s' ; param appId param authorizerAppId param optionName public static async getAuthorizerOption ( appId , authorizerAppId , optionName ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . getAuthorizerOptionUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { component_appid : appId , authorizer_appid : authorizerAppId , option_name : optionName } ) ) ; } private static setAuthorizerOptionUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_set_authorizer_option?component_access_token=%s' ; param appId param authorizerAppId param optionName param optionValue public static async setAuthorizerOption ( appId , authorizerAppId , optionName , optionValue ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . setAuthorizerOptionUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { component_appid : appId , authorizer_appid : authorizerAppId , option_name : optionName , option_value : optionValue } ) ) ; } private static getAuthorizerListUrl = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_list?component_access_token=%s' ; param appId param offset param count public static async getAuthorizerList ( appId , offset , count ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . getAuthorizerListUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { component_appid : appId , offset : offset , count : count } ) ) ; } private static createUrl = 'https://api.weixin.qq.com/cgi-bin/open/create?access_token=%s' ; param appId public static async create ( appId ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . createUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { appid : appId } ) ) ; } private static bindUrl = 'https://api.weixin.qq.com/cgi-bin/open/bind?access_token=%s' ; param appId param openAppId public static async bind ( appId , openAppId ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . bindUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { appid : appId , open_appid : openAppId } ) ) ; } private static unBindUrl = 'https://api.weixin.qq.com/cgi-bin/open/unbind?access_token=%s' ; param appId param openAppId public static async unBind ( appId , openAppId ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . unBindUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { appid : appId , open_appid : openAppId } ) ) ; } private static getUrl = 'https://api.weixin.qq.com/cgi-bin/open/get?access_token=%s' ; param appId public static async get ( appId ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . getUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { appid : appId } ) ) ; } private static clearQuotaUrl = 'https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=%s' ; param appId public static async clearQuota ( appId , accessToken ) { if ( ! accessToken ) { accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; } let url = util . format ( this . clearQuotaUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { appid : appId } ) ) ; } private static clearComponentQuotaUrl = 'https://api.weixin.qq.com/cgi-bin/component/clear_quota?component_access_token=%s' ; param appId public static async clearComponentQuota ( appId ) { let accessToken = await OpenComponentAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . clearComponentQuotaUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { component_appid : appId } ) ) ; } }