@ -29,26 +29,26 @@ Page({
this . setData ( { isEdit : true , editPostId : options.id } )
this . setData ( { isEdit : true , editPostId : options.id } )
}
}
app . waitLogin ( { type : 1 } ) . then ( ( ) = > {
app . waitLogin ( { type : 1 } ) . then ( ( ) = > {
this . getTopicList ( )
const listReady = Promise . all ( [ this . getTopicList ( ) , this . getCategoryList ( ) ] )
this . getCategoryList ( )
if ( this . data . isEdit ) {
if ( this . data . isEdit ) {
this . getPostDetail ( )
listReady . then ( ( ) = > {
this . getPostDetail ( )
} )
}
}
} )
} )
} ,
} ,
getTopicList() {
getTopicList() {
wx . ajax ( {
return wx . ajax ( {
method : 'GET' ,
method : 'GET' ,
url : '?r=psvt/topic/top-list' ,
url : '?r=psvt/topic/top-list' ,
} ) . then ( ( res : any ) = > {
} ) . then ( ( res : any ) = > {
this . setData ( {
this . setData ( {
topicList : res.list || [ ] ,
topicList : res.list || [ ] ,
selectedTopics : this.computeSelectedTopics ( this . data . selectedTopicIds , res . list || [ ] ) ,
} )
} )
} )
} )
} ,
} ,
getCategoryList() {
getCategoryList() {
wx . ajax ( {
return wx . ajax ( {
method : 'GET' ,
method : 'GET' ,
url : '?r=psvt/wall/index-category' ,
url : '?r=psvt/wall/index-category' ,
} ) . then ( ( res : any ) = > {
} ) . then ( ( res : any ) = > {
@ -80,16 +80,20 @@ Page({
url : item.attachmentUrl || '' ,
url : item.attachmentUrl || '' ,
}
}
} )
} )
// Build selectedTopics directly from the post detail, which already carries
// topicName. We must NOT rely on topicList.find() here because topic/top-list
// only returns the top-10 by weight, so a post's topics may not be in it.
const selectedTopics = ( res . topics || [ ] ) . map ( ( t : any ) = > ( {
id : Number ( t . topicId ) ,
topicName : t.topicName || '' ,
} ) )
this . setData ( {
this . setData ( {
title : res.title || '' ,
title : res.title || '' ,
content : res.content || '' ,
content : res.content || '' ,
visibility : res.visibility || 1 ,
visibility : res.visibility || 1 ,
selectedTopicIds : ( res . topics || [ ] ) . map ( ( t : any ) = > t . topicId ) ,
selectedTopicIds : selectedTopics.map ( ( t : any ) = > t . id ) ,
selectedTopics : this.computeSelectedTopics (
selectedTopics ,
( res . topics || [ ] ) . map ( ( t : any ) = > t . topicId ) ,
selectedCategoryIds : ( res . categories || [ ] ) . map ( ( c : any ) = > Number ( c . categoryId ) ) ,
this . data . topicList ,
) ,
selectedCategoryIds : ( res . categories || [ ] ) . map ( ( c : any ) = > c . categoryId ) ,
fileList ,
fileList ,
} )
} )
} )
} )
@ -103,27 +107,29 @@ Page({
handleTopicTap ( e : any ) {
handleTopicTap ( e : any ) {
const id = Number ( e . currentTarget . dataset . id )
const id = Number ( e . currentTarget . dataset . id )
const selectedTopicIds = [ . . . this . data . selectedTopicIds ]
const selectedTopicIds = [ . . . this . data . selectedTopicIds ]
const selectedTopics = [ . . . this . data . selectedTopics ]
const index = selectedTopicIds . indexOf ( id )
const index = selectedTopicIds . indexOf ( id )
if ( index > - 1 ) {
if ( index > - 1 ) {
selectedTopicIds . splice ( index , 1 )
selectedTopicIds . splice ( index , 1 )
selectedTopics . splice ( index , 1 )
} else {
} else {
selectedTopicIds . push ( id )
const topic = this . data . topicList . find ( ( t : any ) = > Number ( t . id ) === id )
if ( topic ) {
selectedTopicIds . push ( id )
selectedTopics . push ( { id : Number ( topic . id ) , topicName : topic.topicName } )
}
}
}
this . setData ( {
this . setData ( { selectedTopicIds , selectedTopics } )
selectedTopicIds ,
selectedTopics : this.computeSelectedTopics ( selectedTopicIds , this . data . topicList ) ,
} )
} ,
} ,
handleRemoveTopic ( e : any ) {
handleRemoveTopic ( e : any ) {
const id = Number ( e . currentTarget . dataset . id )
const id = Number ( e . currentTarget . dataset . id )
const selectedTopicIds = this . data . selectedTopicIds . filter ( ( tid ) = > tid !== id )
const index = this . data . selectedTopicIds . indexOf ( id )
this . setData ( {
if ( index === - 1 ) return
selectedTopicIds ,
const selectedTopicIds = [ . . . this . data . selectedTopicIds ]
selectedTopics : this.computeSelectedTopics ( selectedTopicIds , this . data . topicList ) ,
const selectedTopics = [ . . . this . data . selectedTopics ]
} )
selectedTopicIds . splice ( index , 1 )
} ,
selectedTopics . splice ( index , 1 )
computeSelectedTopics ( ids : number [ ] , list : any [ ] ) {
this . setData ( { selectedTopicIds , selectedTopics } )
return ids . map ( ( id ) = > list . find ( ( t ) = > String ( t . id ) === String ( id ) ) ) . filter ( ( t ) = > t )
} ,
} ,
showTopicPopup() {
showTopicPopup() {
this . setData ( { topicShow : true } )
this . setData ( { topicShow : true } )