李寻欢 fa8bf764db
All checks were successful
BuildImage / build-image (push) Successful in 1m54s
:refactor: 更新登录逻辑,使用用户ID查询机器人并记录首次登录时间
2025-04-28 11:51:28 +08:00

94 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"gitee.ltd/lxh/wechat-robot/internal/utils"
"time"
"gorm.io/gorm"
)
// RobotStatus 表示机器人状态的枚举类型
type RobotStatus string
const (
RobotStatusOnline RobotStatus = "online"
RobotStatusOffline RobotStatus = "offline"
RobotStatusError RobotStatus = "error"
)
// Robot 表示微信机器人实例
type Robot struct {
BaseModel
UserId string `gorm:"column:user_id;index:deleted,unique;" json:"userId"` // 用户ID
ContainerID string `gorm:"column:container_id;index:deleted,unique,length:64" json:"container_id"`
ShortContainerID string `gorm:"column:short_container_id;index" json:"short_container_id"` // 容器ID的前12位
ContainerHost string `gorm:"column:container_host" json:"container_host"` // 容器访问地址格式为ip:port
DeviceId string `gorm:"column:device_id;index:deleted,unique;" json:"deviceId"` // 设备Id
DeviceName string `gorm:"column:device_name" json:"deviceName"` // 设备名称
WechatID string `gorm:"column:wechat_id;index:deleted,unique,length:64" json:"wechat_id"`
Nickname string `gorm:"column:nickname" json:"nickname"`
Avatar string `gorm:"column:avatar" json:"avatar"`
Status RobotStatus `gorm:"column:status;default:'offline'" json:"status"`
FirstLoginAt *time.Time `gorm:"column:first_login_at" json:"first_login_at"` // 首次登录时间
LastLoginAt *time.Time `gorm:"column:last_login_at" json:"last_login_at"`
ErrorMessage string `gorm:"column:error_message" json:"error_message"`
}
// TableName 指定表名
func (Robot) TableName() string {
return "robots"
}
// BeforeCreate GORM钩子在创建前处理
func (r *Robot) BeforeCreate(tx *gorm.DB) error {
// 设置短容器ID
if len(r.ContainerID) > 12 {
r.ShortContainerID = r.ContainerID[:12]
} else {
r.ShortContainerID = r.ContainerID
}
return nil
}
// BeforeSave GORM钩子在保存前处理
func (r *Robot) BeforeSave(tx *gorm.DB) error {
// 确保短容器ID总是保持同步
if len(r.ContainerID) > 12 {
r.ShortContainerID = r.ContainerID[:12]
} else {
r.ShortContainerID = r.ContainerID
}
return nil
}
// AfterCreate GORM钩子在创建后处理
func (r *Robot) AfterCreate(tx *gorm.DB) error {
// 可以在这里进行一些创建后的操作
return nil
}
// BeforeDelete GORM钩子在删除前处理
func (r *Robot) BeforeDelete(tx *gorm.DB) error {
// 在删除机器人之前,可以处理相关依赖
return nil
}
// CheckDevice
// @description: 检查设备信息
// @receiver r
// @return bool 是否重新处理过如果是就返回true
func (r *Robot) CheckDevice() (flag bool) {
// 如果是空的,就获取环境变量配置的值,如果还是空的,就生成一个新的
if r.DeviceId == "" {
r.DeviceId = utils.GetDeviceId()
flag = true
}
// 如果是空的,就获取环境变量配置的值,如果还是空的,就生成一个新的
if r.DeviceName == "" {
r.DeviceName = utils.GetDeviceName()
flag = true
}
return
}