123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package controller
- import (
- "bigdata_archives/app/e"
- "bigdata_archives/global"
- "bigdata_archives/simple_zap"
- "bigdata_archives/unity"
- "bigdata_archives/utils"
- "context"
- "encoding/json"
- "github.com/gin-gonic/gin"
- "io"
- "log"
- "os"
- "path/filepath"
- "strconv"
- "time"
- )
- type EventLog struct {
- utils.BaseModel
- IPAddress string `gorm:"column:ip_address" json:"ipAddress"`
- IPv6Address string `gorm:"column:ipv6_address" json:"ipv6Address"`
- PortNo int `gorm:"column:port_no" json:"portNo"`
- Protocol string `gorm:"column:protocol" json:"protocol"`
- MacAddress string `gorm:"column:mac_address" json:"macAddress"`
- ChannelID int `gorm:"column:channel_id" json:"channelID"`
- DateTime string `gorm:"column:date_time" json:"dateTime"`
- ActivePostCount int `gorm:"column:active_post_count" json:"activePostCount"`
- EventType string `gorm:"column:event_type" json:"eventType"`
- EventState string `gorm:"column:event_state" json:"eventState"`
- EventDescription string `gorm:"column:event_description" json:"eventDescription"`
- ShortSerialNumber string `gorm:"column:short_serial_number" json:"shortSerialNumber"`
- AccessControllerEvent AccessControllerEvent `gorm:"-" json:"AccessControllerEvent"`
- }
- type AccessControllerEvent struct {
- utils.BaseModel
- DeviceName string `gorm:"column:device_name" json:"deviceName"`
- MajorEventType int `gorm:"column:major_event_type" json:"majorEventType"`
- SubEventType int `gorm:"column:sub_event_type" json:"subEventType"`
- CardReaderKind int `gorm:"column:card_reader_kind" json:"cardReaderKind"`
- CardReaderNo int `gorm:"column:card_reader_no" json:"cardReaderNo"`
- VerifyNo int `gorm:"column:verify_no" json:"verifyNo"`
- SerialNo int `gorm:"column:serial_no" json:"serialNo"`
- CurrentVerifyMode string `gorm:"column:current_verify_mode" json:"currentVerifyMode"`
- FrontSerialNo int `gorm:"column:front_serial_no" json:"frontSerialNo"`
- AttendanceStatus string `gorm:"column:attendance_status" json:"attendanceStatus"`
- Label string `gorm:"column:label" json:"label"`
- StatusValue int `gorm:"column:status_value" json:"statusValue"`
- Mask string `gorm:"column:mask" json:"mask"`
- Helmet string `gorm:"column:helmet" json:"helmet"`
- PicturesNumber int `gorm:"column:pictures_number" json:"picturesNumber"`
- PurePwdVerifyEnable bool `gorm:"column:pure_pwd_verify_enable" json:"purePwdVerifyEnable"`
- FaceImagePath string `gorm:"column:face_image_path" json:"faceImagePath"`
- IPAddress string `gorm:"column:ip_address" json:"ipAddress"`
- FaceRect FaceRect `gorm:"-" json:"FaceRect"`
- }
- func (acc *AccessControllerEvent) TableName() string {
- return "access_controller_events"
- }
- type FaceRect struct {
- Height float64 `gorm:"column:height" json:"height"`
- Width float64 `gorm:"column:width" json:"width"`
- X float64 `gorm:"column:x" json:"x"`
- Y float64 `gorm:"column:y" json:"y"`
- }
- func Monitor(c *gin.Context) {
- // 解析 multipart/form-data 请求
- err := c.Request.ParseMultipartForm(10 << 20) // 设置最大文件大小为 10MB
- if err != nil {
- //e.ResponseWithMsg(c, e.ERROR, "读取表单失败")
- simple_zap.WithCtx(context.Background()).Sugar().Error(err, "读取表单失败")
- return
- }
- // 处理 JSON 数据
- jsonData, ok := c.Request.Form["event_log"]
- if !ok {
- //e.ResponseWithMsg(c, e.ERROR, "缺少 event_log 参数")
- simple_zap.WithCtx(context.Background()).Sugar().Error(err, "缺少 event_log 参数")
- return
- }
- jsonStr := jsonData[0]
- // 解析 JSON
- var eventLog EventLog
- err = json.Unmarshal([]byte(jsonStr), &eventLog)
- if err != nil {
- //e.ResponseWithMsg(c, e.ERROR, "json解析失败")
- simple_zap.WithCtx(context.Background()).Sugar().Error(err, "json解析失败")
- return
- }
- // 处理图片
- fileHeader, err := c.FormFile("Picture")
- if err != nil {
- //e.ResponseWithMsg(c, e.ERROR, "未获取文件图片")
- simple_zap.WithCtx(context.Background()).Sugar().Error(err, "未获取文件图片")
- return
- }
- open, err := fileHeader.Open()
- // 打开文件
- if err != nil {
- //e.ResponseWithMsg(c, e.ERROR, "打开文件失败")
- simple_zap.WithCtx(context.Background()).Sugar().Error(err, "打开文件失败")
- return
- }
- // 读取文件内容
- fileBytes, err := io.ReadAll(open)
- if err != nil {
- //e.ResponseWithMsg(c, e.ERROR, "读取文件失败")
- simple_zap.WithCtx(context.Background()).Sugar().Error(err, "读取文件失败")
- return
- }
- // 保存图片到本地文件
- err, path := savePicture(fileBytes)
- if err != nil {
- log.Println(err)
- //e.ResponseWithMsg(c, e.ERROR, "保存文件失败")
- simple_zap.WithCtx(context.Background()).Sugar().Error(err, "保存文件失败")
- return
- }
- var access AccessControllerEvent
- access = eventLog.AccessControllerEvent
- access.FaceImagePath = path
- access.IPAddress = eventLog.IPAddress
- tx := global.DBLink.Create(&eventLog)
- tx = global.DBLink.Create(&access)
- if tx.Error == nil && tx.RowsAffected > 0 {
- //e.ResponseSuccess(c, "200")
- return
- }
- // 返回响应
- //e.ResponseSuccess(c, "200")
- }
- // Getmonitor 获取门禁记录
- func Getmonitor(c *gin.Context) {
- var params unity.TimePageParams
- var access AccessControllerEvent
- err := c.ShouldBindJSON(¶ms)
- if err != nil {
- simple_zap.WithCtx(context.Background()).Sugar().Error(err, "参数解析失败")
- e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
- return
- }
- result, total, err := unity.PaginateWithTime(params, access.TableName(), &access)
- if err != nil {
- e.ResponseWithMsg(c, e.PaginationFailed, e.PaginationFailed.GetMsg())
- return
- }
- e.ResPonsePageTime(c, result, total, params)
- }
- // 保存图片到本地文件
- func savePicture(data []byte) (error, string) {
- // 指定保存图片的路径
- unix := time.Now().Unix()
- picturePath := "/image/" + strconv.FormatInt(unix, 10) + ".jpg"
- // 创建目录
- if err := os.MkdirAll(filepath.Dir(picturePath), os.ModePerm); err != nil {
- return err, ""
- }
- // 创建文件
- file, err := os.Create(picturePath)
- if err != nil {
- return err, ""
- }
- defer file.Close()
- // 写入图片数据
- _, err = file.Write(data)
- if err != nil {
- return err, ""
- }
- return nil, picturePath
- }
|