Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into feature/auth

This commit is contained in:
eson
2023-08-08 14:18:45 +08:00
115 changed files with 3189 additions and 504 deletions

View File

@@ -17,4 +17,13 @@ type Config struct {
Stripe struct {
SK string
}
PayConfig struct {
Stripe struct {
EndpointSecret string
Key string
CancelURL string
SuccessURL string
}
}
}

View File

@@ -72,6 +72,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/api/user/order-cancel",
Handler: UserOrderCancelHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/api/user/logo-list",
Handler: UserLogoListHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/api/user/one-more-order",
Handler: UserAgainOrderHandler(serverCtx),
},
},
)
}

View File

@@ -0,0 +1,35 @@
package handler
import (
"net/http"
"reflect"
"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 UserAgainOrderHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UserAgainOrderReq
userinfo, err := basic.RequestParse(w, r, svcCtx.SharedState, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewUserAgainOrderLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.UserAgainOrder(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@@ -0,0 +1,35 @@
package handler
import (
"net/http"
"reflect"
"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 UserLogoListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UserLogoListReq
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
return
}
// 创建一个业务逻辑层实例
l := logic.NewUserLogoListLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl)
resp := l.UserLogoList(&req, userinfo)
if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp)
}
}
}

View File

@@ -0,0 +1,144 @@
package logic
import (
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"time"
"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 UserAgainOrderLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserAgainOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAgainOrderLogic {
return &UserAgainOrderLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *UserAgainOrderLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UserAgainOrderLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *UserAgainOrderLogic) UserAgainOrder(req *types.UserAgainOrderReq, 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)
orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(l.svcCtx.MysqlConn)
fsOrderDetailModel := gmodel.NewFsOrderDetailModel(l.svcCtx.MysqlConn)
fsProductDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn)
rsbOrder := orderModel.RowSelectBuilder(nil)
rsbOrder = rsbOrder.Where("sn =?", req.Sn).Preload("FsOrderDetails")
rsbOrder = rsbOrder.Preload("FsOrderDetails", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(fsOrderDetailModel.TableName()).Preload("FsOrderDetailTemplateInfo", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(orderDetailTemplateModel.TableName()).Preload("FsProductDesignInfo", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(fsProductDesignModel.TableName())
})
})
})
fsOrderRelInfo, err := orderModel.FindOneByQuery(l.ctx, rsbOrder, 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 len(fsOrderRelInfo.FsOrderDetails) > 0 {
for _, fsOrderDetail := range fsOrderRelInfo.FsOrderDetails {
var isCheck int64 = 1
productDesignInfo := fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo
if productDesignInfo.Id != 0 {
// 查找是否有此材质、产品、大小id的阶梯价格
productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn)
priceStatus := int64(1)
priceReq := gmodel.FindOneProductPriceByParamsReq{
ProductId: productDesignInfo.ProductId,
MaterialId: productDesignInfo.MaterialId,
SizeId: productDesignInfo.SizeId,
Status: &priceStatus,
}
productPriceInfo, err := productPriceModel.FindOneProductPriceByParams(l.ctx, priceReq)
if err == nil && productPriceInfo.Id != 0 && *productPriceInfo.EachBoxNum > 0 {
// 买的数量和每箱数量取余为0 且 份数大于等于最小购买数量才算满足条件
if *fsOrderDetail.BuyNum%*productPriceInfo.EachBoxNum == 0 && int64(float64(*fsOrderDetail.BuyNum)/float64(*productPriceInfo.EachBoxNum)) >= *productPriceInfo.MinBuyNum {
// 查询购物车
cartModel := gmodel.NewFsCartModel(l.svcCtx.MysqlConn)
cartStatus := int64(1)
cartReq := gmodel.FindOneCartByParamsReq{
UserId: &userinfo.UserId,
ProductId: productDesignInfo.ProductId,
TemplateId: productDesignInfo.TemplateId,
PriceId: &productPriceInfo.Id,
DesignId: &productDesignInfo.Id,
MaterialId: productDesignInfo.MaterialId,
Status: &cartStatus,
}
cartInfo, err := cartModel.FindOneCartByParams(l.ctx, cartReq)
if err == nil && (err != nil && errors.Is(err, gorm.ErrRecordNotFound)) {
now := time.Now().Unix()
nowTime := time.Now()
data := gmodel.FsCart{
UserId: &userinfo.UserId,
ProductId: productPriceInfo.ProductId,
TemplateId: productDesignInfo.TemplateId,
PriceId: &productPriceInfo.Id,
MaterialId: productDesignInfo.MaterialId,
SizeId: productDesignInfo.SizeId,
BuyNum: fsOrderDetail.BuyNum,
Cover: productDesignInfo.Cover,
DesignId: &productDesignInfo.Id,
Ctime: &now,
Status: &cartStatus,
OptionalId: productDesignInfo.OptionalId,
IsCheck: &isCheck,
TsTime: &nowTime,
}
if cartInfo == nil {
err = cartModel.Create(l.ctx, data)
} else {
err = cartModel.Update(l.ctx, cartInfo.Id, data)
}
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to add to cart")
}
}
}
}
}
}
}
return resp.SetStatus(basic.CodeOK)
}

View File

@@ -0,0 +1,71 @@
package logic
import (
"errors"
"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 UserLogoListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserLogoListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogoListLogic {
return &UserLogoListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *UserLogoListLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UserLogoListLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *UserLogoListLogic) UserLogoList(req *types.UserLogoListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
// 定义用户ID
var userId int64
var guestId int64
// 检查用户是否是游客
if userinfo.IsGuest() {
// 如果是使用游客ID和游客键名格式
guestId = userinfo.GuestId
} else {
// 否则使用用户ID和用户键名格式
userId = userinfo.UserId
}
userMaterialModel := gmodel.NewFsUserMaterialModel(l.svcCtx.MysqlConn)
userMaterialRSB := userMaterialModel.RowSelectBuilder(nil).
Where("module = ?", "logo").Where("user_id = ?", userId).Where("guest_id = ?", guestId).Order("id desc")
list, err := userMaterialModel.FindAll(l.ctx, userMaterialRSB, nil, "")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "data not found")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get data list")
}
return resp.SetStatus(basic.CodeOK, map[string]interface{}{
"list": list,
})
}

View File

@@ -6,6 +6,8 @@ import (
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/handlers"
"time"
"context"
@@ -50,11 +52,76 @@ func (l *UserOrderCancelLogic) UserOrderCancel(req *types.UserOrderCancelReq, us
}
// 判断订单状态
if *orderInfo.Status == int64(constants.STATUS_NEW_NOT_PAY) {
} else {
var notCancelStatusMap = make(map[int64]struct{}, 3)
notCancelStatusMap[int64(constants.STATUS_NEW_NOT_PAY)] = struct{}{}
notCancelStatusMap[int64(constants.STATUS_NEW_PART_PAY)] = struct{}{}
notCancelStatusMap[int64(constants.STATUS_NEW_PAY_COMPLETED)] = struct{}{}
_, ok := notCancelStatusMap[int64(*orderInfo.Status)]
if !ok {
return resp.SetStatusWithMessage(basic.CodeOrderNotCancelledErr, "the order status not cancle")
}
var cancelTime int64 = time.Now().Unix() - (*orderInfo.Ctime + int64(constants.CANCLE_ORDER_EXPIRE))
// 第一次支付成功后48小时后不能进行取消操作
if *orderInfo.IsPayCompleted == 1 && cancelTime > 0 {
return resp.SetStatusWithMessage(basic.CodeOrderNotCancelledErr, "The current order cannot be cancelled")
}
// 修改订单--取消状态和取消原因
*orderInfo.Status = int64(constants.STATUS_NEW_CANCEL)
*orderInfo.IsCancel = 1
orderInfo.RefundReasonId = &req.RefundReasonId
orderInfo.RefundReason = &req.RefundReason
var nowTime = time.Now().Unix()
var payList []handlers.PayInfo
// 事务处理
err = orderModel.Trans(l.ctx, func(ctx context.Context, connGorm *gorm.DB) (err error) {
// 修改订单信息
orderModelTS := gmodel.NewFsOrderModel(connGorm)
err = orderModelTS.Update(ctx, orderInfo)
if err != nil {
return err
}
// 新增退款记录
var isRefund int64 = 0
refundReasonModelTS := gmodel.NewFsRefundReasonModel(connGorm)
refundReasonModelTS.CreateOrUpdate(ctx, &gmodel.FsRefundReason{
IsRefund: &isRefund,
RefundReasonId: &req.RefundReasonId,
RefundReason: &req.RefundReason,
OrderId: &orderInfo.Id,
CreatedAt: &nowTime,
})
// 退款申请
// 退款申请--查询支付信息
fsPayModelTS := gmodel.NewFsPayModel(connGorm)
rbFsPay := fsPayModelTS.RowSelectBuilder(nil).Where("order_number = ?", orderInfo.Sn).Where("pay_status =?", constants.PAYSTATUS_SUCCESS).Where("is_refund =?", 0)
payInfoList, err := fsPayModelTS.FindAll(ctx, rbFsPay, nil, "")
if err != nil {
return err
}
for _, payInfo := range payInfoList {
var key string
if *payInfo.PaymentMethod == int64(constants.PAYMETHOD_STRIPE) {
key = l.svcCtx.Config.PayConfig.Stripe.Key
}
payList = append(payList, handlers.PayInfo{
TradeNo: *payInfo.TradeNo,
PaymentMethod: *payInfo.PaymentMethod,
Key: key,
})
}
return nil
})
// 退款申请--调取第三方接口发起退款
handlers.PayRefundHandler(&handlers.PayRefundHandlerReq{
PayInfoList: payList,
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeOrderCancelledNotOk, "the order cancle failed")
}
return resp.SetStatus(basic.CodeOK)
}

View File

@@ -5,6 +5,19 @@ import (
"fusenapi/utils/basic"
)
type UserAgainOrderReq struct {
Sn string `form:"sn"` // 订单编号
}
type UserAgainOrderRes struct {
}
type UserLogoListReq struct {
}
type UserLogoListRes struct {
}
type UserOrderDeleteReq struct {
ID int64 `form:"id"` //订单id
}