@@ -8,8 +8,6 @@ import { UserService } from '../user.service'
88// TODO: need better cache impl
99let siteCategoryCache : SiteCat [ ] | null = null
1010
11- const DEFAULT_ORDER = 999999
12-
1311async function getAuthedPostCatReq ( ) {
1412 const token = await AuthManager . acquireToken ( )
1513 // TODO: need better solution
@@ -24,54 +22,7 @@ export namespace PostCatService {
2422 const resp = await req . getAll ( )
2523 const { categories } = < { categories : PostCat [ ] } > JSON . parse ( resp )
2624 if ( categories == null ) return [ ]
27- return categories . map ( x => Object . assign ( new PostCat ( ) , x ) )
28- } catch ( e ) {
29- if ( await UserService . hasBlog ( ) ) void Alert . err ( `查询随笔分类失败: ${ < string > e } ` )
30- throw e
31- }
32- }
33-
34- export async function getFlatAll ( ) {
35- const categories = await getAll ( )
36- if ( categories == null || categories . length === 0 ) return [ ]
37-
38- const flat = [ ]
39- const queue = categories
40- while ( queue . length > 0 ) {
41- const current = queue . pop ( )
42- if ( current == null ) continue
43- flat . push ( current )
44- if ( current . children != null ) for ( const child of current . children ) queue . unshift ( child )
45- }
46-
47- return flat . sort ( ( x , y ) => {
48- const order1 = x . order ?? DEFAULT_ORDER
49- const order2 = y . order ?? DEFAULT_ORDER
50- if ( order1 > order2 ) return 1
51- else if ( order1 < order2 ) return - 1
52- else return x . title . localeCompare ( y . title )
53- } )
54- }
55-
56- export async function getOne ( categoryId : number ) {
57- const req = await getAuthedPostCatReq ( )
58- try {
59- const resp = await req . getOne ( categoryId )
60- const { parent } = < { parent : PostCat | null } > JSON . parse ( resp )
61- return Object . assign ( new PostCat ( ) , parent )
62- } catch ( e ) {
63- if ( await UserService . hasBlog ( ) ) void Alert . err ( `查询随笔分类失败: ${ < string > e } ` )
64- throw e
65- }
66- }
67-
68- export async function getAllUnder ( parentId : number ) {
69- const req = await getAuthedPostCatReq ( )
70- try {
71- const resp = await req . getOne ( parentId )
72- const { categories } = < { categories : PostCat [ ] } > JSON . parse ( resp )
73- if ( categories == null ) return [ ]
74- return categories . map ( x => Object . assign ( new PostCat ( ) , x ) )
25+ return categories
7526 } catch ( e ) {
7627 if ( await UserService . hasBlog ( ) ) void Alert . err ( `查询随笔分类失败: ${ < string > e } ` )
7728 throw e
0 commit comments