接口参数与返回值调整

This commit is contained in:
xiabin 2025-01-18 19:02:58 +08:00
parent 465ed0fe59
commit eac96374bd
5 changed files with 39 additions and 16 deletions

View File

@ -23,8 +23,8 @@ func NewAppUserInfoController(logger *zap.Logger, q *query.Query) *AppUserInfo {
func (ctl *AppUserInfo) SetAppAccount(c *gin.Context) { func (ctl *AppUserInfo) SetAppAccount(c *gin.Context) {
var req struct { var req struct {
ID uint64 `gorm:"column:id;type:bigint unsigned;primaryKey;autoIncrement:true" json:"id" binding:"required"` ID uint64 `gorm:"column:id;type:bigint unsigned;primaryKey;autoIncrement:true" json:"id" binding:"required"`
Nickname string `gorm:"column:nickname;type:varchar(255);not null;comment:昵称" json:"nickname" binding:"required"` // 昵称 Nickname string `gorm:"column:nickname;type:varchar(255);not null;comment:昵称" json:"nickname" binding:"required"` // 昵称
ImageURL string `gorm:"column:image_url;type:varchar(255);not null;comment:头像" json:"image_url" binding:"required"` // 头像 Avatar string `gorm:"column:avatar;type:varchar(255);not null;comment:头像" json:"avatar" binding:"required"` // 头像
} }
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
@ -33,7 +33,7 @@ func (ctl *AppUserInfo) SetAppAccount(c *gin.Context) {
return return
} }
if err := ctl.appUser.UpdateUserNickNameAndImageUrl(c, req.ID, req.Nickname, req.ImageURL); err != nil { if err := ctl.appUser.UpdateUserNickNameAndImageUrl(c, req.ID, req.Nickname, req.Avatar); err != nil {
ctl.logger.Sugar().Error(err) ctl.logger.Sugar().Error(err)
ctl.ResponseErr(c, err) ctl.ResponseErr(c, err)
return return

View File

@ -72,14 +72,25 @@ func NewDouyinOpenApiController(logger *zap.Logger, q *query.Query) *DouyinOpenA
func (ctl *DouyinOpenApiController) GetEcpm(c *gin.Context) { func (ctl *DouyinOpenApiController) GetEcpm(c *gin.Context) {
var req struct { var req struct {
AppId string `json:"app_id" form:"app_id"` AppId string `json:"app_id" form:"app_id"`
OpenId string `json:"open_id" form:"open_id"` UserId uint64 `json:"user_id" form:"user_id"`
} }
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
c.JSON(http.StatusOK, false) c.JSON(http.StatusOK, false)
return return
} }
res, err := ctl.douyinCli.GetEcpmData(req.AppId, req.OpenId, time.Now().Format(time.DateOnly)) 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))
if err != nil { if err != nil {
ctl.logger.Sugar().Error("获取ecpm失败", err) ctl.logger.Sugar().Error("获取ecpm失败", err)
c.JSON(http.StatusOK, false) c.JSON(http.StatusOK, false)
@ -139,17 +150,18 @@ func (ctl *DouyinOpenApiController) Code2OpenId(c *gin.Context) {
return return
} }
err = ctl.appUser.SaveUserInfoByRes(c, &model.AppUserInfo{ aui := &model.AppUserInfo{
AppAccountID: accountId, AppAccountID: accountId,
Openid: res.Openid, Openid: res.Openid,
Unionid: res.Unionid, Unionid: res.Unionid,
AnonymousOpenid: res.AnonymousOpenid, AnonymousOpenid: res.AnonymousOpenid,
}) }
err = ctl.appUser.SaveUserInfoByRes(c, aui)
if err != nil { if err != nil {
ctl.ResponseErr(c, err) ctl.ResponseErr(c, err)
return return
} }
c.String(http.StatusOK, res.Openid) c.JSON(http.StatusOK, gin.H{"userId": aui.ID})
} }

View File

@ -89,16 +89,19 @@ func (ctl *WechatOpenApiController) Code2OpenId(c *gin.Context) {
return return
} }
err = ctl.appUser.SaveUserInfoByRes(c, &model.AppUserInfo{ aui := &model.AppUserInfo{
AppAccountID: accountId, AppAccountID: accountId,
Openid: res.OpenID, Openid: res.OpenID,
Unionid: res.UnionID, Unionid: res.UnionID,
}) }
err = ctl.appUser.SaveUserInfoByRes(c, aui)
if err != nil { if err != nil {
ctl.ResponseErr(c, err) ctl.ResponseErr(c, err)
return return
} }
c.String(http.StatusOK, res.OpenID) c.JSON(http.StatusOK, gin.H{
"user_id": aui.ID,
})
} }

View File

@ -58,12 +58,15 @@ func (s *HttpServer) Run() {
} }
func InitRouter(r *gin.Engine, logger *zap.Logger, q *query.Query) { func InitRouter(r *gin.Engine, logger *zap.Logger, q *query.Query) {
douyinCtl := controller.NewDouyinOpenApiController(logger, q) {
r.GET("/get-ecpm", douyinCtl.GetEcpm) douyinCtl := controller.NewDouyinOpenApiController(logger, q)
r.GET("/code2openId", douyinCtl.Code2OpenId) g := r.Group("/douyin")
g.GET("/get_ecpm", douyinCtl.GetEcpm)
g.GET("/code2userId", douyinCtl.Code2OpenId)
}
appUserCtl := controller.NewAppUserInfoController(logger, q) appUserCtl := controller.NewAppUserInfoController(logger, q)
r.POST("/app_user_info/SetAppAccount", appUserCtl.SetAppAccount) r.POST("/app_user_info/set_app_account", appUserCtl.SetAppAccount)
{ {
rankingCtl := controller.NewRankingList(logger, q) rankingCtl := controller.NewRankingList(logger, q)
@ -75,7 +78,7 @@ func InitRouter(r *gin.Engine, logger *zap.Logger, q *query.Query) {
{ {
wechatCtl := controller.NewWechatOpenApiController(logger, q) wechatCtl := controller.NewWechatOpenApiController(logger, q)
g := r.Group("/wechat") g := r.Group("/wechat")
g.GET("/code2openId", wechatCtl.Code2OpenId) g.GET("/code2userId", wechatCtl.Code2OpenId)
} }
} }

View File

@ -3,6 +3,7 @@ package service
import ( import (
"context" "context"
"errors" "errors"
"github.com/gin-gonic/gin"
"gorm.io/gorm" "gorm.io/gorm"
"youtu_ecpm/dao/model" "youtu_ecpm/dao/model"
"youtu_ecpm/dao/query" "youtu_ecpm/dao/query"
@ -34,3 +35,7 @@ func (a *AppUserInfo) UpdateUserNickNameAndImageUrl(ctx context.Context, id uint
_, err = a.q.AppUserInfo.WithContext(ctx).Where(a.q.AppUserInfo.ID.Eq(id)).Updates(&model.AppUserInfo{ID: id, Nickname: nickname, Avatar: url}) _, err = a.q.AppUserInfo.WithContext(ctx).Where(a.q.AppUserInfo.ID.Eq(id)).Updates(&model.AppUserInfo{ID: id, Nickname: nickname, Avatar: url})
return return
} }
func (a *AppUserInfo) GetUserById(c *gin.Context, id uint64) (*model.AppUserInfo, error) {
return a.q.AppUserInfo.WithContext(c).Where(a.q.AppUserInfo.ID.Eq(id)).Take()
}