diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3bc363e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +#编译阶段镜像 +FROM golang:alpine AS builder + +WORKDIR /app + +COPY . . + +RUN go env -w GOPRIVATE="gitea.youtukeji.com.cn" +RUN go env -w GOPROXY="https://goproxy.cn,direct" +RUN go mod tidy +RUN go build -o /app/ecpm cmd/main.go + +#运行阶段镜像 +FROM alpine AS runtime + +WORKDIR /app + +COPY --from=builder /app/ecpm /app/ecpm + +ENV TZ=Asia/Shanghai +ENV APP_ENV=prod +ENV LOG_PATH=/app/data/logs/ecpm.log + +ENTRYPOINT ["/app/ecpm"] + +LABEL authors="xiabin" diff --git a/api/gin/controller/douyin_open_api.go b/api/gin/controller/douyin_open_api.go index 37fc5fa..4b33c3f 100644 --- a/api/gin/controller/douyin_open_api.go +++ b/api/gin/controller/douyin_open_api.go @@ -82,9 +82,9 @@ func (ctl *DouyinOpenApiController) GetEcpm(c *gin.Context) { func (ctl *DouyinOpenApiController) Code2OpenId(c *gin.Context) { code := c.Query("code") anonymousOpenid := c.Query("anonymous_openid") - mpid := c.Query("mpid") + appId := c.Query("app_id") - douyinCli, err := ctl.douyinCli.GetDouYinOpenApi(mpid) + douyinCli, err := ctl.douyinCli.GetDouYinOpenApi(appId) if err != nil { ctl.logger.Sugar().Error("获取小程序登录地址失败", err) c.JSON(200, gin.H{ @@ -103,5 +103,14 @@ func (ctl *DouyinOpenApiController) Code2OpenId(c *gin.Context) { }) return } - c.String(http.StatusOK, res.Data.Openid) + + if res.Errcode != 0 { + ctl.logger.Sugar().Errorf("Code2Session 错误,res : %+v", res) + c.JSON(200, gin.H{ + "code": 500, + "msg": http.StatusOK, + }) + return + } + c.String(http.StatusOK, res.Openid) } diff --git a/go.mod b/go.mod index 85a4c29..dc1514c 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23.4 replace gitea.youtukeji.com.cn/xiabin/douyin-openapi => ../douyin-openapi require ( - gitea.youtukeji.com.cn/xiabin/douyin-openapi v0.0.1 + gitea.youtukeji.com.cn/xiabin/douyin-openapi v0.0.4 github.com/gin-contrib/zap v1.1.4 github.com/gin-gonic/gin v1.10.0 github.com/google/wire v0.6.0 diff --git a/go.sum b/go.sum index 3763430..5b3a699 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +gitea.youtukeji.com.cn/xiabin/douyin-openapi v0.0.3 h1:aDHEBU2s8Yb6HknPCpEjTBVHtdUXw8ZaROmY3fXvr6A= +gitea.youtukeji.com.cn/xiabin/douyin-openapi v0.0.3/go.mod h1:7d5OkLrsgX/iI4E9nVi2hYC2vvLrDi/QKCuTF4S/k6g= github.com/bytedance/sonic v1.12.1 h1:jWl5Qz1fy7X1ioY74WqO0KjAMtAGQs4sYnjiEBiyX24= github.com/bytedance/sonic v1.12.1/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= diff --git a/pkg/douyinapi/client.go b/pkg/douyinapi/client.go index a110f67..b1de8e0 100644 --- a/pkg/douyinapi/client.go +++ b/pkg/douyinapi/client.go @@ -15,10 +15,9 @@ type DouYinOpenApiClient struct { } type DouYinApi struct { - Api *douyinopenapi.DouYinOpenApi // 抖音openapi客户端 - Token *access_token.DefaultAccessToken // 抖音token管理 - ecpmValue uint32 // ECPM值 - viewCount uint32 //浏览数 + Api *douyinopenapi.DouYinOpenApi // 抖音openapi客户端 + ecpmValue uint32 // ECPM值 + viewCount uint32 //浏览数 } func NewDouYinOpenApiClient() *DouYinOpenApiClient { @@ -57,11 +56,16 @@ func (d *DouYinOpenApiClient) SetDouYinOpenApi(appId string, api *DouYinApi) { // appSecret: 小程序secret // cache: 缓存 func (d *DouYinOpenApiClient) NewAndStoreDouYinOpenApi(appId, appSecret string, ecpmValue uint32, viewCount uint32, cache cache.Cache) { - api := douyinopenapi.NewDouYinOpenApi(douyinopenapi.DouYinOpenApiConfig{}) token := access_token.NewDefaultAccessToken(appId, appSecret, cache, false) + api := douyinopenapi.NewDouYinOpenApi(douyinopenapi.DouYinOpenApiConfig{ + AppId: appId, + AppSecret: appSecret, + AccessToken: token, + Cache: cache, + IsSandbox: false, + }) d.SetDouYinOpenApi(appId, &DouYinApi{ Api: api, - Token: token, ecpmValue: ecpmValue, viewCount: viewCount, }) @@ -98,7 +102,7 @@ func (d *DouYinOpenApiClient) GetEcpmData(appId, openId, dateHour string) (list } //获取accessToken - accessToken, err := douyin.Token.GetAccessToken() + accessToken, err := douyin.Api.GetAccessToken() if err != nil { return }