2025-01-14 13:09:25 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-01-18 17:56:36 +08:00
|
|
|
"errors"
|
2025-01-14 13:09:25 +08:00
|
|
|
"gitea.youtukeji.com.cn/xiabin/douyin-openapi/cache"
|
2025-01-20 00:11:11 +08:00
|
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_ecpm/api/gin/service"
|
|
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_ecpm/dao/model"
|
|
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_ecpm/dao/query"
|
|
|
|
"gitea.youtukeji.com.cn/xiabin/youtu_ecpm/pkg/douyinapi"
|
2025-01-14 13:09:25 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DouyinOpenApiController struct {
|
2025-01-18 17:56:36 +08:00
|
|
|
*Controller
|
|
|
|
logger *zap.Logger
|
|
|
|
douyinCli *douyinapi.DouYinOpenApiClient
|
|
|
|
appUser *service.AppUserInfo
|
|
|
|
appAccount *service.AppAccount
|
2025-01-14 13:09:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDouyinOpenApiController 实例化控制器
|
|
|
|
// logger: 日志
|
|
|
|
// q: 数据库
|
|
|
|
// 将数据库中的数据存储到内存中
|
|
|
|
func NewDouyinOpenApiController(logger *zap.Logger, q *query.Query) *DouyinOpenApiController {
|
|
|
|
// 创建抖音客户端
|
|
|
|
douyinCli := douyinapi.NewDouYinOpenApiClient()
|
|
|
|
// 获取数据库中的数据
|
2025-01-18 17:56:36 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var result []struct {
|
|
|
|
AppID string `gorm:"column:app_id;type:char(20);not null;index:app_id,priority:1" json:"app_id"`
|
|
|
|
Secret string `gorm:"column:secret;type:char(40);not null" json:"secret"`
|
|
|
|
EcpmValue uint32 `gorm:"column:ecpm_value;type:int unsigned;not null;comment:值" json:"ecpm_value"` // 值
|
|
|
|
EcpmView uint32 `gorm:"column:ecpm_view;type:int unsigned;not null;comment:浏览次数" json:"ecpm_view"` // 浏览次数
|
|
|
|
}
|
|
|
|
|
|
|
|
account := q.AppAccount
|
|
|
|
cfg := q.DouyinEcpmConfig
|
|
|
|
|
|
|
|
//获取抖音配置
|
|
|
|
err := account.WithContext(ctx).
|
|
|
|
Select(
|
|
|
|
account.AppID,
|
|
|
|
account.Secret,
|
|
|
|
cfg.EcpmValue,
|
|
|
|
cfg.EcpmView,
|
|
|
|
).Where(account.Type.Eq(0)).
|
|
|
|
LeftJoin(cfg, cfg.AppAccountID.EqCol(account.ID)).
|
|
|
|
Scan(&result)
|
|
|
|
|
2025-01-14 13:09:25 +08:00
|
|
|
if err != nil {
|
|
|
|
logger.Sugar().Error("获取数据失败", err)
|
|
|
|
}
|
|
|
|
// 将数据库中的数据存储到内存中
|
2025-01-18 17:56:36 +08:00
|
|
|
for _, v := range result {
|
2025-01-15 10:11:16 +08:00
|
|
|
douyinCli.NewAndStoreDouYinOpenApi(v.AppID, v.Secret, v.EcpmValue, v.EcpmView, cache.NewMemory())
|
2025-01-14 13:09:25 +08:00
|
|
|
}
|
|
|
|
return &DouyinOpenApiController{
|
2025-01-18 17:56:36 +08:00
|
|
|
logger: logger,
|
|
|
|
douyinCli: douyinCli,
|
|
|
|
appUser: service.NewAppUserInfo(q),
|
|
|
|
appAccount: service.NewAppAccount(q),
|
2025-01-14 13:09:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEcpm 获取ECPM,返回true或false
|
|
|
|
func (ctl *DouyinOpenApiController) GetEcpm(c *gin.Context) {
|
|
|
|
var req struct {
|
|
|
|
AppId string `json:"app_id" form:"app_id"`
|
2025-01-18 19:02:58 +08:00
|
|
|
UserId uint64 `json:"user_id" form:"user_id"`
|
2025-01-14 13:09:25 +08:00
|
|
|
}
|
|
|
|
if err := c.ShouldBind(&req); err != nil {
|
|
|
|
c.JSON(http.StatusOK, false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-18 19:02:58 +08:00
|
|
|
userInfo, err := ctl.appUser.GetUserById(c, req.UserId)
|
|
|
|
if err != nil {
|
|
|
|
ctl.logger.Error(err.Error())
|
|
|
|
c.JSON(http.StatusOK, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
if userInfo == nil {
|
|
|
|
c.JSON(http.StatusOK, false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := ctl.douyinCli.GetEcpmData(req.AppId, userInfo.Openid, time.Now().Format(time.DateOnly))
|
2025-01-14 13:09:25 +08:00
|
|
|
if err != nil {
|
|
|
|
ctl.logger.Sugar().Error("获取ecpm失败", err)
|
|
|
|
c.JSON(http.StatusOK, false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ecpm, err := ctl.douyinCli.GetEcpm(res)
|
|
|
|
if err != nil {
|
|
|
|
ctl.logger.Sugar().Error("计算ecpm失败", err)
|
|
|
|
c.JSON(http.StatusOK, false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 打印日志
|
|
|
|
ctl.logger.Sugar().Info("ECPM:", zap.Float64("ecpm", ecpm))
|
2025-01-15 10:11:16 +08:00
|
|
|
// 判断是否满足ECPM值和浏览数
|
2025-01-14 13:09:25 +08:00
|
|
|
var result bool
|
2025-01-15 10:11:16 +08:00
|
|
|
if ecpm > float64(ctl.douyinCli.GetEcpmValue(req.AppId)) && len(res) > int(ctl.douyinCli.GetEcpmViewCount(req.AppId)) {
|
2025-01-14 13:09:25 +08:00
|
|
|
result = true
|
|
|
|
} else {
|
|
|
|
result = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回结果
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Code2OpenId 获取openId
|
|
|
|
func (ctl *DouyinOpenApiController) Code2OpenId(c *gin.Context) {
|
|
|
|
code := c.Query("code")
|
|
|
|
anonymousOpenid := c.Query("anonymous_openid")
|
2025-01-15 10:31:24 +08:00
|
|
|
appId := c.Query("app_id")
|
2025-01-14 13:09:25 +08:00
|
|
|
|
2025-01-15 10:31:24 +08:00
|
|
|
douyinCli, err := ctl.douyinCli.GetDouYinOpenApi(appId)
|
2025-01-14 13:09:25 +08:00
|
|
|
if err != nil {
|
|
|
|
ctl.logger.Sugar().Error("获取小程序登录地址失败", err)
|
2025-01-18 17:56:36 +08:00
|
|
|
ctl.ResponseErr(c, err)
|
2025-01-14 13:09:25 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := douyinCli.Api.Code2Session(code, anonymousOpenid)
|
|
|
|
if err != nil {
|
|
|
|
ctl.logger.Sugar().Error("获取小程序登录地址失败", err)
|
2025-01-18 17:56:36 +08:00
|
|
|
ctl.ResponseErr(c, err)
|
2025-01-14 13:09:25 +08:00
|
|
|
return
|
|
|
|
}
|
2025-01-15 10:31:24 +08:00
|
|
|
|
|
|
|
if res.Errcode != 0 {
|
|
|
|
ctl.logger.Sugar().Errorf("Code2Session 错误,res : %+v", res)
|
2025-01-18 17:56:36 +08:00
|
|
|
ctl.ResponseErr(c, errors.New(res.Errmsg))
|
2025-01-15 10:31:24 +08:00
|
|
|
return
|
|
|
|
}
|
2025-01-18 17:56:36 +08:00
|
|
|
|
|
|
|
accountId, err := ctl.appAccount.GetAppAccountIdByAppId(c, appId)
|
|
|
|
if err != nil {
|
|
|
|
ctl.ResponseErr(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-18 19:02:58 +08:00
|
|
|
aui := &model.AppUserInfo{
|
2025-01-18 17:56:36 +08:00
|
|
|
AppAccountID: accountId,
|
|
|
|
Openid: res.Openid,
|
|
|
|
Unionid: res.Unionid,
|
|
|
|
AnonymousOpenid: res.AnonymousOpenid,
|
2025-01-18 19:02:58 +08:00
|
|
|
}
|
|
|
|
err = ctl.appUser.SaveUserInfoByRes(c, aui)
|
2025-01-18 17:56:36 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctl.ResponseErr(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-01-18 19:02:58 +08:00
|
|
|
c.JSON(http.StatusOK, gin.H{"userId": aui.ID})
|
2025-01-14 13:09:25 +08:00
|
|
|
}
|