From 92110e02a6bd619dc214295709eaaaf2c5db402f Mon Sep 17 00:00:00 2001 From: Hiven Date: Thu, 20 Jul 2023 15:43:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/getuserorderlisthandler.go | 6 +- .../home-user-auth/internal/handler/routes.go | 10 ++ .../handler/userordercancelhandler.go | 78 +++++++++++++ .../internal/logic/getuserorderlistlogic.go | 8 +- .../internal/logic/userordercancellogic.go | 60 ++++++++++ server/home-user-auth/internal/types/types.go | 64 +++++++++++ server/orders/internal/handler/routes.go | 5 - server/orders/internal/types/types.go | 41 ------- server_api/home-user-auth.api | 107 +++++++++++++++--- server_api/orders.api | 48 -------- 10 files changed, 312 insertions(+), 115 deletions(-) rename server/{orders => home-user-auth}/internal/handler/getuserorderlisthandler.go (93%) create mode 100644 server/home-user-auth/internal/handler/userordercancelhandler.go rename server/{orders => home-user-auth}/internal/logic/getuserorderlistlogic.go (97%) create mode 100644 server/home-user-auth/internal/logic/userordercancellogic.go diff --git a/server/orders/internal/handler/getuserorderlisthandler.go b/server/home-user-auth/internal/handler/getuserorderlisthandler.go similarity index 93% rename from server/orders/internal/handler/getuserorderlisthandler.go rename to server/home-user-auth/internal/handler/getuserorderlisthandler.go index 89309f16..9191d130 100644 --- a/server/orders/internal/handler/getuserorderlisthandler.go +++ b/server/home-user-auth/internal/handler/getuserorderlisthandler.go @@ -10,9 +10,9 @@ import ( "fusenapi/utils/auth" "fusenapi/utils/basic" - "fusenapi/server/orders/internal/logic" - "fusenapi/server/orders/internal/svc" - "fusenapi/server/orders/internal/types" + "fusenapi/server/home-user-auth/internal/logic" + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" ) func GetUserOrderListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index 46030762..9d1c303a 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -72,6 +72,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/user/oauth2/login/google", Handler: UserGoogleLoginHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/user/order-list", + Handler: GetUserOrderListHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/api/user/order-cancel", + Handler: UserOrderCancelHandler(serverCtx), + }, }, ) } diff --git a/server/home-user-auth/internal/handler/userordercancelhandler.go b/server/home-user-auth/internal/handler/userordercancelhandler.go new file mode 100644 index 00000000..04425eae --- /dev/null +++ b/server/home-user-auth/internal/handler/userordercancelhandler.go @@ -0,0 +1,78 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/zeromicro/go-zero/core/logx" + "github.com/zeromicro/go-zero/rest/httpx" + + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "fusenapi/server/home-user-auth/internal/logic" + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" +) + +func UserOrderCancelHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 + }) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 + return + } + + if claims != nil { + // 从token中获取对应的用户信息 + userinfo, err = auth.GetUserInfoFormMapClaims(claims) + // 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息 + if err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 401, + Message: "unauthorized", + }) + logx.Info("unauthorized:", err.Error()) + return + } + } else { + // 如果claims为nil,则认为用户身份为白板用户 + userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} + } + + var req types.UserOrderCancelReq + // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 + if err := httpx.Parse(r, &req); err != nil { + httpx.OkJsonCtx(r.Context(), w, &basic.Response{ + Code: 510, + Message: "parameter error", + }) + logx.Info(err) + return + } + // 创建一个业务逻辑层实例 + l := logic.NewUserOrderCancelLogic(r.Context(), svcCtx) + resp := l.UserOrderCancel(&req, userinfo) + // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; + if resp != nil { + httpx.OkJsonCtx(r.Context(), w, resp) + } else { + err := errors.New("server logic is error, resp must not be nil") + httpx.ErrorCtx(r.Context(), w, err) + logx.Error(err) + } + } +} diff --git a/server/orders/internal/logic/getuserorderlistlogic.go b/server/home-user-auth/internal/logic/getuserorderlistlogic.go similarity index 97% rename from server/orders/internal/logic/getuserorderlistlogic.go rename to server/home-user-auth/internal/logic/getuserorderlistlogic.go index 28f8e4a8..7503c8b1 100644 --- a/server/orders/internal/logic/getuserorderlistlogic.go +++ b/server/home-user-auth/internal/logic/getuserorderlistlogic.go @@ -1,7 +1,6 @@ package logic import ( - "context" "errors" "fusenapi/constants" "fusenapi/model/gmodel" @@ -10,8 +9,10 @@ import ( "fusenapi/utils/format" "math" - "fusenapi/server/orders/internal/svc" - "fusenapi/server/orders/internal/types" + "context" + + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" @@ -34,6 +35,7 @@ func NewGetUserOrderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) * func (l *GetUserOrderListLogic) GetUserOrderList(req *types.GetUserOrderListReq, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null + orderDetailModel := gmodel.NewFsOrderDetailModel(l.svcCtx.MysqlConn) orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn) rowBuilder := orderModel.RowSelectBuilder(nil) diff --git a/server/home-user-auth/internal/logic/userordercancellogic.go b/server/home-user-auth/internal/logic/userordercancellogic.go new file mode 100644 index 00000000..56a9ab4a --- /dev/null +++ b/server/home-user-auth/internal/logic/userordercancellogic.go @@ -0,0 +1,60 @@ +package logic + +import ( + "errors" + "fusenapi/constants" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/home-user-auth/internal/svc" + "fusenapi/server/home-user-auth/internal/types" + + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" +) + +type UserOrderCancelLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewUserOrderCancelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOrderCancelLogic { + return &UserOrderCancelLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *UserOrderCancelLogic) UserOrderCancel(req *types.UserOrderCancelReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + if userinfo == nil || userinfo.UserId == 0 { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found") + } + + //查询订单信息 + orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn) + orderInfo, err := orderModel.FindOne(l.ctx, userinfo.UserId, req.ID) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the order is not exists") + } + logx.Error(err) + return resp.SetStatus(basic.CodeServiceErr, "failed to get order info") + } + + // 判断订单状态 + if *orderInfo.Status == int64(constants.STATUS_NEW_NOT_PAY) { + + } else { + return resp.SetStatusWithMessage(basic.CodeOrderNotCancelledErr, "the order status not cancle") + } + + return resp.SetStatus(basic.CodeOK) +} diff --git a/server/home-user-auth/internal/types/types.go b/server/home-user-auth/internal/types/types.go index 5b0a384f..a6163430 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -5,6 +5,70 @@ import ( "fusenapi/utils/basic" ) +type UserOrderCancelReq struct { + ID int64 `form:"id"` //订单id + RefundReasonId int64 `form:"refund_reason_id"` //退款配置id + RefundReason string `form:"refund_reason,optional"` //退款原因 +} + +type UserOrderCancelRsp struct { +} + +type GetUserOrderListReq struct { + Page int64 `form:"page"` // 分页 + PageSize int64 `form:"page_size"` // 每页数量 + Status int64 `form:"status"` // 状态筛选 + Time int64 `form:"time"` // 时间筛选 + Total int64 `form:"total"` // 总数 + Size int64 `form:"size"` // 图片尺寸 +} + +type GetUserOrderListRsp struct { + Items []Items `json:"items"` + Meta Meta `json:"_meta"` +} + +type Items struct { + ID int64 `json:"id"` + Sn string `json:"sn"` + UserID int64 `json:"user_id"` + TotalAmount int64 `json:"total_amount"` + Ctime string `json:"ctime"` + Status int64 `json:"status"` + DeliveryMethod int64 `json:"delivery_method"` + TsTime string `json:"ts_time"` + IsPayCompleted int64 `json:"is_pay_completed"` + DeliverSn string `json:"deliver_sn"` + PcsBox int64 `json:"pcs_box"` + Pcs int64 `json:"pcs"` + SurplusAt int64 `json:"surplus_at"` + LogisticsStatus int64 `json:"logistics_status"` + StatusTimes []*StatusTime `json:"status_times"` + Deposit int64 `json:"deposit"` + Remaining int64 `json:"remaining"` + ProductList []*Product `json:"productList"` + IsStop int64 `json:"is_stop"` +} + +type StatusTime struct { + Key int `json:"key"` + Time string `json:"time"` +} + +type Product struct { + Cover string `json:"cover"` + Fitting string `json:"fitting"` + OptionPrice int64 `json:"option_price"` + OrderDetailTemplateId int64 `json:"order_detail_template_id"` + OrderId int64 `json:"order_id"` + Pcs int64 `json:"pcs"` + PcsBox int64 `json:"pcs_box"` + Price int64 `json:"price"` + ProductId int64 `json:"product_id"` + Size string `json:"size"` + Title string `json:"title"` +} + type RequestGoogleLogin struct { Code string `form:"code"` Scope string `form:"scope"` diff --git a/server/orders/internal/handler/routes.go b/server/orders/internal/handler/routes.go index e22a9566..467c0530 100644 --- a/server/orders/internal/handler/routes.go +++ b/server/orders/internal/handler/routes.go @@ -22,11 +22,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/order/detail", Handler: GetOrderDetailHandler(serverCtx), }, - { - Method: http.MethodGet, - Path: "/api/user/order-list", - Handler: GetUserOrderListHandler(serverCtx), - }, }, ) } diff --git a/server/orders/internal/types/types.go b/server/orders/internal/types/types.go index ca77da8f..703e7835 100644 --- a/server/orders/internal/types/types.go +++ b/server/orders/internal/types/types.go @@ -76,47 +76,6 @@ type Deposit struct { TransNo string `json:"trans_no"` } -type GetUserOrderListReq struct { - Page int64 `form:"page"` // 分页 - PageSize int64 `form:"page_size"` // 每页数量 - Status int64 `form:"status"` // 状态筛选 - Time int64 `form:"time"` // 时间筛选 - Total int64 `form:"total"` // 总数 - Size int64 `form:"size"` // 图片尺寸 -} - -type GetUserOrderListRsp struct { - Items []Items `json:"items"` - Meta Meta `json:"_meta"` -} - -type StatusTime struct { - Key int `json:"key"` - Time string `json:"time"` -} - -type Items struct { - ID int64 `json:"id"` - Sn string `json:"sn"` - UserID int64 `json:"user_id"` - TotalAmount int64 `json:"total_amount"` - Ctime string `json:"ctime"` - Status int64 `json:"status"` - DeliveryMethod int64 `json:"delivery_method"` - TsTime string `json:"ts_time"` - IsPayCompleted int64 `json:"is_pay_completed"` - DeliverSn string `json:"deliver_sn"` - PcsBox int64 `json:"pcs_box"` - Pcs int64 `json:"pcs"` - SurplusAt int64 `json:"surplus_at"` - LogisticsStatus int64 `json:"logistics_status"` - StatusTimes []*StatusTime `json:"status_times"` - Deposit int64 `json:"deposit"` - Remaining int64 `json:"remaining"` - ProductList []*Product `json:"productList"` - IsStop int64 `json:"is_stop"` -} - type Request struct { } diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index 36a5e192..cb4422cb 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -10,49 +10,126 @@ info ( import "basic.api" service home-user-auth { - + // @handler UserRegisterHandler // post /api/user/register(RequestUserRegister) returns (response); - + @handler UserLoginHandler post /api/user/login(RequestUserLogin) returns (response); - + @handler AcceptCookieHandler post /api/user/accept-cookie(request) returns (response); - + @handler UserFontsHandler get /api/user/fonts(request) returns (response); - + @handler UserGetTypeHandler get /api/user/get-type(request) returns (response); - + @handler UserSaveBasicInfoHandler post /api/user/basic-info(RequestBasicInfoForm) returns (response); - + @handler UserStatusConfigHandler get /api/user/status-config(request) returns (response); - + @handler UserBasicInfoHandler get /api/user/basic-info(request) returns (response); - + @handler UserAddressListHandler get /api/user/address-list(request) returns (response); - + @handler UserAddAddressHandler post /api/user/add-address(RequestAddAddress) returns (response); - + @handler UserContactServiceHandler post /api/user/contact-service (RequestContactService) returns (response); - + // @handler UserOderListHandler // get /api/user/order-list(RequestOrderId) returns (response); - + @handler UserOderDeleteHandler post /api/user/order-delete(RequestOrderId) returns (response); - + @handler UserGoogleLoginHandler get /api/user/oauth2/login/google(RequestGoogleLogin) returns (response); - + + //获取订单列表 + @handler GetUserOrderListHandler + get /api/user/order-list (GetUserOrderListReq) returns (response); + + //取消订单 + @handler UserOrderCancelHandler + get /api/user/order-cancel (UserOrderCancelReq) returns (response); + +} + +//取消订单 +type ( + UserOrderCancelReq { + ID int64 `form:"id"` //订单id + RefundReasonId int64 `form:"refund_reason_id"` //退款配置id + RefundReason string `form:"refund_reason,optional"` //退款原因 + } + UserOrderCancelRsp { + } +) + +// 获取订单列表 +type ( + GetUserOrderListReq { + Page int64 `form:"page"` // 分页 + PageSize int64 `form:"page_size"` // 每页数量 + Status int64 `form:"status"` // 状态筛选 + Time int64 `form:"time"` // 时间筛选 + Total int64 `form:"total"` // 总数 + Size int64 `form:"size"` // 图片尺寸 + } + + GetUserOrderListRsp { + Items []Items `json:"items"` + Meta Meta `json:"_meta"` + } +) + +type Items { + ID int64 `json:"id"` + Sn string `json:"sn"` + UserID int64 `json:"user_id"` + TotalAmount int64 `json:"total_amount"` + Ctime string `json:"ctime"` + Status int64 `json:"status"` + DeliveryMethod int64 `json:"delivery_method"` + TsTime string `json:"ts_time"` + IsPayCompleted int64 `json:"is_pay_completed"` + DeliverSn string `json:"deliver_sn"` + PcsBox int64 `json:"pcs_box"` + Pcs int64 `json:"pcs"` + SurplusAt int64 `json:"surplus_at"` + LogisticsStatus int64 `json:"logistics_status"` + StatusTimes []*StatusTime `json:"status_times"` + Deposit int64 `json:"deposit"` + Remaining int64 `json:"remaining"` + ProductList []*Product `json:"productList"` + IsStop int64 `json:"is_stop"` +} + +type StatusTime { + Key int `json:"key"` + Time string `json:"time"` +} + +type Product { + Cover string `json:"cover"` + Fitting string `json:"fitting"` + OptionPrice int64 `json:"option_price"` + OrderDetailTemplateId int64 `json:"order_detail_template_id"` + OrderId int64 `json:"order_id"` + Pcs int64 `json:"pcs"` + PcsBox int64 `json:"pcs_box"` + Price int64 `json:"price"` + ProductId int64 `json:"product_id"` + Size string `json:"size"` + Title string `json:"title"` } type RequestGoogleLogin { diff --git a/server_api/orders.api b/server_api/orders.api index f2fd298c..3bb63dde 100644 --- a/server_api/orders.api +++ b/server_api/orders.api @@ -16,10 +16,6 @@ service orders { //获取订单详情 @handler GetOrderDetailHandler get /api/order/detail (GetOrderDetailReq) returns (response); - - //获取订单列表 - @handler GetUserOrderListHandler - get /api/user/order-list (GetUserOrderListReq) returns (response); } //获取订单发票 @@ -86,48 +82,4 @@ type PayInfo { type Deposit { Method string `json:"method"` TransNo string `json:"trans_no"` -} - -// 获取订单列表 -type ( - GetUserOrderListReq { - Page int64 `form:"page"` // 分页 - PageSize int64 `form:"page_size"` // 每页数量 - Status int64 `form:"status"` // 状态筛选 - Time int64 `form:"time"` // 时间筛选 - Total int64 `form:"total"` // 总数 - Size int64 `form:"size"` // 图片尺寸 - } - - GetUserOrderListRsp { - Items []Items `json:"items"` - Meta Meta `json:"_meta"` - } -) - -type StatusTime { - Key int `json:"key"` - Time string `json:"time"` -} - -type Items { - ID int64 `json:"id"` - Sn string `json:"sn"` - UserID int64 `json:"user_id"` - TotalAmount int64 `json:"total_amount"` - Ctime string `json:"ctime"` - Status int64 `json:"status"` - DeliveryMethod int64 `json:"delivery_method"` - TsTime string `json:"ts_time"` - IsPayCompleted int64 `json:"is_pay_completed"` - DeliverSn string `json:"deliver_sn"` - PcsBox int64 `json:"pcs_box"` - Pcs int64 `json:"pcs"` - SurplusAt int64 `json:"surplus_at"` - LogisticsStatus int64 `json:"logistics_status"` - StatusTimes []*StatusTime `json:"status_times"` - Deposit int64 `json:"deposit"` - Remaining int64 `json:"remaining"` - ProductList []*Product `json:"productList"` - IsStop int64 `json:"is_stop"` } \ No newline at end of file