Files
EdgeAdmin/internal/web/actions/actionutils/parent_action.go

105 lines
2.3 KiB
Go
Raw Normal View History

2020-07-22 22:19:39 +08:00
package actionutils
import (
2020-07-29 19:34:54 +08:00
"context"
2020-07-22 22:19:39 +08:00
"errors"
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
2020-07-29 19:34:54 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
2020-07-22 22:19:39 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
"github.com/iwind/TeaGo/actions"
2020-07-29 19:34:54 +08:00
"github.com/iwind/TeaGo/logs"
2020-07-22 22:19:39 +08:00
"net/http"
"strconv"
)
type ParentAction struct {
actions.ActionObject
2020-07-29 19:34:54 +08:00
rpcClient *rpc.RPCClient
2020-07-22 22:19:39 +08:00
}
func (this *ParentAction) ErrorPage(err error) {
if err == nil {
return
}
// 日志
this.CreateLog(oplogs.LevelError, "系统发生错误:%s", err.Error())
if this.Request.Method == http.MethodGet {
FailPage(this, err)
} else {
Fail(this, err)
}
}
func (this *ParentAction) ErrorText(err string) {
this.ErrorPage(errors.New(err))
}
func (this *ParentAction) NotFound(name string, itemId int) {
this.ErrorPage(errors.New(name + " id: '" + strconv.Itoa(itemId) + "' is not found"))
}
2020-07-29 19:34:54 +08:00
func (this *ParentAction) NewPage(total int64, size ...int64) *Page {
2020-07-22 22:19:39 +08:00
if len(size) > 0 {
return NewActionPage(this, total, size[0])
}
return NewActionPage(this, total, 10)
}
func (this *ParentAction) Nav(mainMenu string, tab string, firstMenu string) {
this.Data["mainMenu"] = mainMenu
this.Data["mainTab"] = tab
this.Data["firstMenuItem"] = firstMenu
}
func (this *ParentAction) SecondMenu(menuItem string) {
this.Data["secondMenuItem"] = menuItem
}
2020-07-29 19:34:54 +08:00
func (this *ParentAction) AdminId() int64 {
2020-08-21 12:32:16 +08:00
return this.Context.GetInt64("adminId")
2020-07-22 22:19:39 +08:00
}
func (this *ParentAction) CreateLog(level string, description string, args ...interface{}) {
rpcClient, err := rpc.SharedRPC()
if err != nil {
utils.PrintError(err)
return
}
2020-07-29 19:34:54 +08:00
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(this.AdminId()), &pb.CreateAdminLogRequest{
2020-07-22 22:19:39 +08:00
Level: level,
Description: fmt.Sprintf(description, args...),
Action: this.Request.URL.Path,
Ip: this.RequestRemoteIP(),
})
if err != nil {
utils.PrintError(err)
}
}
2020-07-29 19:34:54 +08:00
// 获取RPC
func (this *ParentAction) RPC() *rpc.RPCClient {
if this.rpcClient != nil {
return this.rpcClient
}
// 所有集群
rpcClient, err := rpc.SharedRPC()
if err != nil {
logs.Fatal(err)
return nil
}
this.rpcClient = rpcClient
return rpcClient
}
// 获取Context
func (this *ParentAction) AdminContext() context.Context {
return this.rpcClient.Context(this.AdminId())
}