api
This commit is contained in:
@@ -16,8 +16,6 @@ import (
|
||||
"portal-mini-go/internal/repository/user"
|
||||
)
|
||||
|
||||
const sessionKeyPrefix = "ma:session:"
|
||||
|
||||
// Service app1 业务服务,依赖用户仓储、小程序仓储、微信客户端与会话存储。
|
||||
type Service struct {
|
||||
userRepo *user.Repository
|
||||
@@ -49,12 +47,46 @@ func (s *Service) Login(ctx context.Context, appID, code string) (string, *model
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if err := s.store.Set(ctx, sessionKeyPrefix+token, u.ID, 7*24*time.Hour); err != nil {
|
||||
if err := s.store.Set(ctx, session.MaSessionKey(token), u.ID, 7*24*time.Hour); err != nil {
|
||||
return "", nil, errors.Wrap(50000, "save session", err)
|
||||
}
|
||||
return token, u, nil
|
||||
}
|
||||
|
||||
// GetProfile 获取当前登录用户资料(绑定到 mini_id,杜绝跨小程序访问)。
|
||||
func (s *Service) GetProfile(ctx context.Context, miniID, userID uint64) (*model.User, error) {
|
||||
return s.userRepo.GetByID(ctx, miniID, userID)
|
||||
}
|
||||
|
||||
// UpdateProfile 更新当前登录用户的昵称/头像,为空字段不更新。
|
||||
func (s *Service) UpdateProfile(ctx context.Context, miniID, userID uint64, nickname, avatar string) error {
|
||||
return s.userRepo.UpdateProfile(ctx, miniID, userID, nickname, avatar)
|
||||
}
|
||||
|
||||
// GetUser 获取同小程序下指定用户(对外仅暴露非敏感字段,由 handler 控制)。
|
||||
func (s *Service) GetUser(ctx context.Context, miniID, id uint64) (*model.User, error) {
|
||||
return s.userRepo.GetByID(ctx, miniID, id)
|
||||
}
|
||||
|
||||
// ListUsers 分页列出同小程序下的用户。
|
||||
func (s *Service) ListUsers(ctx context.Context, miniID uint64, page, size int) ([]model.User, int64, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if size < 1 || size > 100 {
|
||||
size = 20
|
||||
}
|
||||
return s.userRepo.List(ctx, miniID, page, size)
|
||||
}
|
||||
|
||||
// Logout 注销当前用户会话(删除会话 token)。
|
||||
func (s *Service) Logout(ctx context.Context, token string) error {
|
||||
if err := s.store.Delete(ctx, session.MaSessionKey(token)); err != nil {
|
||||
return errors.Wrap(50000, "delete session", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newSessionToken() (string, error) {
|
||||
b := make([]byte, 24)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user