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"
|
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-06-29 03:09:58 +00:00
|
|
|
GetServerList(index, size, tp, categoryId, key string) model.ServerAppListCollection
|
|
|
|
GetServerCategoryList() []model.CategoryList
|
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)
|
2022-06-29 03:09:58 +00:00
|
|
|
|
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)
|
2022-06-29 03:09:58 +00:00
|
|
|
GetCasaosVersion() model.Version
|
|
|
|
AsyncGetServerList() (collection model.ServerAppListCollection)
|
|
|
|
AsyncGetServerCategoryList() []model.CategoryList
|
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-06-29 03:09:58 +00:00
|
|
|
func (o *casaService) GetServerList(index, size, tp, categoryId, key string) model.ServerAppListCollection {
|
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)
|
|
|
|
return collection
|
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)))
|
|
|
|
collection = o.AsyncGetServerList()
|
|
|
|
}
|
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
|
|
|
|
|
|
|
return collection
|
|
|
|
|
2021-09-26 02:35:02 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
func (o *casaService) AsyncGetServerList() (collection model.ServerAppListCollection) {
|
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")
|
|
|
|
err := json2.Unmarshal(results, &collection)
|
|
|
|
if err != nil {
|
|
|
|
loger.Error("marshal error", zap.Any("err", err), zap.Any("content", string(results)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if collection.Version == o.GetCasaosVersion().Version {
|
|
|
|
return collection
|
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
|
|
|
|
by, err := json.Marshal(collection)
|
|
|
|
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-06-29 03:09:58 +00:00
|
|
|
func (o *casaService) GetServerCategoryList() (list []model.CategoryList) {
|
|
|
|
category := model.ServerCategoryList{}
|
|
|
|
results := file.ReadFullFile(config.AppInfo.DBPath + "/app_category.json")
|
|
|
|
err := json2.Unmarshal(results, &category)
|
|
|
|
if err != nil {
|
|
|
|
loger.Error("marshal error", zap.Any("err", err), zap.Any("content", string(results)))
|
|
|
|
return o.AsyncGetServerCategoryList()
|
|
|
|
}
|
|
|
|
go o.AsyncGetServerCategoryList()
|
|
|
|
return category.Item
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *casaService) AsyncGetServerCategoryList() []model.CategoryList {
|
|
|
|
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)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if list.Version == o.GetCasaosVersion().Version {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
item := []model.CategoryList{}
|
|
|
|
head := make(map[string]string)
|
|
|
|
head["Authorization"] = GetToken()
|
|
|
|
listS := httper2.Get(config.ServerInfo.ServerApi+"/v2/app/category", head)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:50:34 +00:00
|
|
|
func (o *casaService) GetServerAppInfo(id, t string, language string) model.ServerAppList {
|
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{}
|
|
|
|
json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
|
|
|
|
|
|
|
|
return info
|
|
|
|
}
|
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-06-29 03:09:58 +00:00
|
|
|
func (o *casaService) PushHeart(id, t string, language string) {
|
2022-03-09 08:37:03 +00:00
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
m := model.CasaOSHeart{}
|
|
|
|
m.UuId = id
|
2022-03-09 08:37:03 +00:00
|
|
|
m.Type = t
|
|
|
|
b, _ := json.Marshal(m)
|
|
|
|
|
|
|
|
head := make(map[string]string)
|
|
|
|
|
|
|
|
head["Authorization"] = GetToken()
|
|
|
|
|
2022-06-29 03:09:58 +00:00
|
|
|
infoS := httper2.Post(config.ServerInfo.ServerApi+"/v1/analyse/heart", b, "application/json", head)
|
2022-03-09 08:37:03 +00:00
|
|
|
|
|
|
|
info := model.ServerAppList{}
|
|
|
|
json2.Unmarshal([]byte(gjson.Get(infoS, "data").String()), &info)
|
|
|
|
|
2022-03-18 03:13:07 +00:00
|
|
|
}
|
2022-06-29 03:09:58 +00:00
|
|
|
|
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-03-09 08:37:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
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
|
|
|
}
|