From b84643bdc2eaa37095d7447be62c204595884067 Mon Sep 17 00:00:00 2001 From: lijikun Date: Fri, 27 Dec 2024 21:48:00 +0800 Subject: [PATCH] =?UTF-8?q?12.27=E6=97=A5=EF=BC=8C=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E7=BB=99=E7=94=A8=E6=88=B7=E5=8F=91=E6=94=BE=E5=8D=A1=E5=88=B8?= =?UTF-8?q?=EF=BC=8C=E7=BB=99=E5=8D=95=E4=B8=AA=E7=94=A8=E6=88=B7=E5=8F=91?= =?UTF-8?q?=E6=94=BE=E5=8D=A1=E5=88=B8=EF=BC=8C=E4=BB=8Eexcel=E4=B8=AD?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E6=95=B0=E6=8D=AE=E5=88=B0redis=EF=BC=8C?= =?UTF-8?q?=E8=A7=A3=E6=9E=90token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/v1/couponusers/couponUsers.go | 9 ++ go.mod | 2 +- go.sum | 1 + internal/cmd/cmd.go | 10 +- internal/consts/consts.go | 8 ++ internal/controller/couponusers/couponUsers.go | 122 +++++++++++++++++- internal/dao/internal/coupon_users.go | 2 + internal/logic/couponusers/couponUsers.go | 168 +++++++++++++++++++++++-- internal/model/do/coupon_users.go | 1 + internal/model/dto/Result.go | 9 ++ internal/model/entity/coupon_users.go | 1 + internal/service/couponusers.go | 6 +- utility/utility.go | 129 +++++++++++++++++++ 13 files changed, 445 insertions(+), 23 deletions(-) create mode 100644 utility/utility.go diff --git a/api/v1/couponusers/couponUsers.go b/api/v1/couponusers/couponUsers.go index baf834d..3fd5f27 100644 --- a/api/v1/couponusers/couponUsers.go +++ b/api/v1/couponusers/couponUsers.go @@ -17,3 +17,12 @@ type DelCouponUserByJwcodeReq struct { CouponId int `json:"couponId" dc:"卡券id"` Jwcode int `json:"jwcode" dc:"精网号"` } + +type InsertCouponUserReq struct { + CouponId int `json:"couponId" dc:"卡券id"` + Jwcodes []int `json:"jwcodes" dc:"精网号"` +} + +type IssueCouponUserReq struct { + CouponId int `json:"couponId" dc:"卡券id"` +} diff --git a/go.mod b/go.mod index 1fab199..111b9c7 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/gogf/gf/contrib/drivers/mysql/v2 v2.8.3 github.com/gogf/gf/contrib/nosql/redis/v2 v2.8.3 github.com/gogf/gf/v2 v2.8.3 + github.com/xuri/excelize/v2 v2.9.0 ) require ( @@ -32,7 +33,6 @@ require ( github.com/richardlehane/msoleps v1.0.4 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect - github.com/xuri/excelize/v2 v2.9.0 // indirect github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect go.opentelemetry.io/otel v1.24.0 // indirect go.opentelemetry.io/otel/metric v1.24.0 // indirect diff --git a/go.sum b/go.sum index 49b0fcc..cda0df0 100644 --- a/go.sum +++ b/go.sum @@ -75,6 +75,7 @@ go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY= golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= +golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ= golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI= golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index ca540c9..6d1d5c5 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -17,9 +17,13 @@ var ( Func: func(ctx context.Context, parser *gcmd.Parser) (err error) { s := g.Server() s.Group("/api/coupon_backend", func(group *ghttp.RouterGroup) { - group.POST("/get-coupon-users", couponusers.NewCouponUsers().GetCouponUsers) - group.POST("/delete-coupon-user", couponusers.NewCouponUsers().DeleteCouponUserByJwcode) - group.POST("/import-excel", couponusers.NewCouponUsers().InsertJwcodeByExcel) + 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("/issue-coupon-to-users", couponusers.NewCouponUsers().IssueCouponUser) //给单个用户发放卡券 }) s.Run() return nil diff --git a/internal/consts/consts.go b/internal/consts/consts.go index d709a2b..6f51821 100644 --- a/internal/consts/consts.go +++ b/internal/consts/consts.go @@ -1 +1,9 @@ package consts + +const ( + URL_KEY = "jingwang:cms:env" //Redis中的键 + //内容: 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" +) diff --git a/internal/controller/couponusers/couponUsers.go b/internal/controller/couponusers/couponUsers.go index 2754c63..178081a 100644 --- a/internal/controller/couponusers/couponUsers.go +++ b/internal/controller/couponusers/couponUsers.go @@ -4,6 +4,8 @@ import ( "CouponBackendGo/api/v1/couponusers" "CouponBackendGo/internal/model/dto" "CouponBackendGo/internal/service" + "CouponBackendGo/utility" + "fmt" "github.com/gogf/gf/v2/net/ghttp" "mime/multipart" ) @@ -14,65 +16,173 @@ func NewCouponUsers() *CouponUsers { return &CouponUsers{} } +// 获取拥有卡券的用户列表 func (c *CouponUsers) GetCouponUsers(r *ghttp.Request) { + //解析请求 var req *couponusers.GetCouponUsersReq if err := r.Parse(&req); err != nil { r.Response.WriteJsonExit(dto.Error(err.Error())) } result, err := service.CouponUsers().GetCouponUsersByCondition(r.Context(), req.CouponId, req.Jwcode, req.Name) + //错误处理 if err != nil { r.Response.WriteJsonExit(dto.Error(err.Error())) } + //没有错误,但获取到了0条 if result == nil { r.Response.WriteJsonExit(dto.Error("没有符合条件的用户")) } + //成功处理 r.Response.WriteJsonExit(dto.SuccessWithData(result)) } +// 删除用户卡券 func (c *CouponUsers) DeleteCouponUserByJwcode(r *ghttp.Request) { + // 解析请求 var req *couponusers.DelCouponUserByJwcodeReq if err := r.Parse(&req); err != nil { r.Response.WriteJsonExit(dto.Error(err.Error())) } result, err := service.CouponUsers().DeleteCouponUserByJwcode(r.Context(), req.CouponId, req.Jwcode) + //错误处理 if err != nil { r.Response.WriteJsonExit(dto.Error(err.Error())) } + //获取影响到的数据条数 affected, err := result.RowsAffected() + //没有错误,但删除0条 if affected == 0 { r.Response.WriteJsonExit(dto.Error("删除失败,删除了0条")) } + //成功处理 r.Response.WriteJsonExit(dto.SuccessWithMsg("删除成功")) - //r.Response.WriteJsonExit(dto.SuccessWithData(result)) } -// 待测试 // 接受前端传来的excel表格,并解析,返回jwcode切片,可以有重复的,在插入时进行验证,如果有重复的,只插入一次 func (c *CouponUsers) InsertJwcodeByExcel(r *ghttp.Request) { - // 从请求中获取文件 file, _, err := r.FormFile("excelFile") if err != nil { r.Response.WriteJsonExit(dto.Error(err.Error())) } - // 关闭文件 + // 延时关闭文件 defer func(file multipart.File) { err := file.Close() if err != nil { // 处理关闭文件时的错误 - r.Response.WriteJsonExit(dto.Error("关闭文件失败")) + r.Response.WriteJsonExit(dto.Error("获取的文件关闭失败")) } }(file) //参数校验,检查文件是否为空 if file == nil { - r.Response.WriteJsonExit(dto.Error("文件为空")) + r.Response.WriteJsonExit(dto.Error("获取的文件为空")) } jwcodes, err := service.CouponUsers().InsertJwcodeByExcel(file) + //错误处理 if err != nil { r.Response.WriteJsonExit(dto.Error(err.Error())) } + //成功处理 r.Response.WriteJsonExit(dto.SuccessWithData(jwcodes)) } + +// 给用户发放卡券 +func (c *CouponUsers) InsertCouponUser(r *ghttp.Request) { + // 解析请求 + var req *couponusers.InsertCouponUserReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Error(err.Error())) + } + + result, err := service.CouponUsers().InsertCouponUsersByJwcodes(r.Context(), req.Jwcodes, req.CouponId) + //错误处理 + if err != nil { + r.Response.WriteJsonExit(dto.Error(err.Error())) + } + //没报错,但插入数据0条 + if result == 0 { + r.Response.WriteJsonExit(dto.Error("发放失败,发放了0人")) + } + //成功处理 + r.Response.WriteJsonExit(dto.SuccessWithMsg(fmt.Sprintf("发放成功,发放了%d人", result))) +} + +/*近期使用*/ +// 导入满足条件的用户jwcode到redis +func (c *CouponUsers) InsertJwcodesToRedisByExcel(r *ghttp.Request) { + // 从请求中获取文件 + file, _, err := r.FormFile("EligibleJwcodesExcel") + if err != nil { + r.Response.WriteJsonExit(dto.Error(err.Error())) + } + // 延时关闭文件 + defer func(file multipart.File) { + err := file.Close() + if err != nil { + // 处理关闭文件时的错误 + r.Response.WriteJsonExit(dto.Error("获取的文件关闭失败")) + } + }(file) + + //参数校验,检查文件是否为空 + if file == nil { + r.Response.WriteJsonExit(dto.Error("获取的文件为空")) + } + + err = service.CouponUsers().InsertJwcodesToRedisByExcel(file) + //错误处理 + if err != nil { + r.Response.WriteJsonExit(dto.Error(err.Error())) + } + //成功处理 + r.Response.WriteJsonExit(dto.SuccessWithMsg("导入redis成功")) + +} + +// 给单个用户发放卡券 +func (c *CouponUsers) IssueCouponUser(r *ghttp.Request) { + // 解析请求参数 + //没有token,用于测试 + //var req *couponusers.InsertCouponUserReq + //if err := r.Parse(&req); err != nil { + // r.Response.WriteJsonExit(dto.Error(err.Error())) + //} + + /*正式*/ + var req *couponusers.IssueCouponUserReq + if err := r.Parse(&req); err != nil { + r.Response.WriteJsonExit(dto.Error(err.Error())) + } + //解析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,用于测试 + //err := service.CouponUsers().IssueCouponToUser(r.Context(), req.Jwcodes[0], req.CouponId) + /*正式*/ + err = service.CouponUsers().IssueCouponToUser(r.Context(), jwcode, req.CouponId) + /*正式*/ + //错误处理 + if err != nil { + r.Response.WriteJsonExit(dto.Error(err.Error())) + } + //成功处理 + r.Response.WriteJsonExit(dto.SuccessWithMsg("发放成功")) + +} + +/*近期使用*/ + +//未编写 +//导出拥有卡券的用户 diff --git a/internal/dao/internal/coupon_users.go b/internal/dao/internal/coupon_users.go index b0d66ef..bc119fb 100644 --- a/internal/dao/internal/coupon_users.go +++ b/internal/dao/internal/coupon_users.go @@ -26,6 +26,7 @@ type CouponUsersColumns struct { CouponId string // 优惠券id Code string // State string // 0 未使用 1 已使用 + Record string // 武器记录 } // couponUsersColumns holds the columns for table coupon_users. @@ -36,6 +37,7 @@ var couponUsersColumns = CouponUsersColumns{ CouponId: "coupon_id", Code: "code", State: "state", + Record: "record", } // NewCouponUsersDao creates and returns a new DAO object for table data access. diff --git a/internal/logic/couponusers/couponUsers.go b/internal/logic/couponusers/couponUsers.go index ee5dce1..a5eea82 100644 --- a/internal/logic/couponusers/couponUsers.go +++ b/internal/logic/couponusers/couponUsers.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gtime" "github.com/xuri/excelize/v2" "mime/multipart" "strconv" @@ -23,16 +24,17 @@ func init() { } // 根据优惠券id查看拥有优惠券的用户 -func (s *sCouponUsers) GetCouponUsersByCondition(ctx context.Context, couponId, jwcode int, name string) (users []couponusers.GetCouponUsersRes, err error) { - //从redis中获取数据但不返回 +func (s *sCouponUsers) GetCouponUsersByCondition(ctx context.Context, couponId, jwcode int, name string) (users []couponusers.GetCouponUsersRes, err error) { //待解释 + //从redis中获取所有数据但暂时不返回, 用于条件查询 value, _ := g.Redis().Get(ctx, fmt.Sprintf("%d CouponUsers", couponId)) + //redis中有数据 if value.String() != "" { err = json.Unmarshal(value.Bytes(), &users) if err != nil { return nil, errors.New("redis中数据解析失败") } - } else { - + } else { //redis中没有数据 + //没有从redis中获取成功,从数据库中查询,并存储到redis中 /*查询所有数据*/ //在coupon_users中查询出对应的jwcode err = dao.CouponUsers.Ctx(ctx).Fields("jwcode"). @@ -59,6 +61,7 @@ func (s *sCouponUsers) GetCouponUsersByCondition(ctx context.Context, couponId, } + //条件查询 if jwcode != 0 && name == "" { var result []couponusers.GetCouponUsersRes for _, user := range users { @@ -110,18 +113,16 @@ func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, if err != nil { return nil, errors.New("打开文件失败") } - // 关闭文件 + // 延时关闭文件 defer func(f *excelize.File) { err := f.Close() if err != nil { - // 处理关闭文件时的错误 - return + return // 返回错误 } }(f) //读取所有工作表名称 GetSheetMap := f.GetSheetMap() - fmt.Println(GetSheetMap) if len(GetSheetMap) == 0 { return nil, errors.New("没有工作表") } @@ -155,15 +156,158 @@ func (s *sCouponUsers) InsertJwcodeByExcel(file multipart.File) (jwcodes []int, if err != nil { return nil, errors.New("参数转换失败") } - fmt.Println(jwcode) jwcodes = append(jwcodes, jwcode) /*参数校验*/ } - return + return //返回jwcodes切片 } // 根据精网号发放用户优惠券 -func (s *sCouponUsers) InsertCouponUserByJwcode(ctx context.Context) { +func (s *sCouponUsers) InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num int, err error) { + //去重 + m := make(map[int]bool) + var uniqueJwcodes []int //存放去重后的精网号 + for _, jwcode := range jwcodes { + if _, exist := m[jwcode]; !exist { + m[jwcode] = true + uniqueJwcodes = append(uniqueJwcodes, jwcode) + } + } + + //插入数据 + for _, jwcode := range uniqueJwcodes { + //检查数据库中是否存在 + count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count() + if err != nil { + return num, errors.New("检索数据库中是否已存在数据失败") + } + //不存在,可以插入 + if count == 0 { + result, err := dao.CouponUsers.Ctx(ctx).Insert(g.Map{ + "jwcode": jwcode, + "coupon_id": couponId, + "time": gtime.Now().Unix(), + }) + if err != nil { + return num, errors.New("插入数据库失败") + } + //获取受影响的行数 + affected, err := result.RowsAffected() + num += int(affected) + } + } + + return num, err //返回受影响的行数,即新增的条数 +} + +// 导入满足条件的用户jwcode到redis +func (s *sCouponUsers) InsertJwcodesToRedisByExcel(file multipart.File) (err error) { + var ctx g.Ctx + //打开文件并返回一个excelize.File对象,用于读取和操作Excel文件的内容 + f, err := excelize.OpenReader(file) + if err != nil { + return errors.New("打开文件失败") + } + // 延时关闭文件 + defer func(f *excelize.File) { + err := f.Close() + if err != nil { + // 处理关闭文件时的错误 + return + } + }(f) + + //读取所有工作表名称 + GetSheetMap := f.GetSheetMap() + if len(GetSheetMap) == 0 { + return errors.New("没有工作表") + } + // 读取第一个工作表 + sheetName := GetSheetMap[1] + //rows, err := f.GetRows(f.GetSheetName(1)) + rows, err := f.GetRows(sheetName) + if err != nil { + return err + } + + // 将每行的第一列转换为int,并添加到切片中 + var jwcodes []int + for i, row := range rows { + //跳过第一行 + if i == 0 { + continue + } + // 假设jwcode在每行的第一列 + /*参数校验*/ + //参数校验,检查每行是否有足够数据 + if len(row) == 0 { + continue // 跳过空行 + } + //参数校验,检查jwcode是否为非空字符串 + jwcodeStr := row[0] + if jwcodeStr == "" { + continue // 跳过空行 + } + //将字符串转换为整数 + jwcode, err := strconv.Atoi(jwcodeStr) + if err != nil { + return errors.New("参数转换失败") + } + + jwcodes = append(jwcodes, jwcode) + /*参数校验*/ + } + + //将jwcodes存入redis + MarshalJwcodes, _ := json.Marshal(jwcodes) + _, err = g.Redis().Set(ctx, "EligibleJwcodes", MarshalJwcodes) + if err != nil { + return errors.New("存入redis失败") + } + return //返回nil表示成功 +} + +// 给单个用户发放卡券 +func (s *sCouponUsers) IssueCouponToUser(ctx context.Context, jwcode, couponId int) (err error) { + //查看库中是否已经存在 + count, err := dao.CouponUsers.Ctx(ctx).Where("jwcode = ?", jwcode).Where("coupon_id = ?", couponId).Count() + if err != nil { + return errors.New("检索数据库中是否已存在数据失败") + } + //已存在 不添加,直接返回 + if count > 0 { + return errors.New("该用户已领取该卡券") + } + + //不存在 查看是否满足条件,满足就添加,不满足就返回 + /*从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中数据解析失败") + } + /*从redis中获取符合条件的精网号EligibleJwcodes*/ + + //检查jwcode是否在EligibleJwcodes中 + for _, EligibleJwcode := range EligibleJwcodes { + if EligibleJwcode == jwcode { + //存在,可以插入 + _, err := dao.CouponUsers.Ctx(ctx).Insert(g.Map{ + "jwcode": jwcode, + "coupon_id": couponId, + "time": gtime.Now().Unix(), + }) + if err != nil { + return errors.New("插入数据库失败") + } + return err + } + } + + return errors.New("该用户精网号不符合领取条件") //遍历完了所有满足条件的用户,发现不在其中,不符合条件 - return } diff --git a/internal/model/do/coupon_users.go b/internal/model/do/coupon_users.go index 8aa0d1b..bc39628 100644 --- a/internal/model/do/coupon_users.go +++ b/internal/model/do/coupon_users.go @@ -17,4 +17,5 @@ type CouponUsers struct { CouponId interface{} // 优惠券id Code interface{} // State interface{} // 0 未使用 1 已使用 + Record interface{} // 武器记录 } diff --git a/internal/model/dto/Result.go b/internal/model/dto/Result.go index d3eb4c9..67c0e2d 100644 --- a/internal/model/dto/Result.go +++ b/internal/model/dto/Result.go @@ -34,6 +34,15 @@ func SuccessWithData(data interface{}) *Result { } } +/*成功,自定义信息和数据*/ +func SuccessWithMsgAndData(msg string, data interface{}) *Result { + return &Result{ + Code: 200, + Message: msg, + Data: data, + } +} + /*错误,只有错误信息,错误码0*/ func Error(msg string) *Result { return &Result{ diff --git a/internal/model/entity/coupon_users.go b/internal/model/entity/coupon_users.go index 8a49df3..19453c3 100644 --- a/internal/model/entity/coupon_users.go +++ b/internal/model/entity/coupon_users.go @@ -12,4 +12,5 @@ type CouponUsers struct { CouponId int `json:"couponId" orm:"coupon_id" description:"优惠券id"` // 优惠券id Code string `json:"code" orm:"code" description:""` // State int `json:"state" orm:"state" description:"0 未使用 1 已使用"` // 0 未使用 1 已使用 + Record string `json:"record" orm:"record" description:"武器记录"` // 武器记录 } diff --git a/internal/service/couponusers.go b/internal/service/couponusers.go index d589d91..8f3215e 100644 --- a/internal/service/couponusers.go +++ b/internal/service/couponusers.go @@ -21,7 +21,11 @@ type ( // 通过excel导入精网号 InsertJwcodeByExcel(file multipart.File) (jwcodes []int, err error) // 根据精网号发放用户优惠券 - InsertCouponUserByJwcode(ctx context.Context) + InsertCouponUsersByJwcodes(ctx context.Context, jwcodes []int, couponId int) (num int, err error) + // 导入满足条件的用户jwcode到redis + InsertJwcodesToRedisByExcel(file multipart.File) (err error) + // 给单个用户发放卡券 + IssueCouponToUser(ctx context.Context, jwcode int, couponId int) (err error) } ) diff --git a/utility/utility.go b/utility/utility.go new file mode 100644 index 0000000..43f23bf --- /dev/null +++ b/utility/utility.go @@ -0,0 +1,129 @@ +package utility + +import ( + "CouponBackendGo/internal/consts" + "CouponBackendGo/internal/model/dto" + "encoding/json" + "errors" + "fmt" + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gctx" + "io/ioutil" + "net/http" + "strconv" + "strings" +) + +// 查询数据库中的URL +func selectBaseUrl(hashKey string) *dto.Result { + + //查询数据库 + //env表中存储着一些所需要的地址,值等,可能是需要查出来存放到redis中(value字段中是值) + //key为"HLJW_URL"时,value为:http://39.101.133.168:8828/hljw,为测试环境时前置地址 + value, err := g.DB().Model("env").Where("`key` = ?", hashKey).Value("value") + if err != nil { + return dto.Error("数据库env查询失败") + } + if value.IsNil() || value.String() == "" { + return dto.Error("未找到对应数据") + } + return dto.SuccessWithData(value.String()) +} + +// 获取URL +func getUrl(key, hashKey string) *dto.Result { + ctx := gctx.New() + + //测试环境: http://39.101.133.168:8828/hljw/api/v2/member/info + //正式环境: http://api.homilychart.com:8828/hljw/api/v2/member/info + + //1. 从Redis获取URL + redisUrl, err := g.Redis().Do(ctx, "HGET", key, hashKey) + if err == nil && redisUrl.String() != "" { + return dto.SuccessWithMsgAndData(fmt.Sprintf("从Redis获取URL: %s", redisUrl.String()), redisUrl.String()) + } + + //2. 如果Redis中没有, 查询数据库 + urlResult := selectBaseUrl(hashKey) + if urlResult.Code != 200 { + return urlResult + } + url := urlResult.Data.(string) + + //3. 将URL存入Redis + _, err = g.Redis().Do(ctx, "HSET", key, hashKey, url) + if err != nil { + return dto.Error("将数据存入Redis失败") + } + + return dto.SuccessWithData(url) +} + +// 获取jwcode +func GetJwcodeJSON(token string) (int, error) { + + //获取基础URL 用于解析token的接口地址 + urlResult := getUrl(consts.URL_KEY, consts.URL_HASH_KEY) + if urlResult.Code != 200 { + return 0, errors.New(fmt.Sprintf("获取基础URL失败: %s", urlResult.Message)) + } + baseUrl := urlResult.Data.(string) + + //拼接完整的URL, 用于发送请求并解析token + url := baseUrl + "/api/v2/member/info" + requestBody := strings.NewReader(`{"token":"` + token + `"}`) + + //创建HTTP请求(POST) + req, err := http.NewRequest("POST", url, requestBody) + if err != nil { + return 0, fmt.Errorf("创建HTTP请求失败: %w", err) + } + //设置请求头 + req.Header.Set("Content-Type", "application/json") + + //发送请求 + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return 0, fmt.Errorf("发送HTTP请求失败: %w", err) // + } + //延时关闭响应体 + defer resp.Body.Close() + + //检查HTTP状态码 + if resp.StatusCode != http.StatusOK { + return 0, fmt.Errorf("HTTP请求失败, 状态码: %d", resp.StatusCode) + } + + //读取并解析响应体 + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return 0, fmt.Errorf("读取响应体失败: %w", err) + } + + //解析JSON数据 + var jsonResponse map[string]interface{} + err = json.Unmarshal(body, &jsonResponse) + if err != nil { + return 0, fmt.Errorf("解析JSON数据失败: %w", err) + } + + //提取data节点 + data, ok := jsonResponse["data"].(map[string]interface{}) + if !ok { + return 0, errors.New("响应体中没有有效的data节点") + } + + //提取jwcode字段节点并转换为整数 + jwcode, ok := data["jwcode"].(string) //首先尝试解析为字符串 + if ok { + jwcodeInt, err := strconv.Atoi(jwcode) //将字符串转换为整数 + if err != nil { + return 0, fmt.Errorf("解析jwcode字段为整数失败: %w", err) + } + return jwcodeInt, nil + } + + //如果jwcode不是字符串,返回错误日志 + return 0, errors.New("响应体中没有有效的jwcode字段") +}