diff --git a/go/controllers/api_cursor_equipment.go b/go/controllers/api_cursor_equipment.go index 6dc9887..a5a89c4 100644 --- a/go/controllers/api_cursor_equipment.go +++ b/go/controllers/api_cursor_equipment.go @@ -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, diff --git a/go/controllers/platform_cursor_equipment.go b/go/controllers/platform_cursor_equipment.go index dfdc68c..9b1fff7 100644 --- a/go/controllers/platform_cursor_equipment.go +++ b/go/controllers/platform_cursor_equipment.go @@ -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, + }) +} diff --git a/go/models/init.go b/go/models/init.go index 47428dd..13796ca 100644 --- a/go/models/init.go +++ b/go/models/init.go @@ -58,6 +58,7 @@ func Init(_ string) { new(SystemSoftwareUpgrade), new(PlatformCursorEquipment), new(PlatformCursorActivationCode), + new(PlatformCursorEquipmentIpLog), new(PlatformAccountPoolKiro), new(PlatformAccountPoolWindsurf), new(PlatformAccountPoolCursor), diff --git a/go/models/platform_cursor_equipment_ip_log.go b/go/models/platform_cursor_equipment_ip_log.go new file mode 100644 index 0000000..3e92e9c --- /dev/null +++ b/go/models/platform_cursor_equipment_ip_log.go @@ -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" +} diff --git a/go/models/platform_setting.go b/go/models/platform_setting.go new file mode 100644 index 0000000..695c7fe --- /dev/null +++ b/go/models/platform_setting.go @@ -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 +} diff --git a/go/routers/platform/platform.go b/go/routers/platform/platform.go index e004672..3345aee 100644 --- a/go/routers/platform/platform.go +++ b/go/routers/platform/platform.go @@ -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") diff --git a/platform/components.d.ts b/platform/components.d.ts index 567e500..ffd2440 100644 --- a/platform/components.d.ts +++ b/platform/components.d.ts @@ -18,7 +18,6 @@ declare module 'vue' { ElAvatar: typeof import('element-plus/es')['ElAvatar'] ElBacktop: typeof import('element-plus/es')['ElBacktop'] ElButton: typeof import('element-plus/es')['ElButton'] - ElButtonGroup: typeof import('element-plus/es')['ElButtonGroup'] ElCard: typeof import('element-plus/es')['ElCard'] ElCascader: typeof import('element-plus/es')['ElCascader'] ElCheckbox: typeof import('element-plus/es')['ElCheckbox'] @@ -40,7 +39,6 @@ declare module 'vue' { ElFormItem: typeof import('element-plus/es')['ElFormItem'] ElHeader: typeof import('element-plus/es')['ElHeader'] ElIcon: typeof import('element-plus/es')['ElIcon'] - ElImage: typeof import('element-plus/es')['ElImage'] ElInput: typeof import('element-plus/es')['ElInput'] ElInputNumber: typeof import('element-plus/es')['ElInputNumber'] ElLink: typeof import('element-plus/es')['ElLink'] diff --git a/platform/src/api/cursorEquipment.js b/platform/src/api/cursorEquipment.js index 6355d4d..30c208f 100644 --- a/platform/src/api/cursorEquipment.js +++ b/platform/src/api/cursorEquipment.js @@ -63,3 +63,11 @@ export function getCursorEquipmentExtractRecords(params) { params, }); } + +export function getCursorEquipmentIpLogs(params) { + return request({ + url: `${baseUrl}/ipLogs`, + method: 'get', + params, + }); +} diff --git a/platform/src/api/cursorEquipment.ts b/platform/src/api/cursorEquipment.ts index ce45f74..71a1863 100644 --- a/platform/src/api/cursorEquipment.ts +++ b/platform/src/api/cursorEquipment.ts @@ -88,3 +88,11 @@ export function getCursorEquipmentExtractRecords(params: Record) { params, }); } + +export function getCursorEquipmentIpLogs(params: Record) { + return request({ + url: `${baseUrl}/ipLogs`, + method: 'get', + params, + }); +} diff --git a/platform/src/views/cursor/equipment/components/detail.vue b/platform/src/views/cursor/equipment/components/detail.vue index 5549a98..e73f30e 100644 --- a/platform/src/views/cursor/equipment/components/detail.vue +++ b/platform/src/views/cursor/equipment/components/detail.vue @@ -42,6 +42,12 @@ function copyText(text: unknown, label = '内容') { ElMessage.success(`${label}已复制`); }); } + +function sourceLabel(source: string) { + if (source === 'report') return '心跳上报'; + if (source === 'activateByCode') return '激活码激活'; + return source || '-'; +} @@ -549,6 +605,17 @@ onUnmounted(() => { :total="extractState.total" @refresh="fetchExtractRecords" /> + + @@ -657,6 +724,12 @@ onUnmounted(() => { font-size: 12px; } +.ip-col-text { + font-family: monospace; + font-size: 12px; + color: #303133; +} + .pager { display: flex; justify-content: flex-end; diff --git a/platform/src/views/tools/notebook/components/index.vue b/platform/src/views/tools/notebook/components/index.vue deleted file mode 100644 index e69de29..0000000 diff --git a/sql/yz_platform_cursor_equipment_ip_log.sql b/sql/yz_platform_cursor_equipment_ip_log.sql new file mode 100644 index 0000000..fb12ba7 --- /dev/null +++ b/sql/yz_platform_cursor_equipment_ip_log.sql @@ -0,0 +1,28 @@ +-- 设备 IP 登录日志表 +-- 每次客户端调用 report / activateByCode 接口时,若携带 ipInfo,则向本表插入一条记录 +CREATE TABLE IF NOT EXISTS `yz_platform_cursor_equipment_ip_log` ( + `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键', + `equipment_id` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '关联 yz_platform_cursor_equipment.id,0 表示设备尚未创建时的记录', + `machine_code` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '机器码(冗余,便于无 equipment_id 时查询)', + `source` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '来源:report / activateByCode', + -- ip-api.com 返回的原始字段 + `status` VARCHAR(32) NOT NULL DEFAULT '' COMMENT 'ip-api status', + `country` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '国家', + `country_code` VARCHAR(8) NOT NULL DEFAULT '' COMMENT '国家代码', + `region` VARCHAR(16) NOT NULL DEFAULT '' COMMENT '省/州代码', + `region_name` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '省/州名称', + `city` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '城市', + `zip` VARCHAR(32) NOT NULL DEFAULT '' COMMENT '邮编', + `lat` DOUBLE NOT NULL DEFAULT 0 COMMENT '纬度', + `lon` DOUBLE NOT NULL DEFAULT 0 COMMENT '经度', + `timezone` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '时区', + `isp` VARCHAR(255) NOT NULL DEFAULT '' COMMENT 'ISP 运营商', + `org` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '组织', + `as_info` VARCHAR(255) NOT NULL DEFAULT '' COMMENT 'AS 信息', + `query` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '出口 IP 地址', + `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录时间', + PRIMARY KEY (`id`), + KEY `idx_equipment_id` (`equipment_id`), + KEY `idx_machine_code` (`machine_code`), + KEY `idx_create_time` (`create_time`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='设备 IP 登录日志';