hpds_jkw_web/internal/handler/index.go

59 lines
1.3 KiB
Go

package handler
import (
"encoding/json"
"git.hpds.cc/Component/logging"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"hpds-iot-web/config"
"hpds-iot-web/model"
"time"
"xorm.io/xorm"
)
type HandlerService struct {
AppConfig *config.WebConfig
Engine *xorm.Engine
Logger *logging.Logger
}
func NewHandlerService(cfg *config.WebConfig, engine *xorm.Engine, logger *logging.Logger) *HandlerService {
return &HandlerService{
AppConfig: cfg,
Engine: engine,
Logger: logger,
}
}
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())
}
}
func ToString(data interface{}) string {
b, err := json.Marshal(data)
if err != nil {
return ""
}
return string(b)
}