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

This commit is contained in:
eson 2023-06-12 17:10:51 +08:00
commit c71fa6fc12
44 changed files with 944 additions and 242 deletions

View File

@ -1,4 +1,6 @@
package gorm
package gmodel
import "gorm.io/gorm"
type FsAddress struct {
Id int64 `gorm:"primary_key" json:"id"`
@ -16,3 +18,11 @@ type FsAddress struct {
Status *int64 `gorm:"default:1" json:"status"` // 1正常 0异常
IsDefault *int64 `gorm:"default:0" json:"is_default"` // 1默认地址0非默认地址
}
type FsAddressModel struct {
db *gorm.DB
}
func NewFsAddressModel(db *gorm.DB) *FsAddressModel {
return &FsAddressModel{db}
}

View File

@ -0,0 +1,42 @@
package gmodel
import (
"context"
"gorm.io/gorm"
)
type FsCanteenProduct struct {
Id int64 `gorm:"primary_key" json:"id"` // ID
CanteenType *int64 `gorm:"default:0" json:"canteen_type"` // 餐厅类别id
ProductId *int64 `gorm:"default:0" json:"product_id"` // 产品id
SizeId *int64 `gorm:"default:0" json:"size_id"` // 尺寸id
Sort *int64 `gorm:"default:0" json:"sort"` // 排序
Status *int64 `gorm:"default:1" json:"status"` // 状态位 1启用0停用
Ctime *int64 `gorm:"default:0" json:"ctime"` // 添加时间
Sid *string `gorm:"default:''" json:"sid"` // 前端带入的id
}
type FsCanteenProductModel struct {
db *gorm.DB
}
func NewFsCanteenProductModel(db *gorm.DB) *FsCanteenProductModel {
return &FsCanteenProductModel{db}
}
func (c *FsCanteenProductModel) GetAllByCanteenTypeId(ctx context.Context, typeId int64) (resp []FsCanteenProduct, err error) {
err = c.db.WithContext(ctx).Model(&FsCanteenProduct{}).Where("`canteen_type` = ? and `status` = ?", typeId, 1).Find(&resp).Error
if err != nil {
return nil, err
}
return nil, err
}
func (c *FsCanteenProductModel) UpdateById(ctx context.Context, id int64, data *FsCanteenProduct) error {
return c.db.WithContext(ctx).Model(&FsCanteenProduct{}).Where("`id` = ? ", id).Updates(&data).Error
}
func (c *FsCanteenProductModel) UpdateByIdArr(ctx context.Context, ids []int64, data *FsCanteenProduct) error {
return c.db.WithContext(ctx).Model(&FsCanteenProduct{}).Where("`id` in (?) ", ids).Updates(&data).Error
}
func (c *FsCanteenProductModel) Create(ctx context.Context, data *FsCanteenProduct) error {
return c.db.WithContext(ctx).Model(&FsCanteenProduct{}).Create(data).Error
}

View File

@ -0,0 +1,30 @@
package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
type FsCanteenType struct {
Id int64 `gorm:"primary_key" json:"id"` // ID
Name *string `gorm:"default:''" json:"name"` // 餐厅名字
Sort *int64 `gorm:"default:0" json:"sort"` // 排序
Status *int64 `gorm:"default:1" json:"status"` // 状态位 1启用0停用
Ctime *int64 `gorm:"default:0" json:"ctime"` // 添加时间
}
type FsCanteenTypeModel struct {
db *gorm.DB
}
func NewFsCanteenTypeModel(db *gorm.DB) *FsCanteenTypeModel {
return &FsCanteenTypeModel{db}
}
func (c *FsCanteenTypeModel) FindOne(ctx context.Context, id int64) (resp FsCanteenType, err error) {
err = c.db.WithContext(ctx).Model(&FsCanteenType{}).Where("`id` = ? and `status` = ? ", id, 1).First(&resp).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return FsCanteenType{}, err
}
return
}

View File

@ -1,6 +1,9 @@
package gorm
package gmodel
import "database/sql"
import (
"database/sql"
"gorm.io/gorm"
)
type FsFaq struct {
Id int64 `gorm:"primary_key" json:"id"`
@ -12,3 +15,10 @@ type FsFaq struct {
Sort *int64 `gorm:"default:0" json:"sort"` // 排序
Ctime *sql.NullInt64 `gorm:"default:0" json:"ctime"` // 添加时间
}
type FsFaqModel struct {
db *gorm.DB
}
func NewFsFaqModel(db *gorm.DB) *FsFaqModel {
return &FsFaqModel{db}
}

View File

@ -1,4 +1,6 @@
package gorm
package gmodel
import "gorm.io/gorm"
type FsFont struct {
Id int64 `gorm:"primary_key" json:"id"` // id
@ -7,3 +9,10 @@ type FsFont struct {
FilePath *string `gorm:"default:''" json:"file_path"` // 字体文件路径
Sort *int64 `gorm:"default:0" json:"sort"` // 排序
}
type FsFontModel struct {
db *gorm.DB
}
func NewFsFontModel(db *gorm.DB) *FsFontModel {
return &FsFontModel{db}
}

View File

@ -1,4 +1,9 @@
package gorm
package gmodel
import (
"context"
"gorm.io/gorm"
)
type FsProduct struct {
Id int64 `gorm:"primary_key" json:"id"`
@ -31,3 +36,48 @@ type FsProduct struct {
RecommendProductSort *string `gorm:"default:''" json:"recommend_product_sort"` // 推荐排序例如:1324
SceneIds *string `gorm:"default:''" json:"scene_ids"` // 关联的场景id
}
type FsProductModel struct {
db *gorm.DB
}
func NewFsProductModel(db *gorm.DB) *FsProductModel {
return &FsProductModel{db}
}
func (p *FsProductModel) GetProductListByIds(ctx context.Context, productIds []int64, sort string) (resp []FsProduct, err error) {
db := p.db.Model(&FsProduct{}).
Where("`id` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?", productIds, 0, 1, 1)
switch sort {
case "sort-asc":
db = db.Order("sort ASC")
case "sort-desc":
db = db.Order("sort DESC")
}
if err = db.Find(&resp).Error; err != nil {
return nil, err
}
return
}
func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTypes []int64, sort string) (resp []FsProduct, err error) {
db := p.db.WithContext(ctx).Model(&FsProductModel{}).Where("`type` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?", productTypes, 0, 1, 1)
switch sort {
case "sort-asc":
db = db.Order("`sort` ASC")
case "sort-desc":
db = db.Order("`sort` DESC")
}
err = db.Find(&resp).Error
if err != nil {
return nil, err
}
return
}
func (p *FsProductModel) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error) {
err = p.db.WithContext(ctx).Model(&FsProductModel{}).
Where("`is_del` =? and `is_shelf` = ?", 0, 1).Order("RAND()").Limit(limit).Find(&resp).Error
if err != nil {
return nil, err
}
return
}

View File

@ -1,4 +1,6 @@
package gorm
package gmodel
import "gorm.io/gorm"
type FsProductModel3dLight struct {
Id int64 `gorm:"primary_key" json:"id"`
@ -7,3 +9,10 @@ type FsProductModel3dLight struct {
Status *int64 `gorm:"default:1" json:"status"` // 状态值1显示0删除
Ctime *int64 `gorm:"default:0" json:"ctime"` // 创建时间
}
type FsProductModel3dLightModel struct {
db *gorm.DB
}
func NewFsProductModel3dLightModel(db *gorm.DB) *FsProductModel3dLightModel {
return &FsProductModel3dLightModel{db}
}

View File

@ -1,4 +1,6 @@
package gorm
package gmodel
import "gorm.io/gorm"
type FsProductModel3d struct {
Id int64 `gorm:"primary_key" json:"id"`
@ -20,3 +22,10 @@ type FsProductModel3d struct {
Price *int64 `gorm:"default:0" json:"price"` // 仅配件用,配件的价格, 单位:美分
Sku *string `gorm:"default:''" json:"sku"` // sku
}
type FsProductModel3dModel struct {
db *gorm.DB
}
func NewFsProductModel3dModel(db *gorm.DB) *FsProductModel3dModel {
return &FsProductModel3dModel{db}
}

View File

@ -1,4 +1,9 @@
package gorm
package gmodel
import (
"context"
"gorm.io/gorm"
)
type FsProductPrice struct {
Id int64 `gorm:"primary_key" json:"id"`
@ -15,3 +20,34 @@ type FsProductPrice struct {
Status *int64 `gorm:"default:1" json:"status"` // 是否可用
IsDefault *int64 `gorm:"default:0" json:"is_default"` // 是否默认
}
type FsProductPriceModel struct {
db *gorm.DB
}
func NewFsProductPriceModel(db *gorm.DB) *FsProductPriceModel {
return &FsProductPriceModel{db}
}
type GetPriceListByProductIdsRsp struct {
ProductId int64 `json:"product_id"`
Price string `json:"price"`
}
func (p *FsProductPriceModel) GetPriceListByProductIds(ctx context.Context, productIds []int64) (resp []GetPriceListByProductIdsRsp, err error) {
if len(productIds) == 0 {
return nil, nil
}
db := p.db.WithContext(ctx).Model(&FsProductPriceModel{}).Select("product_id,group_concat(step_price) as price").
Where("`product_id` in (?) and `status` = ? group by product_id", productIds, 1)
if err = db.Find(&resp).Error; err != nil {
return nil, err
}
return
}
func (p *FsProductPriceModel) GetPriceListBySizeIds(ctx context.Context, sizeIds []int64) (resp []FsProductPrice, err error) {
err = p.db.WithContext(ctx).Model(&FsProductPriceModel{}).Where("`size_id` in (?) and `status` = ? ", sizeIds, 1).Find(&resp).Error
if err != nil {
return nil, err
}
return
}

View File

@ -0,0 +1,61 @@
package gmodel
import (
"context"
"gorm.io/gorm"
)
type FsProductSize struct {
Id int64 `gorm:"primary_key" json:"id"`
ProductId *int64 `gorm:"default:0" json:"product_id"` // 产品ID
Title *string `gorm:"default:''" json:"title"` // 标题 10*10*20
Cover *string `gorm:"default:''" json:"cover"` // 封面图
CoverImg *string `gorm:"default:''" json:"cover_img"` // 背景图
Capacity *string `gorm:"default:''" json:"capacity"` // 自己填的尺寸名称
Status *int64 `gorm:"default:1" json:"status"` // 状态位 1显示 0删除
Sort *int64 `gorm:"default:50" json:"sort"` // 排序
Remark *string `gorm:"default:''" json:"remark"` // 备注信息
PartsCanDeleted *int64 `gorm:"default:1" json:"parts_can_deleted"` // 配件是否可移除 1是0否
}
type FsProductSizeModel struct {
db *gorm.DB
}
func NewFsProductSizeModel(db *gorm.DB) *FsProductSizeModel {
return &FsProductSizeModel{db}
}
func (s *FsProductSizeModel) GetAllByIds(ctx context.Context, ids []int64, sort string) (resp []FsProductSize, err error) {
db := s.db.Model(&FsProductSizeModel{}).Where("`id` in (?) and `status` = ?", ids, 1)
switch sort {
case "sort-asc":
db = db.Order("`sort` ASC")
case "sort-desc":
db = db.Order("`sort` DESC")
}
if err = db.Find(&resp).Error; err != nil {
return nil, err
}
return
}
func (s *FsProductSizeModel) CountByStatus(ctx context.Context, status int) (total int64, err error) {
err = s.db.WithContext(ctx).Model(&FsProductSizeModel{}).Where("`status` = ? ", status).Count(&total).Error
if err != nil {
return 0, err
}
return
}
func (s *FsProductSizeModel) GetAllByProductIds(ctx context.Context, productIds []int64, sort string) (resp []FsProductSize, err error) {
db := s.db.WithContext(ctx).Model(&FsProductSizeModel{}).Where("`product_id` in(?) and `status` = ?", productIds, 1)
switch sort {
case "sort-asc":
db = db.Order("`sort` ASC")
case "sort-desc":
db = db.Order("`sort` DESC")
}
err = db.Find(&resp).Error
if err != nil {
return nil, err
}
return
}

View File

@ -1,4 +1,6 @@
package gorm
package gmodel
import "gorm.io/gorm"
type FsProductTemplateTags struct {
Id int64 `gorm:"primary_key" json:"id"` // ID
@ -6,3 +8,10 @@ type FsProductTemplateTags struct {
Status *int64 `gorm:"default:1" json:"status"` // 状态 1可用 0不可用
CreateAt *int64 `gorm:"default:0" json:"create_at"` // 创建时间
}
type FsProductTemplateTagsModel struct {
db *gorm.DB
}
func NewFsProductTemplateTagsModel(db *gorm.DB) *FsProductTemplateTagsModel {
return &FsProductTemplateTagsModel{db}
}

View File

@ -1,4 +1,9 @@
package gorm
package gmodel
import (
"context"
"gorm.io/gorm"
)
type FsProductTemplateV2 struct {
Id int64 `gorm:"primary_key" json:"id"`
@ -18,3 +23,17 @@ type FsProductTemplateV2 struct {
Tag *string `gorm:"default:''" json:"tag"` // 标签(用户自填)
IsDel *int64 `gorm:"default:0" json:"is_del"` // 是否删除 1删除
}
type FsProductTemplateV2Model struct {
db *gorm.DB
}
func NewFsProductTemplateV2Model(db *gorm.DB) *FsProductTemplateV2Model {
return &FsProductTemplateV2Model{db}
}
func (t *FsProductTemplateV2Model) FindAllByProductIds(ctx context.Context, productIds []int64) (resp []FsProductTemplateV2, err error) {
err = t.db.WithContext(ctx).Model(&FsProductTemplateV2Model{}).Where("`id` in (?) and `is_del` = ? and `status` = ?", productIds, 0, 1).Find(&resp).Error
if err != nil {
return nil, err
}
return
}

View File

@ -1,4 +1,10 @@
package gorm
package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
type FsQrcodeSet struct {
Id int64 `gorm:"primary_key" json:"id"` // id
@ -14,3 +20,24 @@ type FsQrcodeSet struct {
Ctime *int64 `gorm:"default:0" json:"ctime"` // 添加时间
Utime *int64 `gorm:"default:0" json:"utime"` // 更新时间
}
type FsQrcodeSetModel struct {
db *gorm.DB
}
func NewFsQrcodeSetModel(db *gorm.DB) *FsQrcodeSetModel {
return &FsQrcodeSetModel{db}
}
func (q *FsQrcodeSetModel) GetAll(ctx context.Context) (resp []FsQrcodeSet, err error) {
err = q.db.WithContext(ctx).Model(&FsQrcodeSetModel{}).Where("`status` = ?", 1).Find(&resp).Error
if err != nil {
return nil, err
}
return
}
func (q *FsQrcodeSetModel) FindOne(ctx context.Context, id int64) (resp FsQrcodeSet, err error) {
err = q.db.WithContext(ctx).Model(&FsQrcodeSetModel{}).Where("`id` = ?", id).First(&resp).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return FsQrcodeSet{}, err
}
return
}

View File

@ -0,0 +1,28 @@
package gmodel
import (
"context"
"gorm.io/gorm"
)
type FsStandardLogo struct {
Id int64 `gorm:"primary_key" json:"id"` // ID
Name *string `gorm:"default:''" json:"name"` // logo名称
Image *string `gorm:"default:''" json:"image"` // 图片地址
Ctime *int64 `gorm:"default:0" json:"ctime"` // 添加时间
Status *int64 `gorm:"default:1" json:"status"` // 状态 1正常 0删除
}
type FsStandardLogoModel struct {
db *gorm.DB
}
func NewFsStandardLogoModel(db *gorm.DB) *FsStandardLogoModel {
return &FsStandardLogoModel{db}
}
func (l *FsStandardLogoModel) GetAll(ctx context.Context) (resp []FsStandardLogo, err error) {
err = l.db.WithContext(ctx).Model(&FsStandardLogoModel{}).Where("`status` = ? ", 1).Find(&resp).Error
if err != nil {
return nil, err
}
return
}

View File

@ -1,4 +1,10 @@
package gorm
package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
type FsTags struct {
Id int64 `gorm:"primary_key" json:"id"` // ID
@ -13,3 +19,31 @@ type FsTags struct {
RecommendProduct *string `gorm:"default:''" json:"recommend_product"` // 推荐产品id例如: 1,3,4,5
RecommendProductSort *string `gorm:"default:''" json:"recommend_product_sort"` // 推荐排序例如:1324
}
type FsTagsModel struct {
db *gorm.DB
}
func NewFsTagsModel(db *gorm.DB) *FsTagsModel {
return &FsTagsModel{db}
}
func (t *FsTagsModel) FindOne(ctx context.Context, id int64) (resp FsTags, err error) {
err = t.db.WithContext(ctx).Model(&FsTagsModel{}).Where("`id` = ? and `status` = ?", id, 1).First(&resp).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return FsTags{}, err
}
return
}
func (t *FsTagsModel) GetAllByIds(ctx context.Context, ids []int64) (resp []FsTags, err error) {
err = t.db.WithContext(ctx).Model(&FsTagsModel{}).Where("`id` in(?) and `status` = ?", ids, 1).Find(&resp).Error
if err != nil {
return nil, err
}
return
}
func (t *FsTagsModel) GetAllByLevel(ctx context.Context, level int) (resp []FsTags, err error) {
err = t.db.Model(&FsTagsModel{}).Where("`level` = ? and `status` = ?", level, 1).Find(&resp).Error
if err != nil {
return nil, err
}
return
}

View File

@ -1,4 +1,10 @@
package gorm
package gmodel
import (
"context"
"errors"
"gorm.io/gorm"
)
type FsUser struct {
Id int64 `gorm:"primary_key" json:"id"` // ID
@ -28,3 +34,18 @@ type FsUser struct {
IsLowRendering int64 `gorm:"default:0" json:"is_low_rendering"` // 是否开启低渲染模型渲染
IsRemoveBg int64 `gorm:"default:1" json:"is_remove_bg"` // 用户上传logo是否去除背景
}
type FsUserModel struct {
db *gorm.DB
}
func NewFsUserModel(db *gorm.DB) *FsUserModel {
return &FsUserModel{db}
}
func (u *FsUserModel) FindOne(ctx context.Context, id int64) (resp FsUser, err error) {
err = u.db.WithContext(ctx).Model(&FsUserModel{}).Where("`id` = ? and is_del = ?", id, 0).First(&resp).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return FsUser{}, err
}
return
}

View File

@ -1,12 +0,0 @@
package gorm
type FsCanteenProduct struct {
Id int64 `gorm:"primary_key" json:"id"` // ID
CanteenType *int64 `gorm:"default:0" json:"canteen_type"` // 餐厅类别id
ProductId *int64 `gorm:"default:0" json:"product_id"` // 产品id
SizeId *int64 `gorm:"default:0" json:"size_id"` // 尺寸id
Sort *int64 `gorm:"default:0" json:"sort"` // 排序
Status *int64 `gorm:"default:1" json:"status"` // 状态位 1启用0停用
Ctime *int64 `gorm:"default:0" json:"ctime"` // 添加时间
Sid *string `gorm:"default:''" json:"sid"` // 前端带入的id
}

View File

@ -1,9 +0,0 @@
package gorm
type FsCanteenType struct {
Id int64 `gorm:"primary_key" json:"id"` // ID
Name *string `gorm:"default:''" json:"name"` // 餐厅名字
Sort *int64 `gorm:"default:0" json:"sort"` // 排序
Status *int64 `gorm:"default:1" json:"status"` // 状态位 1启用0停用
Ctime *int64 `gorm:"default:0" json:"ctime"` // 添加时间
}

View File

@ -1,14 +0,0 @@
package gorm
type FsProductSize struct {
Id int64 `gorm:"primary_key" json:"id"`
ProductId *int64 `gorm:"default:0" json:"product_id"` // 产品ID
Title *string `gorm:"default:''" json:"title"` // 标题 10*10*20
Cover *string `gorm:"default:''" json:"cover"` // 封面图
CoverImg *string `gorm:"default:''" json:"cover_img"` // 背景图
Capacity *string `gorm:"default:''" json:"capacity"` // 自己填的尺寸名称
Status *int64 `gorm:"default:1" json:"status"` // 状态位 1显示 0删除
Sort *int64 `gorm:"default:50" json:"sort"` // 排序
Remark *string `gorm:"default:''" json:"remark"` // 备注信息
PartsCanDeleted *int64 `gorm:"default:1" json:"parts_can_deleted"` // 配件是否可移除 1是0否
}

View File

@ -1,9 +0,0 @@
package gorm
type FsStandardLogo struct {
Id int64 `gorm:"primary_key" json:"id"` // ID
Name *string `gorm:"default:''" json:"name"` // logo名称
Image *string `gorm:"default:''" json:"image"` // 图片地址
Ctime *int64 `gorm:"default:0" json:"ctime"` // 添加时间
Status *int64 `gorm:"default:1" json:"status"` // 状态 1正常 0删除
}

View File

@ -7,15 +7,39 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/canteen/internal/logic"
"fusenapi/server/canteen/internal/svc"
"fusenapi/server/canteen/internal/types"
)
// 获取餐厅详情
func GetCanteenDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
var req types.GetCanteenDetailReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
@ -24,9 +48,11 @@ func GetCanteenDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetCanteenDetailLogic(r.Context(), svcCtx)
resp := l.GetCanteenDetail(&req)
resp := l.GetCanteenDetail(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -7,6 +7,8 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/canteen/internal/logic"
"fusenapi/server/canteen/internal/svc"
"fusenapi/server/canteen/internal/types"
@ -14,7 +16,30 @@ import (
func SaveCanteenTypeProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
var req types.SaveCanteenTypeProductReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
@ -23,9 +48,11 @@ func SaveCanteenTypeProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewSaveCanteenTypeProductLogic(r.Context(), svcCtx)
resp := l.SaveCanteenTypeProduct(&req)
resp := l.SaveCanteenTypeProduct(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -2,14 +2,12 @@ package logic
import (
"context"
"errors"
"fmt"
"fusenapi/model"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"fusenapi/model/gmodel"
"fusenapi/server/canteen/internal/svc"
"fusenapi/server/canteen/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/logx"
)
@ -29,19 +27,19 @@ func NewGetCanteenDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
}
// 获取餐厅详情
func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq) (resp *types.Response) {
func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq, loginInfo *auth.UserInfo) (resp *types.Response) {
//获取餐厅类型数据
canteenTypeModel := model.NewFsCanteenTypeModel(l.svcCtx.MysqlConn)
canteenTypeModel := gmodel.NewFsCanteenTypeModel(l.svcCtx.MysqlConn)
canteenTypeInfo, err := canteenTypeModel.FindOne(l.ctx, req.Id)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get canteen type info ")
}
if canteenTypeInfo == nil {
if canteenTypeInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "canteen type is not exists ")
}
//获取餐厅类型关联的所有产品
canteenProductModel := model.NewFsCanteenProductModel(l.svcCtx.MysqlConn)
canteenProductModel := gmodel.NewFsCanteenProductModel(l.svcCtx.MysqlConn)
canteenProductList, err := canteenProductModel.GetAllByCanteenTypeId(l.ctx, req.Id)
if err != nil {
logx.Error(err)
@ -50,43 +48,43 @@ func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq)
if len(canteenProductList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "ok with no canteen product")
}
productIds := make([]string, 0, len(canteenProductList))
sizeIds := make([]string, 0, len(canteenProductList))
productIds := make([]int64, 0, len(canteenProductList))
sizeIds := make([]int64, 0, len(canteenProductList))
for _, v := range canteenProductList {
productIds = append(productIds, fmt.Sprintf("%d", v.ProductId))
sizeIds = append(sizeIds, fmt.Sprintf("%d", v.SizeId))
productIds = append(productIds, *v.ProductId)
sizeIds = append(sizeIds, *v.SizeId)
}
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByIds(l.ctx, productIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
}
mapProduct := make(map[int64]model.FsProduct)
tagIds := make([]string, 0, len(productList))
mapProduct := make(map[int64]gmodel.FsProduct)
tagIds := make([]int64, 0, len(productList))
for _, v := range productList {
mapProduct[v.Id] = v
tagIds = append(tagIds, fmt.Sprintf("%d", v.Type))
tagIds = append(tagIds, *v.Type)
}
//获取分类列表
tagModel := model.NewFsTagsModel(l.svcCtx.MysqlConn)
tagModel := gmodel.NewFsTagsModel(l.svcCtx.MysqlConn)
tagList, err := tagModel.GetAllByIds(l.ctx, tagIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get tag list")
}
mapTag := make(map[int64]model.FsTags)
mapTag := make(map[int64]gmodel.FsTags)
for _, v := range tagList {
mapTag[v.Id] = v
}
//获取尺寸列表
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeList, err := productSizeModel.GetAllByIds(l.ctx, sizeIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
}
mapSize := make(map[int64]model.FsProductSize)
mapSize := make(map[int64]gmodel.FsProductSize)
for _, v := range productSizeList {
mapSize[v.Id] = v
}
@ -95,18 +93,18 @@ func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq)
for _, v := range canteenProductList {
data := types.CanteenProduct{
Id: v.Id,
SizeId: v.SizeId,
SId: v.Sid,
SizeId: *v.SizeId,
SId: *v.Sid,
}
p, ok := mapProduct[v.ProductId]
p, ok := mapProduct[*v.ProductId]
if !ok {
continue
}
tag, ok := mapTag[p.Type]
tag, ok := mapTag[*p.Type]
if !ok {
continue
}
size, ok := mapSize[v.SizeId]
size, ok := mapSize[*v.SizeId]
if !ok {
continue
}
@ -115,7 +113,7 @@ func (l *GetCanteenDetailLogic) GetCanteenDetail(req *types.GetCanteenDetailReq)
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCanteenDetailRsp{
Id: canteenTypeInfo.Id,
Name: canteenTypeInfo.Name,
Name: *canteenTypeInfo.Name,
ProductList: list,
})
}

View File

@ -3,10 +3,10 @@ package logic
import (
"context"
"fmt"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
"gorm.io/gorm"
"time"
"fusenapi/server/canteen/internal/svc"
@ -30,34 +30,36 @@ func NewSaveCanteenTypeProductLogic(ctx context.Context, svcCtx *svc.ServiceCont
}
// 保存餐厅类型的关联产品
func (l *SaveCanteenTypeProductLogic) SaveCanteenTypeProduct(req *types.SaveCanteenTypeProductReq) (resp *types.Response) {
func (l *SaveCanteenTypeProductLogic) SaveCanteenTypeProduct(req *types.SaveCanteenTypeProductReq, loginInfo *auth.UserInfo) (resp *types.Response) {
if len(req.ProductList) == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "product list can`t be empty")
}
canteenProductModel := model.NewFsCanteenProductModel(l.svcCtx.MysqlConn)
canteenProductModel := gmodel.NewFsCanteenProductModel(l.svcCtx.MysqlConn)
//获取原有餐厅类型的所有产品
oldCanteenProductList, err := canteenProductModel.GetAllByCanteenTypeId(l.ctx, req.Id)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get canteen product list")
}
sizeIds := make([]string, 0, len(req.ProductList))
sizeIds := make([]int64, 0, len(req.ProductList))
for _, v := range req.ProductList {
sizeIds = append(sizeIds, fmt.Sprintf("%d", v.SizeId))
sizeIds = append(sizeIds, v.SizeId)
}
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeList, err := productSizeModel.GetAllByIds(l.ctx, sizeIds, "")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
}
fmt.Println(len(productSizeList))
mapProductSize := make(map[int64]model.FsProductSize)
mapProductSize := make(map[int64]gmodel.FsProductSize)
for _, v := range productSizeList {
mapProductSize[v.Id] = v
}
now := time.Now().Unix()
//开启事务
err = l.svcCtx.MysqlConn.TransactCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
err = l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error {
canteenProductModel = gmodel.NewFsCanteenProductModel(tx)
sort := int64(0)
//新的变更记录
mapUpdateCanteenPid := make(map[int64]struct{})
@ -69,33 +71,47 @@ func (l *SaveCanteenTypeProductLogic) SaveCanteenTypeProduct(req *types.SaveCant
}
if v.Id > 0 { //更新
mapUpdateCanteenPid[v.Id] = struct{}{}
if _, err = session.ExecCtx(l.ctx, "update `fs_canteen_product` set "+
"`size_id` = ?,`sid` = ?,`sort` = ?,`product_id` = ? where `id` = ? ",
v.SizeId, v.SId, sort, sizeInfo.ProductId, v.Id,
); err != nil {
err = canteenProductModel.UpdateById(l.ctx, v.Id, &gmodel.FsCanteenProduct{
SizeId: &v.SizeId,
Sid: &v.SId,
Sort: &sort,
ProductId: sizeInfo.ProductId,
})
if err != nil {
return err
}
continue
}
//新增
if _, err = session.ExecCtx(l.ctx, "insert into `fs_canteen_product` "+
"(`size_id`,`sid`,`sort`,`status`,`ctime`,`canteen_type`,`product_id`) "+
"values (?, ?, ?, ?, ?, ?, ?)", sizeInfo.Id, v.SId, sort, 1, time.Now().Unix(), req.Id, sizeInfo.ProductId); err != nil {
err = canteenProductModel.Create(l.ctx, &gmodel.FsCanteenProduct{
SizeId: &v.SizeId,
Sid: &v.SId,
Sort: &sort,
Ctime: &now,
CanteenType: &req.Id,
ProductId: sizeInfo.ProductId,
})
if err != nil {
return err
}
}
diffCanteenProductId := make([]string, 0, len(oldCanteenProductList))
diffCanteenProductId := make([]int64, 0, len(oldCanteenProductList))
//旧的中不包含在更新的里面则去掉
for _, v := range oldCanteenProductList {
if _, ok := mapUpdateCanteenPid[v.Id]; !ok {
diffCanteenProductId = append(diffCanteenProductId, fmt.Sprintf("%d", v.Id))
diffCanteenProductId = append(diffCanteenProductId, v.Id)
}
}
if len(diffCanteenProductId) == 0 {
return nil
}
_, err = session.ExecCtx(l.ctx, "update `fs_canteen_product` set `status` = ? where `id` in (?)", 0, strings.Join(diffCanteenProductId, ","))
return err
delStatus := int64(0)
if err = canteenProductModel.UpdateByIdArr(l.ctx, diffCanteenProductId, &gmodel.FsCanteenProduct{
Status: &delStatus,
}); err != nil {
return err
}
return nil
})
if err != nil {
logx.Error(err)

View File

@ -1,9 +1,13 @@
package svc
import (
"errors"
"fmt"
"fusenapi/initalize"
"fusenapi/server/canteen/internal/config"
"github.com/golang-jwt/jwt"
"gorm.io/gorm"
"net/http"
)
type ServiceContext struct {
@ -13,8 +17,35 @@ type ServiceContext struct {
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
MysqlConn: initalize.InitMysql(c.SourceMysql),
}
}
func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
AuthKey := r.Header.Get("Authorization")
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 svcCxt.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))
}

View File

@ -2,20 +2,46 @@ package handler
import (
"errors"
"fusenapi/server/data-transfer/internal/types"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/data-transfer/internal/logic"
"fusenapi/server/data-transfer/internal/svc"
)
// 获取二维码配置列表
func GetQrCodeSetListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
l := logic.NewGetQrCodeSetListLogic(r.Context(), svcCtx)
resp := l.GetQrCodeSetList()
resp := l.GetQrCodeSetList(userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -2,18 +2,46 @@ package handler
import (
"errors"
logic2 "fusenapi/server/data-transfer/internal/logic"
svc2 "fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/data-transfer/internal/logic"
"fusenapi/server/data-transfer/internal/svc"
)
func GetStandardLogoListHandler(svcCtx *svc2.ServiceContext) http.HandlerFunc {
func GetStandardLogoListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
l := logic2.NewGetStandardLogoListLogic(r.Context(), svcCtx)
resp := l.GetStandardLogoList()
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
l := logic.NewGetStandardLogoListLogic(r.Context(), svcCtx)
resp := l.GetStandardLogoList(userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -7,6 +7,8 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/data-transfer/internal/logic"
"fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
@ -14,7 +16,30 @@ import (
func UploadLogoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
var req types.UploadLogoReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
@ -23,9 +48,11 @@ func UploadLogoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewUploadLogoLogic(r.Context(), svcCtx)
resp := l.UploadLogo(&req)
resp := l.UploadLogo(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -7,6 +7,8 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/data-transfer/internal/logic"
"fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
@ -14,7 +16,30 @@ import (
func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
var req types.UploadQrcodeReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
@ -23,9 +48,11 @@ func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewUploadQrcodeLogic(r.Context(), svcCtx)
resp := l.UploadQrcode(&req)
resp := l.UploadQrcode(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -2,7 +2,8 @@ package logic
import (
"context"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/data-transfer/internal/svc"
@ -26,8 +27,8 @@ func NewGetQrCodeSetListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
}
// 获取二维码配置列表
func (l *GetQrCodeSetListLogic) GetQrCodeSetList() (resp *types.Response) {
qrCodeModel := model.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
func (l *GetQrCodeSetListLogic) GetQrCodeSetList(loginInfo *auth.UserInfo) (resp *types.Response) {
qrCodeModel := gmodel.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
qrCodeList, err := qrCodeModel.GetAll(l.ctx)
if err != nil {
logx.Error(err)
@ -37,7 +38,7 @@ func (l *GetQrCodeSetListLogic) GetQrCodeSetList() (resp *types.Response) {
for _, v := range qrCodeList {
list = append(list, types.GetQrCodeSetListRsp{
Id: v.Id,
Name: v.Name,
Name: *v.Name,
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)

View File

@ -2,9 +2,10 @@ package logic
import (
"context"
"fusenapi/model"
"fusenapi/model/gmodel"
svc2 "fusenapi/server/data-transfer/internal/svc"
types2 "fusenapi/server/data-transfer/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/core/logx"
@ -25,8 +26,8 @@ func NewGetStandardLogoListLogic(ctx context.Context, svcCtx *svc2.ServiceContex
}
// 获取标准logo列表
func (l *GetStandardLogoListLogic) GetStandardLogoList() (resp *types2.Response) {
standardLogoModel := model.NewFsStandardLogoModel(l.svcCtx.MysqlConn)
func (l *GetStandardLogoListLogic) GetStandardLogoList(loginInfo *auth.UserInfo) (resp *types2.Response) {
standardLogoModel := gmodel.NewFsStandardLogoModel(l.svcCtx.MysqlConn)
logoList, err := standardLogoModel.GetAll(l.ctx)
if err != nil {
logx.Error(err)
@ -36,8 +37,8 @@ func (l *GetStandardLogoListLogic) GetStandardLogoList() (resp *types2.Response)
for _, v := range logoList {
list = append(list, types2.GetStandardLogoListRsp{
Id: v.Id,
Name: v.Name,
Url: v.Image,
Name: *v.Name,
Url: *v.Image,
})
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)

View File

@ -2,6 +2,7 @@ package logic
import (
"context"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/server/data-transfer/internal/svc"
@ -24,7 +25,7 @@ func NewUploadLogoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Upload
}
}
func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq) (resp *types.Response) {
func (l *UploadLogoLogic) UploadLogo(req *types.UploadLogoReq, loginInfo *auth.UserInfo) (resp *types.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
return resp.SetStatus(basic.CodeOK)

View File

@ -2,14 +2,13 @@ package logic
import (
"context"
"errors"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/qrcode"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
)
@ -28,7 +27,7 @@ func NewUploadQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Uplo
}
// 生成base64二维码
func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq) (resp *types.Response) {
func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq, loginInfo *auth.UserInfo) (resp *types.Response) {
if req.Url == "" {
resp.SetStatus(basic.CodeApiErr, "param url is empty")
}
@ -36,23 +35,23 @@ func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq) (resp *type
resp.SetStatus(basic.CodeApiErr, "param QRcodeType must large than 0")
}
//获取二维码模板信息
qrCodeModel := model.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
qrCodeModel := gmodel.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
qrCodeSet, err := qrCodeModel.FindOne(l.ctx, req.QRcodeType)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
if err != nil {
logx.Error(err)
resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get qrcode setting")
}
if qrCodeSet == nil {
if qrCodeSet.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "qrcode setting is not exists")
}
qrType := qrCodeSet.SvgWebsite.String
qrType := *qrCodeSet.SvgWebsite
if strings.Contains(req.Url, "www.instagram.com") {
qrType = qrCodeSet.SvgInstagram.String
qrType = *qrCodeSet.SvgInstagram
} else if strings.Contains(req.Url, "www.facebook.com") {
qrType = qrCodeSet.SvgFacebook.String
qrType = *qrCodeSet.SvgFacebook
}
//生成二维码
imgBase64, err := qrcode.CreateQrCodeBs64WithLogo(req.Url, "", "", 512, int(qrCodeSet.IndexX), int(qrCodeSet.IndexY), true)
imgBase64, err := qrcode.CreateQrCodeBs64WithLogo(req.Url, "", "", 512, int(*qrCodeSet.IndexX), int(*qrCodeSet.IndexY), true)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to generate qrcode")

View File

@ -1,9 +1,13 @@
package svc
import (
"errors"
"fmt"
"fusenapi/initalize"
"fusenapi/server/data-transfer/internal/config"
"github.com/golang-jwt/jwt"
"gorm.io/gorm"
"net/http"
)
type ServiceContext struct {
@ -13,8 +17,35 @@ type ServiceContext struct {
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
MysqlConn: initalize.InitMysql(c.SourceMysql),
}
}
func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
AuthKey := r.Header.Get("Authorization")
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 svcCxt.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))
}

View File

@ -27,9 +27,9 @@ type UploadQrcodeRsp struct {
}
type UploadLogoReq struct {
SkuId int64 `json:"skuId"`
IsRemoveBg bool `json:"is_remove_bg"`
Proportion int64 `json:"proportion"`
SkuId int64 `form:"skuId"`
IsRemoveBg bool `form:"is_remove_bg"`
Proportion int64 `form:"proportion"`
}
type UploadLogoRsp struct {

View File

@ -7,6 +7,8 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/product/internal/logic"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
@ -14,18 +16,43 @@ import (
func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
var req types.GetProductListReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: err.Error(),
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetProductListLogic(r.Context(), svcCtx)
resp := l.GetProductList(&req)
resp := l.GetProductList(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -2,19 +2,46 @@ package handler
import (
"errors"
"fusenapi/server/product/internal/types"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/product/internal/logic"
"fusenapi/server/product/internal/svc"
)
func GetSizeByProductHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
l := logic.NewGetSizeByProductLogic(r.Context(), svcCtx)
resp := l.GetSizeByProduct()
resp := l.GetSizeByProduct(userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -7,6 +7,8 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/utils/auth"
"fusenapi/server/product/internal/logic"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
@ -14,18 +16,43 @@ import (
func GetSuccessRecommandHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// 解析jwtToken
claims, err := svcCtx.ParseJwtToken(r)
// 如果解析出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
// 从Token里获取对应的信息
userinfo, err := auth.GetUserInfoFormMapClaims(claims)
// 如果获取用户信息出错则返回未授权的JSON响应并记录错误消息
if err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 401,
Message: "unauthorized",
})
logx.Info("unauthorized:", err.Error())
}
var req types.GetSuccessRecommandReq
// 如果端点有请求结构体则使用httpx.Parse方法从HTTP请求体中解析请求数据
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: err.Error(),
Message: "parameter error",
})
logx.Info(err)
return
}
// 创建一个业务逻辑层实例
l := logic.NewGetSuccessRecommandLogic(r.Context(), svcCtx)
resp := l.GetSuccessRecommand(&req)
resp := l.GetSuccessRecommand(&req, userinfo)
// 如果响应不为nil则使用httpx.OkJsonCtx方法返回JSON响应;
// 否则发送500内部服务器错误的JSON响应并记录错误消息logx.Error。
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {

View File

@ -4,9 +4,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"fusenapi/utils/auth"
@ -34,12 +33,8 @@ func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
}
// 获取产品列表
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp *types.Response) {
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, loginInfo *auth.UserInfo) (resp *types.Response) {
resp = &types.Response{}
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get login user info err")
}
//如果是demo
if req.IsDemo == 1 {
var demo types.GetProductListRsp
@ -57,18 +52,18 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
req.Size = image.GetCurrentSize(req.Size)
}
//查询用户信息
userModel := model.NewFsUserModel(l.svcCtx.MysqlConn)
userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err")
}
if userInfo == nil {
if userInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "user not exists")
}
//查询符合的产品列表
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByConditions(l.ctx, []string{fmt.Sprintf("%d", req.Cid)}, "sort-desc")
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByTypeIds(l.ctx, []int64{req.Cid}, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
@ -78,11 +73,11 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}
//提取产品ids
productIds := make([]string, 0, productLen)
productIds := make([]int64, 0, productLen)
for _, v := range productList {
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
productIds = append(productIds, v.Id)
}
productPriceModel := model.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceList, err := productPriceModel.GetPriceListByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
@ -104,28 +99,28 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
mapProductMinPrice[v.ProductId] = int64(priceSlice[0])
}
//获取模板
productTemplateModel := model.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
productTemplatesV2, err := productTemplateModel.FindAllByCondition(l.ctx, productIds)
productTemplateModel := gmodel.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn)
productTemplatesV2, err := productTemplateModel.FindAllByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product template_v2 err")
}
mapProductTemplate := make(map[int64]struct{})
for _, v := range productTemplatesV2 {
mapProductTemplate[v.ProductId] = struct{}{}
mapProductTemplate[*v.ProductId] = struct{}{}
}
//获取分类
tagsModel := model.NewFsTagsModel(l.svcCtx.MysqlConn)
tagsModel := gmodel.NewFsTagsModel(l.svcCtx.MysqlConn)
tagInfo, err := tagsModel.FindOne(l.ctx, req.Cid)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get tag err")
}
if tagInfo == nil {
if tagInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "tag is not exists")
}
//获取产品尺寸数量
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeCount, err := productSizeModel.CountByStatus(l.ctx, 1)
if err != nil {
logx.Error(err)
@ -142,11 +137,11 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
}
item := types.Items{
Id: v.Id,
Sn: v.Sn,
Title: v.Title,
Intro: v.Intro.String,
IsEnv: v.IsProtection,
IsMicro: v.IsMicrowave,
Sn: *v.Sn,
Title: *v.Title,
Intro: *v.Intro,
IsEnv: *v.IsProtection,
IsMicro: *v.IsMicrowave,
SizeNum: uint32(productSizeCount),
MiniPrice: format.CentoDollar(minPrice),
}
@ -154,11 +149,11 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
thousandFaceImageFormatReq := image.ThousandFaceImageFormatReq{
Size: int(req.Size),
IsThousandFace: int(userInfo.IsThousandFace),
Cover: v.Cover,
CoverImg: v.CoverImg,
CoverDefault: v.CoverImg,
Cover: *v.Cover,
CoverImg: *v.CoverImg,
CoverDefault: *v.CoverImg,
ProductId: v.Id,
UserInfo: *userInfo,
UserInfo: userInfo,
}
image.ThousandFaceImageFormat(&thousandFaceImageFormatReq)
item.Cover = thousandFaceImageFormatReq.Cover
@ -169,6 +164,6 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetProductListRsp{
Ob: types.Ob{
Items: itemList,
}, TypeName: tagInfo.Title, Description: tagInfo.Description,
}, TypeName: *tagInfo.Title, Description: *tagInfo.Description,
})
}

View File

@ -5,7 +5,8 @@ import (
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/format"
"strings"
@ -31,9 +32,9 @@ func NewGetSizeByProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
}
// 获取分类下的产品以及尺寸
func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) {
func (l *GetSizeByProductLogic) GetSizeByProduct(loginInfo *auth.UserInfo) (resp *types.Response) {
//获取所有网站目录
tagsModel := model.NewFsTagsModel(l.svcCtx.MysqlConn)
tagsModel := gmodel.NewFsTagsModel(l.svcCtx.MysqlConn)
tagsList, err := tagsModel.GetAllByLevel(l.ctx, constants.TYPE_WEBSITE)
if err != nil {
logx.Error(err)
@ -42,41 +43,41 @@ func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) {
if len(tagsList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "tag list is null")
}
tagIds := make([]string, 0, len(tagsList))
tagIds := make([]int64, 0, len(tagsList))
for _, v := range tagsList {
tagIds = append(tagIds, fmt.Sprintf("%d", v.Id))
tagIds = append(tagIds, v.Id)
}
//获取这些类型的产品
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByConditions(l.ctx, tagIds, "sort-desc")
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByIds(l.ctx, tagIds, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get tag product list")
}
productIds := make([]string, 0, len(productList))
productIds := make([]int64, 0, len(productList))
for _, v := range productList {
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
productIds = append(productIds, v.Id)
}
productSizeModel := model.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
}
sizeIds := make([]string, 0, len(productSizeList))
sizeIds := make([]int64, 0, len(productSizeList))
for _, v := range productSizeList {
sizeIds = append(sizeIds, fmt.Sprintf("%d", v.Id))
sizeIds = append(sizeIds, v.Id)
}
//获取价格列表
productPriceModel := model.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceList, err := productPriceModel.GetPriceListBySizeIds(l.ctx, sizeIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product proce list")
}
mapProductPrice := make(map[int64]model.FsProductPrice)
mapProductPrice := make(map[int64]gmodel.FsProductPrice)
for _, v := range productPriceList {
mapProductPrice[v.SizeId] = v
mapProductPrice[*v.SizeId] = v
}
//组装返回
list := make([]types.GetSizeByProductRsp, 0, len(tagsList))
@ -89,7 +90,7 @@ func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) {
}
data := types.GetSizeByProductRsp{
Id: tag.Id,
Name: tag.Title,
Name: *tag.Title,
Children: firstChildrenList,
}
list = append(list, data)
@ -98,21 +99,21 @@ func (l *GetSizeByProductLogic) GetSizeByProduct() (resp *types.Response) {
}
// 第一层子层
func (l *GetSizeByProductLogic) GetFirstChildrenList(tag model.FsTags, productList []model.FsProduct, productSizeList []model.FsProductSize, mapProductPrice map[int64]model.FsProductPrice) (childrenList []types.Children, err error) {
func (l *GetSizeByProductLogic) GetFirstChildrenList(tag gmodel.FsTags, productList []gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[int64]gmodel.FsProductPrice) (childrenList []types.Children, err error) {
childrenList = make([]types.Children, 0, len(productList))
for _, product := range productList {
if product.Type != tag.Id {
if *product.Type != tag.Id {
continue
}
childrenObjList, err := l.GetSecondChildrenList(tag, product, productSizeList, mapProductPrice)
childrenObjList, err := l.GetSecondChildrenList(product, productSizeList, mapProductPrice)
if err != nil {
return nil, err
}
//获取第二层子类
data := types.Children{
Id: product.Id,
Name: product.Title,
Cycle: int(product.DeliveryDays + product.ProduceDays),
Name: *product.Title,
Cycle: int(*product.DeliveryDays + *product.ProduceDays),
ChildrenList: childrenObjList,
}
childrenList = append(childrenList, data)
@ -121,10 +122,10 @@ func (l *GetSizeByProductLogic) GetFirstChildrenList(tag model.FsTags, productLi
}
// 第2层子层
func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product model.FsProduct, productSizeList []model.FsProductSize, mapProductPrice map[int64]model.FsProductPrice) (childrenObjList []types.ChildrenObj, err error) {
func (l *GetSizeByProductLogic) GetSecondChildrenList(product gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[int64]gmodel.FsProductPrice) (childrenObjList []types.ChildrenObj, err error) {
childrenObjList = make([]types.ChildrenObj, 0, len(productSizeList))
for _, productSize := range productSizeList {
if product.Id != productSize.ProductId {
if product.Id != *productSize.ProductId {
continue
}
priceList := make([]types.PriceObj, 0, len(productSizeList))
@ -133,7 +134,7 @@ func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product
if !ok {
childrenObjList = append(childrenObjList, types.ChildrenObj{
Id: productSize.Id,
Name: productSize.Capacity,
Name: *productSize.Capacity,
PriceList: []types.PriceObj{
{Num: 1, Price: 0},
{Num: 1, Price: 0},
@ -142,18 +143,19 @@ func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product
})
continue
}
price.StepNum = strings.Trim(price.StepNum, " ")
price.StepPrice = strings.Trim(price.StepPrice, " ")
if price.StepNum == "" || price.StepPrice == "" {
if price.StepNum == nil || price.StepPrice == nil {
continue
}
*price.StepNum = strings.Trim(*price.StepNum, " ")
*price.StepPrice = strings.Trim(*price.StepPrice, " ")
//阶梯数量切片
stepNum, err := format.StrSlicToIntSlice(strings.Split(price.StepNum, ","))
stepNum, err := format.StrSlicToIntSlice(strings.Split(*price.StepNum, ","))
if err != nil {
return nil, err
}
//阶梯价格切片
stepPrice, err := format.StrSlicToIntSlice(strings.Split(price.StepPrice, ","))
stepPrice, err := format.StrSlicToIntSlice(strings.Split(*price.StepPrice, ","))
if err != nil {
return nil, err
}
@ -162,17 +164,17 @@ func (l *GetSizeByProductLogic) GetSecondChildrenList(tag model.FsTags, product
}
index := 0
// 最小购买数量小于 最大阶梯数量+5
for int(price.MinBuyNum) < (stepNum[len(stepNum)-1]+5) && index < 3 {
for int(*price.MinBuyNum) < (stepNum[len(stepNum)-1]+5) && index < 3 {
priceList = append(priceList, types.PriceObj{
Num: int(price.MinBuyNum * price.EachBoxNum),
Price: l.GetPrice(int(price.MinBuyNum), stepNum, stepPrice),
Num: int(*price.MinBuyNum * *price.EachBoxNum),
Price: l.GetPrice(int(*price.MinBuyNum), stepNum, stepPrice),
})
price.MinBuyNum++
*price.MinBuyNum++
index++
}
data := types.ChildrenObj{
Id: productSize.Id,
Name: productSize.Capacity,
Name: *productSize.Capacity,
PriceList: priceList,
}
childrenObjList = append(childrenObjList, data)

View File

@ -2,15 +2,13 @@ package logic
import (
"context"
"errors"
"fusenapi/model"
"fusenapi/model/gmodel"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/image"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type GetSuccessRecommandLogic struct {
@ -28,20 +26,16 @@ func NewGetSuccessRecommandLogic(ctx context.Context, svcCtx *svc.ServiceContext
}
// 获取推荐的产品列表
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq) (resp *types.Response) {
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, loginInfo *auth.UserInfo) (resp *types.Response) {
resp = &types.Response{}
loginInfo := auth.GetUserInfoFormCtx(l.ctx)
if loginInfo.UserId == 0 {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "get login user info err")
}
//获取用户信息
userModel := model.NewFsUserModel(l.svcCtx.MysqlConn)
userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId)
if err != nil && errors.Is(err, sqlx.ErrNotFound) {
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get user info")
}
if userInfo == nil {
if userInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "failed to get user info")
}
if req.Num == 0 || req.Num > 500 {
@ -51,7 +45,7 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
req.Size = image.GetCurrentSize(req.Size)
}
//随机取8个产品
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num))
if err != nil {
logx.Error(err)
@ -64,8 +58,8 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
list := make([]types.GetSuccessRecommandRsp, 0, len(productList))
for _, v := range productList {
data := types.GetSuccessRecommandRsp{
Title: v.Title,
Sn: v.Sn,
Title: *v.Title,
Sn: *v.Sn,
Id: v.Id,
SkuId: 0, //???????
}
@ -73,11 +67,11 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
thousandFaceImageFormatReq := image.ThousandFaceImageFormatReq{
Size: int(req.Size),
IsThousandFace: int(userInfo.IsThousandFace),
Cover: v.Cover,
CoverImg: v.CoverImg,
CoverDefault: v.CoverImg,
Cover: *v.Cover,
CoverImg: *v.CoverImg,
CoverDefault: *v.CoverImg,
ProductId: v.Id,
UserInfo: *userInfo,
UserInfo: userInfo,
}
image.ThousandFaceImageFormat(&thousandFaceImageFormatReq)
data.Cover = thousandFaceImageFormatReq.Cover

View File

@ -1,9 +1,13 @@
package svc
import (
"errors"
"fmt"
"fusenapi/initalize"
"fusenapi/server/product/internal/config"
"github.com/golang-jwt/jwt"
"gorm.io/gorm"
"net/http"
)
type ServiceContext struct {
@ -13,8 +17,35 @@ type ServiceContext struct {
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
MysqlConn: initalize.InitMysql(c.SourceMysql),
MysqlConn: initalize.InitMysql(c.DataSource),
}
}
func (svcCxt *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) {
AuthKey := r.Header.Get("Authorization")
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 svcCxt.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))
}

View File

@ -50,9 +50,9 @@ type UploadQrcodeRsp {
//上传logo
type UploadLogoReq {
SkuId int64 `json:"skuId"`
IsRemoveBg bool `json:"is_remove_bg"`
Proportion int64 `json:"proportion"`
SkuId int64 `form:"skuId"`
IsRemoveBg bool `form:"is_remove_bg"`
Proportion int64 `form:"proportion"`
}
type UploadLogoRsp {
NobgUrl string `json:"nobg_url"`

View File

@ -3,7 +3,7 @@ package image
import (
"fmt"
"fusenapi/constants"
"fusenapi/model"
"fusenapi/model/gmodel"
"strings"
"time"
)
@ -41,7 +41,7 @@ type ThousandFaceImageFormatReq struct {
CoverImg string
CoverDefault string
ProductId int64
UserInfo model.FsUser
UserInfo gmodel.FsUser
}
func ThousandFaceImageFormat(req *ThousandFaceImageFormatReq) {