import { Context , Application , EggAppConfig } from 'egg' ; import { ApiConfigKit , WeChat , OpenAuthorizerAccessTokenApi , AccessToken } from 'tnwx' ; import * as uuid from 'uuid' ; param app param options export default function openJssdkMiddleWare ( app , options ? ) { return async ( ctx , next ) => { try { app . logger . info ( `options:${ JSON . stringify ( options ) }` ) ; const req = ctx . request ; let appId = ApiConfigKit . getApiConfig . getAppId ; let timestamp = new Date ( ) . getTime ( ) ; let nonceStr = uuid . v1 ( ) ; let url = req . protocol . concat ( '://' ) . concat ( req . host ) . replace ( ':80' , '' ) . concat ( req . url ) ; ctx . app . logger . info ( `url:${ url }` ) ; let accessToken = await OpenAuthorizerAccessTokenApi . getAccessToken ( ctx . app . config . openMpConfig . authAppId ) ; let signature = await WeChat . jssdkSignature ( nonceStr , String ( timestamp ) , url , accessToken ) ; req . body = { appId : appId , timestamp : timestamp , nonceStr : nonceStr , signature : signature } ; await next ( ) ; } catch ( error ) { console . log ( error ) ; } } ; }