package handler import ( "context" "fmt" common "git.mashibing.com/msb_47094/shopping-comm" "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.Uuid) u.UserDataService.SetUserToken(strconv.Itoa(uuid), []byte(res), time.Duration(1)*time.Hour) fmt.Println(">>>>>>>>>>>>> GetUserToken success :", res) } else { resp.IsLogin = false resp.Token = "" } return nil }