新增设备操作
This commit is contained in:
@@ -23,6 +23,7 @@ type User struct {
|
|||||||
type LoginRequestParams struct {
|
type LoginRequestParams struct {
|
||||||
Username string `form:"username" json:"username" uri:"username" xml:"username" binding:"required"`
|
Username string `form:"username" json:"username" uri:"username" xml:"username" binding:"required"`
|
||||||
Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"`
|
Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"`
|
||||||
|
DeviceId string `form:"device_id" json:"device_id" uri:"device_id" xml:"device_id" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RegisterRequestParams struct {
|
type RegisterRequestParams struct {
|
||||||
@@ -69,6 +70,8 @@ func (*User) Login(ctx *gin.Context) {
|
|||||||
ProfileService.UpdateAccount(loginResp.UserId, &models.Profile{
|
ProfileService.UpdateAccount(loginResp.UserId, &models.Profile{
|
||||||
LoginStatus: 1,
|
LoginStatus: 1,
|
||||||
})
|
})
|
||||||
|
// 登录成功之后查看设备是否是当前设备
|
||||||
|
DeviceService := services.InitDeviceService()
|
||||||
ctx.JSON(http.StatusOK, gin.H{
|
ctx.JSON(http.StatusOK, gin.H{
|
||||||
"code": 200,
|
"code": 200,
|
||||||
"msg": "登录成功",
|
"msg": "登录成功",
|
||||||
|
|||||||
25
models/device.go
Normal file
25
models/device.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Device struct {
|
||||||
|
ID int64 `gorm:"id;primary_key"`
|
||||||
|
UserId int64 `gorm:"user_id"`
|
||||||
|
DeviceUid string `gorm:"device_uid"`
|
||||||
|
Token string `gorm:"token"`
|
||||||
|
Hostname string `gorm:"hostname"`
|
||||||
|
Platform string `gorm:"platform"`
|
||||||
|
PlatformVersion string `gorm:"platform_version"`
|
||||||
|
Mac string `gorm:"mac"`
|
||||||
|
Cpu string `gorm:"cpu"`
|
||||||
|
Mem string `gorm:"mem"`
|
||||||
|
Disk string `gorm:"disk"`
|
||||||
|
LoginStatus int `gorm:"login_status"` // 1: 已登录, 0: 未登录
|
||||||
|
CreatedAt time.Time `gorm:"created_at;type:timestamptz"`
|
||||||
|
UpdatedAt time.Time `gorm:"updated_at;type:timestamptz"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// 实现 TableName 方法指定表名
|
||||||
|
func (Device) TableName() string {
|
||||||
|
return "gd_Device"
|
||||||
|
}
|
||||||
63
services/device_service.go
Normal file
63
services/device_service.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go-desk-service/libs"
|
||||||
|
"go-desk-service/models"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DeviceService struct {
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitDeviceService() *DeviceService {
|
||||||
|
db := libs.GetDB()
|
||||||
|
return &DeviceService{
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DeviceService) Create(device *models.Device) bool {
|
||||||
|
device.CreatedAt = time.Now()
|
||||||
|
device.UpdatedAt = time.Now()
|
||||||
|
d.db.Create(device)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DeviceService) GetByUserIdAndDeviceUid(userId int64, deviceUid string) (*models.Device, error) {
|
||||||
|
var device models.Device
|
||||||
|
err := d.db.Model(&models.Device{}).Where("user_id = ? AND device_uid = ?", userId, deviceUid).First(&device).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &device, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DeviceService) Update(device *models.Device) bool {
|
||||||
|
device.UpdatedAt = time.Now()
|
||||||
|
d.db.Model(&models.Device{}).Where("id = ?", device.ID).Updates(device)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DeviceService) GetInfoByToken(token string) (*models.Device, error) {
|
||||||
|
var device models.Device
|
||||||
|
err := d.db.Model(&models.Device{}).Where("token = ?", token).First(&device).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &device, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *DeviceService) GetUserLoginStatus(userId int64) (int, error) {
|
||||||
|
var count int64
|
||||||
|
err := d.db.Model(&models.Device{}).Where("user_id = ? AND login_status = ?", userId, 1).Count(&count).Error
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if count > 0 {
|
||||||
|
return 1, nil // 用户已登录
|
||||||
|
}
|
||||||
|
return 0, nil // 用户未登录
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user