gengxin
This commit is contained in:
@@ -16,44 +16,91 @@ type ApiCursorEquipmentController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// cursorIpInfo 对应 ip-api.com 返回的 JSON 结构
|
||||
type cursorIpInfo struct {
|
||||
Status string `json:"status"`
|
||||
Country string `json:"country"`
|
||||
CountryCode string `json:"countryCode"`
|
||||
Region string `json:"region"`
|
||||
RegionName string `json:"regionName"`
|
||||
City string `json:"city"`
|
||||
Zip string `json:"zip"`
|
||||
Lat float64 `json:"lat"`
|
||||
Lon float64 `json:"lon"`
|
||||
Timezone string `json:"timezone"`
|
||||
ISP string `json:"isp"`
|
||||
Org string `json:"org"`
|
||||
As string `json:"as"`
|
||||
Query string `json:"query"`
|
||||
}
|
||||
|
||||
type cursorEquipmentReportPayload struct {
|
||||
DeviceInfo string `json:"deviceInfo"`
|
||||
DeviceInfoSnake string `json:"device_info"`
|
||||
MachineCode string `json:"machineCode"`
|
||||
MachineCodeSnake string `json:"machine_code"`
|
||||
Status *int8 `json:"status"`
|
||||
System string `json:"system"`
|
||||
Version string `json:"version"`
|
||||
BindAccount string `json:"bindAccount"`
|
||||
BindAccountSnake string `json:"bind_account"`
|
||||
OwnerUserID *uint64 `json:"ownerUserId"`
|
||||
OwnerUserIDSnake *uint64 `json:"owner_user_id"`
|
||||
OwnerUserName string `json:"ownerUserName"`
|
||||
OwnerUserNameSnake string `json:"owner_user_name"`
|
||||
ActivationTime string `json:"activationTime"`
|
||||
ActivationTimeSnake string `json:"activation_time"`
|
||||
ExpireTime string `json:"expireTime"`
|
||||
ExpireTimeSnake string `json:"expire_time"`
|
||||
Remark string `json:"remark"`
|
||||
DeviceInfo string `json:"deviceInfo"`
|
||||
DeviceInfoSnake string `json:"device_info"`
|
||||
MachineCode string `json:"machineCode"`
|
||||
MachineCodeSnake string `json:"machine_code"`
|
||||
Status *int8 `json:"status"`
|
||||
System string `json:"system"`
|
||||
Version string `json:"version"`
|
||||
BindAccount string `json:"bindAccount"`
|
||||
BindAccountSnake string `json:"bind_account"`
|
||||
OwnerUserID *uint64 `json:"ownerUserId"`
|
||||
OwnerUserIDSnake *uint64 `json:"owner_user_id"`
|
||||
OwnerUserName string `json:"ownerUserName"`
|
||||
OwnerUserNameSnake string `json:"owner_user_name"`
|
||||
ActivationTime string `json:"activationTime"`
|
||||
ActivationTimeSnake string `json:"activation_time"`
|
||||
ExpireTime string `json:"expireTime"`
|
||||
ExpireTimeSnake string `json:"expire_time"`
|
||||
Remark string `json:"remark"`
|
||||
IpInfo *cursorIpInfo `json:"ipInfo"`
|
||||
}
|
||||
|
||||
type cursorEquipmentActivatePayload struct {
|
||||
Code string `json:"code"`
|
||||
ActivationCode string `json:"activationCode"`
|
||||
ActivationCodeSnake string `json:"activation_code"`
|
||||
DeviceInfo string `json:"deviceInfo"`
|
||||
DeviceInfoSnake string `json:"device_info"`
|
||||
MachineCode string `json:"machineCode"`
|
||||
MachineCodeSnake string `json:"machine_code"`
|
||||
System string `json:"system"`
|
||||
Version string `json:"version"`
|
||||
BindAccount string `json:"bindAccount"`
|
||||
BindAccountSnake string `json:"bind_account"`
|
||||
OwnerUserID *uint64 `json:"ownerUserId"`
|
||||
OwnerUserIDSnake *uint64 `json:"owner_user_id"`
|
||||
OwnerUserName string `json:"ownerUserName"`
|
||||
OwnerUserNameSnake string `json:"owner_user_name"`
|
||||
Remark string `json:"remark"`
|
||||
Code string `json:"code"`
|
||||
ActivationCode string `json:"activationCode"`
|
||||
ActivationCodeSnake string `json:"activation_code"`
|
||||
DeviceInfo string `json:"deviceInfo"`
|
||||
DeviceInfoSnake string `json:"device_info"`
|
||||
MachineCode string `json:"machineCode"`
|
||||
MachineCodeSnake string `json:"machine_code"`
|
||||
System string `json:"system"`
|
||||
Version string `json:"version"`
|
||||
BindAccount string `json:"bindAccount"`
|
||||
BindAccountSnake string `json:"bind_account"`
|
||||
OwnerUserID *uint64 `json:"ownerUserId"`
|
||||
OwnerUserIDSnake *uint64 `json:"owner_user_id"`
|
||||
OwnerUserName string `json:"ownerUserName"`
|
||||
OwnerUserNameSnake string `json:"owner_user_name"`
|
||||
Remark string `json:"remark"`
|
||||
IpInfo *cursorIpInfo `json:"ipInfo"`
|
||||
}
|
||||
|
||||
// cursorSaveIpLog 将 ipInfo 写入设备 IP 日志表(异步,失败不影响主流程)
|
||||
func cursorSaveIpLog(equipmentID uint64, machineCode, source string, ip *cursorIpInfo) {
|
||||
if ip == nil {
|
||||
return
|
||||
}
|
||||
log := &models.PlatformCursorEquipmentIpLog{
|
||||
EquipmentID: equipmentID,
|
||||
MachineCode: machineCode,
|
||||
Source: source,
|
||||
Status: ip.Status,
|
||||
Country: ip.Country,
|
||||
CountryCode: ip.CountryCode,
|
||||
Region: ip.Region,
|
||||
RegionName: ip.RegionName,
|
||||
City: ip.City,
|
||||
Zip: ip.Zip,
|
||||
Lat: ip.Lat,
|
||||
Lon: ip.Lon,
|
||||
Timezone: ip.Timezone,
|
||||
ISP: ip.ISP,
|
||||
Org: ip.Org,
|
||||
AsInfo: ip.As,
|
||||
Query: ip.Query,
|
||||
}
|
||||
_, _ = models.Orm.Insert(log)
|
||||
}
|
||||
|
||||
func cursorFirstNonEmpty(values ...string) string {
|
||||
@@ -293,6 +340,9 @@ func (c *ApiCursorEquipmentController) Report() {
|
||||
row.UpdateTime = &now
|
||||
}
|
||||
|
||||
// 记录 IP 日志
|
||||
cursorSaveIpLog(row.ID, machineCode, "report", p.IpInfo)
|
||||
|
||||
// 查询该设备最新激活码,补全激活时间和到期时间
|
||||
var retActivationTime interface{} = row.ActivationTime
|
||||
var retExpireTime interface{} = row.ExpireTime
|
||||
@@ -569,6 +619,9 @@ func (c *ApiCursorEquipmentController) ActivateByCode() {
|
||||
}
|
||||
rollback = false
|
||||
|
||||
// 记录 IP 日志
|
||||
cursorSaveIpLog(device.ID, machineCode, "activateByCode", p.IpInfo)
|
||||
|
||||
c.jsonResult(200, "success", map[string]interface{}{
|
||||
"activated": true,
|
||||
"reused": false,
|
||||
|
||||
@@ -143,6 +143,40 @@ func (c *PlatformCursorEquipmentController) rowToMap(row *models.PlatformCursorE
|
||||
lastExtractedAt = latestExtract.ExtractedTime
|
||||
}
|
||||
|
||||
// 查询该设备最近一条 IP 日志
|
||||
var lastLoginIp interface{}
|
||||
var lastLoginIpInfo interface{}
|
||||
var latestIpLog models.PlatformCursorEquipmentIpLog
|
||||
ipCond := orm.NewCondition().
|
||||
AndCond(orm.NewCondition().
|
||||
Or("equipment_id", row.ID).
|
||||
Or("machine_code", row.MachineCode))
|
||||
if err := models.Orm.QueryTable(new(models.PlatformCursorEquipmentIpLog)).
|
||||
SetCond(ipCond).
|
||||
OrderBy("-id").
|
||||
One(&latestIpLog); err == nil {
|
||||
lastLoginIp = latestIpLog.Query
|
||||
lastLoginIpInfo = map[string]interface{}{
|
||||
"id": latestIpLog.ID,
|
||||
"source": latestIpLog.Source,
|
||||
"status": latestIpLog.Status,
|
||||
"country": latestIpLog.Country,
|
||||
"countryCode": latestIpLog.CountryCode,
|
||||
"region": latestIpLog.Region,
|
||||
"regionName": latestIpLog.RegionName,
|
||||
"city": latestIpLog.City,
|
||||
"zip": latestIpLog.Zip,
|
||||
"lat": latestIpLog.Lat,
|
||||
"lon": latestIpLog.Lon,
|
||||
"timezone": latestIpLog.Timezone,
|
||||
"isp": latestIpLog.ISP,
|
||||
"org": latestIpLog.Org,
|
||||
"asInfo": latestIpLog.AsInfo,
|
||||
"query": latestIpLog.Query,
|
||||
"createTime": latestIpLog.CreateTime,
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"id": row.ID,
|
||||
"deviceInfo": row.DeviceInfo,
|
||||
@@ -164,6 +198,8 @@ func (c *PlatformCursorEquipmentController) rowToMap(row *models.PlatformCursorE
|
||||
"activationCount": activationCount,
|
||||
"extractCount": extractCount,
|
||||
"lastExtractedAt": lastExtractedAt,
|
||||
"lastLoginIp": lastLoginIp,
|
||||
"lastLoginIpInfo": lastLoginIpInfo,
|
||||
"remark": row.Remark,
|
||||
"createTime": row.CreateTime,
|
||||
"updateTime": row.UpdateTime,
|
||||
@@ -679,3 +715,85 @@ func (c *PlatformCursorEquipmentController) ExtractRecords() {
|
||||
"pageSize": pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
// IpLogs GET /platform/cursor/equipment/ipLogs?equipmentId=1
|
||||
func (c *PlatformCursorEquipmentController) IpLogs() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
equipmentID, _ := c.GetUint64("equipmentId")
|
||||
if equipmentID == 0 {
|
||||
equipmentID, _ = c.GetUint64("id")
|
||||
}
|
||||
if equipmentID == 0 {
|
||||
c.jsonErr(400, 400, "缺少设备ID")
|
||||
return
|
||||
}
|
||||
|
||||
var equipment models.PlatformCursorEquipment
|
||||
if err := models.Orm.QueryTable(new(models.PlatformCursorEquipment)).
|
||||
Filter("id", equipmentID).
|
||||
Filter("delete_time__isnull", true).
|
||||
One(&equipment); err != nil {
|
||||
c.jsonErr(404, 404, "设备不存在")
|
||||
return
|
||||
}
|
||||
|
||||
page, _ := c.GetInt("page", 1)
|
||||
pageSize, _ := c.GetInt("pageSize", 20)
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize < 1 {
|
||||
pageSize = 20
|
||||
}
|
||||
if pageSize > 200 {
|
||||
pageSize = 200
|
||||
}
|
||||
|
||||
ipCond := orm.NewCondition().
|
||||
AndCond(orm.NewCondition().
|
||||
Or("equipment_id", equipment.ID).
|
||||
Or("machine_code", equipment.MachineCode))
|
||||
|
||||
qs := models.Orm.QueryTable(new(models.PlatformCursorEquipmentIpLog)).SetCond(ipCond)
|
||||
total, _ := qs.Count()
|
||||
|
||||
var rows []models.PlatformCursorEquipmentIpLog
|
||||
if _, err := qs.OrderBy("-id").Limit(pageSize, (page-1)*pageSize).All(&rows); err != nil {
|
||||
c.jsonErr(500, 500, "获取 IP 日志失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
list := make([]map[string]interface{}, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
list = append(list, map[string]interface{}{
|
||||
"id": row.ID,
|
||||
"source": row.Source,
|
||||
"status": row.Status,
|
||||
"country": row.Country,
|
||||
"countryCode": row.CountryCode,
|
||||
"region": row.Region,
|
||||
"regionName": row.RegionName,
|
||||
"city": row.City,
|
||||
"zip": row.Zip,
|
||||
"lat": row.Lat,
|
||||
"lon": row.Lon,
|
||||
"timezone": row.Timezone,
|
||||
"isp": row.ISP,
|
||||
"org": row.Org,
|
||||
"asInfo": row.AsInfo,
|
||||
"query": row.Query,
|
||||
"createTime": row.CreateTime,
|
||||
})
|
||||
}
|
||||
|
||||
c.ok(map[string]interface{}{
|
||||
"list": list,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"pageSize": pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user