diff --git a/constants/pay.go b/constants/pay.go new file mode 100644 index 00000000..cd39d457 --- /dev/null +++ b/constants/pay.go @@ -0,0 +1,8 @@ +package constants + +type PayMethod int64 + +const ( + PAYMETHOD_STRIPE PayMethod = 1 + PAYMETHOD_PAYPAL PayMethod = 2 +) diff --git a/constants/websocket.go b/constants/websocket.go new file mode 100644 index 00000000..7b4c9ca3 --- /dev/null +++ b/constants/websocket.go @@ -0,0 +1,19 @@ +package constants + +type websocket string + +// websocket消息类型 +const ( + //鉴权失败 + WEBSOCKET_UNAUTH = "WEBSOCKET_UNAUTH" + //ws连接成功 + WEBSOCKET_CONNECT_SUCCESS = "WEBSOCKET_CONNECT_SUCCESS" + //图片渲染 + WEBSOCKET_RENDER_IMAGE = "WEBSOCKET_RENDER_IMAGE" + //数据格式错误 + WEBSOCKET_ERR_DATA_FORMAT = "WEBSOCKET_ERR_DATA_FORMAT" + // +) + +// 云渲染完成通知api需要的签名字符串 +const RENDER_NOTIFY_SIGN_KEY = "fusen-render-notify-%s-%d" diff --git a/fs_gen_api_backend.sh b/fs_gen_api_backend.sh index 1acbf5fa..d870c95d 100755 --- a/fs_gen_api_backend.sh +++ b/fs_gen_api_backend.sh @@ -1,6 +1,6 @@ #! /bin/bash name=${1%%\\*} -options=("backend" "product-template" "product-model") +options=("backend" "product-template" "product-template-tag" "product-model") for option in "${options[@]}"; do if [ "$name" = "$option" ]; then echo $name diff --git a/go.mod b/go.mod index f8c4f407..0ff6f62a 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,11 @@ require ( github.com/bwmarrin/snowflake v0.3.0 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/google/uuid v1.3.0 +<<<<<<< HEAD github.com/hashicorp/raft v1.5.0 +======= + github.com/gorilla/websocket v1.5.0 +>>>>>>> d843fff73d7ba5d4e14a5d86281d5166f04cb303 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/stripe/stripe-go/v74 v74.26.0 diff --git a/go.sum b/go.sum index cfa0472a..0bb4c655 100644 --- a/go.sum +++ b/go.sum @@ -184,6 +184,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks= github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 h1:1JYBfzqrWPcCclBwxFCPAou9n+q86mfnu7NAeHfte7A= diff --git a/model/gmodel/fs_order_detail_template_logic.go b/model/gmodel/fs_order_detail_template_logic.go index a4a1cac0..cbdcd360 100755 --- a/model/gmodel/fs_order_detail_template_logic.go +++ b/model/gmodel/fs_order_detail_template_logic.go @@ -25,3 +25,7 @@ func (dt *FsOrderDetailTemplateModel) FindOne(ctx context.Context, id int64) (re err = dt.db.WithContext(ctx).Model(&FsOrderDetailTemplate{}).Where("`id` = ?", id).Take(&resp).Error return resp, err } + +func (m *FsOrderDetailTemplateModel) TableName() string { + return m.name +} diff --git a/model/gmodel/fs_order_logic.go b/model/gmodel/fs_order_logic.go index d63c2e80..16c9a527 100755 --- a/model/gmodel/fs_order_logic.go +++ b/model/gmodel/fs_order_logic.go @@ -77,7 +77,7 @@ func (o *FsOrderModel) FindPageListByPage(ctx context.Context, rowBuilder *gorm. rowBuilder = rowBuilder.Scopes(handler.Paginate(page, pageSize)) // 结果 - result := rowBuilder.WithContext(ctx).Find(&resp) + result := rowBuilder.Debug().WithContext(ctx).Find(&resp) if result.Error != nil { return nil, result.Error } else { @@ -93,8 +93,20 @@ type FsOrderRel struct { type FsOrderDetails struct { FsOrderDetail - FsOrderDetailTemplateInfo FsOrderDetailTemplate `gorm:"foreignKey:id;references:order_detail_template_id"` - FsProductInfo FsProduct `gorm:"foreignKey:id;references:product_id"` + FsOrderDetailTemplateInfo FsOrderDetailTemplateInfo `gorm:"foreignKey:id;references:order_detail_template_id"` + FsProductInfo FsProduct `gorm:"foreignKey:id;references:product_id"` +} + +type FsOrderDetailTemplateInfo struct { + FsOrderDetailTemplate + FsProductDesignInfo FsProductDesignInfo `gorm:"foreignKey:id;references:design_id"` //获取设计数据 + FsProductSizeInfo FsProductSize `gorm:"foreignKey:id;references:size_id"` +} + +type FsProductDesignInfo struct { + FsProductDesign + OptionData FsProduct `gorm:"foreignKey:id;references:optional_id"` //获取配件信息 + TemplateData FsProductTemplateV2 `gorm:"foreignKey:id;references:template_id"` //获取模板信息 } func (m *FsOrderModel) RowSelectBuilder(selectData []string) *gorm.DB { @@ -124,6 +136,27 @@ func (m *FsOrderModel) FindCount(ctx context.Context, countBuilder *gorm.DB, fil } } +// 事务 +func (m *FsOrderModel) Trans(ctx context.Context, fn func(ctx context.Context, connGorm *gorm.DB) error) error { + tx := m.db.Table(m.name).WithContext(ctx).Begin() + defer func() { + if r := recover(); r != nil { + tx.Rollback() + } + }() + + if err := tx.Error; err != nil { + return err + } + + if err := fn(ctx, tx); err != nil { + tx.Rollback() + return err + } + + return tx.Commit().Error +} + func (m *FsOrderModel) TableName() string { return m.name } diff --git a/model/gmodel/fs_pay_gen.go b/model/gmodel/fs_pay_gen.go index 0ee65802..e30685b6 100644 --- a/model/gmodel/fs_pay_gen.go +++ b/model/gmodel/fs_pay_gen.go @@ -7,6 +7,7 @@ import ( // fs_pay 支付记录 type FsPay struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // + PayNo *string `gorm:"default:'';" json:"pay_no"` // 支付编号 UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户id OrderNumber *string `gorm:"default:'';" json:"order_number"` // 订单编号 TradeNo *string `gorm:"index;default:'';" json:"trade_no"` // 第三方支付编号 diff --git a/model/gmodel/fs_pay_logic.go b/model/gmodel/fs_pay_logic.go index b8de697d..885c7dbc 100644 --- a/model/gmodel/fs_pay_logic.go +++ b/model/gmodel/fs_pay_logic.go @@ -14,3 +14,25 @@ func (p *FsPayModel) GetOrderPayList(ctx context.Context, sn string, payStatus i err = p.db.WithContext(ctx).Model(&FsPay{}).Where("`order_number` = ? and `pay_status` = ? and `is_refund` = ?", sn, payStatus, isRefund).Find(&resp).Error return resp, err } + +func (p *FsPayModel) GetListByOrderNumberStage(ctx context.Context, sn string, stage int64) (resp *FsPay, err error) { + err = p.db.Table(p.name).WithContext(ctx).Model(&FsPay{}).Where("`order_number` = ? ", sn).Where("`pay_stage` = ? ", stage).Take(&resp).Error + if err != nil { + return nil, err + } + return resp, nil +} + +func (p *FsPayModel) CreateOrUpdate(ctx context.Context, req *FsPay) (resp *FsPay, err error) { + rowBuilder := p.db.Table(p.name).WithContext(ctx) + if req.Id > 0 { + err = rowBuilder.Save(req).Error + } else { + err = rowBuilder.Create(req).Error + } + return req, err +} + +func (m *FsPayModel) TableName() string { + return m.name +} diff --git a/model/gmodel/fs_product_design_logic.go b/model/gmodel/fs_product_design_logic.go index 29022eb8..a1b4fe62 100755 --- a/model/gmodel/fs_product_design_logic.go +++ b/model/gmodel/fs_product_design_logic.go @@ -34,3 +34,7 @@ func (d *FsProductDesignModel) Create(ctx context.Context, data *FsProductDesign func (d *FsProductDesignModel) UpdateBySn(ctx context.Context, sn string, data *FsProductDesign) error { return d.db.WithContext(ctx).Model(&FsProductDesign{}).Where("`sn` = ?", sn).Updates(&data).Error } + +func (m *FsProductDesignModel) TableName() string { + return m.name +} diff --git a/model/gmodel/fs_product_template_tags_gen.go b/model/gmodel/fs_product_template_tags_gen.go index db246f6e..227af16d 100644 --- a/model/gmodel/fs_product_template_tags_gen.go +++ b/model/gmodel/fs_product_template_tags_gen.go @@ -8,6 +8,7 @@ import ( type FsProductTemplateTags struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // ID Title *string `gorm:"default:'';" json:"title"` // 标题 + CoverImg *string `gorm:"default:'';" json:"cover_img"` // 封面图 Status *int64 `gorm:"default:0;" json:"status"` // 状态 1:可用 CreateAt *int64 `gorm:"default:0;" json:"create_at"` // 创建时间 } diff --git a/model/gmodel/fs_product_template_tags_logic.go b/model/gmodel/fs_product_template_tags_logic.go index f5c87b39..4c26d2fb 100755 --- a/model/gmodel/fs_product_template_tags_logic.go +++ b/model/gmodel/fs_product_template_tags_logic.go @@ -23,3 +23,12 @@ func (pt *FsProductTemplateTagsModel) FindOne(ctx context.Context, id int64, fie err = db.Take(&resp).Error return resp, err } +func (pt *FsProductTemplateTagsModel) GetList(ctx context.Context, page, limit int, orderBy string) (resp []FsProductTemplateTags, err error) { + db := pt.db.WithContext(ctx).Model(&FsProductTemplateTags{}).Where("`status` = ?", 1) + if orderBy != "" { + db = db.Order(orderBy) + } + offset := (page - 1) * limit + err = db.Offset(offset).Limit(limit).Find(&resp).Error + return resp, err +} diff --git a/model/gmodel/fs_resources_gen.go b/model/gmodel/fs_resources_gen.go new file mode 100644 index 00000000..432a8113 --- /dev/null +++ b/model/gmodel/fs_resources_gen.go @@ -0,0 +1,26 @@ +package gmodel + +import ( + "gorm.io/gorm" + "time" +) + +// fs_resources 资源表 +type FsResources struct { + ResourceId string `gorm:"primary_key;default:'';" json:"resource_id"` // 资源 ID + UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID + GuestId *int64 `gorm:"index;default:0;" json:"guest_id"` // 访客 ID + ResourceType *string `gorm:"index;default:'';" json:"resource_type"` // 资源类型 + ResourceUrl *string `gorm:"default:'';" json:"resource_url"` // 资源 URL + UploadedAt *time.Time `gorm:"index;default:'0000-00-00 00:00:00';" json:"uploaded_at"` // 上传时间 + Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式,存储图像分率 + MetaKey1 *string `gorm:"index;default:'';" json:"meta_key1"` // 需要关键信息查询的自定义属性1,可以动态增加 +} +type FsResourcesModel struct { + db *gorm.DB + name string +} + +func NewFsResourcesModel(db *gorm.DB) *FsResourcesModel { + return &FsResourcesModel{db: db, name: "fs_resources"} +} diff --git a/model/gmodel/fs_resources_logic.go b/model/gmodel/fs_resources_logic.go new file mode 100644 index 00000000..e68225aa --- /dev/null +++ b/model/gmodel/fs_resources_logic.go @@ -0,0 +1,2 @@ +package gmodel +// TODO: 使用model的属性做你想做的 \ No newline at end of file diff --git a/model/gmodel/fs_user_gen.go b/model/gmodel/fs_user_gen.go index 05b8cdd1..81d0f008 100644 --- a/model/gmodel/fs_user_gen.go +++ b/model/gmodel/fs_user_gen.go @@ -30,7 +30,7 @@ type FsUser struct { IsPhoneAdvertisement *int64 `gorm:"default:0;" json:"is_phone_advertisement"` // 是否接收短信广告 IsOpenRender *int64 `gorm:"default:0;" json:"is_open_render"` // 是否打开个性化渲染(1:开启,0:关闭) IsThousandFace *int64 `gorm:"default:0;" json:"is_thousand_face"` // 是否已经存在千人千面(1:存在,0:不存在) - IsLowRendering *int64 `gorm:"default:0;" json:"is_low_rendering"` // + IsLowRendering *int64 `gorm:"default:0;" json:"is_low_rendering"` // 是否开启低渲染模型渲染 IsRemoveBg *int64 `gorm:"default:1;" json:"is_remove_bg"` // 用户上传logo是否去除背景 } type FsUserModel struct { diff --git a/model/gmodel/var_gen.go b/model/gmodel/var_gen.go index a5916431..2fac48a5 100644 --- a/model/gmodel/var_gen.go +++ b/model/gmodel/var_gen.go @@ -79,6 +79,7 @@ type AllModelsGen struct { FsQuotationRemarkTemplate *FsQuotationRemarkTemplateModel // fs_quotation_remark_template 报价单备注模板 FsQuotationSaler *FsQuotationSalerModel // fs_quotation_saler 报价单业务员表 FsRefundReason *FsRefundReasonModel // fs_refund_reason + FsResources *FsResourcesModel // fs_resources 资源表 FsStandardLogo *FsStandardLogoModel // fs_standard_logo 标准logo FsTags *FsTagsModel // fs_tags 产品分类表 FsToolLogs *FsToolLogsModel // fs_tool_logs 3d设计工具日志表 @@ -169,6 +170,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen { FsQuotationRemarkTemplate: NewFsQuotationRemarkTemplateModel(gdb), FsQuotationSaler: NewFsQuotationSalerModel(gdb), FsRefundReason: NewFsRefundReasonModel(gdb), + FsResources: NewFsResourcesModel(gdb), FsStandardLogo: NewFsStandardLogoModel(gdb), FsTags: NewFsTagsModel(gdb), FsToolLogs: NewFsToolLogsModel(gdb), diff --git a/server/home-user-auth/internal/handler/routes.go b/server/home-user-auth/internal/handler/routes.go index 230527b4..f97fc8dd 100644 --- a/server/home-user-auth/internal/handler/routes.go +++ b/server/home-user-auth/internal/handler/routes.go @@ -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", diff --git a/server/home-user-auth/internal/handler/userorderdeletehandler.go b/server/home-user-auth/internal/handler/userorderdeletehandler.go new file mode 100644 index 00000000..4b4ca392 --- /dev/null +++ b/server/home-user-auth/internal/handler/userorderdeletehandler.go @@ -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) + } + } +} diff --git a/server/home-user-auth/internal/logic/userorderdeletelogic.go b/server/home-user-auth/internal/logic/userorderdeletelogic.go new file mode 100644 index 00000000..0842b983 --- /dev/null +++ b/server/home-user-auth/internal/logic/userorderdeletelogic.go @@ -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) +} diff --git a/server/home-user-auth/internal/logic/userorderlistlogic.go b/server/home-user-auth/internal/logic/userorderlistlogic.go index b6d2b9a4..c3a8b066 100644 --- a/server/home-user-auth/internal/logic/userorderlistlogic.go +++ b/server/home-user-auth/internal/logic/userorderlistlogic.go @@ -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 diff --git a/server/home-user-auth/internal/types/types.go b/server/home-user-auth/internal/types/types.go index e299ec9f..84c8eea9 100644 --- a/server/home-user-auth/internal/types/types.go +++ b/server/home-user-auth/internal/types/types.go @@ -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 { diff --git a/server/orders/internal/logic/getorderdetaillogic.go b/server/orders/internal/logic/getorderdetaillogic.go index 966decc3..72153f67 100644 --- a/server/orders/internal/logic/getorderdetaillogic.go +++ b/server/orders/internal/logic/getorderdetaillogic.go @@ -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{ diff --git a/server/pay/etc/pay.yaml b/server/pay/etc/pay.yaml new file mode 100644 index 00000000..dcc6a6fe --- /dev/null +++ b/server/pay/etc/pay.yaml @@ -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" \ No newline at end of file diff --git a/server/pay/internal/config/config.go b/server/pay/internal/config/config.go new file mode 100644 index 00000000..d032023a --- /dev/null +++ b/server/pay/internal/config/config.go @@ -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 + } + } +} diff --git a/server/pay/internal/handler/orderpaymentintenthandler.go b/server/pay/internal/handler/orderpaymentintenthandler.go new file mode 100644 index 00000000..d252d73c --- /dev/null +++ b/server/pay/internal/handler/orderpaymentintenthandler.go @@ -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) + } + } +} diff --git a/server/pay/internal/handler/routes.go b/server/pay/internal/handler/routes.go new file mode 100644 index 00000000..13f9ba4c --- /dev/null +++ b/server/pay/internal/handler/routes.go @@ -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), + }, + }, + ) +} diff --git a/server/pay/internal/logic/orderpaymentintentlogic.go b/server/pay/internal/logic/orderpaymentintentlogic.go new file mode 100644 index 00000000..98c5f0d0 --- /dev/null +++ b/server/pay/internal/logic/orderpaymentintentlogic.go @@ -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) +} diff --git a/server/pay/internal/svc/servicecontext.go b/server/pay/internal/svc/servicecontext.go new file mode 100644 index 00000000..e94ec174 --- /dev/null +++ b/server/pay/internal/svc/servicecontext.go @@ -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)) +} diff --git a/server/pay/internal/types/types.go b/server/pay/internal/types/types.go new file mode 100644 index 00000000..13a18778 --- /dev/null +++ b/server/pay/internal/types/types.go @@ -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 +} diff --git a/server/pay/pay.go b/server/pay/pay.go new file mode 100644 index 00000000..08c53e12 --- /dev/null +++ b/server/pay/pay.go @@ -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() +} diff --git a/server/pay/pay_test.go b/server/pay/pay_test.go new file mode 100644 index 00000000..d035c13a --- /dev/null +++ b/server/pay/pay_test.go @@ -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() +} diff --git a/server/product-template-tag/etc/product-template-tag.yaml b/server/product-template-tag/etc/product-template-tag.yaml new file mode 100644 index 00000000..253828fb --- /dev/null +++ b/server/product-template-tag/etc/product-template-tag.yaml @@ -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 \ No newline at end of file diff --git a/server/product-template-tag/internal/config/config.go b/server/product-template-tag/internal/config/config.go new file mode 100644 index 00000000..b5a0124e --- /dev/null +++ b/server/product-template-tag/internal/config/config.go @@ -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 +} diff --git a/server/product-template-tag/internal/handler/getproducttemplatetagshandler.go b/server/product-template-tag/internal/handler/getproducttemplatetagshandler.go new file mode 100644 index 00000000..cf7cf96e --- /dev/null +++ b/server/product-template-tag/internal/handler/getproducttemplatetagshandler.go @@ -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) + } + } +} diff --git a/server/product-template-tag/internal/handler/routes.go b/server/product-template-tag/internal/handler/routes.go new file mode 100644 index 00000000..b2c8f181 --- /dev/null +++ b/server/product-template-tag/internal/handler/routes.go @@ -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), + }, + }, + ) +} diff --git a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go new file mode 100644 index 00000000..68c62363 --- /dev/null +++ b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go @@ -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) +} diff --git a/server/product-template-tag/internal/svc/servicecontext.go b/server/product-template-tag/internal/svc/servicecontext.go new file mode 100644 index 00000000..4ebf8b31 --- /dev/null +++ b/server/product-template-tag/internal/svc/servicecontext.go @@ -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)) +} diff --git a/server/product-template-tag/internal/types/types.go b/server/product-template-tag/internal/types/types.go new file mode 100644 index 00000000..39866da6 --- /dev/null +++ b/server/product-template-tag/internal/types/types.go @@ -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 +} diff --git a/server/product-template-tag/product-template-tag.go b/server/product-template-tag/product-template-tag.go new file mode 100644 index 00000000..18898784 --- /dev/null +++ b/server/product-template-tag/product-template-tag.go @@ -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() +} diff --git a/server/product/internal/logic/gettagproductlistlogic.go b/server/product/internal/logic/gettagproductlistlogic.go index 8a185eea..27ce7ba5 100644 --- a/server/product/internal/logic/gettagproductlistlogic.go +++ b/server/product/internal/logic/gettagproductlistlogic.go @@ -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) diff --git a/server/product/internal/logic/homepagerecommendproductlistlogic.go b/server/product/internal/logic/homepagerecommendproductlistlogic.go index 60c4cdb0..899de460 100644 --- a/server/product/internal/logic/homepagerecommendproductlistlogic.go +++ b/server/product/internal/logic/homepagerecommendproductlistlogic.go @@ -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) diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 8109fec5..f2d27f0a 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -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"` diff --git a/server/websocket/etc/websocket.yaml b/server/websocket/etc/websocket.yaml new file mode 100644 index 00000000..7d2b68d7 --- /dev/null +++ b/server/websocket/etc/websocket.yaml @@ -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 \ No newline at end of file diff --git a/server/websocket/internal/config/config.go b/server/websocket/internal/config/config.go new file mode 100644 index 00000000..3ed0e8c4 --- /dev/null +++ b/server/websocket/internal/config/config.go @@ -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 +} diff --git a/server/websocket/internal/handler/datatransferhandler.go b/server/websocket/internal/handler/datatransferhandler.go new file mode 100644 index 00000000..8ef09842 --- /dev/null +++ b/server/websocket/internal/handler/datatransferhandler.go @@ -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) + } +} diff --git a/server/websocket/internal/handler/rendernotifyhandler.go b/server/websocket/internal/handler/rendernotifyhandler.go new file mode 100644 index 00000000..ab16cdad --- /dev/null +++ b/server/websocket/internal/handler/rendernotifyhandler.go @@ -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) + } + } +} diff --git a/server/websocket/internal/handler/routes.go b/server/websocket/internal/handler/routes.go new file mode 100644 index 00000000..859ecc5b --- /dev/null +++ b/server/websocket/internal/handler/routes.go @@ -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), + }, + }, + ) +} diff --git a/server/websocket/internal/logic/datatransferlogic.go b/server/websocket/internal/logic/datatransferlogic.go new file mode 100644 index 00000000..999bd381 --- /dev/null +++ b/server/websocket/internal/logic/datatransferlogic.go @@ -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: + + } +} diff --git a/server/websocket/internal/logic/rendernotifylogic.go b/server/websocket/internal/logic/rendernotifylogic.go new file mode 100644 index 00000000..06ab1172 --- /dev/null +++ b/server/websocket/internal/logic/rendernotifylogic.go @@ -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) +} diff --git a/server/websocket/internal/logic/ws_render_image_logic.go b/server/websocket/internal/logic/ws_render_image_logic.go new file mode 100644 index 00000000..634b6267 --- /dev/null +++ b/server/websocket/internal/logic/ws_render_image_logic.go @@ -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: + + } + } + } +} diff --git a/server/websocket/internal/svc/servicecontext.go b/server/websocket/internal/svc/servicecontext.go new file mode 100644 index 00000000..9a22471e --- /dev/null +++ b/server/websocket/internal/svc/servicecontext.go @@ -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)) +} diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go new file mode 100644 index 00000000..c8aa0da8 --- /dev/null +++ b/server/websocket/internal/types/types.go @@ -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 +} diff --git a/server/websocket/websocket.go b/server/websocket/websocket.go new file mode 100644 index 00000000..c5e7bdd0 --- /dev/null +++ b/server/websocket/websocket.go @@ -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() +} diff --git a/server_api/home-user-auth.api b/server_api/home-user-auth.api index eb7852e2..9d705ab4 100644 --- a/server_api/home-user-auth.api +++ b/server_api/home-user-auth.api @@ -10,50 +10,82 @@ info ( import "basic.api" service home-user-auth { - + // @handler UserRegisterHandler // post /api/user/register(RequestUserRegister) returns (response); +<<<<<<< HEAD +======= + + @handler UserLoginHandler + post /api/user/login(RequestUserLogin) returns (response); + + @handler AcceptCookieHandler + post /api/user/accept-cookie(request) returns (response); + +>>>>>>> d843fff73d7ba5d4e14a5d86281d5166f04cb303 @handler UserFontsHandler get /api/user/fonts(request) returns (response); - + @handler UserGetTypeHandler get /api/user/get-type(request) returns (response); - + @handler UserSaveBasicInfoHandler post /api/user/basic-info(RequestBasicInfoForm) returns (response); - + @handler UserStatusConfigHandler get /api/user/status-config(request) returns (response); - + @handler UserBasicInfoHandler get /api/user/basic-info(request) returns (response); - + @handler UserAddressListHandler get /api/user/address-list(request) returns (response); - + @handler UserAddAddressHandler post /api/user/add-address(RequestAddAddress) returns (response); - + @handler UserContactServiceHandler post /api/user/contact-service (RequestContactService) returns (response); - + // @handler UserOderListHandler // get /api/user/order-list(RequestOrderId) returns (response); - + @handler UserOderDeleteHandler post /api/user/order-delete(RequestOrderId) returns (response); +<<<<<<< HEAD +======= + + @handler UserGoogleLoginHandler + get /api/user/oauth2/login/google(RequestGoogleLogin) returns (response); + + @handler UserEmailRegisterHandler + get /api/user/oauth2/login/register(RequestEmailRegister) returns (response); + +>>>>>>> d843fff73d7ba5d4e14a5d86281d5166f04cb303 //订单列表 @handler UserOrderListHandler get /api/user/order-list (UserOrderListReq) returns (response); - + + //删除订单 + @handler UserOrderDeleteHandler + get /api/user/order-delete (UserOrderDeleteReq) returns (response); + //取消订单 @handler UserOrderCancelHandler get /api/user/order-cancel (UserOrderCancelReq) returns (response); - + } +type ( + UserOrderDeleteReq { + ID int64 `form:"id"` //订单id + } + UserOrderDeleteRes { + } +) + //取消订单 type ( UserOrderCancelReq { @@ -83,25 +115,25 @@ type ( ) type Items { - ID int64 `json:"id"` - Sn string `json:"sn"` - UserID int64 `json:"user_id"` - TotalAmount int64 `json:"total_amount"` - Ctime string `json:"ctime"` - Status int64 `json:"status"` - DeliveryMethod int64 `json:"delivery_method"` - TsTime string `json:"ts_time"` - IsPayCompleted int64 `json:"is_pay_completed"` - DeliverSn string `json:"deliver_sn"` - PcsBox int64 `json:"pcs_box"` - Pcs int64 `json:"pcs"` - SurplusAt int64 `json:"surplus_at"` - LogisticsStatus int64 `json:"logistics_status"` - StatusTimes []*StatusTime `json:"status_times"` - Deposit int64 `json:"deposit"` - Remaining int64 `json:"remaining"` - ProductList []*Product `json:"productList"` - IsStop int64 `json:"is_stop"` + 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 { diff --git a/server_api/pay.api b/server_api/pay.api new file mode 100644 index 00000000..f94d77a9 --- /dev/null +++ b/server_api/pay.api @@ -0,0 +1,29 @@ +syntax = "v1" + +info ( + title: 支付模块 + desc: 支付相关 + author: "" + email: "" +) + +import "basic.api" + +service pay { + + @handler OrderPaymentIntentHandler + post /api/pay/payment-intent(OrderPaymentIntentReq) returns (response); +} + +// 生成预付款 +type ( + OrderPaymentIntentReq { + Sn string `form:"sn"` //订单编号 + DeliveryMethod int64 `form:"delivery_method"` //发货方式 + AddressId int64 `form:"address_id"` //地址id + PayMethod int64 `form:"pay_method"` //支付方式 + } + OrderPaymentIntentRes { + RedirectUrl string `json:"redirect_url"` + } +) \ No newline at end of file diff --git a/server_api/product-template-tag.api b/server_api/product-template-tag.api new file mode 100644 index 00000000..ae62099a --- /dev/null +++ b/server_api/product-template-tag.api @@ -0,0 +1,25 @@ +syntax = "v1" + +info ( + title: "产品模板标签服务"// TODO: add title + desc: // TODO: add description + author: "" + email: "" +) + +import "basic.api" + +service product-template-tag { + //获取产品模板标签列表 + @handler GetProductTemplateTagsHandler + get /api/product-template/get_product_template_tags(GetProductTemplateTagsReq) returns (response); +} + +//获取产品模板标签列表 +type GetProductTemplateTagsReq { + Limit int `form:"limit"` +} +type GetProductTemplateTagsRsp { + Tag string `json:"tag"` + Cover string `json:"cover"` +} \ No newline at end of file diff --git a/server_api/product.api b/server_api/product.api index a7160506..48ac5a50 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -308,29 +308,24 @@ type GetTagProductListRsp { TagList []TagItem `json:"tag_list"` } type TagItem { - 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 { ProductId int64 `json:"product_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"` HaveOptionalFitting bool `json:"have_optional_fitting"` + Recommended bool `json:"recommended"` } //获取云渲染设计方案信息 type GetRenderDesignReq { @@ -434,14 +429,10 @@ type HomePageRecommendProductListReq { Size uint32 `form:"size"` } type HomePageRecommendProductListRsp { - 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"` diff --git a/server_api/websocket.api b/server_api/websocket.api new file mode 100644 index 00000000..c2dc6430 --- /dev/null +++ b/server_api/websocket.api @@ -0,0 +1,47 @@ +syntax = "v1" + +info ( + title: "websocket"// TODO: add title + desc: // TODO: add description + author: "" + email: "" +) + +import "basic.api" +service websocket { + //websocket数据交互 + @handler DataTransferHandler + get /api/websocket/data_transfer(request) returns (response); + //渲染完了通知接口 + @handler RenderNotifyHandler + post /api/websocket/render_notify(RenderNotifyReq) returns (response); +} + +//websocket数据交互 +type DataTransferData { + T string `json:"t"` //消息类型 + D interface{} `json:"d"` //传递的消息 +} +type RenderImageReqMsg { //websocket接受要云渲染处理的数据 + ProductIds []int64 `json:"product_ids"` //产品 id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 +} +type RenderImageRspMsg { //websocket发送渲染完的数据 + 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 { + Sign string `json:"sign"` + Time int64 `json:"time"` + Info NotifyInfo `json:"info"` +} +type NotifyInfo { + ProductId int64 `json:"product_id"` //产品id + TemplateTagId int64 `json:"template_tag_id"` //模板标签id + AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 + Image string `json:"image"` +} \ No newline at end of file diff --git a/utils/configs/order.go b/utils/configs/order.go index 291ac401..eb1f8d6b 100644 --- a/utils/configs/order.go +++ b/utils/configs/order.go @@ -1,8 +1,75 @@ package configs +import ( + "context" + "encoding/json" + "errors" + "fusenapi/constants" + "fusenapi/model/gmodel" + "strconv" + + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" +) + type WebSetTimeInfo struct { ProductDay int64 FactoryDeliverDay int64 DeliverUpsDay int64 UpsTransDay int64 } + +type WebSetTimeInfoData struct { + ProductDay string `json:"product_day"` + FactoryDeliverDay string `json:"factory_deliver_day"` + DeliverUpsDay string `json:"deliver_ups_day"` + UpsTransDay string `json:"ups_trans_day"` +} + +func GetOrderTimeConfig(ctx context.Context, db *gorm.DB) (res WebSetTimeInfo, err error) { + resData, err := gmodel.NewFsWebSetModel(db).FindValueByKey(ctx, string(constants.WEBSET_TIME_INFO)) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + res.ProductDay = int64(constants.ORDER_PRODUCT_DAY) + res.FactoryDeliverDay = int64(constants.ORDER_FACTORY_DELIVER_DAY) + res.DeliverUpsDay = int64(constants.ORDER_DELIVER_UPS_DAY) + res.UpsTransDay = int64(constants.ORDER_UPS_TRANS_DAY) + return res, nil + } + logx.Error(err) + return res, err + } else { + var timeInfo WebSetTimeInfoData + err = json.Unmarshal([]byte(*resData.Value), &timeInfo) + if err != nil { + logx.Error(err) + return res, err + } + productDay, err := strconv.Atoi(timeInfo.ProductDay) + if err != nil { + logx.Error(err) + return res, err + } + factoryDeliverDay, err := strconv.Atoi(timeInfo.FactoryDeliverDay) + if err != nil { + logx.Error(err) + return res, err + } + deliverUpsDay, err := strconv.Atoi(timeInfo.DeliverUpsDay) + if err != nil { + logx.Error(err) + return res, err + } + upsTransDay, err := strconv.Atoi(timeInfo.UpsTransDay) + if err != nil { + logx.Error(err) + return res, err + } + res.ProductDay = int64(productDay) + res.FactoryDeliverDay = int64(factoryDeliverDay) + res.DeliverUpsDay = int64(deliverUpsDay) + res.UpsTransDay = int64(upsTransDay) + } + + return res, nil +} diff --git a/utils/id_generator/wesocket.go b/utils/id_generator/wesocket.go new file mode 100644 index 00000000..62f21adf --- /dev/null +++ b/utils/id_generator/wesocket.go @@ -0,0 +1,23 @@ +package id_generator + +import "sync" + +type WebsocketId struct { + nodeId uint64 + count uint64 + mu sync.Mutex +} + +func (wid *WebsocketId) Get() uint64 { + wid.mu.Lock() + defer wid.mu.Unlock() + wid.count++ + return (wid.count << 8) | wid.nodeId +} + +func NewWebsocketId(NodeId uint8) *WebsocketId { + return &WebsocketId{ + nodeId: uint64(NodeId), + count: 0, + } +} diff --git a/utils/order/order_status.go b/utils/order/order_status.go index e12af5a8..8198e6d4 100644 --- a/utils/order/order_status.go +++ b/utils/order/order_status.go @@ -37,6 +37,8 @@ type GetOrderStatusAndLogisticsReq struct { OrderStatus constants.Order DeliveryMethod constants.DeliveryMethod IsPayCompleted int64 + OrderCtime int64 + SureTime int64 ProductTime int64 ProductEndtime int64 @@ -46,7 +48,6 @@ type GetOrderStatusAndLogisticsReq struct { ArrivalTime int64 RecevieTime int64 - OrderCtime int64 WebSetTimeInfo configs.WebSetTimeInfo } @@ -70,11 +71,11 @@ func GetOrderStatusAndLogistics(req GetOrderStatusAndLogisticsReq) (res GetOrder times := make([]GetOrderStatusAndLogisticsResTimes, 5) m := 1 for i := 0; i < 5; i++ { - m++ times[i] = GetOrderStatusAndLogisticsResTimes{ Key: m, Time: "", } + m++ } switch req.OrderStatus { @@ -160,6 +161,9 @@ func GetOrderStatusAndLogistics(req GetOrderStatusAndLogisticsReq) (res GetOrder res.Times = times } else { timesData := times[0:3] + if logisticsStatus == constants.LOGISTICS_STATUS_DRAW { + timesData[1].Time = format.TimeIntToFormat(req.OrderCtime + req.WebSetTimeInfo.ProductDay*daySecond) + } res.Times = timesData } return res diff --git a/utils/pay/pay.go b/utils/pay/pay.go new file mode 100644 index 00000000..005d700d --- /dev/null +++ b/utils/pay/pay.go @@ -0,0 +1,40 @@ +package pay + +import "fusenapi/constants" + +type Config struct { + // stripe支付 + Stripe Stripe +} + +// NewPayDriver 实例化方法 +func NewPayDriver(PayMethod int64, config *Config) Pay { + switch PayMethod { + case int64(constants.PAYMETHOD_STRIPE): + return &Stripe{Key: config.Stripe.Key} + default: + return &Stripe{Key: config.Stripe.Key} + } + +} + +// Pay 支付集成接口 +type Pay interface { + GeneratePrepayment(req *GeneratePrepaymentReq) (res *GeneratePrepaymentRes, err error) +} + +type GeneratePrepaymentReq struct { + Amount int64 `json:"amount"` // 支付金额 + Currency string `json:"currency"` // 支付货币 + ProductName string `json:"product_name"` // 商品名称 + ProductDescription string `json:"product_description"` // 商品描述 + ProductImages []*string `json:"product_imageso"` // 商品照片 + Quantity int64 `json:"quantity"` //数量 + SuccessURL string `json:"success_url"` // 支付成功回调 + CancelURL string `json:"cancel_url"` // 支付取消回调 +} + +type GeneratePrepaymentRes struct { + URL string `json:"url"` // 支付重定向地址 + TradeNo string `json:"trade_no"` //交易ID +} diff --git a/utils/pay/stripe.go b/utils/pay/stripe.go new file mode 100644 index 00000000..9dc5a2cf --- /dev/null +++ b/utils/pay/stripe.go @@ -0,0 +1,58 @@ +package pay + +import ( + "github.com/stripe/stripe-go/v74" + "github.com/stripe/stripe-go/v74/checkout/session" +) + +type Stripe struct { + Key string `json:"key"` +} + +// 生成预付款 +func (stripePay *Stripe) GeneratePrepayment(req *GeneratePrepaymentReq) (res *GeneratePrepaymentRes, err error) { + var productData stripe.CheckoutSessionLineItemPriceDataProductDataParams + + if req.ProductName != "" { + productData.Name = stripe.String(req.ProductName) + } + + if req.ProductDescription != "" { + productData.Description = stripe.String(req.ProductDescription) + } + + if len(req.ProductImages) > 0 { + productData.Images = req.ProductImages + } + // var images = make([]*string, 1) + // var image string = "https://img2.woyaogexing.com/2023/07/25/133132a32b9f79cfad84965a4bea57e0.jpg" + // images[0] = stripe.String(image) + // productData.Images = images + stripe.Key = stripePay.Key + + params := &stripe.CheckoutSessionParams{ + LineItems: []*stripe.CheckoutSessionLineItemParams{ + { + PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{ + Currency: stripe.String(req.Currency), + ProductData: &productData, + UnitAmount: stripe.Int64(req.Amount), + }, + Quantity: stripe.Int64(req.Quantity), + }, + }, + Mode: stripe.String(string(stripe.CheckoutSessionModePayment)), + SuccessURL: stripe.String(req.SuccessURL), + CancelURL: stripe.String(req.CancelURL), + } + result, err := session.New(params) + + if err != nil { + return nil, err + } + + return &GeneratePrepaymentRes{ + URL: result.URL, + TradeNo: result.ID, + }, err +}