import * as KoaRouter from 'koa-router' ; import 'reflect-metadata' ; import { paddingRight } from '../util/string' ; import ResultModel , { ResultEnum , } from '../entity_enum/result' ; import { ApiMethod } from '../entity_enum' ; import { Action } from '../entity/action' ; export const KOAROUTER = 'KOAROUTER' ; export const PREFIX = 'PREFIX' ; export class ReqOptions { path : string = '/' ; name : string = '' ; method ? : string = RequestMethod . GET ; needLogin ? : boolean = false ; needUserInfo ? : boolean = false ; reqBody ? : string = '' ; resBody ? : string = '' ; } param param0 export function Request ( option ) { let { path = '/' , method = RequestMethod . GET , needLogin = false , needUserInfo = false , reqBody = '' , resBody = '' } = option ; if ( needLogin ) { needUserInfo = true ; } return function ( target , name , descriptor ) { let fn = descriptor . value ; descriptor . value = async ( router ) => { try { let prefix = Reflect . getMetadata ( PREFIX , target . constructor ) || '/' ; let fullActionPath = '/' + prefix + '/' + path ; fullActionPath = fullActionPath . replace ( /\/{2,999}/g , '/' ) ; if ( fullActionPath . indexOf ( '/api' ) >= 0 ) { let actionModel = new Action ( ) ; actionModel . path = fullActionPath ; actionModel . name = name ; actionModel . reqBody = reqBody ; actionModel . resBody = resBody ; actionModel . method = method as ApiMethod ; console . log ( 'action 模型:' , JSON . stringify ( actionModel ) ) ; } let methodsBeautify = paddingRight ( `【${ method . toUpperCase ( ) }】` , 6 , ' ' ) ; console . log ( `${ methodsBeautify } action path:  ${ fullActionPath }` ) ; router [ method ] ( path , async ( ctx , next ) => { let result = new ResultModel ( ) ; result . code = ResultEnum . NOTAUTH ; result . message = '没有权限，请重新登录后重试' ; let userInfo = null ; if ( needUserInfo ) { let { session_key } = ctx . request . cookie ; console . log ( 'needUserInfo session_key:' , session_key ) ; try { } catch ( err ) { } } if ( needLogin ) { console . log ( 'needlogin userInfo:' , userInfo ) ; if ( ! userInfo ) { ctx . body = result ; return ; } } const res = await fn ( ctx , next ) ; } ) ; } catch ( err ) { console . log ( err ) ; } } ; } ; } export const RequestMethod = { GET : 'get' , POST : 'post' , PUT : 'put' , DELETE : 'delete' } ; export function GET ( option ) { return Request ( option ) ; } export function POST ( option ) { return Request ( { method : RequestMethod . POST , ... option } ) ; } export function PUT ( option ) { return Request ( { method : RequestMethod . PUT , ... option } ) ; } export function DELETE ( option ) { return Request ( { method : RequestMethod . DELETE , ... option } ) ; } export const Controller = ( { prefix = '/' } ) => { let router = new KoaRouter ( ) ; return target => { let parentPrefix = Reflect . getMetadata ( PREFIX , target . prototype . constructor ) || '/' ; let prefixResult = '/' + parentPrefix + '/' + prefix ; prefixResult = prefixResult . replace ( /\/{2,999}/g , '/' ) ; router . prefix ( prefixResult ) ; Reflect . defineMetadata ( PREFIX , prefixResult , target ) ; Reflect . defineMetadata ( KOAROUTER , router , target ) ; let reqList = Object . getOwnPropertyDescriptors ( target . prototype ) ; for ( let v in reqList ) { if ( v !== 'constructor' ) { let fn = reqList [ v ] . value ; fn ( router ) ; } } return target ; } ; } ;