You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.0 KiB

package handler
import (
"context"
"fmt"
"gouser/common"
"gouser/domain/model"
"gouser/domain/service"
"gouser/proto"
"log"
"strconv"
"time"
)
type User struct {
UserDataService service.IUserDataService
}
// 登录 (clientId int32, phone string, systemId int32, verifi
func (u *User) Login(ctx context.Context, loginRequest *proto.LoginRequest, loginResp *proto.LoginResp) error {
userInfo, err := u.UserDataService.Login(loginRequest.ClientId, loginRequest.GetPhone(), loginRequest.SystemId, loginRequest.VerificationCode)
if err != nil {
return err
}
fmt.Println(">>>>>>>>>>>>> login success :", userInfo)
UserForResp(userInfo, loginResp)
u.UserDataService.SetUserToken(strconv.Itoa(int(userInfo.ID)), []byte(loginResp.Token), time.Duration(1)*time.Hour)
return nil
}
func UserForResp(userModel *model.User, resp *proto.LoginResp) *proto.LoginResp {
timeStr := fmt.Sprintf("%d", time.Now().Unix())
resp.Token = common.Md5Encode(timeStr) //"123456"
resp.User = &proto.User{}
log.Println(userModel)
resp.User.Id = userModel.ID
resp.User.Avatar = userModel.Avatar
resp.User.ClientId = userModel.ClientId
resp.User.EmployeeId = 1 //userModel.EmployeeId
resp.User.Nickname = userModel.Nickname
resp.User.SessionId = resp.Token
resp.User.Phone = userModel.Phone
//token 过期时间
tp, _ := time.ParseDuration("1h")
tokenExpireTime := time.Now().Add(tp)
expiretimeStr := tokenExpireTime.Format("2006-01-02 15:04:05")
resp.User.TokenExpireTime = expiretimeStr
resp.User.UnionId = userModel.UnionId
return resp
}
func (u *User) GetUserToken(ctx context.Context, req *proto.TokenReq, resp *proto.TokenResp) error {
res := u.UserDataService.GetUserToken(req.GetUuid())
if res != "" {
resp.IsLogin = true
resp.Token = res
uuid := common.ToInt(req.GetUuid())
fmt.Println(">>>>>>>>>>>>> GetUserToken success :", res)
//续命机制
u.UserDataService.SetUserToken(strconv.Itoa(uuid), []byte(res), time.Duration(1)*time.Hour)
} else {
resp.IsLogin = false
resp.Token = ""
}
return nil
}