From 4fe0c2acdbe4ed0e64c8f9d3b0c8eabb9171a0c7 Mon Sep 17 00:00:00 2001 From: shenzhuan Date: Mon, 24 Oct 2022 16:38:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E7=99=BB=E5=BD=95?= =?UTF-8?q?=20=E5=AD=98=E5=82=A8=E6=AD=A3=E7=A1=AE=E7=9A=84userId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shoppingCart-service/client/client.go | 33 +++++++--- shoppingCart-service/common/utils.go | 65 +++++++++++++++++++ .../domain/repository/cart_repository.go | 1 + 3 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 shoppingCart-service/common/utils.go diff --git a/shoppingCart-service/client/client.go b/shoppingCart-service/client/client.go index 54dbe59..9be6cfd 100644 --- a/shoppingCart-service/client/client.go +++ b/shoppingCart-service/client/client.go @@ -69,30 +69,43 @@ func main() { number, _ := strconv.Atoi(c.Request.FormValue("number")) productId, _ := strconv.Atoi(c.Request.FormValue("productId")) productSkuId, _ := strconv.Atoi(c.Request.FormValue("productSkuId")) - - //拼接请求信息 - req := &proto.AddCartReq{ - Number: int32(number), - ProductId: int32(productId), - ProductSkuId: int32(productSkuId), + uuid := c.Request.Header["Uuid"][0] + + //intUuid, _ := strconv.Atoi(uuid) + // intput := "101010101110110" + cc := common.GetInput(uuid) + out := common.SQ(cc) + sum := 0 + for o := range out { + sum += o } - resp, err := client.AddCart(context.TODO(), req) + //fmt.Println(sum) + //Token校验 //拼接请求信息 tokenReq := &proto.TokenReq{ - Uuid: c.Request.Header["Uuid"][0], + Uuid: uuid, } //响应 tokenResp, err := GetUserTokenClient.GetUserToken(context.TODO(), tokenReq) - + //拼接请求信息 + respErr := &proto.AddCartResp{} if err != nil || tokenResp.IsLogin == false { log.Println("GetUserToken err : ", err) - common.RespFail(c.Writer, resp, "未登录!") + common.RespFail(c.Writer, respErr, "未登录!") return } log.Println("GetUserToken success : ", tokenResp) + //拼接请求信息 + req := &proto.AddCartReq{ + Number: int32(number), + ProductId: int32(productId), + ProductSkuId: int32(productSkuId), + UserId: int32(sum), + } + resp, err := client.AddCart(context.TODO(), req) //商品详情 reqDetail := &proto.ProductDetailReq{ Id: int32(productId), diff --git a/shoppingCart-service/common/utils.go b/shoppingCart-service/common/utils.go new file mode 100644 index 0000000..d5a923f --- /dev/null +++ b/shoppingCart-service/common/utils.go @@ -0,0 +1,65 @@ +package common + +import ( + "math" + "strconv" +) + +/* + @Auth:ShenZ + @Description: 用于UUID 加密算法 +*/ + +func StringToArray(intput string) []int { + output := []int{} + for _, v := range intput { + output = append(output, int(v)) + } + for i, j := 0, len(output)-1; i < j; i, j = i+1, j-1 { + output[i], output[j] = output[j], output[i] + } + return output +} +func GetInput(intput string) <-chan int { + out := make(chan int) + go func() { + for _, b := range StringToArray(intput) { + out <- b + } + close(out) + }() + return out +} +func SQ(in <-chan int) <-chan int { + out := make(chan int) + var base, i float64 = 2, 0 + go func() { + for n := range in { + out <- (n - 48) * int(math.Pow(base, i)) + i++ + } + close(out) + }() + return out +} + +func ToInt(intput string) int { + //intput := "101010101110110" + c := GetInput(intput) + out := SQ(c) + sum := 0 + for o := range out { + sum += o + } + return sum +} + +// int 转 二进制的字符串 +func ConverToBinary(n int) string { + res := "" + for ; n > 0; n /= 2 { + lsb := n % 2 + res = strconv.Itoa(lsb) + res + } + return res +} diff --git a/shoppingCart-service/domain/repository/cart_repository.go b/shoppingCart-service/domain/repository/cart_repository.go index 1a48476..2162f1b 100644 --- a/shoppingCart-service/domain/repository/cart_repository.go +++ b/shoppingCart-service/domain/repository/cart_repository.go @@ -37,6 +37,7 @@ func (u *CartRepository) AddCart(req *proto.AddCartReq) (obj *model.ShoppingCart ProductSkuId: req.ProductSkuId, ProductName: req.ProductName, ProductMainPicture: req.ProductMainPicture, + UserId: req.UserId, } cart.CreateTime = time.Now() // u.mysqlDB.Create(&cart)