开放接口

This commit is contained in:
Break
2026-06-02 16:56:01 +08:00
parent 1b47fb8f32
commit c6e65fde9b
7 changed files with 321 additions and 10 deletions

View File

@@ -41,6 +41,22 @@ type walletPackageOrderRequest struct {
PackageCode string `json:"package_code"`
}
// cardResumeRequest 是"机卡分离卡复机"的请求体。
type cardResumeRequest struct {
CardNo string `json:"card_no"`
}
// deviceSwitchCardRequest 是"设备切网"的请求体。
type deviceSwitchCardRequest struct {
DeviceNo string `json:"device_no"`
ICCID string `json:"iccid"`
}
// deviceOperationRequest 是设备操作(重启/恢复出厂)的请求体。
type deviceOperationRequest struct {
DeviceNo string `json:"device_no"`
}
// client 是第三方调用方视角的独立客户端,不依赖当前项目内部代码。
type client struct {
baseURL string
@@ -53,8 +69,10 @@ func main() {
baseURL := flag.String("base-url", envOrDefault("AGENT_OPEN_API_BASE_URL", defaultBaseURL), "接口服务地址")
account := flag.String("account", envOrDefault("AGENT_OPEN_API_ACCOUNT", defaultAccount), "代理账号用户名或手机号")
password := flag.String("password", envOrDefault("AGENT_OPEN_API_PASSWORD", defaultPassword), "代理账号登录密码")
action := flag.String("action", "wallet-balance", "调用动作realname-status、card-status、card-traffic、packages、wallet-balance、package-order、wallet-transactions")
action := flag.String("action", "wallet-balance", "调用动作realname-status、card-status、card-traffic、card-resume、packages、wallet-balance、package-order、wallet-transactions、device-traffic、device-switch-card、device-reboot、device-reset")
cardNo := flag.String("card-no", "", "卡标识,支持 ICCID、虚拟号、MSISDN")
deviceNo := flag.String("device-no", "", "设备标识支持虚拟号、IMEI")
iccid := flag.String("iccid", "", "目标卡 ICCIDdevice-switch-card 时使用")
cardNos := flag.String("card-nos", "", "卡标识列表,多个值用英文逗号分隔")
packageCode := flag.String("package-code", "", "套餐编码")
page := flag.Int("page", 0, "页码,不传或 0 表示省略")
@@ -91,6 +109,8 @@ func main() {
transactionType: *transactionType,
startDate: *startDate,
endDate: *endDate,
deviceNo: *deviceNo,
iccid: *iccid,
})
if err != nil {
fmt.Fprintf(os.Stderr, "调用失败:%v\n", err)
@@ -118,6 +138,8 @@ type runOptions struct {
transactionType string
startDate string
endDate string
deviceNo string
iccid string
}
func dispatch(ctx context.Context, c *client, opts runOptions) (int, []byte, error) {
@@ -137,6 +159,11 @@ func dispatch(ctx context.Context, c *client, opts runOptions) (int, []byte, err
return 0, nil, errors.New("card-traffic 需要传入 -card-no")
}
return c.queryCardTraffic(ctx, opts.cardNo)
case "card-resume":
if opts.cardNo == "" {
return 0, nil, errors.New("card-resume 需要传入 -card-no")
}
return c.resumeCard(ctx, opts.cardNo)
case "packages":
return c.queryPackages(ctx, packageQuery(opts))
case "wallet-balance":
@@ -152,6 +179,29 @@ func dispatch(ctx context.Context, c *client, opts runOptions) (int, []byte, err
return 0, nil, err
}
return c.queryWalletTransactions(ctx, walletTransactionQuery(opts))
case "device-traffic":
if opts.deviceNo == "" {
return 0, nil, errors.New("device-traffic 需要传入 -device-no")
}
return c.queryDeviceTraffic(ctx, opts.deviceNo)
case "device-switch-card":
if opts.deviceNo == "" {
return 0, nil, errors.New("device-switch-card 需要传入 -device-no")
}
if opts.iccid == "" {
return 0, nil, errors.New("device-switch-card 需要传入 -iccid")
}
return c.switchDeviceCard(ctx, opts.deviceNo, opts.iccid)
case "device-reboot":
if opts.deviceNo == "" {
return 0, nil, errors.New("device-reboot 需要传入 -device-no")
}
return c.rebootDevice(ctx, opts.deviceNo)
case "device-reset":
if opts.deviceNo == "" {
return 0, nil, errors.New("device-reset 需要传入 -device-no")
}
return c.resetDevice(ctx, opts.deviceNo)
default:
return 0, nil, fmt.Errorf("未知 action%s", opts.action)
}
@@ -191,6 +241,28 @@ func (c *client) queryWalletTransactions(ctx context.Context, query url.Values)
return c.get(ctx, "/api/open/v1/wallet/transactions", query)
}
func (c *client) resumeCard(ctx context.Context, cardNo string) (int, []byte, error) {
return c.postJSON(ctx, "/api/open/v1/cards/resume", cardResumeRequest{CardNo: cardNo})
}
func (c *client) queryDeviceTraffic(ctx context.Context, deviceNo string) (int, []byte, error) {
query := url.Values{}
query.Add("device_no", deviceNo)
return c.get(ctx, "/api/open/v1/devices/traffic", query)
}
func (c *client) switchDeviceCard(ctx context.Context, deviceNo string, iccid string) (int, []byte, error) {
return c.postJSON(ctx, "/api/open/v1/devices/switch-card", deviceSwitchCardRequest{DeviceNo: deviceNo, ICCID: iccid})
}
func (c *client) rebootDevice(ctx context.Context, deviceNo string) (int, []byte, error) {
return c.postJSON(ctx, "/api/open/v1/devices/reboot", deviceOperationRequest{DeviceNo: deviceNo})
}
func (c *client) resetDevice(ctx context.Context, deviceNo string) (int, []byte, error) {
return c.postJSON(ctx, "/api/open/v1/devices/reset", deviceOperationRequest{DeviceNo: deviceNo})
}
func (c *client) get(ctx context.Context, path string, query url.Values) (int, []byte, error) {
return c.do(ctx, http.MethodGet, path, query, nil)
}