Files
junhong_cmp_fiber/internal/handler/admin/iot_card.go
break 73f5125d3d
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 8m26s
七月迭代短暂完结,还有很多后端的关键东西没有弄,这是一版赶时间做的东西
2026-07-25 17:06:58 +08:00

161 lines
4.3 KiB
Go

package admin
import (
"github.com/gofiber/fiber/v2"
"github.com/break/junhong_cmp_fiber/internal/model/dto"
iotCardService "github.com/break/junhong_cmp_fiber/internal/service/iot_card"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
"github.com/break/junhong_cmp_fiber/pkg/middleware"
"github.com/break/junhong_cmp_fiber/pkg/response"
)
type IotCardHandler struct {
service *iotCardService.Service
}
func NewIotCardHandler(service *iotCardService.Service) *IotCardHandler {
return &IotCardHandler{
service: service,
}
}
func (h *IotCardHandler) ListStandalone(c *fiber.Ctx) error {
var req dto.ListStandaloneIotCardRequest
if err := c.QueryParser(&req); err != nil {
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
}
result, err := h.service.ListStandalone(c.UserContext(), &req)
if err != nil {
return err
}
return response.SuccessWithPagination(c, result.List, result.Total, result.Page, result.PageSize)
}
// BatchUpdateRealnamePolicy 批量更新卡实名认证策略。
// POST /api/admin/iot-cards/batch-update-realname-policy
func (h *IotCardHandler) BatchUpdateRealnamePolicy(c *fiber.Ctx) error {
var req dto.BatchUpdateAssetRealnamePolicyRequest
if err := c.BodyParser(&req); err != nil {
return errors.New(errors.CodeInvalidParam)
}
result, err := h.service.BatchUpdateRealnamePolicy(c.UserContext(), &req)
if err != nil {
return err
}
return response.Success(c, result)
}
// SetSpeedTier 设置或恢复 IoT 卡固定限速档位。
// PUT /api/admin/iot-cards/:iccid/speed-tier
func (h *IotCardHandler) SetSpeedTier(c *fiber.Ctx) error {
iccid := c.Params("iccid")
if iccid == "" {
return errors.New(errors.CodeInvalidParam, "ICCID 不能为空")
}
var req dto.SetIotCardSpeedTierRequest
if err := c.BodyParser(&req); err != nil {
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
}
result, err := h.service.SetSpeedTier(c.UserContext(), iccid, req.Code)
if err != nil {
return err
}
return response.Success(c, result)
}
func (h *IotCardHandler) AllocateCards(c *fiber.Ctx) error {
var req dto.AllocateStandaloneCardsRequest
if err := c.BodyParser(&req); err != nil {
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
}
ctx := c.UserContext()
operatorID := middleware.GetUserIDFromContext(ctx)
userType := middleware.GetUserTypeFromContext(ctx)
var operatorShopID *uint
if userType == constants.UserTypeAgent {
shopID := middleware.GetShopIDFromContext(ctx)
if shopID > 0 {
operatorShopID = &shopID
}
}
result, err := h.service.AllocateCards(ctx, &req, operatorID, operatorShopID)
if err != nil {
return err
}
return response.Success(c, result)
}
func (h *IotCardHandler) RecallCards(c *fiber.Ctx) error {
var req dto.RecallStandaloneCardsRequest
if err := c.BodyParser(&req); err != nil {
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
}
ctx := c.UserContext()
operatorID := middleware.GetUserIDFromContext(ctx)
userType := middleware.GetUserTypeFromContext(ctx)
var operatorShopID *uint
if userType == constants.UserTypeAgent {
shopID := middleware.GetShopIDFromContext(ctx)
if shopID > 0 {
operatorShopID = &shopID
}
}
result, err := h.service.RecallCards(ctx, &req, operatorID, operatorShopID)
if err != nil {
return err
}
return response.Success(c, result)
}
func (h *IotCardHandler) BatchSetSeriesBinding(c *fiber.Ctx) error {
var req dto.BatchSetCardSeriesBindngRequest
if err := c.BodyParser(&req); err != nil {
return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
}
ctx := c.UserContext()
userType := middleware.GetUserTypeFromContext(ctx)
var operatorShopID *uint
if userType == constants.UserTypeAgent {
shopID := middleware.GetShopIDFromContext(ctx)
if shopID > 0 {
operatorShopID = &shopID
}
}
result, err := h.service.BatchSetSeriesBinding(ctx, &req, operatorShopID)
if err != nil {
return err
}
return response.Success(c, result)
}
// GetRealnameLink 获取实名认证链接
func (h *IotCardHandler) GetRealnameLink(c *fiber.Ctx) error {
iccid := c.Params("iccid")
if iccid == "" {
return errors.New(errors.CodeInvalidParam, "ICCID不能为空")
}
link, err := h.service.GatewayGetRealnameLink(c.UserContext(), iccid)
if err != nil {
return err
}
return response.Success(c, link)
}