Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into feature/auth
This commit is contained in:
@@ -62,6 +62,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
Path: "/api/user/order-list",
|
||||
Handler: UserOrderListHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/user/order-delete",
|
||||
Handler: UserOrderDeleteHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/user/order-cancel",
|
||||
|
||||
@@ -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 UserOrderDeleteHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.UserOrderDeleteReq
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewUserOrderDeleteLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.UserOrderDelete(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
78
server/home-user-auth/internal/logic/userorderdeletelogic.go
Normal file
78
server/home-user-auth/internal/logic/userorderdeletelogic.go
Normal file
@@ -0,0 +1,78 @@
|
||||
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 UserOrderDeleteLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUserOrderDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserOrderDeleteLogic {
|
||||
return &UserOrderDeleteLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *UserOrderDeleteLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *UserOrderDeleteLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *UserOrderDeleteLogic) UserOrderDelete(req *types.UserOrderDeleteReq, 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, "order not found")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get order info")
|
||||
}
|
||||
|
||||
updateStatusMap := make(map[constants.Order]struct{}, 4)
|
||||
updateStatusMap[constants.STATUS_NEW_COMPLETED] = struct{}{}
|
||||
updateStatusMap[constants.STATUS_NEW_CANCEL] = struct{}{}
|
||||
updateStatusMap[constants.STATUS_NEW_REFUNDED] = struct{}{}
|
||||
updateStatusMap[constants.STATUS_NEW_CLOSE] = struct{}{}
|
||||
if _, ok := updateStatusMap[constants.Order(*orderInfo.Status)]; !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order not found")
|
||||
}
|
||||
*orderInfo.Status = int64(constants.STATUS_NEW_DELETE)
|
||||
*orderInfo.IsDeleted = 1
|
||||
err = orderModel.Update(l.ctx, orderInfo)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "fail to delete")
|
||||
}
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
@@ -2,10 +2,14 @@ package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/configs"
|
||||
"fusenapi/utils/image"
|
||||
"strings"
|
||||
|
||||
"fusenapi/utils/format"
|
||||
"fusenapi/utils/order"
|
||||
@@ -39,7 +43,15 @@ func (l *UserOrderListLogic) UserOrderList(req *types.UserOrderListReq, userinfo
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
size := req.Size
|
||||
|
||||
if size > 0 {
|
||||
size = int64(image.GetCurrentSize(uint32(size)))
|
||||
}
|
||||
|
||||
orderDetailModel := gmodel.NewFsOrderDetailModel(l.svcCtx.MysqlConn)
|
||||
orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(l.svcCtx.MysqlConn)
|
||||
fsProductDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn)
|
||||
orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn)
|
||||
rowBuilder := orderModel.RowSelectBuilder(nil)
|
||||
if userinfo == nil || userinfo.UserId == 0 {
|
||||
@@ -50,10 +62,39 @@ func (l *UserOrderListLogic) UserOrderList(req *types.UserOrderListReq, userinfo
|
||||
var page = req.Page
|
||||
var pageSize = req.PageSize
|
||||
var listRes []*gmodel.FsOrderRel
|
||||
rowBuilder = rowBuilder.Where("user_id =?", userinfo.UserId)
|
||||
rowBuilder = rowBuilder.Where("user_id =?", userinfo.UserId).Where("status <> ?", constants.STATUS_NEW_NOT_PAY).Where("status <>?", constants.STATUS_NEW_DELETE)
|
||||
|
||||
// 根据时间来查询不同范围的订单
|
||||
switch req.Time {
|
||||
case 1:
|
||||
rowBuilder = rowBuilder.Where("ctime >?", time.Now().AddDate(0, -1, 0).Unix())
|
||||
case 2:
|
||||
rowBuilder = rowBuilder.Where("ctime >?", time.Now().AddDate(0, -3, 0).Unix())
|
||||
case 3:
|
||||
rowBuilder = rowBuilder.Where("ctime >?", time.Now().AddDate(0, -6, 0).Unix())
|
||||
case 4:
|
||||
rowBuilder = rowBuilder.Where("ctime >?", time.Now().AddDate(-1, 0, 0).Unix())
|
||||
default:
|
||||
}
|
||||
|
||||
//按照订单状态查询不同的订单
|
||||
if req.Status != -1 {
|
||||
rowBuilder = rowBuilder.Where("status = ?", req.Status)
|
||||
switch req.Status {
|
||||
case 1:
|
||||
rowBuilder = rowBuilder.Where("status in ?", [3]constants.Order{constants.STATUS_NEW_PART_PAY, constants.STATUS_NEW_PAY_COMPLETED, constants.STATUS_NEW_SURE})
|
||||
case 2:
|
||||
rowBuilder = rowBuilder.Where("status in ?", [2]constants.Order{constants.STATUS_NEW_PRODUTING, constants.STATUS_NEW_PRODUT_COMPLETED})
|
||||
case 3:
|
||||
rowBuilder = rowBuilder.Where("status in ?", [2]constants.Order{constants.STATUS_NEW_DELIVER, constants.STATUS_NEW_UPS})
|
||||
case 4:
|
||||
rowBuilder = rowBuilder.Where("status =?", constants.STATUS_NEW_ARRIVAL)
|
||||
case 5:
|
||||
rowBuilder = rowBuilder.Where("status =?", constants.STATUS_NEW_COMPLETED).Where("delivery_method =?", constants.DELIVERY_METHOD_ADDRESS)
|
||||
case 7:
|
||||
rowBuilder = rowBuilder.Where("status in ?", [3]constants.Order{constants.STATUS_NEW_CANCEL, constants.STATUS_NEW_REFUNDED, constants.STATUS_NEW_REFUNDING})
|
||||
case 8:
|
||||
rowBuilder = rowBuilder.Where("status =?", constants.STATUS_NEW_COMPLETED).Where("delivery_method =?", constants.DELIVERY_METHOD_CLOUD)
|
||||
}
|
||||
}
|
||||
|
||||
// 查询总数
|
||||
@@ -69,7 +110,11 @@ func (l *UserOrderListLogic) UserOrderList(req *types.UserOrderListReq, userinfo
|
||||
// 查询数据
|
||||
if total > 0 {
|
||||
rowBuilder = rowBuilder.Preload("FsOrderAffiliateInfo").Preload("FsOrderDetails", func(dbPreload *gorm.DB) *gorm.DB {
|
||||
return dbPreload.Table(orderDetailModel.TableName()).Preload("FsOrderDetailTemplateInfo").Preload("FsProductInfo")
|
||||
return dbPreload.Table(orderDetailModel.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()).Preload("OptionData").Preload("TemplateData")
|
||||
}).Preload("FsProductSizeInfo")
|
||||
}).Preload("FsProductInfo")
|
||||
})
|
||||
listRes, err = orderModel.FindPageListByPage(l.ctx, rowBuilder, &page, &pageSize, nil, "")
|
||||
}
|
||||
@@ -86,10 +131,10 @@ func (l *UserOrderListLogic) UserOrderList(req *types.UserOrderListReq, userinfo
|
||||
var respList []types.Items
|
||||
if listResLen > 0 {
|
||||
// 获取订单时间配置
|
||||
// orderTimeConfig, err := configs.GetOrderTimeConfig(l.ctx, l.svcCtx.MysqlConn)
|
||||
// if err != nil {
|
||||
// return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get config time info")
|
||||
// }
|
||||
orderTimeConfig, err := configs.GetOrderTimeConfig(l.ctx, l.svcCtx.MysqlConn)
|
||||
if err != nil {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get config time info")
|
||||
}
|
||||
|
||||
// 数据处理
|
||||
for _, item := range listRes {
|
||||
@@ -107,7 +152,7 @@ func (l *UserOrderListLogic) UserOrderList(req *types.UserOrderListReq, userinfo
|
||||
|
||||
var pcsBox int64
|
||||
var pcs int64
|
||||
var productList []*types.Product
|
||||
var productList []types.Product
|
||||
|
||||
var surplusAt int64
|
||||
|
||||
@@ -121,27 +166,49 @@ func (l *UserOrderListLogic) UserOrderList(req *types.UserOrderListReq, userinfo
|
||||
|
||||
fsOrderAffiliateInfo := item.FsOrderAffiliateInfo
|
||||
|
||||
statusAndLogisticsRes := order.GetOrderStatusAndLogistics(order.GetOrderStatusAndLogisticsReq{
|
||||
var sureTime int64
|
||||
var productTime int64
|
||||
var ProductEndtime int64
|
||||
var deliverTime int64
|
||||
var upsDeliverTime int64
|
||||
var upsTime int64
|
||||
var arrivalTime int64
|
||||
var recevieTime int64
|
||||
if fsOrderAffiliateInfo.Id > 0 {
|
||||
sureTime = *fsOrderAffiliateInfo.SureTime
|
||||
productTime = *fsOrderAffiliateInfo.ProductTime
|
||||
ProductEndtime = *fsOrderAffiliateInfo.ProductEndtime
|
||||
deliverTime = *fsOrderAffiliateInfo.DeliverTime
|
||||
upsDeliverTime = *fsOrderAffiliateInfo.UpsDeliverTime
|
||||
upsTime = *fsOrderAffiliateInfo.UpsTime
|
||||
arrivalTime = *fsOrderAffiliateInfo.ArrivalTime
|
||||
recevieTime = *fsOrderAffiliateInfo.RecevieTime
|
||||
}
|
||||
|
||||
var getOrderStatusAndLogisticsReq = order.GetOrderStatusAndLogisticsReq{
|
||||
OrderStatus: constants.Order(*item.Status),
|
||||
DeliveryMethod: constants.DeliveryMethod(*item.DeliveryMethod),
|
||||
IsPayCompleted: *item.IsAllProductCompleted,
|
||||
SureTime: *fsOrderAffiliateInfo.SureTime,
|
||||
ProductTime: *fsOrderAffiliateInfo.SureTime,
|
||||
ProductEndtime: *fsOrderAffiliateInfo.SureTime,
|
||||
DeliverTime: *fsOrderAffiliateInfo.SureTime,
|
||||
UpsDeliverTime: *fsOrderAffiliateInfo.SureTime,
|
||||
UpsTime: *fsOrderAffiliateInfo.SureTime,
|
||||
ArrivalTime: *fsOrderAffiliateInfo.SureTime,
|
||||
RecevieTime: *fsOrderAffiliateInfo.SureTime,
|
||||
OrderCtime: *item.Ctime,
|
||||
|
||||
OrderCtime: *item.Ctime,
|
||||
//WebSetTimeInfo: orderTimeConfig,
|
||||
})
|
||||
SureTime: sureTime,
|
||||
ProductTime: productTime,
|
||||
ProductEndtime: ProductEndtime,
|
||||
DeliverTime: deliverTime,
|
||||
UpsDeliverTime: upsDeliverTime,
|
||||
UpsTime: upsTime,
|
||||
ArrivalTime: arrivalTime,
|
||||
RecevieTime: recevieTime,
|
||||
|
||||
WebSetTimeInfo: orderTimeConfig,
|
||||
}
|
||||
|
||||
statusAndLogisticsRes := order.GetOrderStatusAndLogistics(getOrderStatusAndLogisticsReq)
|
||||
|
||||
// 流程控制
|
||||
statusTime := make([]*types.StatusTime, 5)
|
||||
var statusTime []types.StatusTime
|
||||
for _, itemTimes := range statusAndLogisticsRes.Times {
|
||||
statusTime = append(statusTime, &types.StatusTime{
|
||||
statusTime = append(statusTime, types.StatusTime{
|
||||
Key: itemTimes.Key,
|
||||
Time: itemTimes.Time,
|
||||
})
|
||||
@@ -150,8 +217,10 @@ func (l *UserOrderListLogic) UserOrderList(req *types.UserOrderListReq, userinfo
|
||||
pbData.LogisticsStatus = int64(statusAndLogisticsRes.LogisticsStatus)
|
||||
pbData.Status = int64(statusAndLogisticsRes.OrderStatus)
|
||||
|
||||
var isStopMax int64
|
||||
if len(item.FsOrderDetails) > 0 {
|
||||
for _, fsOrderDetailItem := range item.FsOrderDetails {
|
||||
|
||||
fsOrderDetailBuyNum := *fsOrderDetailItem.FsOrderDetail.BuyNum
|
||||
fsOrderDetailEachBoxNum := *fsOrderDetailItem.FsOrderDetailTemplateInfo.EachBoxNum
|
||||
pcs = pcs + fsOrderDetailBuyNum
|
||||
@@ -162,23 +231,58 @@ func (l *UserOrderListLogic) UserOrderList(req *types.UserOrderListReq, userinfo
|
||||
}
|
||||
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.Title = *fsOrderDetailItem.FsProductInfo.Title
|
||||
productCover := *fsOrderDetailItem.Cover
|
||||
// 尺寸
|
||||
if size >= 200 {
|
||||
coverArr := strings.Split(*fsOrderDetailItem.Cover, ".")
|
||||
if len(coverArr) < 2 {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "cover split slice item count is less than 2")
|
||||
}
|
||||
productCover = fmt.Sprintf("%s_%d.%s", coverArr[0], req.Size, coverArr[1])
|
||||
}
|
||||
|
||||
productList = append(productList, &product)
|
||||
// 判断stop
|
||||
var isStop int64
|
||||
if fsOrderDetailItem.FsOrderDetailTemplateInfo.FsProductDesignInfo.OptionData.Id != 0 {
|
||||
// 尺寸或者模板下架
|
||||
if fsOrderDetailItem.FsOrderDetailTemplateInfo.FsProductDesignInfo.Id != 0 {
|
||||
isStop = 1
|
||||
} else {
|
||||
isStop = 3
|
||||
}
|
||||
} else {
|
||||
if fsOrderDetailItem.FsOrderDetailTemplateInfo.FsProductDesignInfo.Id != 0 {
|
||||
isStop = 1
|
||||
}
|
||||
}
|
||||
|
||||
// 判断产品是否下架
|
||||
if *fsOrderDetailItem.FsProductInfo.IsShelf == 0 || *fsOrderDetailItem.FsProductInfo.IsDel == 1 {
|
||||
isStop = 2
|
||||
}
|
||||
if isStop > isStopMax {
|
||||
isStopMax = isStop
|
||||
}
|
||||
|
||||
productList = append(productList, types.Product{
|
||||
Cover: productCover,
|
||||
Fitting: *fsOrderDetailItem.OptionalTitle,
|
||||
OptionPrice: *fsOrderDetailItem.OptionPrice,
|
||||
OrderDetailTemplateId: *fsOrderDetailItem.OrderDetailTemplateId,
|
||||
OrderId: *fsOrderDetailItem.OrderId,
|
||||
Pcs: fsOrderDetailBuyNum,
|
||||
PcsBox: pcsBox,
|
||||
Price: *fsOrderDetailItem.FsOrderDetail.Amount,
|
||||
ProductId: *fsOrderDetailItem.OptionPrice,
|
||||
Title: *fsOrderDetailItem.FsProductInfo.Title,
|
||||
Size: *fsOrderDetailItem.FsOrderDetailTemplateInfo.FsProductSizeInfo.Capacity,
|
||||
IsStop: isStop,
|
||||
})
|
||||
}
|
||||
pbData.ProductList = productList
|
||||
}
|
||||
|
||||
pbData.IsStop = isStopMax
|
||||
pbData.PcsBox = pcsBox
|
||||
pbData.Pcs = pcs
|
||||
pbData.SurplusAt = surplusAt
|
||||
|
||||
@@ -5,6 +5,13 @@ import (
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type UserOrderDeleteReq struct {
|
||||
ID int64 `form:"id"` //订单id
|
||||
}
|
||||
|
||||
type UserOrderDeleteRes struct {
|
||||
}
|
||||
|
||||
type UserOrderCancelReq struct {
|
||||
ID int64 `form:"id"` //订单id
|
||||
RefundReasonId int64 `form:"refund_reason_id"` //退款配置id
|
||||
@@ -29,25 +36,25 @@ type UserOrderListRsp struct {
|
||||
}
|
||||
|
||||
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"`
|
||||
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 {
|
||||
|
||||
@@ -202,6 +202,7 @@ func (l *GetOrderDetailLogic) GetOrderDetail(req *types.GetOrderDetailReq, useri
|
||||
IsDefault: *addressInfo.IsDefault,
|
||||
}
|
||||
}
|
||||
data.PayInfo = &types.PayInfo{}
|
||||
//首款
|
||||
if payIndex, ok := mapPay[1]; ok {
|
||||
data.PayInfo.Deposit = types.Deposit{
|
||||
|
||||
13
server/pay/etc/pay.yaml
Normal file
13
server/pay/etc/pay.yaml
Normal file
@@ -0,0 +1,13 @@
|
||||
Name: pay
|
||||
Host: 0.0.0.0
|
||||
Port: 9915
|
||||
SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
|
||||
Auth:
|
||||
AccessSecret: fusen2023
|
||||
AccessExpire: 2592000
|
||||
RefreshAfter: 1592000
|
||||
PayConfig:
|
||||
Stripe:
|
||||
Key: "sk_test_51IisojHygnIJZeghPVSBhkwySfcyDV4SoAduIxu3J7bvSJ9cZMD96LY1LO6SpdbYquLJX5oKvgEBB67KT9pecfCy00iEC4pp9y"
|
||||
SuccessURL: "http://www.baidu.com"
|
||||
CancelURL: "http://www.baidu.com"
|
||||
21
server/pay/internal/config/config.go
Normal file
21
server/pay/internal/config/config.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fusenapi/server/pay/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
SourceMysql string
|
||||
Auth types.Auth
|
||||
|
||||
PayConfig struct {
|
||||
Stripe struct {
|
||||
Key string
|
||||
SuccessURL string
|
||||
CancelURL string
|
||||
}
|
||||
}
|
||||
}
|
||||
35
server/pay/internal/handler/orderpaymentintenthandler.go
Normal file
35
server/pay/internal/handler/orderpaymentintenthandler.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/pay/internal/logic"
|
||||
"fusenapi/server/pay/internal/svc"
|
||||
"fusenapi/server/pay/internal/types"
|
||||
)
|
||||
|
||||
func OrderPaymentIntentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.OrderPaymentIntentReq
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewOrderPaymentIntentLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.OrderPaymentIntent(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
22
server/pay/internal/handler/routes.go
Normal file
22
server/pay/internal/handler/routes.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/server/pay/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/pay/payment-intent",
|
||||
Handler: OrderPaymentIntentHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
183
server/pay/internal/logic/orderpaymentintentlogic.go
Normal file
183
server/pay/internal/logic/orderpaymentintentlogic.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/pay"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/pay/internal/svc"
|
||||
"fusenapi/server/pay/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type OrderPaymentIntentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewOrderPaymentIntentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderPaymentIntentLogic {
|
||||
return &OrderPaymentIntentLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *OrderPaymentIntentLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *OrderPaymentIntentLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *OrderPaymentIntentLogic) OrderPaymentIntent(req *types.OrderPaymentIntentReq, 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.FindOneBySn(l.ctx, userinfo.UserId, req.Sn)
|
||||
|
||||
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 *orderInfo.IsCancel == 1 {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "order cancelled")
|
||||
}
|
||||
|
||||
// 校验地址信息
|
||||
addressModel := gmodel.NewFsAddressModel(l.svcCtx.MysqlConn)
|
||||
_, err = addressModel.GetOne(l.ctx, req.AddressId, userinfo.UserId)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "address not found")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get address info")
|
||||
}
|
||||
|
||||
// 校验订单支付信息
|
||||
if *orderInfo.IsPayCompleted == 1 {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "order is pay completed")
|
||||
}
|
||||
|
||||
// 判断订单状态以及该支付金额
|
||||
// 未支付
|
||||
var payAmount int64
|
||||
if *orderInfo.Status == int64(constants.STATUS_NEW_NOT_PAY) {
|
||||
payAmount = *orderInfo.TotalAmount / 2
|
||||
*orderInfo.DeliveryMethod = req.DeliveryMethod
|
||||
*orderInfo.AddressId = req.AddressId
|
||||
*orderInfo.PayMethod = req.PayMethod
|
||||
} else {
|
||||
payAmount = *orderInfo.TotalAmount - *orderInfo.TotalAmount/2
|
||||
}
|
||||
|
||||
payConfig := &pay.Config{}
|
||||
var generatePrepaymentReq = &pay.GeneratePrepaymentReq{
|
||||
ProductName: "aa",
|
||||
Amount: payAmount,
|
||||
Currency: "eur",
|
||||
Quantity: 1,
|
||||
ProductDescription: "ddddddddddddddddddddddd",
|
||||
}
|
||||
if constants.PayMethod(req.PayMethod) == constants.PAYMETHOD_STRIPE {
|
||||
payConfig.Stripe.Key = l.svcCtx.Config.PayConfig.Stripe.Key
|
||||
generatePrepaymentReq.SuccessURL = l.svcCtx.Config.PayConfig.Stripe.SuccessURL
|
||||
generatePrepaymentReq.CancelURL = l.svcCtx.Config.PayConfig.Stripe.CancelURL
|
||||
}
|
||||
payDriver := pay.NewPayDriver(req.PayMethod, payConfig)
|
||||
|
||||
var resData types.OrderPaymentIntentRes
|
||||
// 事务处理
|
||||
err = orderModel.Trans(l.ctx, func(ctx context.Context, connGorm *gorm.DB) error {
|
||||
// 支付预付--生成
|
||||
prepaymentRes, err := payDriver.GeneratePrepayment(generatePrepaymentReq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resData.RedirectUrl = prepaymentRes.URL
|
||||
|
||||
// 订单信息--修改
|
||||
err = gmodel.NewFsOrderModel(connGorm).Update(ctx, orderInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 支付记录--处理 //支付记录改为一条订单多条,分首款尾款
|
||||
var createdAt int64 = time.Now().Unix()
|
||||
var payStatus int64 = 0
|
||||
var orderSource int64 = 1
|
||||
var payStage int64
|
||||
var fspay *gmodel.FsPay
|
||||
newFsPayModel := gmodel.NewFsPayModel(connGorm)
|
||||
if *orderInfo.Status == int64(constants.STATUS_NEW_NOT_PAY) {
|
||||
fspay, err = newFsPayModel.GetListByOrderNumberStage(ctx, *orderInfo.Sn, 1)
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
payStage = 1
|
||||
} else {
|
||||
fspay, err = newFsPayModel.GetListByOrderNumberStage(ctx, *orderInfo.Sn, 2)
|
||||
if err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
payStage = 2
|
||||
}
|
||||
if fspay == nil {
|
||||
fspay = &gmodel.FsPay{
|
||||
UserId: orderInfo.UserId,
|
||||
OrderNumber: orderInfo.Sn,
|
||||
CreatedAt: &createdAt,
|
||||
}
|
||||
} else {
|
||||
fspay.UpdatedAt = &createdAt
|
||||
}
|
||||
fspay.PayAmount = &payAmount
|
||||
fspay.PayStage = &payStage
|
||||
fspay.TradeNo = &prepaymentRes.TradeNo
|
||||
fspay.PaymentMethod = &req.PayMethod
|
||||
fspay.OrderSource = &orderSource
|
||||
fspay.PayStatus = &payStatus
|
||||
|
||||
_, err = newFsPayModel.CreateOrUpdate(ctx, fspay)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to make payment")
|
||||
}
|
||||
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", resData)
|
||||
}
|
||||
61
server/pay/internal/svc/servicecontext.go
Normal file
61
server/pay/internal/svc/servicecontext.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/server/pay/internal/config"
|
||||
"net/http"
|
||||
|
||||
"fusenapi/initalize"
|
||||
"fusenapi/model/gmodel"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
|
||||
MysqlConn *gorm.DB
|
||||
AllModels *gmodel.AllModelsGen
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
MysqlConn: initalize.InitMysql(c.SourceMysql),
|
||||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
}
|
||||
}
|
||||
|
||||
func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
|
||||
AuthKey := r.Header.Get("Authorization")
|
||||
if AuthKey == "" {
|
||||
return nil, nil
|
||||
}
|
||||
AuthKey = AuthKey[7:]
|
||||
|
||||
if len(AuthKey) <= 50 {
|
||||
return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey)))
|
||||
}
|
||||
|
||||
token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) {
|
||||
// 检查签名方法是否为 HS256
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
// 返回用于验证签名的密钥
|
||||
return []byte(svcCtx.Config.Auth.AccessSecret), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprint("Error parsing token:", err))
|
||||
}
|
||||
|
||||
// 验证成功返回
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
return nil, errors.New(fmt.Sprint("Invalid token", err))
|
||||
}
|
||||
86
server/pay/internal/types/types.go
Normal file
86
server/pay/internal/types/types.go
Normal file
@@ -0,0 +1,86 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package types
|
||||
|
||||
import (
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type OrderPaymentIntentReq struct {
|
||||
Sn string `form:"sn"` //订单编号
|
||||
DeliveryMethod int64 `form:"delivery_method"` //发货方式
|
||||
AddressId int64 `form:"address_id"` //地址id
|
||||
PayMethod int64 `form:"pay_method"` //支付方式
|
||||
}
|
||||
|
||||
type OrderPaymentIntentRes struct {
|
||||
RedirectUrl string `json:"redirect_url"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
AccessSecret string `json:"accessSecret"`
|
||||
AccessExpire int64 `json:"accessExpire"`
|
||||
RefreshAfter int64 `json:"refreshAfter"`
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Filename string `fsfile:"filename"`
|
||||
Header map[string][]string `fsfile:"header"`
|
||||
Size int64 `fsfile:"size"`
|
||||
Data []byte `fsfile:"data"`
|
||||
}
|
||||
|
||||
type Meta struct {
|
||||
TotalCount int64 `json:"totalCount"`
|
||||
PageCount int64 `json:"pageCount"`
|
||||
CurrentPage int `json:"currentPage"`
|
||||
PerPage int `json:"perPage"`
|
||||
}
|
||||
|
||||
// Set 设置Response的Code和Message值
|
||||
func (resp *Response) Set(Code int, Message string) *Response {
|
||||
return &Response{
|
||||
Code: Code,
|
||||
Message: Message,
|
||||
}
|
||||
}
|
||||
|
||||
// Set 设置整个Response
|
||||
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
|
||||
return &Response{
|
||||
Code: Code,
|
||||
Message: Message,
|
||||
Data: Data,
|
||||
}
|
||||
}
|
||||
|
||||
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
||||
|
||||
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
Message: msg,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
||||
36
server/pay/pay.go
Normal file
36
server/pay/pay.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"fusenapi/utils/auth"
|
||||
|
||||
"fusenapi/server/pay/internal/config"
|
||||
"fusenapi/server/pay/internal/handler"
|
||||
"fusenapi/server/pay/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/pay.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
c.Timeout = int64(time.Second * 15)
|
||||
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
|
||||
}))
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
12
server/pay/pay_test.go
Normal file
12
server/pay/pay_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// var configFile = flag.String("f", "etc/home-user-auth.yaml", "the config file")
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
// log.Println(model.RawFieldNames[FsCanteenType]())
|
||||
main()
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
Name: product-template-tag
|
||||
Host: 0.0.0.0
|
||||
Port: 9916
|
||||
SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
|
||||
Auth:
|
||||
AccessSecret: fusen2023
|
||||
AccessExpire: 2592000
|
||||
RefreshAfter: 1592000
|
||||
12
server/product-template-tag/internal/config/config.go
Normal file
12
server/product-template-tag/internal/config/config.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fusenapi/server/product-template-tag/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
SourceMysql string
|
||||
Auth types.Auth
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/product-template-tag/internal/logic"
|
||||
"fusenapi/server/product-template-tag/internal/svc"
|
||||
"fusenapi/server/product-template-tag/internal/types"
|
||||
)
|
||||
|
||||
func GetProductTemplateTagsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.GetProductTemplateTagsReq
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewGetProductTemplateTagsLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.GetProductTemplateTags(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
22
server/product-template-tag/internal/handler/routes.go
Normal file
22
server/product-template-tag/internal/handler/routes.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/server/product-template-tag/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/product-template/get_product_template_tags",
|
||||
Handler: GetProductTemplateTagsHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/product-template-tag/internal/svc"
|
||||
"fusenapi/server/product-template-tag/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetProductTemplateTagsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetProductTemplateTagsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductTemplateTagsLogic {
|
||||
return &GetProductTemplateTagsLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *GetProductTemplateTagsLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *GetProductTemplateTagsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProductTemplateTagsReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if req.Limit <= 0 || req.Limit > 100 {
|
||||
req.Limit = 4
|
||||
}
|
||||
productTemplateTags, err := l.svcCtx.AllModels.FsProductTemplateTags.GetList(l.ctx, 1, req.Limit, "`id` DESC")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template tags")
|
||||
}
|
||||
list := make([]types.GetProductTemplateTagsRsp, 0, len(productTemplateTags))
|
||||
for _, v := range productTemplateTags {
|
||||
list = append(list, types.GetProductTemplateTagsRsp{
|
||||
Tag: *v.Title,
|
||||
Cover: *v.CoverImg,
|
||||
})
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
|
||||
}
|
||||
61
server/product-template-tag/internal/svc/servicecontext.go
Normal file
61
server/product-template-tag/internal/svc/servicecontext.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/server/product-template-tag/internal/config"
|
||||
"net/http"
|
||||
|
||||
"fusenapi/initalize"
|
||||
"fusenapi/model/gmodel"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
|
||||
MysqlConn *gorm.DB
|
||||
AllModels *gmodel.AllModelsGen
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
MysqlConn: initalize.InitMysql(c.SourceMysql),
|
||||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
}
|
||||
}
|
||||
|
||||
func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
|
||||
AuthKey := r.Header.Get("Authorization")
|
||||
if AuthKey == "" {
|
||||
return nil, nil
|
||||
}
|
||||
AuthKey = AuthKey[7:]
|
||||
|
||||
if len(AuthKey) <= 50 {
|
||||
return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey)))
|
||||
}
|
||||
|
||||
token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) {
|
||||
// 检查签名方法是否为 HS256
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
// 返回用于验证签名的密钥
|
||||
return []byte(svcCtx.Config.Auth.AccessSecret), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprint("Error parsing token:", err))
|
||||
}
|
||||
|
||||
// 验证成功返回
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
return nil, errors.New(fmt.Sprint("Invalid token", err))
|
||||
}
|
||||
84
server/product-template-tag/internal/types/types.go
Normal file
84
server/product-template-tag/internal/types/types.go
Normal file
@@ -0,0 +1,84 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package types
|
||||
|
||||
import (
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type GetProductTemplateTagsReq struct {
|
||||
Limit int `form:"limit"`
|
||||
}
|
||||
|
||||
type GetProductTemplateTagsRsp struct {
|
||||
Tag string `json:"tag"`
|
||||
Cover string `json:"cover"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
AccessSecret string `json:"accessSecret"`
|
||||
AccessExpire int64 `json:"accessExpire"`
|
||||
RefreshAfter int64 `json:"refreshAfter"`
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Filename string `fsfile:"filename"`
|
||||
Header map[string][]string `fsfile:"header"`
|
||||
Size int64 `fsfile:"size"`
|
||||
Data []byte `fsfile:"data"`
|
||||
}
|
||||
|
||||
type Meta struct {
|
||||
TotalCount int64 `json:"totalCount"`
|
||||
PageCount int64 `json:"pageCount"`
|
||||
CurrentPage int `json:"currentPage"`
|
||||
PerPage int `json:"perPage"`
|
||||
}
|
||||
|
||||
// Set 设置Response的Code和Message值
|
||||
func (resp *Response) Set(Code int, Message string) *Response {
|
||||
return &Response{
|
||||
Code: Code,
|
||||
Message: Message,
|
||||
}
|
||||
}
|
||||
|
||||
// Set 设置整个Response
|
||||
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
|
||||
return &Response{
|
||||
Code: Code,
|
||||
Message: Message,
|
||||
Data: Data,
|
||||
}
|
||||
}
|
||||
|
||||
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
||||
|
||||
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
Message: msg,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
||||
36
server/product-template-tag/product-template-tag.go
Normal file
36
server/product-template-tag/product-template-tag.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"fusenapi/utils/auth"
|
||||
|
||||
"fusenapi/server/product-template-tag/internal/config"
|
||||
"fusenapi/server/product-template-tag/internal/handler"
|
||||
"fusenapi/server/product-template-tag/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/product-template-tag.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
c.Timeout = int64(time.Second * 15)
|
||||
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
|
||||
}))
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
@@ -100,7 +100,7 @@ func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListR
|
||||
IsDel: &pIsDel,
|
||||
IsShelf: &pIsShelf,
|
||||
Status: &pStatus,
|
||||
OrderBy: "`sort` DESC",
|
||||
OrderBy: "`is_recommend` DESC,`sort` ASC",
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
@@ -223,11 +223,9 @@ func (l *GetTagProductListLogic) dealWithTagMenuData(req dealWithTagMenuDataReq)
|
||||
TagProductList: nil,
|
||||
TypeName: *tagInfo.Title,
|
||||
TypeId: tagInfo.Id,
|
||||
Level: int64(lenLevel),
|
||||
LevelPrefix: *tagInfo.LevelPrefix,
|
||||
Icon: *tagInfo.Icon,
|
||||
Sort: *tagInfo.Sort,
|
||||
Description: *tagInfo.Description,
|
||||
LevelPrefix: *tagInfo.LevelPrefix,
|
||||
ChildTagList: make([]*types.TagItem, 0, 50),
|
||||
}
|
||||
//携带产品
|
||||
@@ -243,8 +241,10 @@ func (l *GetTagProductListLogic) dealWithTagMenuData(req dealWithTagMenuDataReq)
|
||||
Size: req.Size,
|
||||
User: req.User,
|
||||
})
|
||||
//赋值
|
||||
tagTem.TagProductList = productListRsp
|
||||
//tag中产品
|
||||
for _, tmpProduct := range productListRsp {
|
||||
tagTem.TagProductList = append(tagTem.TagProductList, tmpProduct)
|
||||
}
|
||||
}
|
||||
//加入分类
|
||||
req.MapTagLevel[*tagInfo.LevelPrefix] = &tagTem
|
||||
@@ -255,6 +255,7 @@ func (l *GetTagProductListLogic) dealWithTagMenuData(req dealWithTagMenuDataReq)
|
||||
// 组织等级从属关系
|
||||
func (l *GetTagProductListLogic) organizationLevelRelation(minLevel int, mapTagLevel map[string]*types.TagItem) []types.TagItem {
|
||||
mapTop := make(map[string]struct{})
|
||||
//设置归属关系
|
||||
for prefix, tagItem := range mapTagLevel {
|
||||
prefixSlice := strings.Split(prefix, "/")
|
||||
//存储最高等级
|
||||
@@ -275,6 +276,18 @@ func (l *GetTagProductListLogic) organizationLevelRelation(minLevel int, mapTagL
|
||||
})
|
||||
mapTagLevel[parentPrefix] = parent
|
||||
}
|
||||
//把子类的产品列表变成产品id列表并且把产品列表都放到最顶级父级中
|
||||
for _, v := range mapTagLevel {
|
||||
if _, ok := mapTop[v.LevelPrefix]; ok {
|
||||
continue
|
||||
}
|
||||
topPrefixSlic := strings.Split(v.LevelPrefix, "/")[:minLevel]
|
||||
topPrefix := strings.Join(topPrefixSlic, "/")
|
||||
for k, tmpProduct := range v.TagProductList {
|
||||
v.TagProductList[k] = tmpProduct.(types.TagProduct).ProductId
|
||||
mapTagLevel[topPrefix].TagProductList = append(mapTagLevel[topPrefix].TagProductList, tmpProduct)
|
||||
}
|
||||
}
|
||||
//最终值提取最高级别那一层出来
|
||||
rspList := make([]types.TagItem, 0, len(mapTagLevel))
|
||||
for prefix, _ := range mapTop {
|
||||
@@ -326,12 +339,10 @@ func (l *GetTagProductListLogic) getTagProducts(req getTagProductsReq) (productL
|
||||
ProductId: productInfo.Id,
|
||||
Sn: *productInfo.Sn,
|
||||
Title: *productInfo.Title,
|
||||
Intro: *productInfo.Intro,
|
||||
IsEnv: *productInfo.IsProtection,
|
||||
IsMicro: *productInfo.IsMicrowave,
|
||||
SizeNum: uint32(sizeNum),
|
||||
MinPrice: minPrice,
|
||||
HaveOptionalFitting: haveOptionalFitting,
|
||||
Recommended: *productInfo.IsRecommend > 0,
|
||||
}
|
||||
//千人千面处理
|
||||
r := image.ThousandFaceImageFormatReq{
|
||||
@@ -348,7 +359,6 @@ func (l *GetTagProductListLogic) getTagProducts(req getTagProductsReq) (productL
|
||||
}
|
||||
image.ThousandFaceImageFormat(&r)
|
||||
item.Cover = r.Cover
|
||||
item.CoverImg = r.CoverImg
|
||||
item.CoverDefault = r.CoverDefault
|
||||
//加入分类产品切片
|
||||
productListRsp = append(productListRsp, item)
|
||||
|
||||
@@ -146,12 +146,9 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty
|
||||
haveOptionalFitting = true
|
||||
}
|
||||
item := types.HomePageRecommendProductListRsp{
|
||||
ProductId: productInfo.Id,
|
||||
Id: productInfo.Id,
|
||||
Sn: *productInfo.Sn,
|
||||
Title: *productInfo.Title,
|
||||
Intro: *productInfo.Intro,
|
||||
IsEnv: *productInfo.IsProtection,
|
||||
IsMicro: *productInfo.IsMicrowave,
|
||||
SizeNum: uint32(sizeNum),
|
||||
MinPrice: minPrice,
|
||||
HaveOptionalFitting: haveOptionalFitting,
|
||||
@@ -171,7 +168,6 @@ func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *ty
|
||||
}
|
||||
image.ThousandFaceImageFormat(&r)
|
||||
item.Cover = r.Cover
|
||||
item.CoverImg = r.CoverImg
|
||||
item.CoverDefault = r.CoverDefault
|
||||
//加入分类产品切片
|
||||
listRsp = append(listRsp, item)
|
||||
|
||||
@@ -258,15 +258,13 @@ type GetTagProductListRsp struct {
|
||||
}
|
||||
|
||||
type TagItem struct {
|
||||
TypeName string `json:"type_name"`
|
||||
TypeId int64 `json:"type_id"`
|
||||
Description string `json:"description"`
|
||||
Level int64 `json:"level"`
|
||||
LevelPrefix string `json:"level_prefix"`
|
||||
Icon string `json:"icon"`
|
||||
Sort int64 `json:"sort"`
|
||||
TagProductList []TagProduct `json:"tag_product_list"` //分类下的产品
|
||||
ChildTagList []*TagItem `json:"child_tag_list"`
|
||||
TypeName string `json:"type_name"`
|
||||
TypeId int64 `json:"type_id"`
|
||||
Icon string `json:"icon"`
|
||||
Sort int64 `json:"sort"`
|
||||
LevelPrefix string `json:"level_prefix"`
|
||||
TagProductList []interface{} `json:"tag_product_list"` //分类下的产品
|
||||
ChildTagList []*TagItem `json:"child_tag_list"`
|
||||
}
|
||||
|
||||
type TagProduct struct {
|
||||
@@ -274,14 +272,11 @@ type TagProduct struct {
|
||||
Sn string `json:"sn"`
|
||||
Title string `json:"title"`
|
||||
Cover string `json:"cover"`
|
||||
Intro string `json:"intro"`
|
||||
CoverImg string `json:"cover_img"`
|
||||
IsEnv int64 `json:"is_env"`
|
||||
IsMicro int64 `json:"is_micro"`
|
||||
SizeNum uint32 `json:"size_num"`
|
||||
MinPrice int64 `json:"min_price"`
|
||||
CoverDefault string `json:"cover_default"`
|
||||
HaveOptionalFitting bool `json:"have_optional_fitting"`
|
||||
Recommended bool `json:"recommended"`
|
||||
}
|
||||
|
||||
type GetRenderDesignReq struct {
|
||||
@@ -392,14 +387,10 @@ type HomePageRecommendProductListReq struct {
|
||||
}
|
||||
|
||||
type HomePageRecommendProductListRsp struct {
|
||||
ProductId int64 `json:"product_id"`
|
||||
Id int64 `json:"id"`
|
||||
Sn string `json:"sn"`
|
||||
Title string `json:"title"`
|
||||
Cover string `json:"cover"`
|
||||
Intro string `json:"intro"`
|
||||
CoverImg string `json:"cover_img"`
|
||||
IsEnv int64 `json:"is_env"`
|
||||
IsMicro int64 `json:"is_micro"`
|
||||
SizeNum uint32 `json:"size_num"`
|
||||
MinPrice int64 `json:"min_price"`
|
||||
CoverDefault string `json:"cover_default"`
|
||||
|
||||
8
server/websocket/etc/websocket.yaml
Normal file
8
server/websocket/etc/websocket.yaml
Normal file
@@ -0,0 +1,8 @@
|
||||
Name: websocket
|
||||
Host: 0.0.0.0
|
||||
Port: 9914
|
||||
SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
|
||||
Auth:
|
||||
AccessSecret: fusen2023
|
||||
AccessExpire: 2592000
|
||||
RefreshAfter: 1592000
|
||||
13
server/websocket/internal/config/config.go
Normal file
13
server/websocket/internal/config/config.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
SourceMysql string
|
||||
Auth types.Auth
|
||||
}
|
||||
15
server/websocket/internal/handler/datatransferhandler.go
Normal file
15
server/websocket/internal/handler/datatransferhandler.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fusenapi/server/websocket/internal/logic"
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func DataTransferHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewDataTransferLogic(r.Context(), svcCtx)
|
||||
l.DataTransfer(svcCtx, w, r)
|
||||
}
|
||||
}
|
||||
31
server/websocket/internal/handler/rendernotifyhandler.go
Normal file
31
server/websocket/internal/handler/rendernotifyhandler.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fusenapi/server/websocket/internal/logic"
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
"fusenapi/utils/basic"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func RenderNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.RenderNotifyReq
|
||||
_, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewRenderNotifyLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.RenderNotify(&req)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
27
server/websocket/internal/handler/routes.go
Normal file
27
server/websocket/internal/handler/routes.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/websocket/data_transfer",
|
||||
Handler: DataTransferHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/websocket/render_notify",
|
||||
Handler: RenderNotifyHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
||||
266
server/websocket/internal/logic/datatransferlogic.go
Normal file
266
server/websocket/internal/logic/datatransferlogic.go
Normal file
@@ -0,0 +1,266 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/id_generator"
|
||||
"github.com/gorilla/websocket"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DataTransferLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDataTransferLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DataTransferLogic {
|
||||
return &DataTransferLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
//全局websocketid生成器
|
||||
websocketIdGenerator = id_generator.NewWebsocketId(1)
|
||||
//临时缓存对象池
|
||||
buffPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return bytes.Buffer{}
|
||||
},
|
||||
}
|
||||
//升级websocket
|
||||
upgrade = websocket.Upgrader{
|
||||
ReadBufferSize: 1024 * 10, //最大可读取大小 10M
|
||||
//握手超时时间15s
|
||||
HandshakeTimeout: time.Second * 15,
|
||||
//允许跨域
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
WriteBufferPool: &buffPool,
|
||||
EnableCompression: true,
|
||||
}
|
||||
//websocket连接存储
|
||||
mapConnPool = sync.Map{}
|
||||
)
|
||||
|
||||
// 每个连接的连接基本属性
|
||||
type wsConnectItem struct {
|
||||
conn *websocket.Conn //websocket的连接
|
||||
closeChan chan struct{} //ws连接关闭chan
|
||||
isClose bool //是否已经关闭
|
||||
uniqueId uint64 //ws连接唯一标识
|
||||
inChan chan []byte //接受消息缓冲通道
|
||||
outChan chan []byte //发送回客户端的消息
|
||||
mutex sync.Mutex //互斥锁
|
||||
renderProperty renderProperty //扩展云渲染属性
|
||||
}
|
||||
|
||||
func (l *DataTransferLogic) DataTransfer(svcCtx *svc.ServiceContext, w http.ResponseWriter, r *http.Request) {
|
||||
//升级websocket
|
||||
conn, err := upgrade.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
logx.Error("http upgrade websocket err:", err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
w.Header().Set("Connection", "Upgrade")
|
||||
rsp := types.DataTransferData{}
|
||||
//鉴权不成功10秒后断开
|
||||
/*isAuth, _ := l.checkAuth(svcCtx, r)
|
||||
if !isAuth {
|
||||
time.Sleep(time.Second) //兼容下火狐
|
||||
rsp.T = constants.WEBSOCKET_UNAUTH
|
||||
b, _ := json.Marshal(rsp)
|
||||
//先发一条正常信息
|
||||
_ = conn.WriteMessage(websocket.TextMessage, b)
|
||||
//发送关闭信息
|
||||
_ = conn.WriteMessage(websocket.CloseMessage, nil)
|
||||
return
|
||||
}*/
|
||||
//生成连接唯一标识
|
||||
uniqueId := websocketIdGenerator.Get()
|
||||
ws := wsConnectItem{
|
||||
conn: conn,
|
||||
uniqueId: uniqueId,
|
||||
closeChan: make(chan struct{}, 1),
|
||||
inChan: make(chan []byte, 100),
|
||||
outChan: make(chan []byte, 100),
|
||||
renderProperty: renderProperty{
|
||||
renderImageTask: make(map[string]struct{}),
|
||||
renderImageTaskCtlChan: make(chan renderImageControlChanItem, 100),
|
||||
},
|
||||
}
|
||||
//保存连接
|
||||
mapConnPool.Store(uniqueId, ws)
|
||||
defer ws.close()
|
||||
//把连接成功消息发回去
|
||||
time.Sleep(time.Second) //兼容下火狐
|
||||
rsp.T = constants.WEBSOCKET_CONNECT_SUCCESS
|
||||
rsp.D = uniqueId
|
||||
b, _ := json.Marshal(rsp)
|
||||
_ = conn.WriteMessage(websocket.TextMessage, b)
|
||||
//循环读客户端信息
|
||||
go ws.readLoop()
|
||||
//循环把数据发送给客户端
|
||||
go ws.writeLoop()
|
||||
//推消息到云渲染
|
||||
go ws.sendLoop()
|
||||
//操作连接中渲染任务的增加/删除
|
||||
go ws.operationRenderTask()
|
||||
//心跳
|
||||
ws.heartbeat()
|
||||
}
|
||||
|
||||
// 鉴权
|
||||
func (l *DataTransferLogic) checkAuth(svcCtx *svc.ServiceContext, r *http.Request) (isAuth bool, userInfo *auth.UserInfo) {
|
||||
// 解析JWT token,并对空用户进行判断
|
||||
claims, err := svcCtx.ParseJwtToken(r)
|
||||
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
if claims != nil {
|
||||
// 从token中获取对应的用户信息
|
||||
userInfo, err = auth.GetUserInfoFormMapClaims(claims)
|
||||
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
} else {
|
||||
return false, nil
|
||||
}
|
||||
return true, userInfo
|
||||
}
|
||||
|
||||
// 心跳
|
||||
func (w *wsConnectItem) heartbeat() {
|
||||
tick := time.Tick(time.Second * 5)
|
||||
for {
|
||||
select {
|
||||
case <-w.closeChan:
|
||||
return
|
||||
case <-tick:
|
||||
//发送心跳信息
|
||||
if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil {
|
||||
logx.Error("发送心跳信息异常,关闭连接:", w.uniqueId, err)
|
||||
w.close()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭连接
|
||||
func (w *wsConnectItem) close() {
|
||||
w.mutex.Lock()
|
||||
defer w.mutex.Unlock()
|
||||
logx.Info("websocket:", w.uniqueId, " is closing...")
|
||||
//发送关闭信息
|
||||
_ = w.conn.WriteMessage(websocket.CloseMessage, nil)
|
||||
w.conn.Close()
|
||||
mapConnPool.Delete(w.uniqueId)
|
||||
if !w.isClose {
|
||||
w.isClose = true
|
||||
close(w.closeChan)
|
||||
}
|
||||
logx.Info("websocket:", w.uniqueId, " is closed")
|
||||
}
|
||||
|
||||
// 读取输出返回给客户端
|
||||
func (w *wsConnectItem) writeLoop() {
|
||||
for {
|
||||
select {
|
||||
case <-w.closeChan: //如果关闭了
|
||||
return
|
||||
case data := <-w.outChan:
|
||||
if err := w.conn.WriteMessage(websocket.TextMessage, data); err != nil {
|
||||
logx.Error("websocket write loop err:", err)
|
||||
w.close()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 接受客户端发来的消息
|
||||
func (w *wsConnectItem) readLoop() {
|
||||
for {
|
||||
select {
|
||||
case <-w.closeChan: //如果关闭了
|
||||
return
|
||||
default:
|
||||
msgType, data, err := w.conn.ReadMessage()
|
||||
if err != nil {
|
||||
logx.Error("接受信息错误:", err)
|
||||
//关闭连接
|
||||
w.close()
|
||||
return
|
||||
}
|
||||
//ping的消息不处理
|
||||
if msgType != websocket.PingMessage {
|
||||
//消息传入缓冲通道
|
||||
w.inChan <- data
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 把收到的消息发往不同的地方处理
|
||||
func (w *wsConnectItem) sendLoop() {
|
||||
for {
|
||||
select {
|
||||
case <-w.closeChan:
|
||||
return
|
||||
case data := <-w.inChan:
|
||||
w.dealwithReciveData(data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 把要传递给客户端的数据放入outchan
|
||||
func (w *wsConnectItem) sendToOutChan(data []byte) {
|
||||
select {
|
||||
case <-w.closeChan:
|
||||
return
|
||||
case w.outChan <- data:
|
||||
logx.Info("notify send render result to out chan")
|
||||
}
|
||||
}
|
||||
|
||||
// 获取需要渲染图片的map key
|
||||
func (w *wsConnectItem) getRenderImageMapKey(productId, templateTagId int64, algorithmVersion string) string {
|
||||
return fmt.Sprintf("%d-%d-%s", productId, templateTagId, algorithmVersion)
|
||||
}
|
||||
|
||||
// 处理接受到的数据
|
||||
func (w *wsConnectItem) dealwithReciveData(data []byte) {
|
||||
var parseInfo types.DataTransferData
|
||||
if err := json.Unmarshal(data, &parseInfo); err != nil {
|
||||
logx.Error("invalid format of websocket message")
|
||||
return
|
||||
}
|
||||
d, _ := json.Marshal(parseInfo.D)
|
||||
//分消息类型给到不同逻辑处理,可扩展
|
||||
switch parseInfo.T {
|
||||
//图片渲染
|
||||
case constants.WEBSOCKET_RENDER_IMAGE:
|
||||
go w.SendToCloudRender(d)
|
||||
default:
|
||||
|
||||
}
|
||||
}
|
||||
89
server/websocket/internal/logic/rendernotifylogic.go
Normal file
89
server/websocket/internal/logic/rendernotifylogic.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/utils/basic"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type RenderNotifyLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewRenderNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RenderNotifyLogic {
|
||||
return &RenderNotifyLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *RenderNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *RenderNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *RenderNotifyLogic) RenderNotify(req *types.RenderNotifyReq) (resp *basic.Response) {
|
||||
if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid param time")
|
||||
}
|
||||
//验证签名 sha256
|
||||
notifyByte, _ := json.Marshal(req.Info)
|
||||
h := sha256.New()
|
||||
h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time)))
|
||||
signHex := h.Sum(nil)
|
||||
sign := hex.EncodeToString(signHex)
|
||||
//fmt.Println(sign)
|
||||
if req.Sign != sign {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign")
|
||||
}
|
||||
//遍历websocket链接把数据传进去
|
||||
mapConnPool.Range(func(key, value any) bool {
|
||||
//断言连接
|
||||
ws, ok := value.(wsConnectItem)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
renderKey := ws.getRenderImageMapKey(req.Info.ProductId, req.Info.TemplateTagId, req.Info.AlgorithmVersion)
|
||||
//查询有无该渲染任务
|
||||
_, ok = ws.renderProperty.renderImageTask[renderKey]
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
rspData := types.DataTransferData{
|
||||
T: constants.WEBSOCKET_RENDER_IMAGE,
|
||||
D: types.RenderImageRspMsg{
|
||||
ProductId: req.Info.ProductId,
|
||||
TemplateTagId: req.Info.TemplateTagId,
|
||||
Image: req.Info.Image,
|
||||
},
|
||||
}
|
||||
b, _ := json.Marshal(rspData)
|
||||
//删除对应的需要渲染的图片map
|
||||
ws.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
|
||||
Option: 0, //0删除 1添加
|
||||
Key: renderKey,
|
||||
}
|
||||
//发送数据到out chan
|
||||
ws.sendToOutChan(b)
|
||||
return true
|
||||
})
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
63
server/websocket/internal/logic/ws_render_image_logic.go
Normal file
63
server/websocket/internal/logic/ws_render_image_logic.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fusenapi/server/websocket/internal/types"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
// 云渲染属性
|
||||
type renderProperty struct {
|
||||
renderImageTask map[string]struct{} //需要渲染的图片任务
|
||||
renderImageTaskCtlChan chan renderImageControlChanItem //渲染任务新增移除的控制通道
|
||||
}
|
||||
|
||||
// 渲染任务新增移除的控制通道的数据
|
||||
type renderImageControlChanItem struct {
|
||||
Option int // 0删除 1添加
|
||||
Key string //map的key
|
||||
}
|
||||
|
||||
// 渲染请求数据处理发送云渲染服务处理
|
||||
func (w *wsConnectItem) SendToCloudRender(data []byte) {
|
||||
var renderImageData types.RenderImageReqMsg
|
||||
if err := json.Unmarshal(data, &renderImageData); err != nil {
|
||||
logx.Error("invalid format of websocket render image message", err)
|
||||
return
|
||||
}
|
||||
logx.Info("收到请求云渲染图片数据:", renderImageData)
|
||||
//把需要渲染的图片任务加进去
|
||||
for _, productId := range renderImageData.ProductIds {
|
||||
select {
|
||||
case <-w.closeChan: //连接关闭了
|
||||
return
|
||||
default:
|
||||
//加入渲染任务
|
||||
key := w.getRenderImageMapKey(productId, renderImageData.TemplateTagId, renderImageData.AlgorithmVersion)
|
||||
w.renderProperty.renderImageTaskCtlChan <- renderImageControlChanItem{
|
||||
Option: 1, //0删除 1添加
|
||||
Key: key,
|
||||
}
|
||||
// TODO 数据发送给云渲染服务器
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 操作连接中渲染任务的增加/删除
|
||||
func (w *wsConnectItem) operationRenderTask() {
|
||||
for {
|
||||
select {
|
||||
case <-w.closeChan:
|
||||
return
|
||||
case data := <-w.renderProperty.renderImageTaskCtlChan:
|
||||
switch data.Option {
|
||||
case 0: //删除任务
|
||||
delete(w.renderProperty.renderImageTask, data.Key)
|
||||
case 1: //新增任务
|
||||
w.renderProperty.renderImageTask[data.Key] = struct{}{}
|
||||
default:
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
61
server/websocket/internal/svc/servicecontext.go
Normal file
61
server/websocket/internal/svc/servicecontext.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/server/websocket/internal/config"
|
||||
"net/http"
|
||||
|
||||
"fusenapi/initalize"
|
||||
"fusenapi/model/gmodel"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
|
||||
MysqlConn *gorm.DB
|
||||
AllModels *gmodel.AllModelsGen
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
MysqlConn: initalize.InitMysql(c.SourceMysql),
|
||||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
}
|
||||
}
|
||||
|
||||
func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
|
||||
AuthKey := r.Header.Get("Authorization")
|
||||
if AuthKey == "" {
|
||||
return nil, nil
|
||||
}
|
||||
AuthKey = AuthKey[7:]
|
||||
|
||||
if len(AuthKey) <= 50 {
|
||||
return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey)))
|
||||
}
|
||||
|
||||
token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) {
|
||||
// 检查签名方法是否为 HS256
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
// 返回用于验证签名的密钥
|
||||
return []byte(svcCtx.Config.Auth.AccessSecret), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.New(fmt.Sprint("Error parsing token:", err))
|
||||
}
|
||||
|
||||
// 验证成功返回
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
return nil, errors.New(fmt.Sprint("Invalid token", err))
|
||||
}
|
||||
106
server/websocket/internal/types/types.go
Normal file
106
server/websocket/internal/types/types.go
Normal file
@@ -0,0 +1,106 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
package types
|
||||
|
||||
import (
|
||||
"fusenapi/utils/basic"
|
||||
)
|
||||
|
||||
type DataTransferData struct {
|
||||
T string `json:"t"` //消息类型
|
||||
D interface{} `json:"d"` //传递的消息
|
||||
}
|
||||
|
||||
type RenderImageReqMsg struct {
|
||||
ProductIds []int64 `json:"product_ids"` //产品 id
|
||||
TemplateTagId int64 `json:"template_tag_id"` //模板标签id
|
||||
AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本
|
||||
}
|
||||
|
||||
type RenderImageRspMsg struct {
|
||||
ProductId int64 `json:"product_id"` //产品 id
|
||||
TemplateTagId int64 `json:"template_tag_id"` //模板标签id
|
||||
AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本
|
||||
Image string `json:"image"` //渲染后的图片
|
||||
}
|
||||
|
||||
type RenderNotifyReq struct {
|
||||
Sign string `json:"sign"`
|
||||
Time int64 `json:"time"`
|
||||
Info NotifyInfo `json:"info"`
|
||||
}
|
||||
|
||||
type NotifyInfo struct {
|
||||
ProductId int64 `json:"product_id"` //产品id
|
||||
TemplateTagId int64 `json:"template_tag_id"` //模板标签id
|
||||
AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
type Auth struct {
|
||||
AccessSecret string `json:"accessSecret"`
|
||||
AccessExpire int64 `json:"accessExpire"`
|
||||
RefreshAfter int64 `json:"refreshAfter"`
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Filename string `fsfile:"filename"`
|
||||
Header map[string][]string `fsfile:"header"`
|
||||
Size int64 `fsfile:"size"`
|
||||
Data []byte `fsfile:"data"`
|
||||
}
|
||||
|
||||
type Meta struct {
|
||||
TotalCount int64 `json:"totalCount"`
|
||||
PageCount int64 `json:"pageCount"`
|
||||
CurrentPage int `json:"currentPage"`
|
||||
PerPage int `json:"perPage"`
|
||||
}
|
||||
|
||||
// Set 设置Response的Code和Message值
|
||||
func (resp *Response) Set(Code int, Message string) *Response {
|
||||
return &Response{
|
||||
Code: Code,
|
||||
Message: Message,
|
||||
}
|
||||
}
|
||||
|
||||
// Set 设置整个Response
|
||||
func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response {
|
||||
return &Response{
|
||||
Code: Code,
|
||||
Message: Message,
|
||||
Data: Data,
|
||||
}
|
||||
}
|
||||
|
||||
// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
||||
|
||||
// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数
|
||||
func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response {
|
||||
newResp := &Response{
|
||||
Code: sr.Code,
|
||||
Message: msg,
|
||||
}
|
||||
if len(data) == 1 {
|
||||
newResp.Data = data[0]
|
||||
}
|
||||
return newResp
|
||||
}
|
||||
36
server/websocket/websocket.go
Normal file
36
server/websocket/websocket.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"fusenapi/utils/auth"
|
||||
|
||||
"fusenapi/server/websocket/internal/config"
|
||||
"fusenapi/server/websocket/internal/handler"
|
||||
"fusenapi/server/websocket/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/websocket.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
c.Timeout = int64(time.Second * 15)
|
||||
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
|
||||
}))
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
Reference in New Issue
Block a user