diff --git a/README.md b/README.md index 51d7320..697d734 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ -goctl api go -api .\game_open_api\app_user.api -dir .\game_open_api\ --style=goZero \ No newline at end of file + + +```shell +goctl api go -api .\game_open_api\app_user.api -dir .\game_open_api\ --style=go_zero + + +goctl rpc protoc .\dw_server\dw_server.proto --go-grpc_out .\dw_server\ --go_out .\dw_server\ --zrpc_out .\dw_server\ -c -style go_zero +``` \ No newline at end of file diff --git a/game_open_api/etc/gameopenapi-api.yaml b/game_open_api/etc/gameopenapi-api.yaml index 9068432..2367191 100644 --- a/game_open_api/etc/gameopenapi-api.yaml +++ b/game_open_api/etc/gameopenapi-api.yaml @@ -15,4 +15,8 @@ Log: Level: debug # Mode: file Path: ./logs - MaxSize: 100 \ No newline at end of file + MaxSize: 100 + +DWCache: + Host: 127.0.0.1:6379 + IdleTimeout: 60 \ No newline at end of file diff --git a/game_open_api/internal/app_api_helper/app_api_helper.go b/game_open_api/internal/app_api_helper/app_api_helper.go deleted file mode 100644 index 646b23c..0000000 --- a/game_open_api/internal/app_api_helper/app_api_helper.go +++ /dev/null @@ -1,35 +0,0 @@ -package app_api_helper - -import ( - "context" - "gitea.youtukeji.com.cn/xiabin/douyin-openapi/cache" - "gitea.youtukeji.com.cn/xiabin/youtu_ecpm/pkg/douyinapi" - "gitea.youtukeji.com.cn/xiabin/youtu_ecpm/pkg/wechat_api" - "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/model" - "sync" -) - -var once sync.Once - -var DouyinCli *douyinapi.DouYinOpenApiClient -var WechatCli *wechat_api.WechatApi - -func Init(ctx context.Context, appAccount model.AppAccountModel) { - once.Do(func() { - DouyinCli = douyinapi.NewDouYinOpenApiClient() - WechatCli = wechat_api.NewWechatOpenApiClient() - result, err := appAccount.FindAll(ctx) - if err != nil { - panic(err) - } - - for _, v := range *result { - if v.Type == 0 { - DouyinCli.NewAndStoreDouYinOpenApi(v.AppID, v.Secret, v.EcpmValue.V, v.EcpmView.V, cache.NewMemory()) - } else { - WechatCli.NewAndStoreWechatOpenApi(v.AppID, v.Secret, cache.NewMemory()) - } - } - }) - -} diff --git a/game_open_api/internal/config/config.go b/game_open_api/internal/config/config.go index fa1f610..feae364 100644 --- a/game_open_api/internal/config/config.go +++ b/game_open_api/internal/config/config.go @@ -17,4 +17,9 @@ type Config struct { AccessSecret string AccessExpire int64 } + + DWCache struct { + Host string // 缓存服务器地址 + IdleTimeout int + } // 抖音微信缓存配置 } diff --git a/game_open_api/internal/logic/douyin/douyinCode2tokenLogic.go b/game_open_api/internal/logic/douyin/douyinCode2tokenLogic.go index b9cef06..f462eea 100644 --- a/game_open_api/internal/logic/douyin/douyinCode2tokenLogic.go +++ b/game_open_api/internal/logic/douyin/douyinCode2tokenLogic.go @@ -2,7 +2,6 @@ package douyin import ( "context" - "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/app_api_helper" "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/model" "github.com/golang-jwt/jwt/v4" "time" @@ -20,7 +19,6 @@ type DouyinCode2tokenLogic struct { } func NewDouyinCode2tokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DouyinCode2tokenLogic { - app_api_helper.Init(ctx, svcCtx.AppAccount) return &DouyinCode2tokenLogic{ Logger: logx.WithContext(ctx), ctx: ctx, @@ -31,7 +29,7 @@ func NewDouyinCode2tokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) * func (l *DouyinCode2tokenLogic) DouyinCode2token(req *types.DouyinCode2TokenRequest) (resp *types.Auth, err error) { resp = new(types.Auth) - douyinCli, err := app_api_helper.DouyinCli.GetDouYinOpenApi(req.AppId) + douyinCli, err := l.svcCtx.DouyinCli.GetDouYinOpenApi(req.AppId) if err != nil { return } diff --git a/game_open_api/internal/logic/douyin/getEcpmLogic.go b/game_open_api/internal/logic/douyin/getEcpmLogic.go index 773738c..0fb4796 100644 --- a/game_open_api/internal/logic/douyin/getEcpmLogic.go +++ b/game_open_api/internal/logic/douyin/getEcpmLogic.go @@ -2,7 +2,6 @@ package douyin import ( "context" - "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/app_api_helper" "time" "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/svc" @@ -16,7 +15,6 @@ type GetEcpmLogic struct { } func NewGetEcpmLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetEcpmLogic { - app_api_helper.Init(ctx, svcCtx.AppAccount) return &GetEcpmLogic{ Logger: logx.WithContext(ctx), ctx: ctx, @@ -31,20 +29,22 @@ func (l *GetEcpmLogic) GetEcpm() (result bool, errNeverNil error) { errNeverNil = err return } + at.AppIdStr = "tt5bc32d7ec4f6ccb907" + at.OpenId = "_0005ITFnsqTvXpTC7wqg6yvQ1_mXhk1TbOv" - res, err := app_api_helper.DouyinCli.GetEcpmData(at.AppIdStr, at.OpenId, time.Now().Format(time.DateOnly)) + res, err := l.svcCtx.DouyinCli.GetEcpmData(at.AppIdStr, at.OpenId, time.Now().Format(time.DateOnly)) if err != nil { l.Logger.Error(err.Error()) return } - ecpm, err := app_api_helper.DouyinCli.GetEcpm(res) + ecpm, err := l.svcCtx.DouyinCli.GetEcpm(res) if err != nil { l.Logger.Error(err.Error()) return } - if ecpm > float64(app_api_helper.DouyinCli.GetEcpmValue(at.AppIdStr)) && len(res) > int(app_api_helper.DouyinCli.GetEcpmViewCount(at.AppIdStr)) { + if ecpm > float64(l.svcCtx.DouyinCli.GetEcpmValue(at.AppIdStr)) && len(res) > int(l.svcCtx.DouyinCli.GetEcpmViewCount(at.AppIdStr)) { result = true } else { result = false diff --git a/game_open_api/internal/logic/wechat/wechatCode2UserIdLogic.go b/game_open_api/internal/logic/wechat/wechatCode2UserIdLogic.go index 42e6a9c..af47eaf 100644 --- a/game_open_api/internal/logic/wechat/wechatCode2UserIdLogic.go +++ b/game_open_api/internal/logic/wechat/wechatCode2UserIdLogic.go @@ -3,7 +3,6 @@ package wechat import ( "context" "errors" - "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/app_api_helper" "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/model" "github.com/golang-jwt/jwt/v4" "time" @@ -21,7 +20,6 @@ type WechatCode2UserIdLogic struct { } func NewWechatCode2UserIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WechatCode2UserIdLogic { - app_api_helper.Init(ctx, svcCtx.AppAccount) return &WechatCode2UserIdLogic{ Logger: logx.WithContext(ctx), ctx: ctx, @@ -31,7 +29,7 @@ func NewWechatCode2UserIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) func (l *WechatCode2UserIdLogic) WechatCode2UserId(req *types.WechatCode2TokenRequest) (resp *types.Auth, err error) { resp = new(types.Auth) - wechatCli, err := app_api_helper.WechatCli.GetWechatOpenApi(req.AppId) + wechatCli, err := l.svcCtx.WechatCli.GetWechatOpenApi(req.AppId) if err != nil { return } diff --git a/game_open_api/internal/svc/serviceContext.go b/game_open_api/internal/svc/serviceContext.go index 31b4aa7..853959d 100644 --- a/game_open_api/internal/svc/serviceContext.go +++ b/game_open_api/internal/svc/serviceContext.go @@ -6,6 +6,8 @@ import ( "errors" "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/internal/config" "gitea.youtukeji.com.cn/xiabin/youtu_server/game_open_api/model" + helper "gitea.youtukeji.com.cn/youtu/openapi-helper" + redisCache "github.com/silenceper/wechat/v2/cache" "github.com/zeromicro/go-zero/core/stores/sqlx" ) @@ -14,15 +16,36 @@ type ServiceContext struct { AppUser model.AppUserModel GameScore model.GameScoreModel AppAccount model.AppAccountModel + DouyinCli *helper.DouYinOpenApiClient + WechatCli *helper.WechatApi } func NewServiceContext(c config.Config) *ServiceContext { - return &ServiceContext{ + svc := &ServiceContext{ Config: c, AppUser: model.NewAppUserModel(sqlx.NewMysql(c.DB.DataSource), c.Cache), GameScore: model.NewGameScoreModel(sqlx.NewMysql(c.DB.DataSource), c.Cache), AppAccount: model.NewAppAccountModel(sqlx.NewMysql(c.DB.DataSource), c.Cache), } + + dwCache := redisCache.NewRedis(context.Background(), &redisCache.RedisOpts{Host: c.DWCache.Host, IdleTimeout: c.DWCache.IdleTimeout}) + + svc.DouyinCli = helper.NewDouYinOpenApiClient() + svc.WechatCli = helper.NewWechatOpenApiClient() + result, err := svc.AppAccount.FindAll(context.Background()) + if err != nil { + panic(err) + } + + for _, v := range *result { + if v.Type == 0 { + svc.DouyinCli.NewAndStoreDouYinOpenApi(v.AppID, v.Secret, v.EcpmValue.V, v.EcpmView.V, dwCache) + } else { + svc.WechatCli.NewAndStoreWechatOpenApi(v.AppID, v.Secret, dwCache) + } + } + + return svc } type AccessToken struct { diff --git a/go.mod b/go.mod index c3a8ffb..9a032f5 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module gitea.youtukeji.com.cn/xiabin/youtu_server go 1.23.4 require ( - gitea.youtukeji.com.cn/xiabin/douyin-openapi v0.0.4 - gitea.youtukeji.com.cn/xiabin/youtu_ecpm v0.0.2 + gitea.youtukeji.com.cn/youtu/openapi-helper v0.0.2 github.com/golang-jwt/jwt/v4 v4.5.1 + github.com/silenceper/wechat/v2 v2.1.7 github.com/zeromicro/go-zero v1.7.6 ) @@ -24,9 +24,7 @@ require ( github.com/go-redis/redis/v8 v8.11.5 // indirect github.com/go-sql-driver/mysql v1.8.1 // indirect github.com/google/uuid v1.6.0 // indirect - github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1 // indirect - github.com/jinzhu/inflection v1.0.0 // indirect - github.com/jinzhu/now v1.1.5 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.0 // indirect github.com/klauspost/compress v1.17.11 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -39,7 +37,6 @@ require ( github.com/prometheus/common v0.62.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/redis/go-redis/v9 v9.7.0 // indirect - github.com/silenceper/wechat/v2 v2.1.7 // indirect github.com/sirupsen/logrus v1.9.3 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spf13/cast v1.7.1 // indirect @@ -68,5 +65,4 @@ require ( google.golang.org/grpc v1.69.4 // indirect google.golang.org/protobuf v1.36.3 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gorm.io/gorm v1.25.12 // indirect ) diff --git a/go.sum b/go.sum index 3adcc3b..690355c 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -gitea.youtukeji.com.cn/xiabin/douyin-openapi v0.0.4 h1:xQmWix8yK+OlCJQlpNl5RiLRwbh+kli8YCcStZfdWCU= -gitea.youtukeji.com.cn/xiabin/douyin-openapi v0.0.4/go.mod h1:7d5OkLrsgX/iI4E9nVi2hYC2vvLrDi/QKCuTF4S/k6g= -gitea.youtukeji.com.cn/xiabin/youtu_ecpm v0.0.2 h1:i9BbGtp5SZuZxtHqaMg+3SPjrNNSvkWx0xr64n4eruQ= -gitea.youtukeji.com.cn/xiabin/youtu_ecpm v0.0.2/go.mod h1:YkSYZfYADDMBQ5dunlhLueYKWBlb6sk2sWWXrJ7Nj6s= +gitea.youtukeji.com.cn/youtu/openapi-helper v0.0.2 h1:Rf6cjwTVJzPf5nB4Q6JCffMnBlxw6VykXVdMNSwznoY= +gitea.youtukeji.com.cn/youtu/openapi-helper v0.0.2/go.mod h1:o3XiYjUmxptrwcYPbTwNc2SQSOeOj7qbQPdNNVU0H5w= github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc= @@ -42,9 +40,8 @@ github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= -github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= -github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -77,16 +74,12 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1 h1:VNqngBF40hVlDloBruUehVYC3ArSgIyScOAyMRqBxRg= -github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1/go.mod h1:RBRO7fro65R6tjKzYgLAFo0t1QEXY1Dp+i/bvpRiqiQ= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.0 h1:VD1gqscl4nYs1YxVuSdemTrSgTKrwOWDK0FVFMqm+Cg= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.0/go.mod h1:4EgsQoS4TOhJizV+JTFg40qx1Ofh3XmXEQNBpgvNT40= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= -github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= -github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -284,7 +277,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= -gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 h1:pUdcCO1Lk/tbT5ztQWOBi5HBgbBP1J8+AsQnQCKsi8A= k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=