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"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
2020-12-23 09:52:31 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
2020-07-22 22:19:39 +08:00
|
|
|
"github.com/iwind/TeaGo/actions"
|
2020-07-29 19:34:54 +08:00
|
|
|
"github.com/iwind/TeaGo/logs"
|
2021-07-06 20:06:20 +08:00
|
|
|
"github.com/iwind/TeaGo/maps"
|
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
|
|
|
}
|
|
|
|
|
|
2021-05-26 14:43:29 +08:00
|
|
|
// Parent 可以调用自身的一个简便方法
|
2020-09-21 19:51:50 +08:00
|
|
|
func (this *ParentAction) Parent() *ParentAction {
|
|
|
|
|
return this
|
|
|
|
|
}
|
|
|
|
|
|
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))
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 20:37:07 +08:00
|
|
|
func (this *ParentAction) NotFound(name string, itemId int64) {
|
|
|
|
|
this.ErrorPage(errors.New(name + " id: '" + strconv.FormatInt(itemId, 10) + "' is not found"))
|
2020-07-22 22:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-13 20:37:07 +08:00
|
|
|
func (this *ParentAction) FirstMenu(menuItem string) {
|
|
|
|
|
this.Data["firstMenuItem"] = menuItem
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 22:19:39 +08:00
|
|
|
func (this *ParentAction) SecondMenu(menuItem string) {
|
|
|
|
|
this.Data["secondMenuItem"] = menuItem
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-21 19:51:50 +08:00
|
|
|
func (this *ParentAction) TinyMenu(menuItem string) {
|
|
|
|
|
this.Data["tinyMenuItem"] = 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{}) {
|
2020-11-20 15:32:42 +08:00
|
|
|
desc := fmt.Sprintf(description, args...)
|
|
|
|
|
if level == oplogs.LevelInfo {
|
|
|
|
|
if this.Code != 200 {
|
|
|
|
|
level = oplogs.LevelWarn
|
|
|
|
|
if len(this.Message) > 0 {
|
|
|
|
|
desc += " 失败:" + this.Message
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-23 09:52:31 +08:00
|
|
|
err := dao.SharedLogDAO.CreateAdminLog(this.AdminContext(), level, this.Request.URL.Path, desc, this.RequestRemoteIP())
|
2020-07-22 22:19:39 +08:00
|
|
|
if err != nil {
|
|
|
|
|
utils.PrintError(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-29 19:34:54 +08:00
|
|
|
|
2020-11-20 15:32:42 +08:00
|
|
|
func (this *ParentAction) CreateLogInfo(description string, args ...interface{}) {
|
|
|
|
|
this.CreateLog(oplogs.LevelInfo, description, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 14:43:29 +08:00
|
|
|
// RPC 获取RPC
|
2020-07-29 19:34:54 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 14:43:29 +08:00
|
|
|
// AdminContext 获取Context
|
2020-07-29 19:34:54 +08:00
|
|
|
func (this *ParentAction) AdminContext() context.Context {
|
2020-10-04 20:38:27 +08:00
|
|
|
if this.rpcClient == nil {
|
|
|
|
|
rpcClient, err := rpc.SharedRPC()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logs.Fatal(err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
this.rpcClient = rpcClient
|
|
|
|
|
}
|
2020-07-29 19:34:54 +08:00
|
|
|
return this.rpcClient.Context(this.AdminId())
|
|
|
|
|
}
|
2021-07-06 20:06:20 +08:00
|
|
|
|
|
|
|
|
// ViewData 视图里可以使用的数据
|
|
|
|
|
func (this *ParentAction) ViewData() maps.Map {
|
|
|
|
|
return this.Data
|
|
|
|
|
}
|