import { Controller } from 'egg' ; import { OpenCpAccessTokenApi , AccessTokenType , OpenCorpAccessTokenApi , OpenCpApi , AccessToken , QySendMsgApi , QyTextMsg , QyText } from 'tnwx' ; export default class OpenCpApiController extends Controller { public async index ( ) { const { ctx } = this ; let type = parseInt ( ctx . query . type ) ; let data = '企业微信开放平台...' ; ctx . app . logger . info ( `type:${ type }` ) ; switch ( type ) { case 1 : data = JSON . stringify ( await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . PROVIDER_TOKEN ) ) ; break ; case 2 : data = JSON . stringify ( await OpenCpAccessTokenApi . getAccessToken ( AccessTokenType . SUITE_TOKEN ) ) ; break ; case 3 : let authCode = 'A_m53fEQ0eeY3gsr-sGdlIJqu35qYNKxGmU155MgIPjRz0MYxQsNa-ng69tKjuggOwxSt5sedJdRAv-l44Jk7MMu3lM4oEFT-nrAWdjCBEE' ; data = JSON . stringify ( await OpenCpApi . getPermanentCode ( authCode ) ) ; break ; case 4 : let authCorpid = 'corpid' ; let permanentCode = 'permanent_code' ; let agentId = '1000009' ; let accessToken = await OpenCorpAccessTokenApi . getAccessToken ( authCorpid , permanentCode ) ; let text = new QyTextMsg ( new QyText ( '开放平台发送文本消息 By TNWX' ) , agentId , 'Javen' ) ; let result = await QySendMsgApi . sendTextMessage ( text , accessToken ) ; data = JSON . stringify ( result ) ; break ; default : break ; } ctx . app . logger . info ( data ) ; ctx . body = data ; } }