分布式登录 存储正确的userId

master
shenzhuan 2 years ago
parent 4117638ad4
commit 4fe0c2acdb

@ -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),

@ -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
}

@ -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)

Loading…
Cancel
Save