yunzerwebsiteallinone/go/controllers/api_cursor_detect.go
2026-06-16 23:15:34 +08:00

118 lines
3.2 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 controllers
import (
"fmt"
"strconv"
"strings"
"server/models"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
)
// ApiCursorDetectController Cursor Token 顺序读取接口(不改变号池状态)
//
// 用途:
// - 前端传入 start_id/current_id/id从该 ID 开始按 id 从小到大读取 Cursor Token。
// - 只读取未提取记录,不更新 is_extracted、extracted_time、extracted_platform、is_used 等任何状态。
// - 如果传入 ID 对应记录已提取,会自动跳过,继续找下一条未提取记录。
// - 返回 next_id前端下一次点击时传 next_id即可实现 11 -> 12 -> 13 递增读取。
//
// 示例:
//
// GET /api/cursor/token/peek?id=11
// GET /api/cursor/token/peek?start_id=11
// GET /api/cursor/token/peek?current_id=11
//
// 可选参数:
// - data_type=tk/account/account_tk默认 tk
type ApiCursorDetectController struct {
beego.Controller
}
func (c *ApiCursorDetectController) cursorDetectJSONErr(httpStatus, code int, msg string) {
c.Ctx.Output.SetStatus(httpStatus)
c.Data["json"] = map[string]interface{}{
"code": code,
"msg": msg,
}
_ = c.ServeJSON()
}
// PeekToken 按 ID 顺序读取 Cursor Token不改变任何状态。
func (c *ApiCursorDetectController) PeekToken() {
startID, err := c.readStartID()
if err != nil {
c.cursorDetectJSONErr(400, 400, err.Error())
return
}
dataType := strings.TrimSpace(c.GetString("data_type"))
if dataType == "" {
dataType = "tk"
}
if !isValidPoolType(dataType) {
c.cursorDetectJSONErr(400, 400, "data_type 不合法,支持: account/tk/account_tk")
return
}
var row models.PlatformAccountPoolCursor
qs := models.Orm.QueryTable(new(models.PlatformAccountPoolCursor)).
Filter("id__gte", startID).
Filter("data_type", dataType).
Filter("delete_time__isnull", true).
Filter("extracted_time__isnull", true).
Filter("is_extracted", 0).
Exclude("token", "")
err = qs.OrderBy("id").One(&row)
if err != nil {
if err == orm.ErrNoRows {
c.cursorDetectJSONErr(404, 404, "从当前 ID 开始暂无未提取 Cursor Token")
return
}
c.cursorDetectJSONErr(500, 500, "查询失败: "+err.Error())
return
}
c.Data["json"] = map[string]interface{}{
"code": 200,
"msg": "success",
"data": map[string]interface{}{
"id": row.ID,
"next_id": row.ID + 1,
"data_type": row.DataType,
"account": row.Account,
"password": row.Password,
"token": row.Token,
"remark": row.Remark,
"is_used": row.IsUsed,
"create_time": row.CreateTime,
// 明确告诉前端:本接口只是读取,没有更新号池状态。
"state_changed": false,
},
}
_ = c.ServeJSON()
}
func (c *ApiCursorDetectController) readStartID() (uint64, error) {
raw := strings.TrimSpace(c.GetString("id"))
if raw == "" {
raw = strings.TrimSpace(c.GetString("start_id"))
}
if raw == "" {
raw = strings.TrimSpace(c.GetString("current_id"))
}
if raw == "" {
return 0, fmt.Errorf("缺少参数 id/start_id/current_id")
}
id, err := strconv.ParseUint(raw, 10, 64)
if err != nil || id == 0 {
return 0, fmt.Errorf("id 必须是大于 0 的整数")
}
return id, nil
}