Documentation
¶
Overview ¶
Package user 的社交接口解析辅助函数。
Package user 的相似用户接口声明(按返回实体归属 UserService)。
Package user 的社交与动态接口声明。
Follows/Followeds/Events/Record/Level 五个接口。复用 UserService。
Package user 的特殊接口声明(不走标准 Execute 缓存路径)。
DetailByName/FollowEachOther 是匿名 cookie 的直接调用。DetailByName 用公开搜索实现, 仍是 engine.Endpoint;FollowEachOther 需要动态 path(uid 拼进 URL)且要遍历关注列表 做业务判断,改用 Meta 函数 + Request/Parse 形式,service 层调 RawDoWithCookieAndInput (第三条执行路径,见 ADR §4.5)。两者不缓存(动态/即时数据)。
Package user 定义用户模块接口的声明。
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Account = &engine.Endpoint[*mmpb.AccountRequest, *mmpb.AccountResponse]{ Meta: engine.Meta{ Path: "/weapi/w/nuser/account/get", Method: "POST", Crypto: engine.CryptoWeAPI, Auth: session.AuthLoggedIn, }, NewResp: func() *mmpb.AccountResponse { return &mmpb.AccountResponse{} }, MapRequest: func(*mmpb.AccountRequest) (map[string]any, error) { return map[string]any{}, nil }, MapResponse: func(_ *mmpb.AccountRequest, raw json.RawMessage) (*mmpb.AccountResponse, error) { u, err := model.DecodeUserDetail(raw) if err != nil { return nil, err } return &mmpb.AccountResponse{User: u}, nil }, }
Account 是获取当前登录账号信息的接口声明(需登录态)。
var Detail = &engine.Endpoint[*mmpb.DetailRequest, *mmpb.DetailResponse]{ Meta: engine.Meta{ Path: "/weapi/v3/user/detail", Method: "POST", Crypto: engine.CryptoWeAPI, Auth: session.AuthAnonymous, }, Cache: &engine.CachePolicy[*mmpb.DetailRequest]{ Key: func(req *mmpb.DetailRequest) string { return fmt.Sprintf("user:detail:%d", req.GetUserId()) }, TTL: 24 * time.Hour, }, NewResp: func() *mmpb.DetailResponse { return &mmpb.DetailResponse{} }, MapRequest: func(req *mmpb.DetailRequest) (map[string]any, error) { return map[string]any{"id": fmt.Sprintf("%d", req.GetUserId())}, nil }, MapResponse: func(_ *mmpb.DetailRequest, raw json.RawMessage) (*mmpb.DetailResponse, error) { u, err := model.DecodeUserDetail(raw) if err != nil { return nil, err } return &mmpb.DetailResponse{User: u}, nil }, }
Detail 是获取用户详情的接口声明。
var DetailByName = &engine.Endpoint[*mmpb.DetailByNameRequest, *mmpb.DetailByNameResponse]{ Meta: engine.Meta{ Path: "/api/search/get", Method: "GET", Crypto: engine.CryptoNone, Auth: session.AuthAnonymous, }, MapRequest: func(req *mmpb.DetailByNameRequest) (map[string]any, error) { return map[string]any{ "s": req.GetNickname(), "type": 1002, "limit": 1, "offset": 0, }, nil }, MapResponse: func(_ *mmpb.DetailByNameRequest, raw json.RawMessage) (*mmpb.DetailByNameResponse, error) { var resp struct { Result struct { Userprofiles []struct { UserID int64 `json:"userId"` // 用户ID } `json:"userprofiles"` } `json:"result"` } if err := json.Unmarshal(raw, &resp); err != nil { return nil, fmt.Errorf("解析昵称查询失败: %w", err) } if len(resp.Result.Userprofiles) == 0 { return nil, fmt.Errorf("未找到该昵称对应的用户") } return &mmpb.DetailByNameResponse{UserId: resp.Result.Userprofiles[0].UserID}, nil }, }
DetailByName 根据 nickname 获取 userid(匿名 cookie)。
上游没有昵称直查接口(旧 path /weapi/v1/w/user/info/detail 真机返回 404), 改用公开搜索 type=1002(用户)取第一条结果。
var Events = &engine.Endpoint[*mmpb.EventsRequest, *mmpb.EventsResponse]{ Meta: engine.Meta{ Path: "/weapi/event/get", Method: "POST", Crypto: engine.CryptoWeAPI, Auth: session.AuthAnonymous, }, Cache: &engine.CachePolicy[*mmpb.EventsRequest]{ Key: func(req *mmpb.EventsRequest) string { return fmt.Sprintf("user:events:%d:%d:%d", req.GetUserId(), req.GetLimit(), req.GetLastEventId()) }, TTL: 10 * time.Minute, }, NewResp: func() *mmpb.EventsResponse { return &mmpb.EventsResponse{} }, MapRequest: func(req *mmpb.EventsRequest) (map[string]any, error) { limit := req.GetLimit() if limit <= 0 { limit = 30 } return map[string]any{ "uid": fmt.Sprintf("%d", req.GetUserId()), "limit": limit, "lasttime": req.GetLastEventId(), }, nil }, MapResponse: func(_ *mmpb.EventsRequest, raw json.RawMessage) (*mmpb.EventsResponse, error) { var resp struct { Events []struct { ID int64 `json:"id"` // 动态ID UserID int64 `json:"userId"` // 发布者ID Type int `json:"type"` // 动态类型 JSON string `json:"json"` // 动态JSON内容 ShowTime int64 `json:"showTime"` // 发布时间(毫秒) } `json:"events"` More bool `json:"more"` // 是否更多 LastEvent int64 `json:"lasttime"` // 最后动态ID } if err := json.Unmarshal(raw, &resp); err != nil { return nil, fmt.Errorf("解析动态列表失败: %w", err) } out := &mmpb.EventsResponse{More: resp.More, LastEventId: resp.LastEvent} for _, e := range resp.Events { out.Events = append(out.Events, &mmpb.UserEvent{ EventId: e.ID, UserId: e.UserID, Type: int32(e.Type), JsonContent: e.JSON, EventTime: e.ShowTime, }) } return out, nil }, }
Events 是获取用户动态的接口声明。
var Followeds = &engine.Endpoint[*mmpb.FollowedsRequest, *mmpb.FollowedsResponse]{ Meta: engine.Meta{ Path: "/weapi/user/getfolloweds", Method: "POST", Crypto: engine.CryptoWeAPI, Auth: session.AuthAnonymous, }, Cache: &engine.CachePolicy[*mmpb.FollowedsRequest]{ Key: func(req *mmpb.FollowedsRequest) string { return fmt.Sprintf("user:followeds:%d:%d:%d", req.GetUserId(), req.GetLimit(), req.GetLastTime()) }, TTL: 24 * time.Hour, }, NewResp: func() *mmpb.FollowedsResponse { return &mmpb.FollowedsResponse{} }, MapRequest: func(req *mmpb.FollowedsRequest) (map[string]any, error) { limit := req.GetLimit() if limit <= 0 { limit = 30 } return map[string]any{ "userId": fmt.Sprintf("%d", req.GetUserId()), "limit": limit, "lasttime": req.GetLastTime(), }, nil }, MapResponse: parseFollowedList, }
Followeds 是获取用户粉丝列表的接口声明。
var Follows = &engine.Endpoint[*mmpb.FollowsRequest, *mmpb.FollowsResponse]{ Meta: engine.Meta{ Path: "/weapi/user/getfollows", Method: "POST", Crypto: engine.CryptoWeAPI, Auth: session.AuthAnonymous, }, Cache: &engine.CachePolicy[*mmpb.FollowsRequest]{ Key: func(req *mmpb.FollowsRequest) string { return fmt.Sprintf("user:follows:%d:%d:%d", req.GetUserId(), req.GetLimit(), req.GetOffset()) }, TTL: 24 * time.Hour, }, NewResp: func() *mmpb.FollowsResponse { return &mmpb.FollowsResponse{} }, MapRequest: func(req *mmpb.FollowsRequest) (map[string]any, error) { limit := req.GetLimit() if limit <= 0 { limit = 30 } return map[string]any{"uid": fmt.Sprintf("%d", req.GetUserId()), "limit": limit, "offset": req.GetOffset()}, nil }, MapResponse: parseFollowList, }
Follows 是获取用户关注列表的接口声明。
var Level = &engine.Endpoint[*mmpb.LevelRequest, *mmpb.LevelResponse]{ Meta: engine.Meta{ Path: "/weapi/user/level", Method: "POST", Crypto: engine.CryptoWeAPI, Auth: session.AuthAnonymous, }, Cache: &engine.CachePolicy[*mmpb.LevelRequest]{ Key: func(req *mmpb.LevelRequest) string { return fmt.Sprintf("user:level:%d", req.GetUserId()) }, TTL: 24 * time.Hour, }, NewResp: func() *mmpb.LevelResponse { return &mmpb.LevelResponse{} }, MapRequest: func(req *mmpb.LevelRequest) (map[string]any, error) { return map[string]any{"userId": fmt.Sprintf("%d", req.GetUserId())}, nil }, MapResponse: func(_ *mmpb.LevelRequest, raw json.RawMessage) (*mmpb.LevelResponse, error) { var resp struct { Data struct { Level int `json:"level"` // 当前等级 Now int64 `json:"now"` // 当前经验 Next int64 `json:"next"` // 升级所需 } `json:"data"` } if err := json.Unmarshal(raw, &resp); err != nil { return nil, fmt.Errorf("解析用户等级失败: %w", err) } return &mmpb.LevelResponse{ Level: &mmpb.UserLevel{Level: int32(resp.Data.Level), Now: resp.Data.Now, Next: resp.Data.Next}, }, nil }, }
Level 是获取用户等级信息的接口声明。
var Record = &engine.Endpoint[*mmpb.RecordRequest, *mmpb.RecordResponse]{ Meta: engine.Meta{ Path: "/weapi/v1/play/record", Method: "POST", Crypto: engine.CryptoWeAPI, Auth: session.AuthAnonymous, }, Cache: &engine.CachePolicy[*mmpb.RecordRequest]{ Key: func(req *mmpb.RecordRequest) string { return fmt.Sprintf("user:record:%d:%d", req.GetUserId(), req.GetType()) }, TTL: 10 * time.Minute, }, NewResp: func() *mmpb.RecordResponse { return &mmpb.RecordResponse{} }, MapRequest: func(req *mmpb.RecordRequest) (map[string]any, error) { return map[string]any{ "uid": fmt.Sprintf("%d", req.GetUserId()), "type": req.GetType(), }, nil }, MapResponse: parsePlayRecord, }
Record 是获取用户播放记录的接口声明。
var SimilarUsers = &engine.Endpoint[*mmpb.SimilarUsersRequest, *mmpb.SimilarUsersResponse]{ Meta: engine.Meta{ Path: "/weapi/discovery/simiUser", Method: "POST", Crypto: engine.CryptoWeAPI, Auth: session.AuthAnonymous, }, Cache: &engine.CachePolicy[*mmpb.SimilarUsersRequest]{ Key: func(r *mmpb.SimilarUsersRequest) string { return fmt.Sprintf("user:similar:%d:%d:%d", r.GetSongId(), r.GetLimit(), r.GetOffset()) }, TTL: 24 * time.Hour, }, NewResp: func() *mmpb.SimilarUsersResponse { return &mmpb.SimilarUsersResponse{} }, MapRequest: func(req *mmpb.SimilarUsersRequest) (map[string]any, error) { limit := req.GetLimit() if limit <= 0 { limit = 50 } return map[string]any{"songid": req.GetSongId(), "limit": limit, "offset": req.GetOffset()}, nil }, MapResponse: func(req *mmpb.SimilarUsersRequest, raw json.RawMessage) (*mmpb.SimilarUsersResponse, error) { var resp struct { Userprofiles []struct { UserID int64 `json:"userId"` // 用户ID Nickname string `json:"nickname"` // 昵称 AvatarUrl string `json:"avatarUrl"` // 头像URL } `json:"userprofiles"` } if err := json.Unmarshal(raw, &resp); err != nil { return nil, fmt.Errorf("解析相似用户失败: %w", err) } out := make([]*mmpb.User, 0, len(resp.Userprofiles)) for _, u := range resp.Userprofiles { out = append(out, &mmpb.User{ Id: u.UserID, Nickname: u.Nickname, AvatarUrl: u.AvatarUrl, }) } return &mmpb.SimilarUsersResponse{Users: out}, nil }, }
SimilarUsers 基于歌曲获取听歌的人(weapi,匿名,24h 缓存)。
var SubCount = &engine.Endpoint[*mmpb.SubCountRequest, *mmpb.SubCountResponse]{ Meta: engine.Meta{ Path: "/weapi/subcount", Method: "POST", Crypto: engine.CryptoWeAPI, Auth: session.AuthAnonymous, }, Cache: &engine.CachePolicy[*mmpb.SubCountRequest]{ Key: func(req *mmpb.SubCountRequest) string { return fmt.Sprintf("user:subcount:%d", req.GetUserId()) }, TTL: 24 * time.Hour, }, NewResp: func() *mmpb.SubCountResponse { return &mmpb.SubCountResponse{} }, MapRequest: func(req *mmpb.SubCountRequest) (map[string]any, error) { return map[string]any{}, nil }, MapResponse: func(_ *mmpb.SubCountRequest, raw json.RawMessage) (*mmpb.SubCountResponse, error) { c, err := model.DecodeUserSubCount(raw) if err != nil { return nil, err } return &mmpb.SubCountResponse{Count: c}, nil }, }
SubCount 是获取用户数量统计的接口声明。
var UserPlaylist = &engine.Endpoint[*mmpb.UserPlaylistRequest, *mmpb.UserPlaylistResponse]{ Meta: engine.Meta{ Path: "/weapi/user/playlist", Method: "POST", Crypto: engine.CryptoWeAPI, Auth: session.AuthAnonymous, }, Cache: &engine.CachePolicy[*mmpb.UserPlaylistRequest]{ Key: func(req *mmpb.UserPlaylistRequest) string { return fmt.Sprintf("user:playlist:%d:%d:%d:%d", req.GetUserId(), req.GetFilter(), req.GetLimit(), req.GetOffset()) }, TTL: 24 * time.Hour, }, NewResp: func() *mmpb.UserPlaylistResponse { return &mmpb.UserPlaylistResponse{} }, MapRequest: func(req *mmpb.UserPlaylistRequest) (map[string]any, error) { limit := int(req.GetLimit()) if limit <= 0 { limit = 30 } return map[string]any{ "uid": fmt.Sprintf("%d", req.GetUserId()), "limit": limit, "offset": req.GetOffset(), }, nil }, MapResponse: func(req *mmpb.UserPlaylistRequest, raw json.RawMessage) (*mmpb.UserPlaylistResponse, error) { pls, total, err := model.DecodeUserPlaylists(raw, req.GetUserId(), req.GetFilter()) if err != nil { return nil, err } return &mmpb.UserPlaylistResponse{Playlists: pls, Total: total}, nil }, }
UserPlaylist 是获取用户歌单列表的接口声明。
filter(ALL/CREATED/SUBSCRIBED)在 MapResponse 内按 creator.userId == 请求 userId 判断, ownerUserID 直接取自请求,无需 service 层后置过滤。
Functions ¶
func FollowEachOtherMeta ¶
FollowEachOtherMeta 构造获取 target 关注列表的 Meta(uid 拼进 path)。
func FollowEachOtherRequest ¶
func FollowEachOtherRequest(_ *mmpb.FollowEachOtherRequest) map[string]any
FollowEachOtherRequest 构造关注列表入参(最多拉前 100 条做包含判断)。
func ParseFollowEachOther ¶
func ParseFollowEachOther(req *mmpb.FollowEachOtherRequest, raw json.RawMessage) (*mmpb.FollowEachOtherResponse, error)
ParseFollowEachOther 遍历响应的 follow 数组,判断是否包含 req.UserId。
Types ¶
This section is empty.