451 lines
12 KiB
Go
451 lines
12 KiB
Go
package cast
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"fonchain-fiee/api/accountFiee"
|
|
"fonchain-fiee/api/aryshare"
|
|
"fonchain-fiee/api/bundle"
|
|
"fonchain-fiee/api/cast"
|
|
"fonchain-fiee/cmd/config"
|
|
"fonchain-fiee/pkg/e"
|
|
modelCast "fonchain-fiee/pkg/model/cast"
|
|
"fonchain-fiee/pkg/service"
|
|
"fonchain-fiee/pkg/service/check"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func MediaUserList(ctx *gin.Context) {
|
|
var req *cast.MediaUserListReq
|
|
var err error
|
|
if err = ctx.ShouldBind(&req); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if err = req.Validate(); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
resp, err := service.CastProvider.MediaUserList(ctx, req)
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
service.Success(ctx, resp)
|
|
return
|
|
}
|
|
|
|
func UnbindManager(ctx *gin.Context) {
|
|
var req *cast.UnbindManagerReq
|
|
var resp *cast.UnbindManagerResp
|
|
var err error
|
|
if err = ctx.ShouldBind(&req); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if err = req.Validate(); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
resp, err = service.CastProvider.UnbindManager(ctx, req)
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
zap.L().Info("UnbindManager resp", zap.Any("resp", resp))
|
|
if resp.OldBindArtistUuid != "" {
|
|
userID, _ := strconv.ParseInt(resp.OldBindArtistUuid, 10, 64)
|
|
_, err = service.BundleProvider.AddBundleBalance(context.Background(), &bundle.AddBundleBalanceReq{
|
|
UserId: int32(userID),
|
|
AccountConsumptionNumber: -1,
|
|
})
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
}
|
|
service.Success(ctx, nil)
|
|
return
|
|
}
|
|
|
|
func BindManager(ctx *gin.Context) {
|
|
var req *cast.BindManagerReq
|
|
var err error
|
|
if err = ctx.ShouldBind(&req); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if err = req.Validate(); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
_, err = service.CastProvider.BindManager(ctx, req)
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
service.Success(ctx, nil)
|
|
return
|
|
}
|
|
|
|
func UpdateMediaAccount(ctx *gin.Context) {
|
|
var req *cast.UpdateMediaAccountReq
|
|
var infoResp *accountFiee.UserInfoResponse
|
|
var err error
|
|
if err = ctx.ShouldBind(&req); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if err = req.Validate(); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
// 查询艺人的信息
|
|
// 字符串转整型
|
|
artistID, err := strconv.ParseUint(req.ArtistUuid, 10, 64)
|
|
infoResp, err = GetArtistAccountInfo(artistID)
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if infoResp.SubNum == "" {
|
|
service.Error(ctx, errors.New("用户不存在"))
|
|
return
|
|
}
|
|
|
|
//TODO 判断是否注册ay
|
|
if err = CheckAsProfile(infoResp); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
req.ArtistName = infoResp.Name
|
|
req.ArtistPhone = infoResp.TelNum
|
|
req.ArtistPhoneAreaCode = infoResp.TelAreaCode
|
|
if _, ok := cast.PlatformIDENUM_name[int32(req.PlatformID)]; !ok {
|
|
service.Error(ctx, errors.New(e.GetMsg(e.InvalidParams)))
|
|
return
|
|
}
|
|
userID, _ := strconv.ParseInt(req.ArtistUuid, 10, 64)
|
|
if req.MediaAccountUuid == "" {
|
|
if err = CheckUserBundleBalance(int32(userID), modelCast.BalanceTypeAccountValue); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
_, err = service.BundleProvider.AddBundleBalance(context.Background(), &bundle.AddBundleBalanceReq{
|
|
UserId: int32(userID),
|
|
AccountConsumptionNumber: 1,
|
|
})
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
}
|
|
resp, err := service.CastProvider.UpdateMediaAccount(ctx, req)
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
if req.MediaAccountUuid == "" {
|
|
_, err = service.BundleProvider.AddBundleBalance(context.Background(), &bundle.AddBundleBalanceReq{
|
|
UserId: int32(userID),
|
|
AccountConsumptionNumber: -1,
|
|
})
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
service.Success(ctx, resp)
|
|
return
|
|
}
|
|
|
|
// 账号授权
|
|
func OAuthAccount(ctx *gin.Context) {
|
|
var req *cast.OAuthAccountV2Req
|
|
var resp *cast.OAuthAccountV2Resp
|
|
var err error
|
|
if err = ctx.ShouldBind(&req); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if err = req.Validate(); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if resp, err = service.CastProvider.OAuthAccountV2(ctx, req); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if cast.PlatformIDENUM_DM == cast.PlatformIDENUM(resp.PlatformID) {
|
|
service.Success(ctx, resp)
|
|
return
|
|
}
|
|
if resp.ProfileKey == "" {
|
|
service.Error(ctx, errors.New("艺人未添加平台账号"))
|
|
return
|
|
}
|
|
jwtReq := &aryshare.GenerateJWTRequest{
|
|
Domain: "",
|
|
PrivateKey: "",
|
|
ProfileKey: resp.ProfileKey,
|
|
Logout: false,
|
|
Redirect: fmt.Sprintf("%s?artistUuid=%s&platformID=%d", config.AppConfig.System.AuthCallback, resp.ArtistUuid, resp.PlatformID),
|
|
AllowedSocial: []string{modelCast.PlatformNameKv[resp.PlatformID]},
|
|
Verify: false,
|
|
Base64: false,
|
|
ExpiresIn: 0,
|
|
Email: nil,
|
|
}
|
|
jwtResp, err := service.AyrshareProvider.GenerateJWT(context.Background(), jwtReq)
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
service.Success(ctx, jwtResp)
|
|
return
|
|
}
|
|
|
|
func OAuth2Callback(ctx *gin.Context) {
|
|
var (
|
|
platformIds string
|
|
userID string
|
|
)
|
|
code := ctx.Query("code")
|
|
state := ctx.Query("state")
|
|
stateMM, _ := url.ParseQuery(state)
|
|
if len(stateMM["platform_id"]) > 0 {
|
|
platformIds = stateMM["platform_id"][0]
|
|
}
|
|
if len(stateMM["user_id"]) > 0 {
|
|
userID = stateMM["user_id"][0]
|
|
}
|
|
platformID, _ := strconv.ParseInt(platformIds, 10, 64)
|
|
req := &cast.OAuthCodeToTokenReq{
|
|
Code: code,
|
|
UserID: userID,
|
|
}
|
|
switch platformID {
|
|
case int64(cast.PlatformIDENUM_DM):
|
|
req.PlatformID = cast.PlatformIDENUM_DM
|
|
case int64(cast.PlatformIDENUM_TIKTOK):
|
|
req.PlatformID = cast.PlatformIDENUM_TIKTOK
|
|
default:
|
|
service.Error(ctx, errors.New(e.GetMsg(e.InvalidParams)))
|
|
return
|
|
}
|
|
zap.L().Info("OAuth2Callback", zap.Any("req", req), zap.Any("code", code), zap.Any("state", state))
|
|
_, err := service.CastProvider.OAuthCodeToToken(ctx, req)
|
|
if err != nil {
|
|
zap.L().Info("OAuth2Callback error", zap.Error(err))
|
|
|
|
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?%s", config.AppConfig.System.AuthRedirectUrl, "status=1"))
|
|
return
|
|
}
|
|
// TODO 跳转到前端页面
|
|
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?%s", config.AppConfig.System.AuthRedirectUrl, "status=0"))
|
|
//service.Success(ctx, map[string]interface{}{
|
|
// "req": req,
|
|
// "resp": resp,
|
|
//})
|
|
|
|
return
|
|
}
|
|
|
|
func AsOAuth2Callback(ctx *gin.Context) {
|
|
var (
|
|
//platformIds string
|
|
//userID string
|
|
)
|
|
artistUuid := ctx.Query("artistUuid")
|
|
//platformIDs := ctx.Query("platformID")
|
|
//platformID, _ := strconv.ParseInt(platformIDs, 10, 64)
|
|
// 刷新授权
|
|
var err error
|
|
var req modelCast.OAuthPlatformReq
|
|
if err = ctx.ShouldBind(&req); err != nil {
|
|
//service.Error(ctx, err)
|
|
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?status=%d&message=%s", config.AppConfig.System.AuthRedirectUrl, 1, "参数错误"))
|
|
return
|
|
}
|
|
resp, err := service.CastProvider.GetArtist(context.Background(), &cast.GetArtistReq{ArtistUuid: req.ArtistUuid})
|
|
if err != nil {
|
|
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?status=%d&message=%s", config.AppConfig.System.AuthRedirectUrl, 1, "获取艺人信息错误"))
|
|
return
|
|
}
|
|
if resp == nil || resp.ArtistInfo == nil || resp.ArtistInfo.ProfileKey == "" {
|
|
//service.Error(ctx, errors.New("艺人平台信息为空"))
|
|
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?status=%d&message=%s", config.AppConfig.System.AuthRedirectUrl, 1, "艺人未注册"))
|
|
return
|
|
}
|
|
userResp, err := service.AyrshareProvider.GetUser(context.Background(), &aryshare.GetUserRequest{
|
|
ProfileKey: resp.ArtistInfo.ProfileKey,
|
|
InstagramDetails: true,
|
|
})
|
|
if err != nil {
|
|
//service.Error(ctx, errors.New("获取艺人绑定信息错误"))
|
|
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?status=%d&message=%s", config.AppConfig.System.AuthRedirectUrl, 1, "获取艺人绑定信息错误"))
|
|
return
|
|
}
|
|
if len(userResp.DisplayNames) == 0 {
|
|
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?status=%d&message=%s", config.AppConfig.System.AuthRedirectUrl, 1, "没有授权信息"))
|
|
return
|
|
}
|
|
var authReq *cast.UpdateOAuthReq
|
|
authReq = &cast.UpdateOAuthReq{Data: make([]*cast.UpdateOAuthReq_Info, 0)}
|
|
var asInfoB []byte
|
|
for _, v := range userResp.DisplayNames {
|
|
asInfoB, _ = json.Marshal(v)
|
|
platformIDENUM := cast.PlatformIDENUM(modelCast.NamePlatformIDKv[v.Platform])
|
|
if platformIDENUM == cast.PlatformIDENUM_UNKNOWN {
|
|
continue
|
|
}
|
|
authReq.Data = append(authReq.Data, &cast.UpdateOAuthReq_Info{
|
|
ArtistUuid: artistUuid,
|
|
PlatformID: platformIDENUM,
|
|
AsID: v.Id,
|
|
PlatformUserName: v.Username,
|
|
AutInfo: string(asInfoB),
|
|
})
|
|
}
|
|
if len(authReq.Data) != 0 {
|
|
_, err = service.CastProvider.UpdateOAuth(context.Background(), authReq)
|
|
if err != nil {
|
|
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?status=%d&message=%s", config.AppConfig.System.AuthRedirectUrl, 1, "同步授权信息失败"))
|
|
return
|
|
}
|
|
}
|
|
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s?status=%d&message=%s", config.AppConfig.System.AuthRedirectUrl, 0, ""))
|
|
return
|
|
}
|
|
|
|
func RefreshToken(ctx *gin.Context) {
|
|
var req *cast.RefreshTokenReq
|
|
var resp *cast.RefreshTokenResp
|
|
var err error
|
|
if err = ctx.ShouldBind(&req); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if err = req.Validate(); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if resp, err = service.CastProvider.RefreshToken(ctx, req); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
service.Success(ctx, resp)
|
|
return
|
|
}
|
|
|
|
func ArtistInfo(ctx *gin.Context) {
|
|
var req *cast.ArtistInfoReq
|
|
var err error
|
|
if err = ctx.ShouldBind(&req); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
if err = req.Validate(); err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
resp, err := service.CastProvider.ArtistInfo(ctx, req)
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
service.Success(ctx, resp)
|
|
return
|
|
}
|
|
|
|
func Test(ctx *gin.Context) {
|
|
id := ctx.PostForm("id")
|
|
profileKey := ctx.PostForm("profileKey")
|
|
resp, err := service.AyrshareProvider.GetPost(context.Background(), &aryshare.GetPostRequest{
|
|
Id: id,
|
|
ProfileKey: profileKey,
|
|
})
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}
|
|
|
|
/*profileKey := ctx.PostForm("profileKey")
|
|
resp, err := service.AyrshareProvider.GetUser(context.Background(), &aryshare.GetUserRequest{
|
|
ProfileKey: profileKey,
|
|
InstagramDetails: true,
|
|
})
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}*/
|
|
//profileKey := ctx.PostForm("profileKey")
|
|
/*resp, err := service.AyrshareProvider.GetProfiles(context.Background(), &aryshare.GetProfilesRequest{
|
|
Title: "",
|
|
RefId: "",
|
|
HasActiveSocialAccounts: false,
|
|
IncludesActiveSocialAccounts: nil,
|
|
ActionLog: nil,
|
|
Limit: 0,
|
|
Cursor: "",
|
|
})
|
|
if err != nil {
|
|
service.Error(ctx, err)
|
|
return
|
|
}*/
|
|
service.Success(ctx, resp)
|
|
return
|
|
}
|
|
|
|
func CheckAsProfile(infoResp *accountFiee.UserInfoResponse) error {
|
|
var asArtistResp *cast.GetArtistResp
|
|
var err error
|
|
title := fmt.Sprintf("%s_%s", config.AppConfig.System.AppMode, infoResp.SubNum)
|
|
// 查询艺人的信息
|
|
if asArtistResp, err = service.CastProvider.GetArtist(context.Background(), &cast.GetArtistReq{
|
|
ArtistUuid: fmt.Sprint(infoResp.Id),
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
if asArtistResp != nil && asArtistResp.ArtistInfo != nil && asArtistResp.ArtistInfo.ProfileKey != "" {
|
|
return nil
|
|
}
|
|
createProfileResp, err := service.AyrshareProvider.CreateProfile(context.Background(), &aryshare.CreateProfileRequest{
|
|
Title: title,
|
|
MessagingActive: true,
|
|
HideTopHeader: false,
|
|
TopHeader: "",
|
|
DisableSocial: nil,
|
|
Team: false,
|
|
Email: "",
|
|
SubHeader: "",
|
|
Tags: []string{config.AppConfig.System.AppMode},
|
|
})
|
|
if err != nil {
|
|
zap.L().Error("CreateProfile error", zap.Error(err))
|
|
err = errors.New("创建平台艺人账号失败")
|
|
return err
|
|
}
|
|
zap.L().Info("CreateProfile success", zap.Any("createProfileResp", createProfileResp), zap.Any("title", title))
|
|
_, err = service.CastProvider.UpdateArtist(context.Background(), &cast.UpdateArtistReq{
|
|
Uuid: asArtistResp.Uuid,
|
|
ArtistInfo: &cast.ArtistInfo{
|
|
ArtistUuid: fmt.Sprint(infoResp.Id),
|
|
RefID: createProfileResp.RefId,
|
|
ProfileKey: createProfileResp.ProfileKey,
|
|
SubNum: infoResp.SubNum,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|