feat(server/reward): GET /v1/invite — 邀请码/链接/战绩/TG 任务态

This commit is contained in:
wangjia
2026-07-13 07:46:07 +08:00
parent de1117b0e6
commit d23f7bca8f
2 changed files with 94 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
package reward
import (
"encoding/json"
"net/http"
"github.com/wangjia/pangolin/server/internal/apierr"
"github.com/wangjia/pangolin/server/internal/auth"
)
const inviteLinkBase = "https://pangolin.yanmeiai.com/i/"
type Handler struct {
svc *Service
st *Store
tgEnabled bool
channel string
}
func NewHandler(s *Service, st *Store, tgEnabled bool, channel string) *Handler {
return &Handler{svc: s, st: st, tgEnabled: tgEnabled, channel: channel}
}
func (h *Handler) GetInvite(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
uid, ok := auth.UserIDFromContext(ctx)
if !ok {
apierr.WriteJSON(w, http.StatusUnauthorized, apierr.ErrUnauthorized)
return
}
code, err := h.svc.EnsureCode(ctx, uid)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.ErrInternal)
return
}
invited, converted, earned, err := h.st.Summary(ctx, uid)
if err != nil {
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.ErrInternal)
return
}
joined, _ := h.st.TelegramClaimed(ctx, uid)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(map[string]any{
"invite_code": code,
"invite_link": inviteLinkBase + code,
"invited": invited,
"converted": converted,
"earned_days": earned,
"telegram": map[string]any{
"enabled": h.tgEnabled,
"joined": joined,
"channel": h.channel,
},
})
}