CasaOS/service/casa.go

234 lines
6.2 KiB
Go
Raw Normal View History

2021-09-26 02:35:02 +00:00
package service
import (
2022-02-17 10:43:25 +00:00
"encoding/json"
2021-09-26 02:35:02 +00:00
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 {
2022-01-26 10:50:34 +00:00
GetServerList(index, size, tp, categoryId, key, language string) (recommend, list, community []model.ServerAppList)
2021-09-26 02:35:02 +00:00
GetServerCategoryList() []model.ServerCategoryList
GetTaskList(size int) []model2.TaskDBModel
2022-01-26 10:50:34 +00:00
GetServerAppInfo(id, t string, language string) model.ServerAppList
2021-11-25 09:35:01 +00:00
ShareAppFile(body []byte) string
2022-02-17 10:43:25 +00:00
PushHeart(id, t string, language string)
PushAppAnalyse(uuid, t string, name, language string)
2022-03-18 03:13:07 +00:00
PushConnectionStatus(uuid, err string, from, to, event string)
2022-04-06 04:10:51 +00:00
PushUserInfo()
GetUserInfoByShareId(shareId string) model.UserInfo
GetPersonPublic() (list []model.FriendsModel)
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
}
2022-01-26 10:50:34 +00:00
func (o *casaService) GetServerList(index, size, tp, categoryId, key, language string) (recommend, list, community []model.ServerAppList) {
2021-12-29 08:42:20 +00:00
2022-01-26 10:50:34 +00:00
keyName := fmt.Sprintf("list_%s_%s_%s_%s_%s", index, size, tp, categoryId, language)
2021-12-29 08:42:20 +00: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 02:35:02 +00:00
head := make(map[string]string)
head["Authorization"] = GetToken()
2021-09-26 02:35:02 +00:00
2022-01-26 10:50:34 +00: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 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
}
2022-01-26 10:50:34 +00:00
func (o *casaService) GetServerAppInfo(id, t string, language string) model.ServerAppList {
head := make(map[string]string)
head["Authorization"] = GetToken()
2022-01-26 10:50:34 +00: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 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
}
2022-02-17 10:43:25 +00: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 (o *casaService) PushAppAnalyse(uuid, t string, name, language string) {
m := model.AppAnalyse{}
m.UUId = uuid
m.Type = t
m.Name = name
m.Language = language
b, _ := json.Marshal(m)
head := make(map[string]string)
head["Authorization"] = GetToken()
infoS := httper2.Post(config.ServerInfo.ServerApi+"/v1/analyse/app", b, "application/json", head)
info := model.ServerAppList{}
json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
2022-03-18 03:13:07 +00:00
}
func (o *casaService) PushConnectionStatus(uuid, err string, from, to, event string) {
m := model.ConnectionStatus{}
m.UUId = uuid
m.Error = err
m.From = from
m.To = to
m.Event = event
b, _ := json.Marshal(m)
head := make(map[string]string)
head["Authorization"] = GetToken()
2022-04-06 04:10:51 +00:00
infoS := httper2.Post(config.ServerInfo.ServerApi+"/v1/analyse/connect", b, "application/json", head)
info := model.ServerAppList{}
json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
}
func (o *casaService) PushUserInfo() {
m := model.UserInfo{}
m.Desc = config.UserInfo.Description
m.Avatar = config.UserInfo.Avatar
m.NickName = config.UserInfo.NickName
m.ShareId = config.ServerInfo.Token
b, _ := json.Marshal(m)
head := make(map[string]string)
head["Authorization"] = GetToken()
infoS := httper2.Post(config.ServerInfo.ServerApi+"/v1/user/info", b, "application/json", head)
2022-03-18 03:13:07 +00:00
info := model.ServerAppList{}
json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
}
2022-04-06 04:10:51 +00:00
func (o *casaService) GetUserInfoByShareId(shareId string) model.UserInfo {
head := make(map[string]string)
head["Authorization"] = GetToken()
infoS := httper2.Get(config.ServerInfo.ServerApi+"/v1/user/info/"+shareId, head)
info := model.UserInfo{}
json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
return info
}
func (o *casaService) GetPersonPublic() (list []model.FriendsModel) {
head := make(map[string]string)
head["Authorization"] = GetToken()
listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/person/public", head)
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
return list
}
func NewCasaService() CasaService {
return &casaService{}
2021-09-26 02:35:02 +00:00
}