From b2528505571b789b480036545c4c88a6a565c059 Mon Sep 17 00:00:00 2001 From: lijikun Date: Sat, 28 Dec 2024 14:50:31 +0800 Subject: [PATCH] =?UTF-8?q?12.28=E6=97=A5=E6=A3=80=E6=B5=8B=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=98=AF=E5=90=A6=E6=9C=89=E6=8A=BD=E5=A5=96=E6=9C=BA?= =?UTF-8?q?=E4=BC=9A,=E6=98=AF=E5=90=A6=E6=8A=BD=E5=8F=96=E8=BF=87,?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/couponusers/couponUsers.go | 4 ++ internal/cmd/cmd.go | 3 ++ internal/consts/consts.go | 3 +- internal/controller/couponusers/couponUsers.go | 45 +++++++++++++++++++++- internal/logic/couponusers/couponUsers.go | 52 ++++++++++++++++++++++++-- internal/logic/logic.go | 1 + internal/logic/middleware/interceptor.go | 10 +++++ internal/service/couponusers.go | 2 + internal/service/middleware.go | 8 ++++ 9 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 internal/logic/middleware/interceptor.go create mode 100644 internal/service/middleware.go diff --git a/api/v1/couponusers/couponUsers.go b/api/v1/couponusers/couponUsers.go index 3fd5f27..c32bfce 100644 --- a/api/v1/couponusers/couponUsers.go +++ b/api/v1/couponusers/couponUsers.go @@ -26,3 +26,7 @@ type InsertCouponUserReq struct { type IssueCouponUserReq struct { CouponId int `json:"couponId" dc:"卡券id"` } + +type IsEligibleUserReq struct { + CouponIds []int `json:"couponIds" dc:"能抽出的卡券id"` +} diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 6d1d5c5..36c557d 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -2,6 +2,7 @@ package cmd import ( "CouponBackendGo/internal/controller/couponusers" + "CouponBackendGo/internal/logic/middleware" "context" "github.com/gogf/gf/v2/frame/g" @@ -17,12 +18,14 @@ var ( Func: func(ctx context.Context, parser *gcmd.Parser) (err error) { s := g.Server() s.Group("/api/coupon_backend", func(group *ghttp.RouterGroup) { + group.Middleware(middleware.MiddlewareCORS) group.POST("/get-coupon-users", couponusers.NewCouponUsers().GetCouponUsers) // 获取拥有卡券的用户列表 group.POST("/delete-coupon-user", couponusers.NewCouponUsers().DeleteCouponUserByJwcode) //删除用户卡券 group.POST("/import-excel", couponusers.NewCouponUsers().InsertJwcodeByExcel) // 通过excel导入jwcode group.POST("/insert-coupon-user", couponusers.NewCouponUsers().InsertCouponUser) //给用户发放卡券 //近期使用 group.POST("/insert-users-to-redis", couponusers.NewCouponUsers().InsertJwcodesToRedisByExcel) //导入满足条件的用户jwcode到redis + group.POST("/is-eligible-user", couponusers.NewCouponUsers().IsEligibleUser) //判断用户是否满足领取条件 group.POST("/issue-coupon-to-users", couponusers.NewCouponUsers().IssueCouponUser) //给单个用户发放卡券 }) s.Run() diff --git a/internal/consts/consts.go b/internal/consts/consts.go index 6f51821..8db707d 100644 --- a/internal/consts/consts.go +++ b/internal/consts/consts.go @@ -5,5 +5,6 @@ const ( //内容: http://39.101.133.168:8828/hljw //测试环境解析token接口: http://39.101.133.168:8828/hljw/api/v2/member/info //正式环境解析token接口: http://api.homilychart.com:8828/hljw/api/v2/member/info - URL_HASH_KEY = "HLJW_BASE_URL" + //URL_HASH_KEY = "HLJW_BASE_URL" + URL_HASH_KEY = "HLJW_URL" ) diff --git a/internal/controller/couponusers/couponUsers.go b/internal/controller/couponusers/couponUsers.go index 178081a..352ed1a 100644 --- a/internal/controller/couponusers/couponUsers.go +++ b/internal/controller/couponusers/couponUsers.go @@ -142,9 +142,42 @@ func (c *CouponUsers) InsertJwcodesToRedisByExcel(r *ghttp.Request) { } +// 判断某用户能否抽到卡券 +func (c *CouponUsers) IsEligibleUser(r *ghttp.Request) { + // 解析请求 + var req *couponusers.IsEligibleUserReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Error(err.Error())) + } + fmt.Println(req.CouponIds) + + /*解析token,获取jwcode*/ + token := r.Header.Get("token") + if token == "" { + r.Response.WriteJsonExit(dto.Error("token为空")) + } + + jwcode, err := utility.GetJwcodeJSON(token) + if err != nil { + r.Response.WriteJsonExit(dto.Error(err.Error())) + } + /*解析token,获取jwcode*/ + + img, err := service.CouponUsers().IsEligibleUser(r.Context(), jwcode, req.CouponIds) + if err != nil { + r.Response.WriteJsonExit(dto.Error(err.Error())) + } + if img != "" { + r.Response.WriteJsonExit(dto.SuccessWithMsgAndData("该用户已领取过该卡券", img)) + } + r.Response.WriteJsonExit(dto.SuccessWithMsg("用户符合领取条件")) + +} + // 给单个用户发放卡券 func (c *CouponUsers) IssueCouponUser(r *ghttp.Request) { // 解析请求参数 + //没有token,用于测试 //var req *couponusers.InsertCouponUserReq //if err := r.Parse(&req); err != nil { @@ -184,5 +217,13 @@ func (c *CouponUsers) IssueCouponUser(r *ghttp.Request) { /*近期使用*/ -//未编写 -//导出拥有卡券的用户 +// 未编写 +// 导出拥有卡券的用户 +func (c *CouponUsers) ExportCouponUsers(r *ghttp.Request) { + var req *couponusers.GetCouponUsersReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Error(err.Error())) + } + //查询数据,创建excel文件,设置表头,写入数据,设置文件名,保存到缓冲区并返回,设置响应头,指定内容类型为excel文件,指定文件名为fileName,将buffer中的内容写入到响应 + +} diff --git a/internal/logic/couponusers/couponUsers.go b/internal/logic/couponusers/couponUsers.go index a5eea82..4286988 100644 --- a/internal/logic/couponusers/couponUsers.go +++ b/internal/logic/couponusers/couponUsers.go @@ -127,9 +127,9 @@ func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, return nil, errors.New("没有工作表") } // 读取第一个工作表 - sheetName := GetSheetMap[1] + //sheetName := GetSheetMap[1] //rows, err := f.GetRows(f.GetSheetName(1)) - rows, err := f.GetRows(sheetName) + rows, err := f.GetRows("Sheet1") if err != nil { return nil, err } @@ -222,10 +222,11 @@ func (s *sCouponUsers) InsertJwcodesToRedisByExcel(file multipart.File) (err err if len(GetSheetMap) == 0 { return errors.New("没有工作表") } + // 读取第一个工作表 - sheetName := GetSheetMap[1] + //sheetName := GetSheetMap[1] //rows, err := f.GetRows(f.GetSheetName(1)) - rows, err := f.GetRows(sheetName) + rows, err := f.GetRows("Sheet1") if err != nil { return err } @@ -267,6 +268,46 @@ func (s *sCouponUsers) InsertJwcodesToRedisByExcel(file multipart.File) (err err return //返回nil表示成功 } +// 判断某用户能否抽到卡券 +func (s *sCouponUsers) IsEligibleUser(ctx context.Context, jwcode int, couponIds []int) (img string, err error) { + /*从redis中获取符合条件的精网号EligibleJwcodes*/ + redisResult, err := g.Redis().Get(ctx, "EligibleJwcodes") + if err != nil { + return "", errors.New("从redis中获取数据失败") + } + var EligibleJwcodes []int + err = json.Unmarshal(redisResult.Bytes(), &EligibleJwcodes) + if err != nil { + return "", errors.New("redis中数据解析失败") + } + if len(EligibleJwcodes) == 0 { + return "", errors.New("redis中数据为空") + } + /*从redis中获取符合条件的精网号EligibleJwcodes*/ + + //检查jwcode是否在EligibleJwcodes中 + for _, EligibleJwcode := range EligibleJwcodes { + if EligibleJwcode == jwcode { + //存在,有资格,判断是否抽取过 + for _, couponId := range couponIds { + count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count() + if err != nil { + return "", errors.New("检索数据库中是否已存在数据失败") + } + //有记录,抽取过 + if count > 0 { + err = dao.CouponUsers.Ctx(ctx).Fields("img_url").Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Scan(&img) + return img, errors.New("该用户已领取过该卡券") + } + } + //所有的都没有记录,没有抽取过 + return "", nil + } + } + + return "", errors.New("该用户不满足领取条件") +} + // 给单个用户发放卡券 func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId int) (err error) { //查看库中是否已经存在 @@ -290,6 +331,9 @@ func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId i if err != nil { return errors.New("redis中数据解析失败") } + if len(EligibleJwcodes) == 0 { + return errors.New("redis中数据为空") + } /*从redis中获取符合条件的精网号EligibleJwcodes*/ //检查jwcode是否在EligibleJwcodes中 diff --git a/internal/logic/logic.go b/internal/logic/logic.go index 64cb80c..18ff594 100644 --- a/internal/logic/logic.go +++ b/internal/logic/logic.go @@ -6,4 +6,5 @@ package logic import ( _ "CouponBackendGo/internal/logic/couponusers" + _ "CouponBackendGo/internal/logic/middleware" ) diff --git a/internal/logic/middleware/interceptor.go b/internal/logic/middleware/interceptor.go new file mode 100644 index 0000000..9dc959a --- /dev/null +++ b/internal/logic/middleware/interceptor.go @@ -0,0 +1,10 @@ +package middleware + +import ( + "github.com/gogf/gf/v2/net/ghttp" +) + +func MiddlewareCORS(r *ghttp.Request) { + r.Response.CORSDefault() + r.Middleware.Next() +} diff --git a/internal/service/couponusers.go b/internal/service/couponusers.go index 8f3215e..e06d13a 100644 --- a/internal/service/couponusers.go +++ b/internal/service/couponusers.go @@ -24,6 +24,8 @@ type ( InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num int, err error) // 导入满足条件的用户jwcode到redis InsertJwcodesToRedisByExcel(file multipart.File) (err error) + // 判断某用户能否抽到卡券 + IsEligibleUser(ctx context.Context, jwcode int, couponIds []int) (img string, err error) // 给单个用户发放卡券 IssueCouponToUser(ctx context.Context, jwcode int, couponId int) (err error) } diff --git a/internal/service/middleware.go b/internal/service/middleware.go new file mode 100644 index 0000000..3d70438 --- /dev/null +++ b/internal/service/middleware.go @@ -0,0 +1,8 @@ +// ================================================================================ +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// You can delete these comments if you wish manually maintain this interface file. +// ================================================================================ + +package service + +type ()