import { Body , Controller , Get , HttpStatus , Param , Post , Req , UseGuards } from '@nestjs/common' ; import { JwtService } from '@nestjs/jwt' ; import { MiniProgram , Wechat } from 'wechat-jssdk' ; import * as APP_CONFIG from '../../../app.config' ; import { CacheService } from '../../../cache/cache.service' ; import * as CACHE_KEY from '../../../common/constants/cache.constant' ; import { Permission } from '../../../common/generated-types' ; import { HttpProcessor } from '../../../decorators/http.decorator' ; import { UserService } from '../../../service' ; import { Allow } from '../../decorators/allow.decorator' ; import { JwtAuthGuard } from '../../middleware/guards/auth.guard' ; @ Controller ( 'wechat' ) export class WechatController { private wechatConfig : object = { appId : 'appId' , appSecret : 'appSecret' , miniProgram : { appId : 'wx53d04eb9e7c3d68d' , appSecret : '71ac7ed4bf869bc2b7b578395a89eb44' , } , } ; private wx : Wechat ; private mp : MiniProgram ; constructor ( private readonly jwtService , private readonly usersService , private readonly cacheService ) { this . wx = new Wechat ( this . wechatConfig ) ; this . mp = this . wx . miniProgram ; } param code @ Get ( ':code' ) @ Allow ( Permission . Public ) async login ( @ Param ( 'code' ) code ) { console . log ( 'get Code ....' ) ; const { openid , session_key } = await this . mp . getSession ( code ) ; if ( session_key ) { const user = await this . usersService . createOrUpdate ( { identifier : openid , } ) ; await this . cacheService . set ( `${ CACHE_KEY . WECHAT_SESSION_KEY }${ openid }` , session_key ) ; const token = this . jwtService . sign ( { type : 'wechat' , identifier : openid , id : user . id , } ) ; return Promise . resolve ( { token , expires_in : APP_CONFIG . AUTH . expiresIn , } ) ; } } @ Post ( 'verify' ) @ Allow ( Permission . Public ) @ HttpProcessor . handle ( { message : '数据完整性验证' , error : HttpStatus . FORBIDDEN } ) async verifySignature ( @ Req ( ) req , @ Body ( ) body ) { const sessionKey = await this . cacheService . get < string > ( `${ CACHE_KEY . WECHAT_SESSION_KEY }${ req . user }` ) ; try { await this . mp . verifySignature ( body . rawData , body . signature , sessionKey ) ; return 'ok' ; } catch ( e ) { throw e ; } } @ Post ( 'decrypt' ) @ Allow ( Permission . Owner ) async decryptData ( @ Req ( ) req , @ Body ( ) body ) { const sessionKey = await this . cacheService . get < string > ( `${ CACHE_KEY . WECHAT_SESSION_KEY }${ req . user }` ) ; const res = await this . mp . decryptData ( body . encryptedData , body . iv , sessionKey ) ; const user = await this . usersService . findByIdentifier ( req . user ) ; if ( user && user . metas ) { for ( const meta of user . metas ) { if ( meta . key === '_wechat' ) { meta . value = res ; } } const newUser = await this . usersService . updateUser ( user ) ; const resData = { } ; if ( newUser && newUser . metas ) { for ( const meta of newUser . metas ) { if ( meta . key === '_wechat' ) { Object . assign ( resData , { id : newUser . id , ... meta . value , } ) ; } } } return resData ; } } @ Post ( 'check' ) @ Allow ( Permission . Owner ) check ( @ Body ( ) body ) { return false ; } @ Post ( 'me' ) @ Allow ( Permission . Owner ) me ( @ Req ( ) req ) { } }