From c5eadb85b4b0ef949dc9932ff6a2a4e5de4d7d7a Mon Sep 17 00:00:00 2001 From: wuchao <1272174216@qq.com> Date: Tue, 25 Jun 2024 13:51:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E8=81=94=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 1 + app/constants/errorcode/error_code.go | 3 +++ app/http/controllers/base.go | 5 +++-- app/http/controllers/front/OrderController.go | 6 ++---- app/http/entities/front/User.go | 2 +- app/http/middlewares/base.go | 2 +- app/http/routes/route.go | 4 ++-- app/services/UserService.go | 13 ++++++------- app/third/youchu/youchu_api.go | 1 + app/utils/util.go | 14 +++++++------- 10 files changed, 27 insertions(+), 24 deletions(-) diff --git a/Dockerfile b/Dockerfile index c31e551..eabfa31 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,7 @@ WORKDIR /app # 设置环境变量 ENV GOPROXY=https://goproxy.cn,direct +RUN go env -w GOPROXY=https://goproxy.cn,direct # 复制项目源码 COPY . . diff --git a/app/constants/errorcode/error_code.go b/app/constants/errorcode/error_code.go index f2c5082..ae555ae 100644 --- a/app/constants/errorcode/error_code.go +++ b/app/constants/errorcode/error_code.go @@ -46,6 +46,8 @@ const ( InsertUserFail = 3001 //邮储code解析失败 YouChuCodeFail = 4001 + //客户号为空 + YouChuCustNoEmpty = 4002 ) var MsgEN = map[int]string{ @@ -75,6 +77,7 @@ var MsgZH = map[int]string{ InsertUserFail: "用户新增失败", Fail: "请求失败", YouChuCodeFail: "Code解析失败", + YouChuCustNoEmpty: "客户编号为空", } var MsgMap map[string]map[int]string = map[string]map[int]string{"en": MsgZH} diff --git a/app/http/controllers/base.go b/app/http/controllers/base.go index adf4d9c..5949d62 100644 --- a/app/http/controllers/base.go +++ b/app/http/controllers/base.go @@ -185,8 +185,9 @@ func GetUserId(c *gin.Context) int { return 0 } -func GetUserPhone(c *gin.Context) string { - userIdStr, _ := c.Get("phone") +// GetUserCustNo 获取用户custNo +func GetUserCustNo(c *gin.Context) string { + userIdStr, _ := c.Get("custNo") if userIdStr != nil { var userId, _ = userIdStr.(string) return userId diff --git a/app/http/controllers/front/OrderController.go b/app/http/controllers/front/OrderController.go index 113efd2..d7631be 100644 --- a/app/http/controllers/front/OrderController.go +++ b/app/http/controllers/front/OrderController.go @@ -18,16 +18,14 @@ import ( func CreateOrder(c *gin.Context) { var request = controllers.GetRequest(c).(*front.OrderCreateRequest) - //userId := controllers.GetUserId(c) - userId := 1 + userId := controllers.GetUserId(c) code, data := services.CreateOrderService(userId, request.ProductId) controllers.HandCodeRes(c, data, code) } func OrderList(c *gin.Context) { var request = controllers.GetRequest(c).(*front.OrderListRequest) - //userId := controllers.GetUserId(c) - userId := 1 + userId := controllers.GetUserId(c) code, orderList, count := services.OrderQueryService(userId, request) var rsp []front.OrderListResponse if count > 0 { diff --git a/app/http/entities/front/User.go b/app/http/entities/front/User.go index 3519167..eb3a211 100644 --- a/app/http/entities/front/User.go +++ b/app/http/entities/front/User.go @@ -5,7 +5,7 @@ type UnionLoginRequest struct { } type LoginResponse struct { - Token string + Token string `json:"token"` } type YouChuDecryptData struct { diff --git a/app/http/middlewares/base.go b/app/http/middlewares/base.go index f992a6c..b6be469 100644 --- a/app/http/middlewares/base.go +++ b/app/http/middlewares/base.go @@ -31,7 +31,7 @@ func Auth() gin.HandlerFunc { } if err == nil { c.Set("userId", claims.Id) - c.Set("phone", claims.Phone) + c.Set("custNo", claims.CustNo) c.Next() return } else { diff --git a/app/http/routes/route.go b/app/http/routes/route.go index e92ca13..468d094 100644 --- a/app/http/routes/route.go +++ b/app/http/routes/route.go @@ -58,8 +58,8 @@ func RegisterRoute(router *gin.Engine) { v1.POST("/product/detail", front.ProductDetail) //auth - auth := v1.Group("auth") - //auth := v1.Group("auth", middlewares.Auth()) + //auth := v1.Group("auth") + auth := v1.Group("auth", middlewares.Auth()) { order := auth.Group("/order") { diff --git a/app/services/UserService.go b/app/services/UserService.go index 7dba61a..32cf6df 100644 --- a/app/services/UserService.go +++ b/app/services/UserService.go @@ -20,21 +20,20 @@ func YouChuLogin(req *front.UnionLoginRequest) (code int, login front.LoginRespo } if YouChuResponse.RespCode == "000000" { // 获取用户信息 - var user usersmodel.Users - err := usersmodel.GetInstance().GetDb().Where("status = ?", common.STATUSABLED).Where("custNo = ?", YouChuResponse.CustNo).Find(&user) - code = handErr(err) - if user.Id == "" { + user := usersmodel.Users{} + has, _ := usersmodel.GetInstance().GetDb().Where("status = ?", common.STATUSABLED).Where("custNo = ?", YouChuResponse.CustNo).Get(&user) + if !has && YouChuResponse.CustNo != "" { user.CustNo = YouChuResponse.CustNo user.Status = strconv.Itoa(common.STATUSABLED) user.CreateTime = time.Now() - _, err := usersmodel.GetInstance().GetDb().Where("status = ?", common.STATUSABLED).Insert(&user) + _, err := usersmodel.GetInstance().GetDb().Insert(&user) if err != nil { return errorcode.InsertUserFail, front.LoginResponse{} } } login.Token = utils.GeneratorJwtToken(utils.User{ - Id: user.Id, - Phone: user.CustNo, + Id: user.Id, + CustNo: user.CustNo, }) return errorcode.Success, login } else { diff --git a/app/third/youchu/youchu_api.go b/app/third/youchu/youchu_api.go index 7b61735..1eee353 100644 --- a/app/third/youchu/youchu_api.go +++ b/app/third/youchu/youchu_api.go @@ -39,6 +39,7 @@ func (this *YouChuClient) Login(code string) (Code int, response front.YouChuLog } requestData := EncryptRequest(request) bytes, err := json.Marshal(requestData) + utils.Log(nil, "YouChuLoginRequest", string(bytes)) if err != nil { return errorcode.SystemError, response } diff --git a/app/utils/util.go b/app/utils/util.go index e15b38d..43223de 100644 --- a/app/utils/util.go +++ b/app/utils/util.go @@ -372,8 +372,8 @@ func IsNil(x interface{}) bool { } type User struct { - Id string - Phone string + Id string + CustNo string } func GeneratorJwtToken(user User) string { @@ -382,9 +382,9 @@ func GeneratorJwtToken(user User) string { // 创建一个MapClaims对象,用于存放自定义的声明信息 claims := jwt.MapClaims{ - "id": user.Id, - "phone": user.Phone, - "exp": time.Now().Add(time.Hour * 24).Unix(), // 设置过期时间为24小时后 + "id": user.Id, + "custNo": user.CustNo, + "exp": time.Now().Add(time.Hour * 24).Unix(), // 设置过期时间为24小时后 } // 使用HS256算法创建一个Token对象 token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) @@ -399,8 +399,8 @@ func GeneratorJwtToken(user User) string { } type Claims struct { - Id int - Phone string + Id string + CustNo string jwt.StandardClaims }