增加产品列表接口

This commit is contained in:
laodaming 2023-06-01 15:32:28 +08:00
parent 5446c4123a
commit d26586c794
19 changed files with 451 additions and 11 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

9
.idea/fusenapi.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/fusenapi.iml" filepath="$PROJECT_DIR$/.idea/fusenapi.iml" />
</modules>
</component>
</project>

7
.idea/sqldialects.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/ddl/fs_faq.sql" dialect="MariaDB" />
<file url="file://$PROJECT_DIR$/ddl/fs_font.sql" dialect="MariaDB" />
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

29
.idea/watcherTasks.xml Normal file
View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions">
<TaskOptions isEnabled="true">
<option name="arguments" value="fmt $FilePath$" />
<option name="checkSyntaxErrors" value="true" />
<option name="description" />
<option name="exitCodeBehavior" value="ERROR" />
<option name="fileExtension" value="go" />
<option name="immediateSync" value="false" />
<option name="name" value="go fmt" />
<option name="output" value="$FilePath$" />
<option name="outputFilters">
<array />
</option>
<option name="outputFromStdout" value="false" />
<option name="program" value="$GoExecPath$" />
<option name="runOnExternalChanges" value="false" />
<option name="scopeName" value="Project Files" />
<option name="trackOnlyRoot" value="true" />
<option name="workingDir" value="$ProjectFileDir$" />
<envs>
<env name="GOROOT" value="$GOROOT$" />
<env name="GOPATH" value="$GOPATH$" />
<env name="PATH" value="$GoBinDirs$" />
</envs>
</TaskOptions>
</component>
</project>

35
ddl/fs_product.sql Normal file
View File

@ -0,0 +1,35 @@
-- fusentest.fs_product definition
CREATE TABLE `fs_product` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`sn` char(8) NOT NULL COMMENT '商品编号 P98f087j',
`type` tinyint(4) NOT NULL COMMENT '分类ID',
`title` varchar(64) NOT NULL COMMENT '名称',
`title_cn` varchar(64) NOT NULL COMMENT '中文名称',
`cover` varchar(128) NOT NULL COMMENT '封面图',
`imgs` text NOT NULL COMMENT '一个或多个介绍图或视频',
`keywords` varchar(128) NOT NULL COMMENT '关键字',
`intro` text COMMENT '简要描述',
`sort` smallint(6) DEFAULT '0' COMMENT '排序',
`selled_num` int(11) DEFAULT '0' COMMENT '已卖数量',
`ctime` int(11) DEFAULT NULL COMMENT '添加时间',
`view` int(11) DEFAULT '0' COMMENT '浏览量',
`size_ids` varchar(128) DEFAULT NULL COMMENT '尺寸 1,2,3,4',
`material_ids` varchar(128) NOT NULL DEFAULT '' COMMENT '材质 1,2,3',
`tag_ids` varchar(128) DEFAULT NULL COMMENT '标签 逗号间隔',
`status` tinyint(4) DEFAULT '0' COMMENT '状态位 弃用',
`produce_days` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '生产天数',
`delivery_days` mediumint(8) unsigned NOT NULL DEFAULT '0' COMMENT '运送天数',
`cover_img` varchar(128) NOT NULL DEFAULT '' COMMENT '背景图',
`is_shelf` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否上架',
`is_recommend` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否推荐',
`is_hot` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否热销',
`is_protection` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否环保',
`is_microwave` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否可微波炉',
`is_del` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除',
`recommend_product` varchar(500) DEFAULT NULL COMMENT '推荐产品id例如: 1,3,4,5',
`recommend_product_sort` varchar(500) DEFAULT NULL COMMENT '推荐排序例如:1324',
`scene_ids` varchar(500) DEFAULT NULL COMMENT '关联的场景id',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `sn` (`sn`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=60 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='产品表';

24
model/fsproductmodel.go Executable file
View File

@ -0,0 +1,24 @@
package model
import "github.com/zeromicro/go-zero/core/stores/sqlx"
var _ FsProductModel = (*customFsProductModel)(nil)
type (
// FsProductModel is an interface to be customized, add more methods here,
// and implement the added methods in customFsProductModel.
FsProductModel interface {
fsProductModel
}
customFsProductModel struct {
*defaultFsProductModel
}
)
// NewFsProductModel returns a model for the database table.
func NewFsProductModel(conn sqlx.SqlConn) FsProductModel {
return &customFsProductModel{
defaultFsProductModel: newFsProductModel(conn),
}
}

126
model/fsproductmodel_gen.go Executable file
View File

@ -0,0 +1,126 @@
// Code generated by goctl. DO NOT EDIT.
package model
import (
"context"
"database/sql"
"fmt"
"strings"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var (
fsProductFieldNames = builder.RawFieldNames(&FsProduct{})
fsProductRows = strings.Join(fsProductFieldNames, ",")
fsProductRowsExpectAutoSet = strings.Join(stringx.Remove(fsProductFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
fsProductRowsWithPlaceHolder = strings.Join(stringx.Remove(fsProductFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)
type (
fsProductModel interface {
Insert(ctx context.Context, data *FsProduct) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*FsProduct, error)
FindOneBySn(ctx context.Context, sn string) (*FsProduct, error)
Update(ctx context.Context, data *FsProduct) error
Delete(ctx context.Context, id int64) error
}
defaultFsProductModel struct {
conn sqlx.SqlConn
table string
}
FsProduct struct {
Id int64 `db:"id"`
Sn string `db:"sn"` // 商品编号 P98f087j
Type int64 `db:"type"` // 分类ID
Title string `db:"title"` // 名称
TitleCn string `db:"title_cn"` // 中文名称
Cover string `db:"cover"` // 封面图
Imgs string `db:"imgs"` // 一个或多个介绍图或视频
Keywords string `db:"keywords"` // 关键字
Intro sql.NullString `db:"intro"` // 简要描述
Sort int64 `db:"sort"` // 排序
SelledNum int64 `db:"selled_num"` // 已卖数量
Ctime sql.NullInt64 `db:"ctime"` // 添加时间
View int64 `db:"view"` // 浏览量
SizeIds sql.NullString `db:"size_ids"` // 尺寸 1,2,3,4
MaterialIds string `db:"material_ids"` // 材质 1,2,3
TagIds sql.NullString `db:"tag_ids"` // 标签 逗号间隔
Status int64 `db:"status"` // 状态位 弃用
ProduceDays int64 `db:"produce_days"` // 生产天数
DeliveryDays int64 `db:"delivery_days"` // 运送天数
CoverImg string `db:"cover_img"` // 背景图
IsShelf int64 `db:"is_shelf"` // 是否上架
IsRecommend int64 `db:"is_recommend"` // 是否推荐
IsHot int64 `db:"is_hot"` // 是否热销
IsProtection int64 `db:"is_protection"` // 是否环保
IsMicrowave int64 `db:"is_microwave"` // 是否可微波炉
IsDel int64 `db:"is_del"` // 是否删除
RecommendProduct sql.NullString `db:"recommend_product"` // 推荐产品id例如: 1,3,4,5
RecommendProductSort sql.NullString `db:"recommend_product_sort"` // 推荐排序例如:1324
SceneIds sql.NullString `db:"scene_ids"` // 关联的场景id
}
)
func newFsProductModel(conn sqlx.SqlConn) *defaultFsProductModel {
return &defaultFsProductModel{
conn: conn,
table: "`fs_product`",
}
}
func (m *defaultFsProductModel) Delete(ctx context.Context, id int64) error {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
_, err := m.conn.ExecCtx(ctx, query, id)
return err
}
func (m *defaultFsProductModel) FindOne(ctx context.Context, id int64) (*FsProduct, error) {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", fsProductRows, m.table)
var resp FsProduct
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultFsProductModel) FindOneBySn(ctx context.Context, sn string) (*FsProduct, error) {
var resp FsProduct
query := fmt.Sprintf("select %s from %s where `sn` = ? limit 1", fsProductRows, m.table)
err := m.conn.QueryRowCtx(ctx, &resp, query, sn)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultFsProductModel) Insert(ctx context.Context, data *FsProduct) (sql.Result, error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, fsProductRowsExpectAutoSet)
ret, err := m.conn.ExecCtx(ctx, query, data.Sn, data.Type, data.Title, data.TitleCn, data.Cover, data.Imgs, data.Keywords, data.Intro, data.Sort, data.SelledNum, data.Ctime, data.View, data.SizeIds, data.MaterialIds, data.TagIds, data.Status, data.ProduceDays, data.DeliveryDays, data.CoverImg, data.IsShelf, data.IsRecommend, data.IsHot, data.IsProtection, data.IsMicrowave, data.IsDel, data.RecommendProduct, data.RecommendProductSort, data.SceneIds)
return ret, err
}
func (m *defaultFsProductModel) Update(ctx context.Context, newData *FsProduct) error {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fsProductRowsWithPlaceHolder)
_, err := m.conn.ExecCtx(ctx, query, newData.Sn, newData.Type, newData.Title, newData.TitleCn, newData.Cover, newData.Imgs, newData.Keywords, newData.Intro, newData.Sort, newData.SelledNum, newData.Ctime, newData.View, newData.SizeIds, newData.MaterialIds, newData.TagIds, newData.Status, newData.ProduceDays, newData.DeliveryDays, newData.CoverImg, newData.IsShelf, newData.IsRecommend, newData.IsHot, newData.IsProtection, newData.IsMicrowave, newData.IsDel, newData.RecommendProduct, newData.RecommendProductSort, newData.SceneIds, newData.Id)
return err
}
func (m *defaultFsProductModel) tableName() string {
return m.table
}

View File

@ -6,18 +6,16 @@ info (
author: ""
email: ""
)
type request {
import "basic.api"
type GetProductListReq {
// TODO: add members here and delete this comment
Name string `path:"name,options=you|me"` // parameters are auto validated
Cid uint32 `form:"cid"`
Size uint32 `form:"size"`
Page uint32 `form:"page"`
IsDemo uint32 `form:"is_demo" , options=0|1"`
}
type response {
// TODO: add members here and delete this comment
Message string `json:"message"`
}
service user-auth {
@handler GreetHandler
get /greet/from/:name(request) returns (response);
service product {
@handler GetProductListHandler
get /product/list(GetProductListReq) returns (response);
}

4
product/etc/product.yaml Normal file
View File

@ -0,0 +1,4 @@
Name: product
Host: 0.0.0.0
Port: 8888
DataSource: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest

View File

@ -0,0 +1,8 @@
package config
import "github.com/zeromicro/go-zero/rest"
type Config struct {
rest.RestConf
DataSource string
}

View File

@ -0,0 +1,28 @@
package handler
import (
"net/http"
"fusenapi/product/internal/logic"
"fusenapi/product/internal/svc"
"fusenapi/product/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
func GetProductListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.GetProductListReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := logic.NewGetProductListLogic(r.Context(), svcCtx)
resp, err := l.GetProductList(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -0,0 +1,22 @@
// Code generated by goctl. DO NOT EDIT.
package handler
import (
"net/http"
"fusenapi/product/internal/svc"
"github.com/zeromicro/go-zero/rest"
)
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodGet,
Path: "/product/list",
Handler: GetProductListHandler(serverCtx),
},
},
)
}

View File

@ -0,0 +1,35 @@
package logic
import (
"context"
"fusenapi/utils/image"
"fusenapi/product/internal/svc"
"fusenapi/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetProductListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductListLogic {
return &GetProductListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 获取产品列表
func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq) (resp *types.Response, err error) {
//获取合适尺寸
if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size)
}
//获取是否存在千人千面
return
}

View File

@ -0,0 +1,19 @@
package svc
import (
"fusenapi/model"
"fusenapi/product/internal/config"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type ServiceContext struct {
Config config.Config
FsProductModel model.FsProductModel
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
FsProductModel: model.NewFsProductModel(sqlx.NewMysql(c.DataSource)),
}
}

View File

@ -0,0 +1,15 @@
// Code generated by goctl. DO NOT EDIT.
package types
type GetProductListReq struct {
Cid uint32 `form:"cid"`
Size uint32 `form:"size"`
Page uint32 `form:"page"`
IsDemo uint32 `form:"is_demo" , options=0|1"`
}
type Response struct {
Code int `json:"code"`
Message string `json:"msg"`
Data interface{} `json:"data"`
}

31
product/product.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
"flag"
"fmt"
"fusenapi/product/internal/config"
"fusenapi/product/internal/handler"
"fusenapi/product/internal/svc"
"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/rest"
)
var configFile = flag.String("f", "etc/product.yaml", "the config file")
func main() {
flag.Parse()
var c config.Config
conf.MustLoad(*configFile, &c)
server := rest.MustNewServer(c.RestConf)
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()
}

28
utils/image/image_size.go Normal file
View File

@ -0,0 +1,28 @@
package image
// 定义尺寸规则
var sizeArray = []uint32{1200, 1000, 750, 500, 128}
// 裁剪尺寸阶梯
var newSizeArray = []uint32{200, 400, 600, 800}
func GetCurrentSize(clientSize uint32) uint32 {
lenNewSize := len(newSizeArray)
//大于最大尺寸则返回规则最大尺寸
if clientSize >= newSizeArray[lenNewSize-1] {
return newSizeArray[lenNewSize-1]
}
//小于最小尺寸则返回规则最小尺寸
if clientSize < newSizeArray[0] {
return clientSize
}
for k, v := range newSizeArray {
if v == clientSize {
return v
}
if clientSize > v && k < lenNewSize-1 && clientSize < newSizeArray[k+1] {
return newSizeArray[k+1]
}
}
return clientSize
}