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.
166 lines
4.1 KiB
166 lines
4.1 KiB
package service
|
|
|
|
import (
|
|
"context"
|
|
pb "customer/api/customer"
|
|
"customer/api/verifyCode"
|
|
"customer/internal/biz"
|
|
"customer/internal/data"
|
|
consul "github.com/go-kratos/kratos/contrib/registry/consul/v2"
|
|
"github.com/go-kratos/kratos/v2/middleware/auth/jwt"
|
|
"github.com/go-kratos/kratos/v2/selector"
|
|
"github.com/go-kratos/kratos/v2/selector/wrr"
|
|
"github.com/go-kratos/kratos/v2/transport/grpc"
|
|
jwtv4 "github.com/golang-jwt/jwt/v4"
|
|
"github.com/hashicorp/consul/api"
|
|
"regexp"
|
|
"time"
|
|
)
|
|
|
|
type CustomerService struct {
|
|
pb.UnimplementedCustomerServer
|
|
CD *data.CustomerData
|
|
cb *biz.CustomerBiz
|
|
}
|
|
|
|
func NewCustomerService(cd *data.CustomerData, cb *biz.CustomerBiz) *CustomerService {
|
|
return &CustomerService{
|
|
CD: cd,
|
|
cb: cb,
|
|
}
|
|
}
|
|
|
|
func (s *CustomerService) GetVerifyCode(ctx context.Context, req *pb.GetVerifyCodeReq) (*pb.GetVerifyCodeResp, error) {
|
|
pattern := regexp.MustCompile(`^(13\d|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18\d|19[0-35-9])\d{8}$`)
|
|
if !pattern.MatchString(req.Telephone) {
|
|
return &pb.GetVerifyCodeResp{
|
|
Code: 1,
|
|
Message: "电话号码格式错误",
|
|
}, nil
|
|
}
|
|
|
|
consulConfig := api.DefaultConfig()
|
|
consulConfig.Address = "localhost:8500"
|
|
consulClient, err := api.NewClient(consulConfig)
|
|
if err != nil {
|
|
//log.Fatal(err)
|
|
return &pb.GetVerifyCodeResp{
|
|
Code: 1,
|
|
Message: "验证码服务不可用",
|
|
}, nil
|
|
}
|
|
dis := consul.New(consulClient)
|
|
//selector.SetGlobalSelector(random.NewBuilder())
|
|
selector.SetGlobalSelector(wrr.NewBuilder())
|
|
//selector.SetGlobalSelector(p2c.NewBuilder())
|
|
endpoint := "discovery:///verifyCode"
|
|
conn, err := grpc.DialInsecure(
|
|
context.Background(),
|
|
grpc.WithEndpoint(endpoint),
|
|
grpc.WithDiscovery(dis),
|
|
//grpc.WithEndpoint("127.0.0.1:9000"),
|
|
)
|
|
if err != nil {
|
|
return &pb.GetVerifyCodeResp{
|
|
Code: 1,
|
|
Message: "验证码服务不可用",
|
|
}, nil
|
|
}
|
|
defer func() {
|
|
_ = conn.Close()
|
|
}()
|
|
client := verifyCode.NewVerifyCodeClient(conn)
|
|
|
|
reply, err := client.GetVerifyCode(
|
|
context.Background(),
|
|
&verifyCode.GetVerifyCodeRequest{
|
|
Length: 6,
|
|
Type: verifyCode.TYPE_DIGIT,
|
|
},
|
|
)
|
|
if err != nil {
|
|
return &pb.GetVerifyCodeResp{
|
|
Code: 1,
|
|
Message: "验证码服务异常",
|
|
}, nil
|
|
}
|
|
const life = 60
|
|
err = s.CD.SetVerifyCode(req.Telephone, reply.Code, life)
|
|
if err != nil {
|
|
return &pb.GetVerifyCodeResp{
|
|
Code: 1,
|
|
Message: "验证码获取失败",
|
|
}, nil
|
|
}
|
|
return &pb.GetVerifyCodeResp{
|
|
Code: 0,
|
|
VerifyCode: reply.Code,
|
|
VerifyCodeTime: time.Now().Unix(),
|
|
VerifyCodeLife: life,
|
|
}, nil
|
|
}
|
|
|
|
func (s *CustomerService) Login(ctx context.Context, req *pb.LoginReq) (*pb.LoginResp, error) {
|
|
code := s.CD.GetVerifyCode(req.Telephone)
|
|
if code == "" || code != req.VerifyCode {
|
|
return &pb.LoginResp{
|
|
Code: 1,
|
|
Message: "验证码不匹配",
|
|
}, nil
|
|
}
|
|
cusomer, err := s.CD.GetCusomerByTelephone(req.Telephone)
|
|
if err != nil {
|
|
return &pb.LoginResp{
|
|
Code: 1,
|
|
Message: "顾客信息获取错误",
|
|
}, nil
|
|
}
|
|
|
|
token, err := s.CD.GenerateTokenAndSave(cusomer, biz.CustomerDuration*time.Second, biz.CustomerSecret)
|
|
if err != nil {
|
|
return &pb.LoginResp{
|
|
Code: 1,
|
|
Message: "生成token失败",
|
|
}, nil
|
|
}
|
|
return &pb.LoginResp{
|
|
Code: 0,
|
|
Message: "登录成功",
|
|
Token: token,
|
|
TokenCreateAt: time.Now().Unix(),
|
|
TokenLife: biz.CustomerDuration,
|
|
}, nil
|
|
}
|
|
|
|
func (s *CustomerService) Logout(ctx context.Context, req *pb.LogoutReq) (*pb.LogoutResp, error) {
|
|
claims, _ := jwt.FromContext(ctx)
|
|
claimsMap := claims.(jwtv4.MapClaims)
|
|
if err := s.CD.DelToken(claimsMap["jti"]); err != nil {
|
|
return &pb.LogoutResp{
|
|
Code: 1,
|
|
Message: "Token删除失败",
|
|
}, nil
|
|
}
|
|
return &pb.LogoutResp{
|
|
Code: 0,
|
|
Message: "logout success",
|
|
}, nil
|
|
}
|
|
|
|
func (s *CustomerService) EstimatePrice(ctx context.Context, req *pb.EstimatePriceReq) (*pb.EstimatePriceResp, error) {
|
|
price, err := s.cb.GetEstimatePrice(req.Origin, req.Destination)
|
|
if err != nil {
|
|
return &pb.EstimatePriceResp{
|
|
Code: 1,
|
|
Message: err.Error(),
|
|
}, nil
|
|
}
|
|
return &pb.EstimatePriceResp{
|
|
Code: 0,
|
|
Message: "SUCCESS",
|
|
Origin: req.Origin,
|
|
Destination: req.Destination,
|
|
Price: price,
|
|
}, nil
|
|
}
|