40760aa884
- server:Go 网关(WS 流式识别中继/计费配额/微信登录支付 mock/反馈/埋点),gummy provider 已真实联调 - desktop:Tauri 2(全局快捷键 push-to-talk/浮层/托盘/设置/登录购买/反馈/首启引导) - android:Compose 主 App + IME(键盘内录音直传) - ios:App + 键盘扩展(1A spike 实证键盘内不可录音,走 deep link 听写) - design/design-pipeline:设计系统 + token 导出 iOS/Android 主题 - doc:前后端设计文档(HTML);web:官网宣传页;todo:任务看板 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
// Package user /v1/me 聚合、设备登记、应用更新检查。
|
|
package user
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
|
|
"dudu/server/internal/auth"
|
|
"dudu/server/internal/quota"
|
|
"dudu/server/internal/store"
|
|
"dudu/server/pkg/protocol"
|
|
)
|
|
|
|
type Handlers struct {
|
|
DB *gorm.DB
|
|
Quota *quota.Manager
|
|
}
|
|
|
|
// Me GET /v1/me(需登录):余额/试用/账户态聚合 + 设备心跳登记。
|
|
func (h *Handlers) Me(c *gin.Context) {
|
|
uid := auth.UserID(c)
|
|
var u store.User
|
|
if err := h.DB.First(&u, "id = ?", uid).Error; err != nil {
|
|
c.JSON(http.StatusUnauthorized, protocol.NewAPIError(protocol.ErrUnauthorized))
|
|
return
|
|
}
|
|
snap, err := h.Quota.Get(c, uid)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, protocol.NewAPIError(protocol.ErrInternal))
|
|
return
|
|
}
|
|
h.touchDevice(c, uid)
|
|
|
|
state := protocol.AccountOK
|
|
switch {
|
|
case snap.BalanceSeconds > 0:
|
|
state = protocol.AccountOK
|
|
case snap.TrialUsedToday < protocol.TrialDailySeconds:
|
|
state = protocol.AccountTrial
|
|
default:
|
|
state = protocol.AccountQuota
|
|
}
|
|
c.JSON(http.StatusOK, protocol.MeResponse{
|
|
UserID: uid,
|
|
NicknameMasked: auth.MaskNickname(u.Nickname),
|
|
BalanceSeconds: snap.BalanceSeconds,
|
|
TrialDailyLimit: protocol.TrialDailySeconds,
|
|
TrialUsedToday: snap.TrialUsedToday,
|
|
AccountState: state,
|
|
})
|
|
}
|
|
|
|
// touchDevice 设备登记/心跳(X-Device-ID / X-Platform / X-App-Version 头)。
|
|
func (h *Handlers) touchDevice(c *gin.Context, uid string) {
|
|
did := c.GetHeader("X-Device-ID")
|
|
if did == "" {
|
|
return
|
|
}
|
|
dev := store.Device{
|
|
ID: did, UserID: uid,
|
|
Platform: c.GetHeader("X-Platform"), AppVersion: c.GetHeader("X-App-Version"),
|
|
LastSeen: time.Now(),
|
|
}
|
|
_ = h.DB.Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "id"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"user_id", "platform", "app_version", "last_seen"}),
|
|
}).Create(&dev).Error
|
|
}
|
|
|
|
// AppLatest GET /v1/app/latest?platform=mac|win|android|ios 🔓
|
|
// 版本信息来自 APP_LATEST_JSON 环境变量({"mac":{"version":...,"url":...},...}),
|
|
// MVP 不建表;未配置平台返回 204。
|
|
func (h *Handlers) AppLatest(c *gin.Context) {
|
|
raw := os.Getenv("APP_LATEST_JSON")
|
|
if raw == "" {
|
|
c.Status(http.StatusNoContent)
|
|
return
|
|
}
|
|
var all map[string]protocol.AppLatestResponse
|
|
if err := json.Unmarshal([]byte(raw), &all); err != nil {
|
|
c.Status(http.StatusNoContent)
|
|
return
|
|
}
|
|
if v, ok := all[c.Query("platform")]; ok {
|
|
c.JSON(http.StatusOK, v)
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|