checkin.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package controller
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/logger"
  8. "github.com/QuantumNous/new-api/model"
  9. "github.com/QuantumNous/new-api/setting/operation_setting"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // GetCheckinStatus 获取用户签到状态和历史记录
  13. func GetCheckinStatus(c *gin.Context) {
  14. setting := operation_setting.GetCheckinSetting()
  15. if !setting.Enabled {
  16. common.ApiErrorMsg(c, "签到功能未启用")
  17. return
  18. }
  19. userId := c.GetInt("id")
  20. // 获取月份参数,默认为当前月份
  21. month := c.DefaultQuery("month", time.Now().Format("2006-01"))
  22. stats, err := model.GetUserCheckinStats(userId, month)
  23. if err != nil {
  24. c.JSON(http.StatusOK, gin.H{
  25. "success": false,
  26. "message": err.Error(),
  27. })
  28. return
  29. }
  30. c.JSON(http.StatusOK, gin.H{
  31. "success": true,
  32. "data": gin.H{
  33. "enabled": setting.Enabled,
  34. "min_quota": setting.MinQuota,
  35. "max_quota": setting.MaxQuota,
  36. "stats": stats,
  37. },
  38. })
  39. }
  40. // DoCheckin 执行用户签到
  41. func DoCheckin(c *gin.Context) {
  42. setting := operation_setting.GetCheckinSetting()
  43. if !setting.Enabled {
  44. common.ApiErrorMsg(c, "签到功能未启用")
  45. return
  46. }
  47. userId := c.GetInt("id")
  48. checkin, err := model.UserCheckin(userId)
  49. if err != nil {
  50. c.JSON(http.StatusOK, gin.H{
  51. "success": false,
  52. "message": err.Error(),
  53. })
  54. return
  55. }
  56. model.RecordLog(userId, model.LogTypeSystem, fmt.Sprintf("用户签到,获得额度 %s", logger.LogQuota(checkin.QuotaAwarded)))
  57. c.JSON(http.StatusOK, gin.H{
  58. "success": true,
  59. "message": "签到成功",
  60. "data": gin.H{
  61. "quota_awarded": checkin.QuotaAwarded,
  62. "checkin_date": checkin.CheckinDate},
  63. })
  64. }