WH01243 %!s(int64=4) %!d(string=hai) anos
pai
achega
fc9ee5dcc4

+ 1 - 1
api/integral.api

@@ -58,7 +58,7 @@ type (
 		SourceType    string `form:"sourceType,optional"`
 		Point         int64  `form:"point,range=[0:100000000]"`
 		EndDate       string `form:"endDate,optional"`
-		OperationType bool   `form:"operationType"`
+		OperationType bool   `form:"operationType" `
 		Abstract      string `form:"abstract,optional"`
 	}
 

+ 0 - 1
api/internal/handler/integralbalancecheckhandler.go

@@ -9,7 +9,6 @@ import (
 
 	"github.com/tal-tech/go-zero/rest/httpx"
 )
-
 func IntegralBalanceCheckHandler(ctx *svc.ServiceContext) http.HandlerFunc {
 	return func(w http.ResponseWriter, r *http.Request) {
 		var req types.CheckReq

+ 12 - 11
entity/integral.go

@@ -38,17 +38,18 @@ type BalanceUpdate struct {
 
 //积分流水
 type Flow struct {
-	Id         int64  `xorm:"pk autoincr id" form:"id" json:"id"`
-	UserId     string `xorm:"userId" form:"userId" json:"userId"`             //用户标识
-	PointType  int64  `xorm:"pointType" form:"pointType" json:"pointType"`    //积分类型
-	SourceId   string `xorm:"sourceId" form:"sourceId" json:"sourceId"`       //来源标识
-	SourceType string `xorm:"sourceType" form:"sourceType" json:"sourceType"` //来源类型
-	Point      int64  `xorm:"point" form:"point" json:"point"`                //积分
-	CreateTime string `xorm:"createTime" form:"createTime" json:"createTime"` //创建时间
-	EndDate    string `xorm:"endDate" form:"endDate" json:"endDate"`          //截止时间
-	AppId      string `xorm:"appId" form:"appId" json:"appId"`                //剑鱼标识
-	Sort       int64  `xorm:"sort" form:"sort" json:"sort"`                   //操作类型1加-1减
-	Abstract   string `xorm:"abstract" form:"abstract" json:"abstract"`       //积分类型名称
+	Id            int64  `xorm:"pk autoincr id" form:"id" json:"id"`
+	UserId        string `xorm:"userId" form:"userId" json:"userId"`                      //用户标识
+	PointType     int64  `xorm:"pointType" form:"pointType" json:"pointType"`             //积分类型
+	SourceId      string `xorm:"sourceId" form:"sourceId" json:"sourceId"`                //来源标识
+	SourceType    string `xorm:"sourceType" form:"sourceType" json:"sourceType"`          //来源类型
+	Point         int64  `xorm:"point" form:"point" json:"point"`                         //积分
+	CreateTime    string `xorm:"createTime" form:"createTime" json:"createTime"`          //创建时间
+	EndDate       string `xorm:"endDate" form:"endDate" json:"endDate"`                   //截止时间
+	AppId         string `xorm:"appId" form:"appId" json:"appId"`                         //剑鱼标识
+	Sort          int64  `xorm:"sort" form:"sort" json:"sort"`                            //操作类型1加-1减
+	OperationType int64  `xorm:"OperationType" form:"OperationType" json:"OperationType"` //1永久2时效
+	Abstract      string `xorm:"abstract" form:"abstract" json:"abstract"`                //积分类型名称
 }
 
 //积分流水

+ 1 - 0
go.mod

@@ -10,6 +10,7 @@ require (
 	github.com/iancoleman/strcase v0.1.3 // indirect
 	github.com/tal-tech/go-zero v1.1.5
 	go.uber.org/automaxprocs v1.4.0 // indirect
+	golang.org/x/time v0.0.0-20191024005414-555d28b269f0
 	google.golang.org/grpc v1.29.1
 	google.golang.org/protobuf v1.25.0
 

+ 2 - 0
rpc/etc/integral.yaml

@@ -1,5 +1,7 @@
 Name: integral.rpc
 ListenOn: 127.0.0.1:8087
+Log:
+  Mode: console
 Etcd:
   Hosts:
     - 127.0.0.1:2379

+ 11 - 0
rpc/integral.go

@@ -7,12 +7,14 @@ import (
 	"app.yhyue.com/moapp/jyPoints/rpc/internal/server"
 	"app.yhyue.com/moapp/jyPoints/rpc/internal/svc"
 	"app.yhyue.com/moapp/jyPoints/service"
+	"context"
 	"flag"
 	"fmt"
 	_ "github.com/go-sql-driver/mysql"
 	"github.com/go-xorm/xorm"
 	"github.com/tal-tech/go-zero/core/conf"
 	"github.com/tal-tech/go-zero/zrpc"
+	"golang.org/x/time/rate"
 	"google.golang.org/grpc"
 	"log"
 )
@@ -22,6 +24,7 @@ var c config.Config
 var (
 	integralService = &service.IntegralService{}
 )
+var limiter = rate.NewLimiter(rate.Limit(100), 100)
 
 
 func main() {
@@ -31,6 +34,7 @@ func main() {
 	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
 		integral.RegisterIntegralServer(grpcServer, srv)
 	})
+	s.AddUnaryInterceptors(rateLimitInterceptor)
 	defer s.Stop()
 	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
 	s.Start()
@@ -46,4 +50,11 @@ func init() {
 		log.Fatal("数据库连接失败:", err)
 	}
 	fmt.Println(c.DataSource+"链接成功")
+}
+
+
+func rateLimitInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
+	fmt.Println("拦截器")
+	log.Println("方法名:",info.FullMethod,"  参数:",req)
+	return handler(ctx, req)
 }

+ 0 - 1
rpc/integral/integral.pb.go

@@ -33,7 +33,6 @@ type Req struct {
 	state         protoimpl.MessageState
 	sizeCache     protoimpl.SizeCache
 	unknownFields protoimpl.UnknownFields
-
 	UserId        string `protobuf:"bytes,1,opt,name=userId,proto3" json:"userId,omitempty"`
 	AppId         string `protobuf:"bytes,2,opt,name=appId,proto3" json:"appId,omitempty"`
 	PointType     int64  `protobuf:"varint,3,opt,name=pointType,proto3" json:"pointType,omitempty"`

+ 2 - 2
rpc/internal/logic/integralbalancechecklogic.go

@@ -1,9 +1,9 @@
 package logic
 
 import (
-	"context"
 	"app.yhyue.com/moapp/jyPoints/rpc/integral"
 	"app.yhyue.com/moapp/jyPoints/rpc/internal/svc"
+	"context"
 
 	"github.com/tal-tech/go-zero/core/logx"
 )
@@ -26,7 +26,7 @@ func NewIntegralBalanceCheckLogic(ctx context.Context, svcCtx *svc.ServiceContex
 func (l *IntegralBalanceCheckLogic) IntegralBalanceCheck(in *integral.Req) (*integral.Resp, error) {
 	// todo: add your logic here and delete this line
 	result := &integral.Resp{}
-	flag,total,expire := integralService.IntegralBalanceCheckService(in.UserId,in.AppId)
+	flag, total, expire := integralService.IntegralBalanceCheckService(in.UserId, in.AppId)
 	if flag {
 		result.Code = 1
 		result.Data = total

+ 25 - 20
service/integralService.go

@@ -20,14 +20,17 @@ func (service *IntegralService) IntegralAddService(data entity.FlowJSON) (int64,
 	flow.SourceId = data.SourceId
 	//flow.SourceType = data.SourceType
 	flow.PointType = data.PointType
-	flow.SourceType=fmt.Sprint(data.PointType)
+	flow.SourceType = fmt.Sprint(data.PointType)
 	flow.Point = data.Point
-	flow.EndDate=data.EndDate
+	flow.EndDate = data.EndDate
 	flow.CreateTime = time.Now().Format("2006-01-02 15:04:05")
 	flow.AppId = data.AppId
 	flow.Sort = entity.AddCode
-	flow.Abstract=data.Abstract
+	flow.Abstract = data.Abstract
 	var numb = int64(0)
+	if data.OperationType {
+		flow.OperationType = 1
+	}
 	numb, err = orm.Table("integral_flow").Insert(flow)
 	if err != nil && numb == 0 {
 		log.Print("新增流水失败:", err)
@@ -41,6 +44,7 @@ func (service *IntegralService) IntegralAddService(data entity.FlowJSON) (int64,
 	if data.OperationType {
 		//永久积分
 		//先查看是否有EndDate的积分
+
 		soldelist := []entity.Solde{}
 		err = orm.Table("integral_solde").Where("appId=? and  userId=? and endDate='' and perManEntPoints != 0 AND timePoints = 0 ", data.AppId, data.UserId).Find(&soldelist)
 		if len(soldelist) > 0 {
@@ -62,6 +66,7 @@ func (service *IntegralService) IntegralAddService(data entity.FlowJSON) (int64,
 		}
 
 	} else {
+		flow.OperationType = 0
 		solde.EndDate = data.EndDate
 		//先查看是否有EndDate的积分
 		soldelist := []entity.Solde{}
@@ -128,11 +133,11 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 	b, err := orm.Table("integral_balance").Select("countPoints,id").
 		Where("userId = ? AND appId = ?", data.UserId, data.AppId).
 		Get(&balance)
-	if  err != nil {
+	if err != nil {
 		log.Printf("积分余额查询出错,userId:[%s],err:[%v]", data.UserId, err)
 		return entity.ErrorCode, "积分余额查询出错"
 	}
-	if !b{
+	if !b {
 		log.Printf("没有查询到[%s]的积分记录,err:[%v]", data.UserId, err)
 		return entity.ErrorCode, "没有查询到积分记录"
 	}
@@ -161,7 +166,7 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 				//够消耗
 				solde.PerManEntPoints = solde.PerManEntPoints - point
 				point = 0
-				af, err =  orm.Table("integral_solde").
+				af, err = orm.Table("integral_solde").
 					ID(solde.Id).
 					Cols("perManEntPoints").
 					Update(solde)
@@ -218,10 +223,10 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 	//消耗积分流水记录
 	flow := entity.Flow{}
 	flow.UserId = data.UserId
-	flow.SourceType=fmt.Sprint(data.PointType)
+	flow.SourceType = fmt.Sprint(data.PointType)
 	flow.PointType = data.PointType
 	flow.SourceId = data.SourceId
-	flow.Abstract=data.Abstract
+	flow.Abstract = data.Abstract
 	//flow.SourceType = data.SourceType
 	flow.Point = data.Point
 	flow.CreateTime = time.Now().Format("2006-01-02 15:04:05")
@@ -229,7 +234,7 @@ func (service *IntegralService) IntegralConsumeService(data entity.FlowJSON) (in
 	flow.AppId = data.AppId
 	flow.Sort = entity.ReduceCode
 	af, err = orm.Table("integral_flow").Insert(&flow)
-	if err != nil || af == 0{
+	if err != nil || af == 0 {
 		log.Print("积分记录失败")
 		orm.Rollback()
 		return entity.ErrorCode, "积分记录失败"
@@ -470,7 +475,7 @@ func (service *IntegralService) IntegralGuardService(endDate string) bool {
 }
 
 //积分余额查询
-func (service *IntegralService) IntegralBalanceCheckService(userId,appId string  ) (bool, int64,int64) {
+func (service *IntegralService) IntegralBalanceCheckService(userId, appId string) (bool, int64, int64) {
 	orm := entity.Engine
 	//积分总余额
 	balance := entity.Balance{}
@@ -479,7 +484,7 @@ func (service *IntegralService) IntegralBalanceCheckService(userId,appId string
 		Get(&balance)
 	if err != nil {
 		log.Println("积分查询出错")
-		return false, 0,0
+		return false, 0, 0
 	}
 	//积分30天内到期余额
 	var solde entity.Solde
@@ -489,9 +494,9 @@ func (service *IntegralService) IntegralBalanceCheckService(userId,appId string
 		Get(&solde)
 	if err != nil {
 		log.Println(err)
-		return false, 0,0
+		return false, 0, 0
 	}
-	return true, balance.CountPoints,solde.TimePoints
+	return true, balance.CountPoints, solde.TimePoints
 }
 
 //按月查询积分使用情况
@@ -499,11 +504,11 @@ func (service *IntegralService) IntegralDetailedCheck(data entity.FlowJSON) ([]*
 	orm := entity.Engine.NewSession()
 	//查询时间范围内的
 	//查询新增消耗的积分和消耗积分 searchType 0全部 1新增 -1消耗
-	searchCode :="1=1"
-	if data.SearchType==1{
-		searchCode="   flow.Sort=1"
-	}else if data.SearchType==-1{
-		searchCode="   flow.Sort=-1"
+	searchCode := "1=1"
+	if data.SearchType == 1 {
+		searchCode = "   flow.Sort=1"
+	} else if data.SearchType == -1 {
+		searchCode = "   flow.Sort=-1"
 	}
 	var flowList []*entity.FlowReq
 	numb, err := orm.Table("integral_flow").Alias("flow").
@@ -512,7 +517,7 @@ func (service *IntegralService) IntegralDetailedCheck(data entity.FlowJSON) ([]*
 		Where("flow.userId = ? AND flow.appId = ?", data.UserId, data.AppId).
 		And(searchCode).
 		Desc("flow.createTime").
-		Limit(int(data.PageSize), (int(data.Page-1))*int(data.PageSize)).
+		Limit(int(data.PageSize), (int(data.Page - 1))*int(data.PageSize)).
 		FindAndCount(&flowList)
 	var flowReq []*integral.Point
 	for _, value := range flowList {
@@ -526,7 +531,7 @@ func (service *IntegralService) IntegralDetailedCheck(data entity.FlowJSON) ([]*
 		point.SourceId = value.SourceId
 		point.SourceType = value.SourceType
 		point.Name = value.Name
-		point.Abstract=value.Abstract
+		point.Abstract = value.Abstract
 		flowReq = append(flowReq, &point)
 	}
 	fmt.Println(flowList)