import * as util from 'util' ; import { HttpKit } from '@tnwx/kits' ; import { AccessToken , OpenCpAccessTokenApi , AccessTokenType } from '@tnwx/accesstoken' ; author Javen
 *  copyright description export class OpenCpApi { private static getPreAuthCodeUrl = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_pre_auth_code?suite_access_token=%s' ; /**
     * 获取预授权码
     */ public static async getPreAuthCode ( ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . SUITE_TOKEN ) ; let url = util . format ( this . getPreAuthCodeUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpGet ( url ) ; } private static setSessionInfoUrl = 'https://qyapi.weixin.qq.com/cgi-bin/service/set_session_info?suite_access_token=%s' ; param preAuthCode param authType param appId public static async setSessionInfo ( preAuthCode , authType = 0 , appId ? ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . SUITE_TOKEN ) ; let url = util . format ( this . setSessionInfoUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { pre_auth_code : preAuthCode , session_info : { appid : appId , auth_type : authType } } ) ) ; } private static getPermanentCodeUrl = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code?suite_access_token=%s' ; param authCode public static async getPermanentCode ( authCode ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . SUITE_TOKEN ) ; let url = util . format ( this . getPermanentCodeUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { auth_code : authCode } ) ) ; } private static getAuthInfoUrl = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_auth_info?suite_access_token=%s' ; param authCorpId param permanentCode public static async getAuthInfo ( authCorpId , permanentCode ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . SUITE_TOKEN ) ; let url = util . format ( this . getAuthInfoUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { auth_corpid : authCorpId , permanent_code : permanentCode } ) ) ; } private static getAdminListUrl = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_admin_list?suite_access_token=%s' ; param authCorpId param agentId public static async getAdminList ( authCorpId , agentId ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . SUITE_TOKEN ) ; let url = util . format ( this . getAdminListUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { auth_corpid : authCorpId , agentid : agentId } ) ) ; } private static searchContactUrl = 'https://qyapi.weixin.qq.com/cgi-bin/service/contact/search?provider_access_token=%s' ; param authCorpId param queryWord param queryType param agentId param offset param limit public static async searchContact ( authCorpId , queryWord , queryType , agentId ? , offset ? , limit ? ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . PROVIDER_TOKEN ) ; let url = util . format ( this . searchContactUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { auth_corpid : authCorpId , query_word : queryWord , query_type : queryType , agentid : agentId , offset : offset , limit : limit } ) ) ; } private static batchSearchContactUrl = 'https://qyapi.weixin.qq.com/cgi-bin/service/contact/batchsearch?provider_access_token=%s' ; param authCorpId param queryRequestList param agentId public static async batchSearchContact ( authCorpId , queryRequestList , agentId ? ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . PROVIDER_TOKEN ) ; let url = util . format ( this . batchSearchContactUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { auth_corpid : authCorpId , query_request_list : queryRequestList , agentid : agentId } ) ) ; } private static uploadUrl : string = 'https://qyapi.weixin.qq.com/cgi-bin/service/media/upload?provider_access_token=%s&type=%s' ; param mediaType param filePath param accessToken public static async upload ( mediaType , filePath ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( ) ; let url = util . format ( this . uploadUrl , accessToken . getAccessToken , mediaType ) ; return HttpKit . getHttpDelegate . upload ( url , filePath ) ; } private static contactIdTranslateUrl = 'https://qyapi.weixin.qq.com/cgi-bin/service/contact/id_translate?provider_access_token=%s' ; param authCorpId param mediaIdList param outputFileName public static async contactIdTranslate ( authCorpId , mediaIdList , outputFileName ? ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . PROVIDER_TOKEN ) ; let url = util . format ( this . contactIdTranslateUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { auth_corpid : authCorpId , media_id_list : mediaIdList , output_file_name : outputFileName } ) ) ; } private static getBatchResultUrl = 'https://qyapi.weixin.qq.com/cgi-bin/service/batch/getresult?provider_access_token=%s&jobid=%s' ; param jobId public static async getBatchResult ( jobId ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . PROVIDER_TOKEN ) ; let url = util . format ( this . getBatchResultUrl , accessToken . getAccessToken , jobId ) ; return HttpKit . getHttpDelegate . httpGet ( url ) ; } private static sortContactUrl = 'https://qyapi.weixin.qq.com/cgi-bin/service/contact/sort?provider_access_token=%s' ; param authCorpId param userIdList param sortType public static async sortContact ( authCorpId , userIdList , sortType ? ) { let accessToken = await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . PROVIDER_TOKEN ) ; let url = util . format ( this . sortContactUrl , accessToken . getAccessToken ) ; return HttpKit . getHttpDelegate . httpPost ( url , JSON . stringify ( { auth_corpid : authCorpId , useridlist : userIdList , sort_type : sortType } ) ) ; } }