@@ -186,9 +186,37 @@ var getInfo = function (req, option, headers) {
186186 option . crossDomain ? headers : { } ) , option . headConfig )
187187 } ;
188188} ;
189+ var formatResult = function ( _a ) {
190+ var data = _a . data , method = _a . method , params = _a . params , result = _a . result ;
191+ try {
192+ result = JSON . parse ( JSON . stringify ( typeof data [ method ] == 'function'
193+ ? data [ method ] ( method , params , result )
194+ : data [ method ] instanceof Object
195+ ? data [ method ]
196+ : { } ) ) ;
197+ }
198+ catch ( e ) {
199+ result = { } ;
200+ }
201+ if ( ! ( result instanceof Object ) ) {
202+ result = { } ;
203+ }
204+ return result ;
205+ } ;
189206var mockResult = function ( result ) {
190207 return result instanceof Object ? Mock . mock ( result ) : result ;
191208} ;
209+ var authHandler = function ( _a ) {
210+ var http = _a . http , data = _a . data , option = _a . option , urlKey = _a . urlKey , headConfig = _a . headConfig ;
211+ if ( urlKey !== option . loginUrl && option . bounded && ! data . public && ! auth . verify ( ) ) {
212+ http . writeHead ( 401 , headConfig ) ;
213+ http . end ( {
214+ code : 401 ,
215+ message : urlKey && urlKey === option . logoutUrl ? '退出失败' : '权限不足, 请先登录'
216+ } ) ;
217+ return false ;
218+ }
219+ } ;
192220var errorHandler = function ( _a ) {
193221 var url = _a . url , http = _a . http , data = _a . data , method = _a . method , params = _a . params , result = _a . result , headConfig = _a . headConfig ;
194222 if ( typeof data . error === 'function' ) {
@@ -345,23 +373,6 @@ var createServer = function (option, callback) {
345373 createServer ( option , callback ) ;
346374 } ) ;
347375} ;
348- var formatResult = function ( _a ) {
349- var data = _a . data , method = _a . method , params = _a . params , result = _a . result ;
350- try {
351- result = JSON . parse ( JSON . stringify ( typeof data [ method ] == 'function'
352- ? data [ method ] ( method , params , result )
353- : data [ method ] instanceof Object
354- ? data [ method ]
355- : { } ) ) ;
356- }
357- catch ( e ) {
358- result = { } ;
359- }
360- if ( ! ( result instanceof Object ) ) {
361- result = { } ;
362- }
363- return result ;
364- } ;
365376/**
366377 * 启动mock服务
367378 * @func
@@ -424,6 +435,10 @@ var Server$1 = function (option, callback) {
424435 return ;
425436 }
426437 // 3. 处理错误
438+ if ( authHandler ( allOption ) === false ) {
439+ return ;
440+ }
441+ // 3. 处理错误
427442 if ( errorHandler ( allOption ) === false ) {
428443 return ;
429444 }
0 commit comments