hpds_jkw_web/internal/handler/index.go

59 lines
1.3 KiB
Go
Raw Permalink Normal View History

2023-01-06 10:09:23 +08:00
package handler
import (
2023-01-13 11:26:39 +08:00
"encoding/json"
2023-01-06 19:15:52 +08:00
"git.hpds.cc/Component/logging"
2023-01-06 10:09:23 +08:00
"github.com/gin-gonic/gin"
"go.uber.org/zap"
2023-01-11 18:05:29 +08:00
"hpds-iot-web/config"
2023-01-06 10:09:23 +08:00
"hpds-iot-web/model"
"time"
"xorm.io/xorm"
)
type HandlerService struct {
2023-01-11 18:05:29 +08:00
AppConfig *config.WebConfig
Engine *xorm.Engine
Logger *logging.Logger
2023-01-06 10:09:23 +08:00
}
2023-01-11 18:05:29 +08:00
func NewHandlerService(cfg *config.WebConfig, engine *xorm.Engine, logger *logging.Logger) *HandlerService {
2023-01-06 10:09:23 +08:00
return &HandlerService{
2023-01-11 18:05:29 +08:00
AppConfig: cfg,
Engine: engine,
Logger: logger,
2023-01-06 10:09:23 +08:00
}
}
func (s HandlerService) Health(c *gin.Context) (data interface{}, err error) {
return time.Now().Unix(), nil
}
func (s HandlerService) SaveLog(action, category, targetId, oldValue, newValue, operatorId, operatorAddr, operatorDevice string) {
operationLog := &model.OperationLog{
Action: action,
Category: category,
TargetId: targetId,
OldValue: oldValue,
NewValue: newValue,
Operator: operatorId,
OperateAddr: operatorAddr,
OperateDevice: operatorDevice,
}
_, err := s.Engine.Insert(operationLog)
if err != nil {
s.Logger.With(zap.Namespace("DAO"),
zap.String("method", "SaveLog"),
zap.Any("operationLog", operationLog),
).Error(err.Error())
}
}
2023-01-13 11:26:39 +08:00
func ToString(data interface{}) string {
b, err := json.Marshal(data)
if err != nil {
return ""
}
return string(b)
}