2021-09-26 02:35:02 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
json2 "encoding/json"
|
2021-10-29 10:37:27 +00:00
|
|
|
"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 OasisService interface {
|
|
|
|
GetServerList(index, size, tp, categoryId, key string) ([]model.ServerAppList, int64)
|
|
|
|
GetServerCategoryList() []model.ServerCategoryList
|
|
|
|
GetTaskList(size int) []model2.TaskDBModel
|
|
|
|
}
|
|
|
|
|
|
|
|
type oasisService struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *oasisService) GetTaskList(size int) []model2.TaskDBModel {
|
|
|
|
head := make(map[string]string)
|
|
|
|
|
2021-10-29 10:37:27 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *oasisService) GetServerList(index, size, tp, categoryId, key string) ([]model.ServerAppList, int64) {
|
|
|
|
|
|
|
|
head := make(map[string]string)
|
|
|
|
|
2021-10-29 10:37:27 +00:00
|
|
|
head["Authorization"] = GetToken()
|
2021-09-26 02:35:02 +00:00
|
|
|
|
|
|
|
listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/app/list?index="+index+"&size="+size+"&type="+tp+"&category_id="+categoryId+"&key="+key, head)
|
|
|
|
|
|
|
|
list := []model.ServerAppList{}
|
|
|
|
|
|
|
|
count := gjson.Get(listS, "data.count").Int()
|
|
|
|
json2.Unmarshal([]byte(gjson.Get(listS, "data.items").String()), &list)
|
|
|
|
|
|
|
|
return list, count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *oasisService) GetServerCategoryList() []model.ServerCategoryList {
|
|
|
|
|
|
|
|
head := make(map[string]string)
|
2021-10-29 10:37:27 +00:00
|
|
|
head["Authorization"] = GetToken()
|
2021-09-26 02:35:02 +00:00
|
|
|
|
2021-10-29 10:37:27 +00:00
|
|
|
listS := httper2.Get(config.ServerInfo.ServerApi+"/v1/app/category", head)
|
|
|
|
|
|
|
|
list := []model.ServerCategoryList{}
|
|
|
|
|
|
|
|
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetToken() string {
|
2021-09-26 02:35:02 +00:00
|
|
|
t := make(chan string)
|
2021-10-29 10:37:27 +00:00
|
|
|
keyName := "casa_token"
|
2021-09-26 02:35:02 +00:00
|
|
|
|
2021-10-29 10:37:27 +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()
|
|
|
|
}()
|
2021-10-29 10:37:27 +00:00
|
|
|
auth = <-t
|
2021-09-26 02:35:02 +00:00
|
|
|
|
2021-10-29 10:37:27 +00:00
|
|
|
Cache.SetDefault(keyName, auth)
|
|
|
|
return auth
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewOasisService() OasisService {
|
|
|
|
return &oasisService{}
|
|
|
|
}
|