1
0
mirror of https://github.com/IceWhaleTech/CasaOS.git synced 2025-07-03 23:30:39 +02:00
Files
CasaOS/service/casa.go

149 lines
3.9 KiB
Go
Raw Normal View History

2021-09-26 10:35:02 +08:00
package service
import (
2022-02-17 18:43:25 +08:00
"encoding/json"
2021-09-26 10:35:02 +08:00
json2 "encoding/json"
2021-12-29 16:42:20 +08:00
"fmt"
"strconv"
2021-09-27 14:17:36 +08: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 10:35:02 +08:00
"github.com/tidwall/gjson"
)
type CasaService interface {
2022-01-26 18:50:34 +08:00
GetServerList(index, size, tp, categoryId, key, language string) (recommend, list, community []model.ServerAppList)
2021-09-26 10:35:02 +08:00
GetServerCategoryList() []model.ServerCategoryList
GetTaskList(size int) []model2.TaskDBModel
2022-01-26 18:50:34 +08:00
GetServerAppInfo(id, t string, language string) model.ServerAppList
2021-11-25 17:35:01 +08:00
ShareAppFile(body []byte) string
2022-02-17 18:43:25 +08:00
PushHeart(id, t string, language string)
2021-09-26 10:35:02 +08:00
}
type casaService struct {
2021-09-26 10:35:02 +08:00
}
2021-11-25 17:35:01 +08: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 10:35:02 +08:00
head := make(map[string]string)
head["Authorization"] = GetToken()
2021-09-26 10:35:02 +08: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
}
2022-01-26 18:50:34 +08:00
func (o *casaService) GetServerList(index, size, tp, categoryId, key, language string) (recommend, list, community []model.ServerAppList) {
2021-12-29 16:42:20 +08:00
2022-01-26 18:50:34 +08:00
keyName := fmt.Sprintf("list_%s_%s_%s_%s_%s", index, size, tp, categoryId, language)
2021-12-29 16:42:20 +08:00
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 10:35:02 +08:00
head := make(map[string]string)
head["Authorization"] = GetToken()
2021-09-26 10:35:02 +08:00
2022-01-26 18:50:34 +08:00
listS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/newlist?index="+index+"&size="+size+"&rank="+tp+"&category_id="+categoryId+"&key="+key+"&language="+language, head)
2021-09-26 10:35:02 +08:00
2021-12-29 16:42:20 +08: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 10:35:02 +08:00
2021-12-29 16:42:20 +08:00
if len(list) > 0 {
Cache.SetDefault(keyName, listS)
}
return
2021-09-26 10:35:02 +08:00
}
func (o *casaService) GetServerCategoryList() []model.ServerCategoryList {
2021-09-26 10:35:02 +08:00
head := make(map[string]string)
head["Authorization"] = GetToken()
2021-09-26 10:35:02 +08:00
2021-12-29 16:42:20 +08: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
}
2022-01-26 18:50:34 +08:00
func (o *casaService) GetServerAppInfo(id, t string, language string) model.ServerAppList {
head := make(map[string]string)
head["Authorization"] = GetToken()
2022-01-26 18:50:34 +08:00
infoS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/info/"+id+"?t="+t+"&language="+language, head)
info := model.ServerAppList{}
json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
return info
}
func GetToken() string {
2021-09-26 10:35:02 +08:00
t := make(chan string)
keyName := "casa_token"
2021-09-26 10:35:02 +08:00
var auth string
if result, ok := Cache.Get(keyName); ok {
auth, ok = result.(string)
if ok {
return auth
}
}
2021-09-26 10:35:02 +08:00
go func() {
str := httper2.Get(config.ServerInfo.ServerApi+"/token", nil)
t <- gjson.Get(str, "data").String()
}()
auth = <-t
2021-09-26 10:35:02 +08:00
Cache.SetDefault(keyName, auth)
return auth
2021-09-26 10:35:02 +08:00
}
2022-02-17 18:43:25 +08:00
func (o *casaService) PushHeart(id, t string, language string) {
m := model.CasaOSHeart{}
m.UuId = id
m.Type = t
b, _ := json.Marshal(m)
head := make(map[string]string)
head["Authorization"] = GetToken()
infoS := httper2.Post(config.ServerInfo.ServerApi+"/v1/analyse/heart", b, "application/json", head)
info := model.ServerAppList{}
json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
}
func NewOasisService() CasaService {
return &casaService{}
2021-09-26 10:35:02 +08:00
}