author Javen
 *  copyright description import * as util from 'util' ; import { AccessToken , AccessTokenApi , ApiConfigKit } from '@tnwx/accesstoken' ; import { ICache } from '@tnwx/cache' ; import { HttpKit } from '@tnwx/kits' ; import { JsTicket } from '../entity/JsTicket' ; import { JsApiType } from '../Enums' ; export class JsTicketApi { private static getTicketUrl : string = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=%s' ; param type param accessToken public static async getTicket ( type , accessToken ? ) { let appId = ApiConfigKit . getApiConfig . getAppId ; let key = appId + ':' + type ; let cache = ApiConfigKit . getCache ; let jsTicketJson = await cache . get ( key ) ; const isAvailable = this . isAvailable ( jsTicketJson ) ; if ( jsTicketJson && isAvailable ) { if ( ApiConfigKit . isDevMode ( ) ) { console . debug ( '缓存中获取api_ticket...' ) ; } return new JsTicket ( jsTicketJson ) ; } if ( ! accessToken ) { accessToken = await AccessTokenApi . getAccessToken ( ) ; } let url = util . format ( this . getTicketUrl , accessToken . getAccessToken , type ) ; let data = await HttpKit . getHttpDelegate . httpGet ( url ) ; if ( data ) { data = JSON . stringify ( data ) ; let jsTicket = new JsTicket ( data ) ; let cache = ApiConfigKit . getCache ; cache . set ( key , jsTicket . getCacheJson ) ; if ( ApiConfigKit . isDevMode ( ) ) { console . debug ( '通过接口获取api_ticket...' ) ; } return jsTicket ; } } param jsTicketJson public static isAvailable ( jsTicketJson ) { if ( ! jsTicketJson ) return false ; const ticket = JSON . parse ( jsTicketJson ) ; if ( ! ticket . expired_time ) return false ; if ( ticket . errcode ) return false ; if ( ticket . expired_time < new Date ( ) . getTime ( ) ) return false ; return ticket . access_token != null ; } }