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,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -58,6 +58,7 @@ func Init(_ string) {
|
||||
new(SystemSoftwareUpgrade),
|
||||
new(PlatformCursorEquipment),
|
||||
new(PlatformCursorActivationCode),
|
||||
new(PlatformCursorEquipmentIpLog),
|
||||
new(PlatformAccountPoolKiro),
|
||||
new(PlatformAccountPoolWindsurf),
|
||||
new(PlatformAccountPoolCursor),
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
// PlatformCursorEquipmentIpLog 设备 IP 登录日志 yz_platform_cursor_equipment_ip_log
|
||||
// 每次客户端调用 report / activateByCode 时,若携带 ipInfo 则写入一条记录。
|
||||
type PlatformCursorEquipmentIpLog struct {
|
||||
ID uint64 `orm:"column(id);pk;auto" json:"id"`
|
||||
EquipmentID uint64 `orm:"column(equipment_id);default(0)" json:"equipmentId"`
|
||||
MachineCode string `orm:"column(machine_code);size(128)" json:"machineCode"`
|
||||
Source string `orm:"column(source);size(32)" json:"source"`
|
||||
Status string `orm:"column(status);size(32)" json:"status"`
|
||||
Country string `orm:"column(country);size(64)" json:"country"`
|
||||
CountryCode string `orm:"column(country_code);size(8)" json:"countryCode"`
|
||||
Region string `orm:"column(region);size(16)" json:"region"`
|
||||
RegionName string `orm:"column(region_name);size(128)" json:"regionName"`
|
||||
City string `orm:"column(city);size(128)" json:"city"`
|
||||
Zip string `orm:"column(zip);size(32)" json:"zip"`
|
||||
Lat float64 `orm:"column(lat)" json:"lat"`
|
||||
Lon float64 `orm:"column(lon)" json:"lon"`
|
||||
Timezone string `orm:"column(timezone);size(64)" json:"timezone"`
|
||||
ISP string `orm:"column(isp);size(255)" json:"isp"`
|
||||
Org string `orm:"column(org);size(255)" json:"org"`
|
||||
AsInfo string `orm:"column(as_info);size(255)" json:"asInfo"`
|
||||
Query string `orm:"column(query);size(64)" json:"query"`
|
||||
CreateTime time.Time `orm:"column(create_time);auto_now_add;type(datetime)" json:"createTime"`
|
||||
}
|
||||
|
||||
func (m *PlatformCursorEquipmentIpLog) TableName() string {
|
||||
return "yz_platform_cursor_equipment_ip_log"
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package models
|
||||
|
||||
// GetPlatformSettingValue 从 yz_system_tenant_setting_items 表中读取平台级别(tid=0)的配置值。
|
||||
// key 不存在或读取失败时返回空字符串。
|
||||
func GetPlatformSettingValue(key string) string {
|
||||
type row struct {
|
||||
SettingValue string
|
||||
}
|
||||
var r row
|
||||
// tid=0 表示平台全局配置,与租户无关
|
||||
err := Orm.Raw(
|
||||
"SELECT IFNULL(setting_value, '') AS setting_value FROM yz_system_tenant_setting_items WHERE tid = 0 AND setting_key = ? AND delete_time IS NULL LIMIT 1",
|
||||
key,
|
||||
).QueryRow(&r)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return r.SettingValue
|
||||
}
|
||||
|
||||
// SetPlatformSettingValue 写入或更新平台级别(tid=0)的配置值。
|
||||
func SetPlatformSettingValue(key, value string) error {
|
||||
_, err := Orm.Raw(`
|
||||
INSERT INTO yz_system_tenant_setting_items (tid, setting_key, setting_value, create_time, update_time)
|
||||
VALUES (0, ?, ?, NOW(), NOW())
|
||||
ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value), update_time = NOW(), delete_time = NULL
|
||||
`, key, value).Exec()
|
||||
return err
|
||||
}
|
||||
@@ -171,6 +171,7 @@ func Register() {
|
||||
beego.Router("/platform/cursor/equipment/activate", &controllers.PlatformCursorEquipmentController{}, "post:Activate")
|
||||
beego.Router("/platform/cursor/equipment/activationRecords", &controllers.PlatformCursorEquipmentController{}, "get:ActivationRecords")
|
||||
beego.Router("/platform/cursor/equipment/extractRecords", &controllers.PlatformCursorEquipmentController{}, "get:ExtractRecords")
|
||||
beego.Router("/platform/cursor/equipment/ipLogs", &controllers.PlatformCursorEquipmentController{}, "get:IpLogs")
|
||||
|
||||
// Cursor 激活码管理(yz_platform_cursor_activation_code)
|
||||
beego.Router("/platform/cursor/activationcode/list", &controllers.PlatformCursorActivationCodeController{}, "get:List")
|
||||
|
||||
Reference in New Issue
Block a user