84 lines
1.6 KiB
Go
84 lines
1.6 KiB
Go
package applistser
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// PageVM 福利应用页模板数据
|
|
type PageVM struct {
|
|
Categories []CategoryVM
|
|
}
|
|
|
|
type CategoryVM struct {
|
|
ID int64
|
|
SectionID string
|
|
Title string
|
|
Intro string
|
|
Total int
|
|
Apps []AppCardVM
|
|
IsFirst bool
|
|
}
|
|
|
|
type AppCardVM struct {
|
|
ID int64
|
|
Name string
|
|
Intro string
|
|
Logo string
|
|
URL string
|
|
}
|
|
|
|
// BuildPageVM 按接口返回的 categoryList / appList 顺序组装页面数据,不做二次排序。
|
|
func BuildPageVM(data *AppListResp) PageVM {
|
|
vm := PageVM{Categories: []CategoryVM{}}
|
|
if data == nil {
|
|
return vm
|
|
}
|
|
list := data.CategoryList
|
|
if len(list) == 0 && data.Category.ID > 0 {
|
|
list = []AppCategoryInfo{data.Category}
|
|
}
|
|
if len(list) == 0 {
|
|
return vm
|
|
}
|
|
|
|
for i, cat := range list {
|
|
cards := make([]AppCardVM, 0, len(cat.AppList))
|
|
for _, app := range cat.AppList {
|
|
name := strings.TrimSpace(app.Name)
|
|
if name == "" {
|
|
continue
|
|
}
|
|
href := strings.TrimSpace(app.URL)
|
|
if href == "" {
|
|
href = "#"
|
|
}
|
|
intro := strings.TrimSpace(app.Intro)
|
|
if intro == "" {
|
|
intro = strings.TrimSpace(app.Description)
|
|
}
|
|
cards = append(cards, AppCardVM{
|
|
ID: app.ID,
|
|
Name: name,
|
|
Intro: intro,
|
|
Logo: strings.TrimSpace(app.Logo),
|
|
URL: href,
|
|
})
|
|
}
|
|
title := strings.TrimSpace(cat.Title)
|
|
if title == "" {
|
|
title = "应用分类"
|
|
}
|
|
vm.Categories = append(vm.Categories, CategoryVM{
|
|
ID: cat.ID,
|
|
SectionID: fmt.Sprintf("app-category-%d", cat.ID),
|
|
Title: title,
|
|
Intro: strings.TrimSpace(cat.Intro),
|
|
Total: len(cards),
|
|
Apps: cards,
|
|
IsFirst: i == 0,
|
|
})
|
|
}
|
|
return vm
|
|
}
|