simCardManageControllers.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * SIM Card Manage API List
  3. * /simCardManage/info - GET 查询SIM卡信息
  4. * /simCardManage/monthly - GET 查询SIM卡月数据
  5. */
  6. package controllers
  7. import (
  8. "fwh-toolbox-server/services"
  9. "regexp"
  10. "strconv"
  11. "github.com/gofiber/fiber/v2"
  12. "go.uber.org/zap"
  13. )
  14. // 兼容两个平台的月份格式:
  15. // - SmartHub: yyyy-MM(如 2026-09)
  16. // - Acceleronix: yyyyMM (如 202609)
  17. // 连字符可选,月份固定两位且限定 01~12。
  18. var yearMonthRegexp = regexp.MustCompile(`^\d{4}-?(0[1-9]|1[0-2])$`)
  19. // 查询SIM卡信息
  20. // /simCardManage/info
  21. func GetSimCardBatchInfo(c *fiber.Ctx) error {
  22. search := c.Query("search", "")
  23. pageStr := c.Query("page", "1")
  24. page, err := strconv.ParseUint(pageStr, 10, 64)
  25. if err != nil {
  26. zap.L().Warn("Invalid request parameter", zap.String("err", err.Error()), zap.String("page", pageStr))
  27. return Fail(c, "Invalid request parameter (page)")
  28. }
  29. sizeStr := c.Query("size", "20")
  30. size, err := strconv.ParseUint(sizeStr, 10, 64)
  31. if err != nil {
  32. zap.L().Warn("Invalid request parameter", zap.String("err", err.Error()), zap.String("size", sizeStr))
  33. return Fail(c, "Invalid request parameter (size)")
  34. }
  35. status := c.Query("status", "")
  36. var statusPtr *string
  37. if status != "" {
  38. statusPtr = &status
  39. }
  40. platform := c.Query("platform", "")
  41. var platformPtr *string
  42. if platform != "" {
  43. platformPtr = &platform
  44. }
  45. date, err := services.GetSimCardBatchInfo(uint(page), uint(size), search, statusPtr, platformPtr)
  46. if err != nil {
  47. return Error(c, "Get SIM card info failed.")
  48. }
  49. return Success(c, date)
  50. }
  51. // 查询SIM卡月数据
  52. // /simCardManage/monthly
  53. func GetSimCardMonthlyData(c *fiber.Ctx) error {
  54. id := c.Query("id", "")
  55. month := c.Query("month", "")
  56. if id == "" {
  57. zap.L().Warn("id parameter is empty")
  58. return Fail(c, "id parameter is empty")
  59. }
  60. if !yearMonthRegexp.MatchString(month) {
  61. zap.L().Warn("month parameter is in the wrong format", zap.String("month", month))
  62. return Fail(c, "month parameter is in the wrong format")
  63. }
  64. date, err := services.GetSimCardMonthlyData(month, id)
  65. if err != nil {
  66. return Error(c, "Get SIM card monthly data failed.")
  67. }
  68. return Success(c, date)
  69. }