feat(server/reward): GET /v1/invite — 邀请码/链接/战绩/TG 任务态
This commit is contained in:
@@ -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,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user