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"
|
2022-06-29 03:09:58 +00:00
|
|
|
"sort"
|
2021-10-29 10:37:27 +00:00
|
|
|
"strconv"
|
2022-06-08 10:19:45 +00:00
|
|
|
"time"
|
2021-10-29 10:37:27 +00:00
|
|
|
|
2021-09-27 06:17:36 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/model"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/config"
|
2022-06-29 03:09:58 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/file"
|
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
|
2021-09-27 06:17:36 +00:00
|
|
|
httper2 "github.com/IceWhaleTech/CasaOS/pkg/utils/httper"
|
2022-06-29 03:09:58 +00:00
|
|
|
"github.com/IceWhaleTech/CasaOS/pkg/utils/loger"
|
2022-07-22 03:02:11 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-09-26 02:35:02 +00:00
|
|
|
"github.com/tidwall/gjson"
|
2022-06-29 03:09:58 +00:00
|
|
|
"go.uber.org/zap"
|
2021-09-26 02:35:02 +00:00
|
|
|
)
|
|
|
|
|
2021-11-03 09:07:46 +00:00
|
|
|
type CasaService interface {
|
2022-07-22 03:02:11 +00:00
|
|
|
GetServerList(index, size, tp, categoryId, key string) (model.ServerAppListCollection, error)
|
|
|
|
GetServerCategoryList() (list []model.CategoryList, err error)
|
|
|
|
GetServerAppInfo(id, t string, language string) (model.ServerAppList, error)
|
2021-11-25 09:35:01 +00:00
|
|
|
ShareAppFile(body []byte) string
|
2022-06-29 03:09:58 +00:00
|
|
|
GetCasaosVersion() model.Version
|
2022-07-22 03:02:11 +00:00
|
|
|
AsyncGetServerList() (collection model.ServerAppListCollection, err error)
|
|
|
|
AsyncGetServerCategoryList() ([]model.CategoryList, error)
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-03 09:07:46 +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
|
|
|
|
}
|
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
func (o *casaService) GetServerList(index, size, tp, categoryId, key string) (model.ServerAppListCollection, error) {
|
2021-12-29 08:42:20 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
keyName := fmt.Sprintf("list_%s_%s_%s_%s_%s", index, size, tp, categoryId, "en")
|
|
|
|
collection := model.ServerAppListCollection{}
|
2021-12-29 08:42:20 +00:00
|
|
|
if result, ok := Cache.Get(keyName); ok {
|
|
|
|
res, ok := result.(string)
|
|
|
|
if ok {
|
2022-06-29 03:09:58 +00:00
|
|
|
json2.Unmarshal([]byte(res), &collection)
|
2022-07-22 03:02:11 +00:00
|
|
|
return collection, nil
|
2021-12-29 08:42:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-26 02:35:02 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
collectionStr := file.ReadFullFile(config.AppInfo.DBPath + "/app_list.json")
|
2021-09-26 02:35:02 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
err := json2.Unmarshal(collectionStr, &collection)
|
|
|
|
if err != nil {
|
|
|
|
loger.Error("marshal error", zap.Any("err", err), zap.Any("content", string(collectionStr)))
|
2022-07-22 03:02:11 +00:00
|
|
|
collection, err = o.AsyncGetServerList()
|
|
|
|
if err != nil {
|
|
|
|
return collection, err
|
|
|
|
}
|
2022-06-29 03:09:58 +00:00
|
|
|
}
|
2021-09-26 02:35:02 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
go o.AsyncGetServerList()
|
2021-09-26 02:35:02 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
if categoryId != "0" {
|
|
|
|
categoryInt, _ := strconv.Atoi(categoryId)
|
|
|
|
nList := []model.ServerAppList{}
|
|
|
|
for _, v := range collection.List {
|
|
|
|
if v.CategoryId == categoryInt {
|
|
|
|
nList = append(nList, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
collection.List = nList
|
|
|
|
nCommunity := []model.ServerAppList{}
|
|
|
|
for _, v := range collection.Community {
|
|
|
|
if v.CategoryId == categoryInt {
|
|
|
|
nCommunity = append(nCommunity, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
collection.Community = nCommunity
|
|
|
|
}
|
|
|
|
if tp != "name" {
|
|
|
|
if tp == "new" {
|
|
|
|
sort.Slice(collection.List, func(i, j int) bool {
|
|
|
|
return collection.List[i].CreatedAt.After(collection.List[j].CreatedAt)
|
|
|
|
})
|
|
|
|
sort.Slice(collection.Community, func(i, j int) bool {
|
|
|
|
return collection.Community[i].CreatedAt.After(collection.Community[j].CreatedAt)
|
|
|
|
})
|
|
|
|
} else if tp == "rank" {
|
|
|
|
sort.Slice(collection.List, func(i, j int) bool {
|
|
|
|
return collection.List[i].QueryCount > collection.List[j].QueryCount
|
|
|
|
})
|
|
|
|
sort.Slice(collection.Community, func(i, j int) bool {
|
|
|
|
return collection.Community[i].QueryCount > collection.Community[j].QueryCount
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sizeInt, _ := strconv.Atoi(size)
|
|
|
|
|
|
|
|
if index != "1" {
|
|
|
|
indexInt, _ := strconv.Atoi(index)
|
|
|
|
collection.List = collection.List[(indexInt-1)*sizeInt : indexInt*sizeInt]
|
|
|
|
collection.Community = collection.Community[(indexInt-1)*sizeInt : indexInt*sizeInt]
|
|
|
|
} else {
|
|
|
|
if len(collection.List) > sizeInt {
|
|
|
|
collection.List = collection.List[:sizeInt]
|
|
|
|
}
|
|
|
|
if len(collection.Community) > sizeInt {
|
|
|
|
collection.Community = collection.Community[:sizeInt]
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 02:35:02 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
if len(collection.List) > 0 {
|
|
|
|
by, _ := json.Marshal(collection)
|
|
|
|
Cache.Set(keyName, string(by), time.Minute*10)
|
2021-12-29 08:42:20 +00:00
|
|
|
}
|
2022-06-29 03:09:58 +00:00
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
return collection, nil
|
2022-06-29 03:09:58 +00:00
|
|
|
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
func (o *casaService) AsyncGetServerList() (collection model.ServerAppListCollection, err error) {
|
2022-06-08 10:19:45 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
results := file.ReadFullFile(config.AppInfo.DBPath + "/app_list.json")
|
2022-07-22 07:24:13 +00:00
|
|
|
errr := json2.Unmarshal(results, &collection)
|
|
|
|
if errr != nil {
|
2022-06-29 03:09:58 +00:00
|
|
|
loger.Error("marshal error", zap.Any("err", err), zap.Any("content", string(results)))
|
2022-07-22 07:24:13 +00:00
|
|
|
} else {
|
|
|
|
if collection.Version == o.GetCasaosVersion().Version {
|
|
|
|
return collection, err
|
|
|
|
}
|
2022-06-08 10:19:45 +00:00
|
|
|
}
|
2021-09-26 02:35:02 +00:00
|
|
|
|
|
|
|
head := make(map[string]string)
|
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
head["Authorization"] = GetToken()
|
2021-10-29 10:37:27 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
listS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/newlist?index=1&size=1000&rank=name&category_id=0&key=&language=en", head)
|
|
|
|
listModel := []model.ServerAppList{}
|
|
|
|
communityModel := []model.ServerAppList{}
|
|
|
|
recommendModel := []model.ServerAppList{}
|
|
|
|
json2.Unmarshal([]byte(gjson.Get(listS, "data.list").String()), &listModel)
|
|
|
|
json2.Unmarshal([]byte(gjson.Get(listS, "data.recommend").String()), &recommendModel)
|
|
|
|
json2.Unmarshal([]byte(gjson.Get(listS, "data.community").String()), &communityModel)
|
|
|
|
|
|
|
|
if len(listModel) > 0 {
|
|
|
|
collection.Community = communityModel
|
|
|
|
collection.List = listModel
|
|
|
|
collection.Recommend = recommendModel
|
|
|
|
collection.Version = o.GetCasaosVersion().Version
|
2022-07-22 07:24:13 +00:00
|
|
|
var by []byte
|
|
|
|
by, err = json.Marshal(collection)
|
2022-06-29 03:09:58 +00:00
|
|
|
if err != nil {
|
|
|
|
loger.Error("marshal error", zap.Any("err", err))
|
|
|
|
}
|
|
|
|
file.WriteToPath(by, config.AppInfo.DBPath, "app_list.json")
|
2022-06-08 10:19:45 +00:00
|
|
|
}
|
2022-06-29 03:09:58 +00:00
|
|
|
return
|
2021-10-29 10:37:27 +00:00
|
|
|
}
|
2022-06-08 10:19:45 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
// func (o *casaService) GetServerCategoryList() (list []model.ServerCategoryList) {
|
2022-06-08 10:19:45 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
// keyName := fmt.Sprintf("category_list")
|
|
|
|
// if result, ok := Cache.Get(keyName); ok {
|
|
|
|
// res, ok := result.(string)
|
|
|
|
// if ok {
|
|
|
|
// json2.Unmarshal([]byte(gjson.Get(res, "data").String()), &list)
|
|
|
|
// return list
|
|
|
|
// }
|
2022-06-08 10:19:45 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
// head := make(map[string]string)
|
|
|
|
// head["Authorization"] = GetToken()
|
2022-06-29 03:09:58 +00:00
|
|
|
|
2022-06-08 10:19:45 +00:00
|
|
|
// listS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/category", head)
|
2022-06-29 03:09:58 +00:00
|
|
|
|
|
|
|
// json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &list)
|
|
|
|
// if len(list) > 0 {
|
|
|
|
// Cache.Set(keyName, listS, time.Hour*24)
|
2022-06-08 10:19:45 +00:00
|
|
|
// }
|
2022-06-29 03:09:58 +00:00
|
|
|
// return list
|
2022-06-08 10:19:45 +00:00
|
|
|
// }
|
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
func (o *casaService) GetServerCategoryList() (list []model.CategoryList, err error) {
|
2022-06-29 03:09:58 +00:00
|
|
|
category := model.ServerCategoryList{}
|
|
|
|
results := file.ReadFullFile(config.AppInfo.DBPath + "/app_category.json")
|
2022-07-22 03:02:11 +00:00
|
|
|
err = json2.Unmarshal(results, &category)
|
2022-06-29 03:09:58 +00:00
|
|
|
if err != nil {
|
|
|
|
loger.Error("marshal error", zap.Any("err", err), zap.Any("content", string(results)))
|
|
|
|
return o.AsyncGetServerCategoryList()
|
|
|
|
}
|
|
|
|
go o.AsyncGetServerCategoryList()
|
2022-07-22 03:02:11 +00:00
|
|
|
return category.Item, err
|
2022-06-29 03:09:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
func (o *casaService) AsyncGetServerCategoryList() ([]model.CategoryList, error) {
|
2022-06-29 03:09:58 +00:00
|
|
|
list := model.ServerCategoryList{}
|
|
|
|
results := file.ReadFullFile(config.AppInfo.DBPath + "/app_category.json")
|
|
|
|
err := json2.Unmarshal(results, &list)
|
|
|
|
if err != nil {
|
|
|
|
loger.Error("marshal error", zap.Any("err", err), zap.Any("content", string(results)))
|
2022-07-22 03:02:11 +00:00
|
|
|
} else {
|
|
|
|
if list.Version == o.GetCasaosVersion().Version {
|
|
|
|
return list.Item, nil
|
|
|
|
}
|
2022-06-29 03:09:58 +00:00
|
|
|
}
|
|
|
|
item := []model.CategoryList{}
|
|
|
|
head := make(map[string]string)
|
|
|
|
head["Authorization"] = GetToken()
|
|
|
|
listS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/category", head)
|
2022-07-22 03:02:11 +00:00
|
|
|
if len(listS) == 0 {
|
|
|
|
return item, errors.New("server error")
|
|
|
|
}
|
2022-06-29 03:09:58 +00:00
|
|
|
json2.Unmarshal([]byte(gjson.Get(listS, "data").String()), &item)
|
|
|
|
if len(item) > 0 {
|
|
|
|
list.Version = o.GetCasaosVersion().Version
|
|
|
|
list.Item = item
|
|
|
|
by, err := json.Marshal(list)
|
|
|
|
if err != nil {
|
|
|
|
loger.Error("marshal error", zap.Any("err", err))
|
|
|
|
}
|
|
|
|
file.WriteToPath(by, config.AppInfo.DBPath, "app_category.json")
|
|
|
|
}
|
2022-07-22 03:02:11 +00:00
|
|
|
return item, nil
|
2022-06-29 03:09:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
func (o *casaService) GetServerAppInfo(id, t string, language string) (model.ServerAppList, error) {
|
2021-10-29 10:37:27 +00:00
|
|
|
|
2021-11-03 09:07:46 +00:00
|
|
|
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)
|
2021-11-03 09:07:46 +00:00
|
|
|
|
|
|
|
info := model.ServerAppList{}
|
2022-07-22 03:02:11 +00:00
|
|
|
if infoS == "" {
|
|
|
|
return info, errors.New("server error")
|
|
|
|
}
|
2022-07-26 03:03:45 +00:00
|
|
|
err := json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(infoS)
|
|
|
|
return info, err
|
|
|
|
}
|
2021-11-03 09:07:46 +00:00
|
|
|
|
2022-07-22 03:02:11 +00:00
|
|
|
return info, nil
|
2021-11-03 09:07:46 +00:00
|
|
|
}
|
2021-10-29 10:37:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
/**
|
|
|
|
* @description: get remote version
|
|
|
|
* @return {model.Version}
|
|
|
|
*/
|
|
|
|
func (o *casaService) GetCasaosVersion() model.Version {
|
|
|
|
keyName := "casa_version"
|
|
|
|
var dataStr string
|
|
|
|
var version model.Version
|
|
|
|
if result, ok := Cache.Get(keyName); ok {
|
|
|
|
dataStr, ok = result.(string)
|
|
|
|
if ok {
|
|
|
|
data := gjson.Get(dataStr, "data")
|
|
|
|
json2.Unmarshal([]byte(data.String()), &version)
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
}
|
2022-02-17 10:43:25 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
v := httper.OasisGet(config.ServerInfo.ServerApi + "/v1/sys/version")
|
|
|
|
data := gjson.Get(v, "data")
|
|
|
|
json2.Unmarshal([]byte(data.String()), &version)
|
2022-02-17 10:43:25 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
if len(version.Version) > 0 {
|
|
|
|
Cache.Set(keyName, v, time.Minute*20)
|
|
|
|
}
|
2022-02-17 10:43:25 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
return version
|
2022-02-17 10:43:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 08:37:03 +00:00
|
|
|
func NewCasaService() CasaService {
|
2021-11-03 09:07:46 +00:00
|
|
|
return &casaService{}
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|