接口调整
This commit is contained in:
@@ -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 GetUserOrderListHandler(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.GetUserOrderListReq
|
||||
// 如果端点有请求结构体,则使用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.NewGetUserOrderListLogic(r.Context(), svcCtx)
|
||||
resp := l.GetUserOrderList(&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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
148
server/home-user-auth/internal/logic/getuserorderlistlogic.go
Normal file
148
server/home-user-auth/internal/logic/getuserorderlistlogic.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/format"
|
||||
"math"
|
||||
|
||||
"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 GetUserOrderListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetUserOrderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserOrderListLogic {
|
||||
return &GetUserOrderListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
if userinfo == nil || userinfo.UserId == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
|
||||
}
|
||||
|
||||
// 查询条件
|
||||
var page = req.Page
|
||||
var pageSize = req.PageSize
|
||||
var listRes []*gmodel.FsOrderRel
|
||||
rowBuilder = rowBuilder.Where("user_id =?", userinfo.UserId).Where("status =?", req.Status)
|
||||
|
||||
// 查询总数
|
||||
total, err := orderModel.FindCount(l.ctx, rowBuilder, nil)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
if total > 0 {
|
||||
rowBuilder = rowBuilder.Preload("FsOrderDetails", func(dbPreload *gorm.DB) *gorm.DB {
|
||||
return dbPreload.Table(orderDetailModel.TableName()).Preload("FsOrderDetailTemplateInfo").Preload("FsProductInfo")
|
||||
})
|
||||
listRes, err = orderModel.FindPageListByPage(l.ctx, rowBuilder, &page, &pageSize, nil, "")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info")
|
||||
}
|
||||
listResLen := len(listRes)
|
||||
|
||||
var respList []types.Items
|
||||
if listResLen > 0 {
|
||||
// 数据处理
|
||||
for _, item := range listRes {
|
||||
var pbData types.Items
|
||||
pbData.ID = item.Id
|
||||
pbData.Sn = *item.Sn
|
||||
pbData.UserID = *item.UserId
|
||||
pbData.TotalAmount = *item.TotalAmount
|
||||
pbData.Ctime = format.TimeIntToFormat(*item.Ctime)
|
||||
pbData.Status = *item.Status
|
||||
pbData.DeliveryMethod = *item.DeliveryMethod
|
||||
pbData.TsTime = format.TimeToFormat(*item.TsTime)
|
||||
pbData.IsPayCompleted = *item.IsPayCompleted
|
||||
pbData.DeliverSn = *item.DeliverSn
|
||||
|
||||
var pcsBox int64
|
||||
var pcs int64
|
||||
var productList []*types.Product
|
||||
if len(item.FsOrderDetails) > 0 {
|
||||
for _, fsOrderDetailItem := range item.FsOrderDetails {
|
||||
fsOrderDetailBuyNum := *fsOrderDetailItem.FsOrderDetail.BuyNum
|
||||
fsOrderDetailEachBoxNum := *fsOrderDetailItem.FsOrderDetailTemplateInfo.EachBoxNum
|
||||
pcs = pcs + fsOrderDetailBuyNum
|
||||
pcsBoxNum := fsOrderDetailBuyNum / fsOrderDetailEachBoxNum
|
||||
var csBoxNumF int64
|
||||
if (fsOrderDetailBuyNum % fsOrderDetailEachBoxNum) > 0 {
|
||||
csBoxNumF = 1
|
||||
}
|
||||
pcsBox = pcsBox + pcsBoxNum + csBoxNumF
|
||||
|
||||
var product types.Product
|
||||
product.Cover = *fsOrderDetailItem.Cover
|
||||
product.Fitting = *fsOrderDetailItem.OptionalTitle
|
||||
product.OptionPrice = *fsOrderDetailItem.OptionPrice
|
||||
product.OrderDetailTemplateId = *fsOrderDetailItem.OrderDetailTemplateId
|
||||
product.OrderId = *fsOrderDetailItem.OrderId
|
||||
product.Pcs = fsOrderDetailBuyNum
|
||||
product.PcsBox = pcsBox
|
||||
product.Price = *fsOrderDetailItem.FsOrderDetail.Amount
|
||||
product.ProductId = *fsOrderDetailItem.OptionPrice
|
||||
//product.Size = *fsOrderDetailItem.FsProductInfo.s
|
||||
product.Title = *fsOrderDetailItem.FsProductInfo.Title
|
||||
|
||||
productList = append(productList, &product)
|
||||
}
|
||||
pbData.ProductList = productList
|
||||
}
|
||||
|
||||
pbData.PcsBox = pcsBox
|
||||
pbData.Pcs = pcs
|
||||
pbData.SurplusAt = *item.Ctime + constants.CANCLE_ORDER_EXPIRE
|
||||
pbData.LogisticsStatus = 1
|
||||
pbData.Deposit = *item.TotalAmount / 2
|
||||
pbData.Remaining = pbData.Deposit
|
||||
respList = append(respList, pbData)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetUserOrderListRsp{
|
||||
Items: respList,
|
||||
Meta: types.Meta{
|
||||
TotalCount: total,
|
||||
PageCount: int64(math.Ceil(float64(total) / float64(pageSize))),
|
||||
CurrentPage: int(page),
|
||||
PerPage: int(pageSize),
|
||||
},
|
||||
})
|
||||
}
|
||||
60
server/home-user-auth/internal/logic/userordercancellogic.go
Normal file
60
server/home-user-auth/internal/logic/userordercancellogic.go
Normal file
@@ -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)
|
||||
}
|
||||
@@ -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"`
|
||||
|
||||
Reference in New Issue
Block a user