backend/app/zeus/service/lark/internal/server/user.go

64 lines
1.3 KiB
Go

package server
import (
"context"
lark2 "git.icechen.cn/monorepo/backend/app/zeus/service/lark/internal/lark"
"git.icechen.cn/monorepo/backend/app/zeus/service/lark/internal/model"
"git.icechen.cn/monorepo/backend/pkg/proto/zeus/lark"
"github.com/jinzhu/copier"
)
type UserServer struct{}
func (s UserServer) Login(ctx context.Context, in *lark.LoginRequest) (*lark.LoginResponse, error) {
userInfo, err := lark2.Client().Auth(in.Code)
if err != nil {
return nil, err
}
// userInfo to db
user := model.UserInfo{}
err = copier.Copy(&user, userInfo)
if err != nil {
return nil, err
}
err = model.CreateUser(&user)
if err != nil {
return nil, err
}
// gen token
token, err := model.CreateToken(user.ID)
if err != nil {
return nil, err
}
// return token
resp := &lark.LoginResponse{
Token: token,
}
return resp, nil
}
func (s UserServer) Info(ctx context.Context, in *lark.InfoRequest) (*lark.InfoResponse, error) {
// token to user info
_ = in.Token
info, err := getUserByToken(in.Token)
if err != nil {
return nil, err
}
resp := new(lark.InfoResponse)
err = copier.Copy(resp, info)
return resp, err
}
func getUserByToken(token string) (model.UserInfo, error) {
userToken, err := model.GetToken(token)
if err != nil {
return model.UserInfo{}, err
}
return model.GetUserByID(userToken.UserInfoID)
}