CasaOS/service/casa.go

129 lines
3.4 KiB
Go
Raw Normal View History

2021-09-26 02:35:02 +00:00
package service
import (
json2 "encoding/json"
2021-12-29 08:42:20 +00:00
"fmt"
"strconv"
2021-09-27 06:17:36 +00:00
"github.com/IceWhaleTech/CasaOS/model"
"github.com/IceWhaleTech/CasaOS/pkg/config"
httper2 "github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
model2 "github.com/IceWhaleTech/CasaOS/service/model"
2021-09-26 02:35:02 +00:00
"github.com/tidwall/gjson"
)
type CasaService interface {
2021-12-29 08:42:20 +00:00
GetServerList(index, size, tp, categoryId, key string) (recommend, list, community []model.ServerAppList)
2021-09-26 02:35:02 +00:00
GetServerCategoryList() []model.ServerCategoryList
GetTaskList(size int) []model2.TaskDBModel
GetServerAppInfo(id, t string) model.ServerAppList
2021-11-25 09:35:01 +00:00
ShareAppFile(body []byte) string
2021-09-26 02:35:02 +00:00
}
type casaService struct {
2021-09-26 02:35:02 +00:00
}
2021-11-25 09:35:01 +00:00
func (o *casaService) ShareAppFile(body []byte) string {
head := make(map[string]string)
head["Authorization"] = GetToken()
content := httper2.Post(config.ServerInfo.ServerApi+"/v1/community/add", body, "application/json", head)
return content
}
func (o *casaService) GetTaskList(size int) []model2.TaskDBModel {
2021-09-26 02:35:02 +00:00
head := make(map[string]string)
head["Authorization"] = GetToken()
2021-09-26 02:35:02 +00:00
listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/task/list/"+strconv.Itoa(size), head)
list := []model2.TaskDBModel{}
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
return list
}
2021-12-29 08:42:20 +00:00
func (o *casaService) GetServerList(index, size, tp, categoryId, key string) (recommend, list, community []model.ServerAppList) {
keyName := fmt.Sprintf("list_%s_%s_%s_%s", index, size, tp, categoryId)
if result, ok := Cache.Get(keyName); ok {
res, ok := result.(string)
if ok {
json2.Unmarshal([]byte(gjson.Get(res, "data.list").String()), &list)
json2.Unmarshal([]byte(gjson.Get(res, "data.recommend").String()), &recommend)
json2.Unmarshal([]byte(gjson.Get(res, "data.community").String()), &community)
return
}
}
2021-09-26 02:35:02 +00:00
head := make(map[string]string)
head["Authorization"] = GetToken()
2021-09-26 02:35:02 +00:00
2021-12-29 08:42:20 +00:00
listS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/newlist?index="+index+"&size="+size+"&rank="+tp+"&category_id="+categoryId+"&key="+key, head)
2021-09-26 02:35:02 +00:00
2021-12-29 08:42:20 +00:00
json2.Unmarshal([]byte(gjson.Get(listS, "data.list").String()), &list)
json2.Unmarshal([]byte(gjson.Get(listS, "data.recommend").String()), &recommend)
json2.Unmarshal([]byte(gjson.Get(listS, "data.community").String()), &community)
2021-09-26 02:35:02 +00:00
2021-12-29 08:42:20 +00:00
if len(list) > 0 {
Cache.SetDefault(keyName, listS)
}
return
2021-09-26 02:35:02 +00:00
}
func (o *casaService) GetServerCategoryList() []model.ServerCategoryList {
2021-09-26 02:35:02 +00:00
head := make(map[string]string)
head["Authorization"] = GetToken()
2021-09-26 02:35:02 +00:00
2021-12-29 08:42:20 +00:00
listS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/category", head)
list := []model.ServerCategoryList{}
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
return list
}
func (o *casaService) GetServerAppInfo(id, t string) model.ServerAppList {
head := make(map[string]string)
head["Authorization"] = GetToken()
infoS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/info/"+id+"?t="+t, head)
info := model.ServerAppList{}
json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
return info
}
func GetToken() string {
2021-09-26 02:35:02 +00:00
t := make(chan string)
keyName := "casa_token"
2021-09-26 02:35:02 +00:00
var auth string
if result, ok := Cache.Get(keyName); ok {
auth, ok = result.(string)
if ok {
return auth
}
}
2021-09-26 02:35:02 +00:00
go func() {
str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil)
t <- gjson.Get(str, "data").String()
}()
auth = <-t
2021-09-26 02:35:02 +00:00
Cache.SetDefault(keyName, auth)
return auth
2021-09-26 02:35:02 +00:00
}
func NewOasisService() CasaService {
return &casaService{}
2021-09-26 02:35:02 +00:00
}