Merge branch 'old/master' into old

This commit is contained in:
2025-12-28 17:15:11 -08:00
542 changed files with 16488 additions and 16353 deletions
@@ -1,136 +0,0 @@
package common
import (
"context"
"strings"
"github.com/perfect-panel/server/internal/model/application"
"github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors"
"gorm.io/gorm"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/logger"
)
type GetApplicationLogic struct {
logger.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// Get Tos Content
func NewGetApplicationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApplicationLogic {
return &GetApplicationLogic{
Logger: logger.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetApplicationLogic) GetApplication() (resp *types.GetAppcationResponse, err error) {
resp = &types.GetAppcationResponse{}
cfg, err := l.svcCtx.ApplicationModel.FindOneConfig(l.ctx, 1)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
l.Logger.Error("[GetAppInfo] FindOneAppConfig error: ", logger.Field("error", err.Error()))
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "GetAppInfo FindOneAppConfig error: %v", err.Error())
}
if err != nil {
resp.Config = types.ApplicationConfig{}
} else {
resp.Config = types.ApplicationConfig{
AppId: cfg.AppId,
EncryptionKey: cfg.EncryptionKey,
EncryptionMethod: cfg.EncryptionMethod,
Domains: strings.Split(cfg.Domains, ";"),
StartupPicture: cfg.StartupPicture,
StartupPictureSkipTime: cfg.StartupPictureSkipTime,
}
}
var applications []*application.Application
err = l.svcCtx.ApplicationModel.Transaction(l.ctx, func(tx *gorm.DB) (err error) {
return tx.Model(applications).Preload("ApplicationVersions").Find(&applications).Error
})
if err != nil {
l.Errorw("[QueryApplicationConfig] get application error: ", logger.Field("error", err.Error()))
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get application error: %v", err.Error())
}
if len(applications) == 0 {
return resp, nil
}
for _, app := range applications {
applicationResponse := types.ApplicationResponseInfo{
Id: app.Id,
Name: app.Name,
Icon: app.Icon,
Description: app.Description,
SubscribeType: app.SubscribeType,
}
applicationVersions := app.ApplicationVersions
if len(applicationVersions) != 0 {
for _, applicationVersion := range applicationVersions {
/*if !applicationVersion.IsDefault {
continue
}*/
switch applicationVersion.Platform {
case "ios":
applicationResponse.Platform.IOS = append(applicationResponse.Platform.IOS, &types.ApplicationVersion{
Id: applicationVersion.Id,
Url: applicationVersion.Url,
Version: applicationVersion.Version,
IsDefault: applicationVersion.IsDefault,
Description: applicationVersion.Description,
})
case "macos":
applicationResponse.Platform.MacOS = append(applicationResponse.Platform.MacOS, &types.ApplicationVersion{
Id: applicationVersion.Id,
Url: applicationVersion.Url,
Version: applicationVersion.Version,
IsDefault: applicationVersion.IsDefault,
Description: applicationVersion.Description,
})
case "linux":
applicationResponse.Platform.Linux = append(applicationResponse.Platform.Linux, &types.ApplicationVersion{
Id: applicationVersion.Id,
Url: applicationVersion.Url,
Version: applicationVersion.Version,
IsDefault: applicationVersion.IsDefault,
Description: applicationVersion.Description,
})
case "android":
applicationResponse.Platform.Android = append(applicationResponse.Platform.Android, &types.ApplicationVersion{
Id: applicationVersion.Id,
Url: applicationVersion.Url,
Version: applicationVersion.Version,
IsDefault: applicationVersion.IsDefault,
Description: applicationVersion.Description,
})
case "windows":
applicationResponse.Platform.Windows = append(applicationResponse.Platform.Windows, &types.ApplicationVersion{
Id: applicationVersion.Id,
Url: applicationVersion.Url,
Version: applicationVersion.Version,
IsDefault: applicationVersion.IsDefault,
Description: applicationVersion.Description,
})
case "harmony":
applicationResponse.Platform.Harmony = append(applicationResponse.Platform.Harmony, &types.ApplicationVersion{
Id: applicationVersion.Id,
Url: applicationVersion.Url,
Version: applicationVersion.Version,
IsDefault: applicationVersion.IsDefault,
Description: applicationVersion.Description,
})
}
}
}
resp.Applications = append(resp.Applications, applicationResponse)
}
return
}
+56
View File
@@ -0,0 +1,56 @@
package common
import (
"context"
"encoding/json"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/logger"
"github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors"
)
type GetClientLogic struct {
logger.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// Get Client
func NewGetClientLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetClientLogic {
return &GetClientLogic{
Logger: logger.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetClientLogic) GetClient() (resp *types.GetSubscribeClientResponse, err error) {
data, err := l.svcCtx.ClientModel.List(l.ctx)
if err != nil {
l.Errorf("Failed to get subscribe application list: %v", err)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "Failed to get subscribe application list")
}
var list []types.SubscribeClient
for _, item := range data {
var temp types.DownloadLink
if item.DownloadLink != "" {
_ = json.Unmarshal([]byte(item.DownloadLink), &temp)
}
list = append(list, types.SubscribeClient{
Id: item.Id,
Name: item.Name,
Description: item.Description,
Icon: item.Icon,
Scheme: item.Scheme,
IsDefault: item.IsDefault,
DownloadLink: temp,
})
}
resp = &types.GetSubscribeClientResponse{
Total: int64(len(list)),
List: list,
}
return
}
@@ -2,6 +2,7 @@ package common
import (
"context"
"encoding/json"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
@@ -67,6 +68,10 @@ func (l *GetGlobalConfigLogic) GetGlobalConfig() (resp *types.GetGlobalConfigRes
for _, method := range authMethods {
if *method.Enabled {
methods = append(methods, method.Method)
if method.Method == "device" {
_ = json.Unmarshal([]byte(method.Config), &resp.Auth.Device)
resp.Auth.Device.Enable = true
}
}
}
resp.OAuthMethods = methods
@@ -1,41 +0,0 @@
package common
import (
"context"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/logger"
"github.com/perfect-panel/server/pkg/tool"
"github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors"
)
type GetSubscriptionLogic struct {
logger.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// Get Subscription
func NewGetSubscriptionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSubscriptionLogic {
return &GetSubscriptionLogic{
Logger: logger.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetSubscriptionLogic) GetSubscription() (resp *types.GetSubscriptionResponse, err error) {
resp = &types.GetSubscriptionResponse{
List: make([]types.Subscribe, 0),
}
// Get the subscription list
data, err := l.svcCtx.SubscribeModel.QuerySubscribeListByShow(l.ctx)
if err != nil {
l.Errorw("[Site GetSubscription]", logger.Field("err", err.Error()))
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "get subscription list error: %v", err.Error())
}
tool.DeepCopy(&resp.List, data)
return
}
+33
View File
@@ -0,0 +1,33 @@
package common
import (
"context"
"time"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/logger"
)
type HeartbeatLogic struct {
logger.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// NewHeartbeatLogic Heartbeat
func NewHeartbeatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HeartbeatLogic {
return &HeartbeatLogic{
Logger: logger.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *HeartbeatLogic) Heartbeat() (resp *types.HeartbeatResponse, err error) {
return &types.HeartbeatResponse{
Status: true,
Message: "service is alive",
Timestamp: time.Now().Unix(),
}, nil
}
+7 -27
View File
@@ -1,11 +1,9 @@
package common
import (
"bytes"
"context"
"encoding/json"
"fmt"
"text/template"
"time"
"github.com/hibiken/asynq"
@@ -88,14 +86,16 @@ func (l *SendEmailCodeLogic) SendEmailCode(req *types.SendCodeRequest) (resp *ty
var taskPayload queue.SendEmailPayload
// Generate verification code
code := random.Key(6, 0)
taskPayload.Type = queue.EmailTypeVerify
taskPayload.Email = req.Email
taskPayload.Subject = "Verification code"
content, err := l.initTemplate(req.Type, code)
if err != nil {
l.Logger.Error("[SendEmailCode]: InitTemplate Error", logger.Field("error", err.Error()))
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "Failed to init template")
taskPayload.Content = map[string]interface{}{
"Type": req.Type,
"SiteLogo": l.svcCtx.Config.Site.SiteLogo,
"SiteName": l.svcCtx.Config.Site.SiteName,
"Expire": 5,
"Code": code,
}
taskPayload.Content = content
// Save to Redis
payload = CacheKeyPayload{
Code: code,
@@ -134,23 +134,3 @@ func (l *SendEmailCodeLogic) SendEmailCode(req *types.SendCodeRequest) (resp *ty
}, nil
}
}
func (l *SendEmailCodeLogic) initTemplate(t uint8, code string) (string, error) {
data := VerifyTemplate{
Type: t,
SiteLogo: l.svcCtx.Config.Site.SiteLogo,
SiteName: l.svcCtx.Config.Site.SiteName,
Expire: 5,
Code: code,
}
tpl, err := template.New("verify").Parse(l.svcCtx.Config.Email.VerifyEmailTemplate)
if err != nil {
return "", err
}
var result bytes.Buffer
err = tpl.Execute(&result, data)
if err != nil {
return "", err
}
return result.String(), nil
}