@@ -297,6 +297,8 @@ class interfaceController extends baseController {
297297 this . projectModel . up ( params . project_id , { up_time : new Date ( ) . getTime ( ) } ) . then ( ) ;
298298 } ) ;
299299
300+ await this . autoAddTag ( params ) ;
301+
300302 ctx . body = yapi . commons . resReturn ( result ) ;
301303 }
302304
@@ -387,6 +389,45 @@ class interfaceController extends baseController {
387389 // return ctx.body = yapi.commons.resReturn(null, 400, 'path第一位必需为 /, 只允许由 字母数字-/_:.! 组成');
388390 }
389391
392+ async autoAddTag ( params ) {
393+ //检查是否提交了目前不存在的tag
394+ let tags = params . tag ;
395+ if ( tags && Array . isArray ( tags ) && tags . length > 0 ) {
396+ let projectData = await this . projectModel . get ( params . project_id ) ;
397+ let tagsInProject = projectData . tag ;
398+ let needUpdate = false ;
399+ if ( tagsInProject && Array . isArray ( tagsInProject ) && tagsInProject . length > 0 ) {
400+ tags . forEach ( tag => {
401+ if ( ! _ . find ( tagsInProject , item => {
402+ return item . name === tag ;
403+ } ) ) { //tag不存在
404+ needUpdate = true ;
405+ tagsInProject . push ( {
406+ name : tag ,
407+ desc : tag
408+ } ) ;
409+ }
410+ } ) ;
411+ } else {
412+ needUpdate = true
413+ tagsInProject = [ ]
414+ tags . forEach ( tag => {
415+ tagsInProject . push ( {
416+ name : tag ,
417+ desc : tag
418+ } ) ;
419+ } ) ;
420+ }
421+ if ( needUpdate ) { //需要更新tag
422+ let data = {
423+ tag : tagsInProject ,
424+ up_time : yapi . commons . time ( )
425+ } ;
426+ await this . projectModel . up ( params . project_id , data ) ;
427+ }
428+ }
429+ }
430+
390431 /**
391432 * 获取项目分组
392433 * @interface /interface/get
@@ -781,6 +822,8 @@ class interfaceController extends baseController {
781822 }
782823
783824 yapi . emitHook ( 'interface_update' , id ) . then ( ) ;
825+ await this . autoAddTag ( params ) ;
826+
784827 ctx . body = yapi . commons . resReturn ( result ) ;
785828 return 1 ;
786829 }
0 commit comments