@@ -90,10 +90,11 @@ export default class ImportDrawer extends React.Component<
9090 }
9191 }
9292
93- handleConfirm ( ) {
93+ async handleConfirm ( ) {
9494 // 确认导入
95- const queue : any [ ] = [ ] ;
95+ const queue : Promise < void > [ ] = [ ] ;
9696 const tasks = new Set < string > ( ) ;
97+ const rules : BasicRule [ ] = [ ] ;
9798 this . state . list . forEach ( ( e : any ) => {
9899 // 不导入
99100 if ( e . importAction === 3 ) {
@@ -113,19 +114,56 @@ export default class ImportDrawer extends React.Component<
113114 e . enable = true ;
114115 }
115116 getRuleUsedTasks ( e ) . forEach ( task => tasks . add ( task ) ) ;
116- queue . push ( Api . saveRule ( e ) ) ;
117+ rules . push ( e ) ;
117118 } ) ;
118119 // 处理 task 导入
120+ const taskKeyAlias = new Map < string , string > ( ) ;
119121 if ( tasks . size > 0 ) {
122+ const allTasks = await Api . getTasks ( ) ;
123+ const taskKeys = allTasks . map ( x => x . key ) ;
120124 Array . from ( tasks )
121125 . map ( x => this . tasks ?. [ x ] )
122- . forEach ( t => queue . push ( t ? Api . saveTask ( t ) : Promise . resolve ( ) ) ) ;
126+ . forEach ( t => {
127+ if ( ! t ) {
128+ return Promise . resolve ( ) ;
129+ }
130+ while ( taskKeys . includes ( t . key ) ) {
131+ const newKey = `${ t . key } ${ Math . random ( ) . toString ( 36 ) . substring ( 1 ) } ` ;
132+ taskKeyAlias . set ( t . key , newKey ) ;
133+ t . key = newKey ;
134+ taskKeys . push ( newKey ) ;
135+ }
136+ queue . push ( Api . saveTask ( t ) ) ;
137+ } ) ;
123138 }
124- Promise . all ( queue ) . then ( ( ) => {
125- // this.imports.status = 0;
126- Toast . success ( t ( 'import_success' ) ) ;
127- this . props . onSuccess ?.( ) ;
139+
140+ const replaceTaskKeys = ( s : string ) => {
141+ let res = s ;
142+ taskKeyAlias . forEach ( ( value , key ) => {
143+ res = res . replaceAll ( `{\$TASK.${ key } .}` , `{\$TASK.${ value } .}` ) ;
144+ } ) ;
145+ return res ;
146+ } ;
147+
148+ // 处理 rule 导入
149+ rules . forEach ( e => {
150+ if ( e . headers ) {
151+ Object . keys ( e . headers ) . forEach ( key => {
152+ e . headers ! [ key ] = replaceTaskKeys ( e . headers ! [ key ] ) ;
153+ } ) ;
154+ }
155+ if ( e . to ) {
156+ e . to = replaceTaskKeys ( e . to ) ;
157+ }
158+ if ( e . body ?. value ) {
159+ e . body . value = replaceTaskKeys ( e . body . value ) ;
160+ }
161+ queue . push ( Api . saveRule ( e ) ) ;
128162 } ) ;
163+ await Promise . all ( queue ) ;
164+ // this.imports.status = 0;
165+ Toast . success ( t ( 'import_success' ) ) ;
166+ this . props . onSuccess ?.( ) ;
129167 this . setState ( {
130168 list : [ ] ,
131169 visible : false ,
0 commit comments