You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
869 lines
24 KiB
869 lines
24 KiB
9 months ago
|
package service
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"net/url"
|
||
|
"pure/global"
|
||
|
"pure/initialize/api"
|
||
|
"pure/model"
|
||
|
"pure/model/request"
|
||
|
"pure/model/response"
|
||
|
"pure/utils"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
|
|
||
|
uuid "github.com/satori/go.uuid"
|
||
|
"golang.org/x/oauth2"
|
||
|
"golang.org/x/oauth2/google"
|
||
|
"golang.org/x/oauth2/instagram"
|
||
|
)
|
||
|
|
||
|
func UserLogin(u *request.UserLogin) (err error, userInter *model.User) {
|
||
|
var user model.User
|
||
|
switch u.Type {
|
||
|
case "1":
|
||
|
if global.MG_CONFIG.System.Env != "prod" && u.Code == "888888" {
|
||
|
|
||
|
} else {
|
||
|
resp, err := global.SMS_CLIENT.VerifyCode(context.Background(), &api.SmsCodeVerifyRequest{
|
||
|
Phone: u.Phone,
|
||
|
Code: u.Code,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err, nil
|
||
|
}
|
||
|
if resp.Code != 0 {
|
||
|
return errors.New("验证码验证失败"), nil
|
||
|
}
|
||
|
}
|
||
|
err = global.MG_DB.Where("phone = ? and appid=? and type=? and log_off_time is null", u.Phone, u.Appid, "influencer").Find(&user).Error
|
||
|
if err != nil {
|
||
|
return errors.New("获取用户失败"), nil
|
||
|
}
|
||
|
case "2":
|
||
|
code := RedisGet("email_code:" + u.Email)
|
||
|
if global.MG_CONFIG.System.Env != "prod" && u.Code == "888888" {
|
||
|
|
||
|
} else if code != u.Code {
|
||
|
return errors.New("验证码验证失败"), nil
|
||
|
}
|
||
|
err = global.MG_DB.Where("email = ? and appid=? and type=? and log_off_time is null", u.Email, u.Appid, "influencer").Find(&user).Error
|
||
|
if err != nil {
|
||
|
return errors.New("获取用户失败"), nil
|
||
|
}
|
||
|
}
|
||
|
if user.UUID == uuid.Nil {
|
||
|
return errors.New("用户名或密码错误"), nil
|
||
|
}
|
||
|
return err, &user
|
||
|
}
|
||
|
|
||
|
func UserRegister(u *request.UserRegister) (err error, userInter *model.User) {
|
||
|
var user model.User
|
||
|
global.MG_DB.Where("email = ? and appid=? and type=? and log_off_time is null", u.Email, u.Appid, "influencer").Find(&user)
|
||
|
if user.UUID == uuid.Nil {
|
||
|
//用户注册
|
||
|
user.UUID = uuid.NewV4()
|
||
|
user.NickName = u.Nickname
|
||
|
user.Email = u.Email
|
||
|
user.Avatar = u.Avatar
|
||
|
user.Appid = u.Appid
|
||
|
user.Type = "influencer"
|
||
|
user.CountryCode = u.CountryCode
|
||
|
err = global.MG_DB.Model(&model.User{}).Create(&user).Error
|
||
|
if err != nil {
|
||
|
return errors.New("注册失败,请稍后再试"), nil
|
||
|
}
|
||
|
var wallet model.Wallet
|
||
|
wallet.UserID = user.UUID.String()
|
||
|
wallet.Platform = "influencer"
|
||
|
wallet.State = 0
|
||
|
global.MG_DB.Model(&model.Wallet{}).Create(&wallet)
|
||
|
} else {
|
||
|
return errors.New("用户已存在"), nil
|
||
|
}
|
||
|
return err, &user
|
||
|
}
|
||
|
|
||
|
func UserBandPhone(u *request.UserBandPhone, appid, userID string) (err error) {
|
||
|
if global.MG_CONFIG.System.Env != "prod" && u.Code == "888888" {
|
||
|
|
||
|
} else {
|
||
|
resp, err := global.SMS_CLIENT.VerifyCode(context.Background(), &api.SmsCodeVerifyRequest{
|
||
|
Phone: u.Phone,
|
||
|
Code: u.Code,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if resp.Code != 0 {
|
||
|
return errors.New("验证码验证失败")
|
||
|
}
|
||
|
}
|
||
|
var user model.User
|
||
|
err = global.MG_DB.Where("phone = ? and appid=? and type=?", u.Phone, appid, "influencer").Find(&user).Error
|
||
|
if err != nil {
|
||
|
return errors.New("获取用户失败")
|
||
|
}
|
||
|
if user.UUID != uuid.Nil {
|
||
|
return errors.New("手机号已被绑定")
|
||
|
}
|
||
|
err = global.MG_DB.Model(&model.User{}).Where("uuid=?", userID).Update("phone", u.Phone).Error
|
||
|
if err != nil {
|
||
|
return errors.New("绑定失败")
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func UserPlatformAuth(u *request.UserPlatformAuth, userID string) (err error) {
|
||
|
var (
|
||
|
platformJson string
|
||
|
uMap = make(map[string]interface{})
|
||
|
user model.UserSimple
|
||
|
platforms []request.Platform
|
||
|
dictData []model.SysDictData
|
||
|
)
|
||
|
err = global.MG_DB.Model(&model.User{}).Where("uuid=?", userID).Find(&user).Error
|
||
|
if err != nil {
|
||
|
return errors.New("获取用户失败")
|
||
|
}
|
||
|
err = global.MG_DB.Model(&model.SysDictData{}).Where("type_code=?", "release_channel").Find(&dictData).Error
|
||
|
if err != nil {
|
||
|
return errors.New("获取用户失败")
|
||
|
}
|
||
|
err = json.Unmarshal([]byte(user.Platform), &platforms)
|
||
|
t := time.Now()
|
||
|
u.Platform.CreatedAt = &t
|
||
|
u.Platform.IsAuth = false
|
||
|
if err != nil {
|
||
|
for i := 0; i < len(dictData); i++ {
|
||
|
platforms = append(platforms, request.Platform{
|
||
|
Platform: dictData[i].Value,
|
||
|
})
|
||
|
}
|
||
|
for i := 0; i < len(platforms); i++ {
|
||
|
if platforms[i].Platform == u.Platform.Platform {
|
||
|
platforms[i] = u.Platform
|
||
|
platforms[i].CreatedAt = &t
|
||
|
}
|
||
|
}
|
||
|
// platforms = append(platforms, u.Platform)
|
||
|
} else {
|
||
|
for i := 0; i < len(platforms); i++ {
|
||
|
if platforms[i].Platform == u.Platform.Platform && !platforms[i].IsAuth {
|
||
|
platforms[i] = u.Platform
|
||
|
platforms[i].CreatedAt = &t
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
platformByte, err := json.Marshal(platforms)
|
||
|
if err != nil {
|
||
|
return errors.New("平台认证格式不正确")
|
||
|
}
|
||
|
platformJson = string(platformByte)
|
||
|
uMap["platform"] = platformJson
|
||
|
uMap["tags"] = u.Tags
|
||
|
tx := global.MG_DB.Begin()
|
||
|
err = tx.Model(&model.User{}).Where("uuid=?", userID).Updates(uMap).Error
|
||
|
if err != nil {
|
||
|
return errors.New("提交失败")
|
||
|
}
|
||
|
var platformAuth model.PlatformAuth
|
||
|
platformAuth.UserID = userID
|
||
|
platformAuth.Platform = u.Platform
|
||
|
platformAuth.Status = "0"
|
||
|
err = tx.Model(&model.PlatformAuth{}).Create(&platformAuth).Error
|
||
|
if err != nil {
|
||
|
tx.Rollback()
|
||
|
return errors.New("提交失败")
|
||
|
}
|
||
|
tx.Commit()
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func UserAuthorized(u *request.UserAuthorized) (Iuser model.User, err error) {
|
||
|
var (
|
||
|
aceessToken string
|
||
|
userID string
|
||
|
provider model.Provider
|
||
|
user model.ProviderUser
|
||
|
providerList model.ClentProvider
|
||
|
application model.Application
|
||
|
)
|
||
|
err = global.MG_DB.Where("appid=?", u.Appid).Find(&application).Error
|
||
|
if err != nil {
|
||
|
return Iuser, errors.New("获取应用失败")
|
||
|
}
|
||
|
if application.ID == 0 {
|
||
|
return Iuser, errors.New("应用不存在")
|
||
|
}
|
||
|
err = json.Unmarshal([]byte(application.Provider), &providerList)
|
||
|
if err != nil {
|
||
|
return Iuser, errors.New("获取应用失败")
|
||
|
}
|
||
|
switch u.Client {
|
||
|
case "android":
|
||
|
for _, v := range providerList.Android {
|
||
|
if v.Code == u.Platform {
|
||
|
provider = v
|
||
|
}
|
||
|
}
|
||
|
case "ios":
|
||
|
for _, v := range providerList.Ios {
|
||
|
if v.Code == u.Platform {
|
||
|
provider = v
|
||
|
}
|
||
|
}
|
||
|
default:
|
||
|
for _, v := range providerList.Web {
|
||
|
if v.Code == u.Platform {
|
||
|
provider = v
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if provider.Code == "" {
|
||
|
return Iuser, errors.New("平台不存在")
|
||
|
}
|
||
|
if u.Uuid == "" {
|
||
|
if u.Code != "" {
|
||
|
//换取token
|
||
|
err, aceessToken, userID = GetAccessToken(provider, u.Code)
|
||
|
if err != nil {
|
||
|
fmt.Println(err.Error())
|
||
|
return Iuser, errors.New("获取token失败")
|
||
|
}
|
||
|
}
|
||
|
if u.Token != "" {
|
||
|
aceessToken = u.Token
|
||
|
}
|
||
|
//换取用户信息
|
||
|
if provider.UserInfoURI != "" {
|
||
|
err, user = GetUserInfo(provider, aceessToken, userID)
|
||
|
if err != nil {
|
||
|
return Iuser, errors.New("获取用户信息失败")
|
||
|
}
|
||
|
if user.Avatar == "" {
|
||
|
user.Avatar = "https://minio.sumweal.com/nft/221109/C82M980ZLumQCzt857yxur92iAsGdCc7271sDn8MPf666sk44V.png"
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
user.UserID = u.Uuid
|
||
|
if user.Avatar == "" {
|
||
|
user.Avatar = "https://minio.sumweal.com/nft/221109/C82M980ZLumQCzt857yxur92iAsGdCc7271sDn8MPf666sk44V.png"
|
||
|
}
|
||
|
}
|
||
|
if user.UserID == "" {
|
||
|
return Iuser, errors.New("授权登录失败")
|
||
|
}
|
||
|
//查询用户是否存在
|
||
|
query := fmt.Sprintf("%s = ? AND appid = ? AND type = ? and log_off_time is null", provider.Code)
|
||
|
global.MG_DB.Where(query, user.UserID, u.Appid, "influencer").Find(&Iuser)
|
||
|
if Iuser.UUID == uuid.Nil {
|
||
|
//注册
|
||
|
Iuser.UUID = uuid.NewV4()
|
||
|
Iuser.NickName = user.NickName
|
||
|
Iuser.Avatar = user.Avatar
|
||
|
Iuser.Appid = u.Appid
|
||
|
Iuser.Type = "influencer"
|
||
|
switch provider.Code {
|
||
|
case "tiktok":
|
||
|
Iuser.Tiktok = user.UserID
|
||
|
case "facebook":
|
||
|
Iuser.Facebook = user.UserID
|
||
|
case "twitter":
|
||
|
Iuser.Twitter = user.UserID
|
||
|
case "google":
|
||
|
Iuser.Google = user.UserID
|
||
|
case "youtube":
|
||
|
Iuser.Youtube = user.UserID
|
||
|
case "instagram":
|
||
|
Iuser.Instagram = user.UserID
|
||
|
case "ios":
|
||
|
Iuser.Ios = user.UserID
|
||
|
}
|
||
|
if Iuser.NickName == "" {
|
||
|
Iuser.NickName = "BKB_" + utils.GetInvitationLen(4) + "_user"
|
||
|
}
|
||
|
if Iuser.Avatar == "" {
|
||
|
Iuser.Avatar = "https://minio.sumweal.com/nft/221109/C82M980ZLumQCzt857yxur92iAsGdCc7271sDn8MPf666sk44V.png"
|
||
|
}
|
||
|
err = global.MG_DB.Model(&model.User{}).Create(&Iuser).Error
|
||
|
if err != nil {
|
||
|
return Iuser, errors.New("注册失败,请稍后再试")
|
||
|
}
|
||
|
//添加三方登录标识
|
||
|
var wallet model.Wallet
|
||
|
wallet.UserID = Iuser.UUID.String()
|
||
|
wallet.Platform = "influencer"
|
||
|
wallet.State = 0
|
||
|
global.MG_DB.Model(&model.Wallet{}).Create(&wallet)
|
||
|
return Iuser, nil
|
||
|
}
|
||
|
return Iuser, nil
|
||
|
}
|
||
|
|
||
|
func GetAccessToken(provider model.Provider, Code string) (err error, aceessToken string, userID string) {
|
||
|
//发送post请求
|
||
|
switch provider.Code {
|
||
|
case "tiktok":
|
||
|
aceessToken, err = GetTiktokAccessToken(provider.ClientID, provider.ClientSecret, Code, provider.RedirectURI)
|
||
|
case "facebook":
|
||
|
//facebook可以直接获取token 暂不提供
|
||
|
case "twitter":
|
||
|
aceessToken, err = GetTwitterAccessToken(provider.ClientID, provider.ClientSecret, Code, provider.RedirectURI)
|
||
|
case "google":
|
||
|
var (
|
||
|
googleOauthConfig = &oauth2.Config{
|
||
|
RedirectURL: provider.RedirectURI,
|
||
|
ClientID: provider.ClientID,
|
||
|
ClientSecret: provider.ClientSecret,
|
||
|
Scopes: []string{"https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"},
|
||
|
Endpoint: google.Endpoint,
|
||
|
}
|
||
|
token *oauth2.Token
|
||
|
)
|
||
|
token, err = googleOauthConfig.Exchange(context.Background(), Code)
|
||
|
if err != nil {
|
||
|
return err, aceessToken, ""
|
||
|
}
|
||
|
aceessToken = token.AccessToken
|
||
|
case "youtube":
|
||
|
var (
|
||
|
googleOauthConfig = &oauth2.Config{
|
||
|
RedirectURL: provider.RedirectURI,
|
||
|
ClientID: provider.ClientID,
|
||
|
ClientSecret: provider.ClientSecret,
|
||
|
Scopes: []string{"https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"},
|
||
|
Endpoint: google.Endpoint,
|
||
|
}
|
||
|
token *oauth2.Token
|
||
|
)
|
||
|
fmt.Println(provider, Code)
|
||
|
token, err = googleOauthConfig.Exchange(context.Background(), Code)
|
||
|
if err != nil {
|
||
|
return err, aceessToken, ""
|
||
|
}
|
||
|
aceessToken = token.AccessToken
|
||
|
case "instagram":
|
||
|
var (
|
||
|
instagramOauthConfig = &oauth2.Config{
|
||
|
RedirectURL: provider.RedirectURI,
|
||
|
ClientID: provider.ClientID,
|
||
|
ClientSecret: provider.ClientSecret,
|
||
|
Scopes: []string{"user_profile"},
|
||
|
Endpoint: instagram.Endpoint,
|
||
|
}
|
||
|
)
|
||
|
token, err := instagramOauthConfig.Exchange(context.Background(), Code)
|
||
|
fmt.Println(token)
|
||
|
if err != nil {
|
||
|
return err, aceessToken, ""
|
||
|
}
|
||
|
aceessToken = token.AccessToken
|
||
|
userID = strconv.Itoa(int(token.Extra("user_id").(float64)))
|
||
|
case "ios":
|
||
|
|
||
|
default:
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func GetUserInfo(provider model.Provider, aceessToken, userID string) (err error, user model.ProviderUser) {
|
||
|
switch provider.Code {
|
||
|
case "tiktok":
|
||
|
tuser, err := getUserInfo(aceessToken)
|
||
|
if err != nil {
|
||
|
fmt.Println("Error:", err)
|
||
|
return err, user
|
||
|
}
|
||
|
user.UserID = tuser.OpenID
|
||
|
user.NickName = tuser.DisplayName
|
||
|
user.Avatar = tuser.AvatarURL
|
||
|
return nil, user
|
||
|
case "facebook":
|
||
|
var userID string
|
||
|
//校验token
|
||
|
tokenCheckURI := provider.TokenCheckURI + "?input_token=" + aceessToken + "&access_token=" + getAccessToken(provider.ClientID, provider.ClientSecret)
|
||
|
err, userID := DebugFacebookToken(aceessToken, tokenCheckURI)
|
||
|
if err != nil {
|
||
|
return errors.New("token校验失败"), user
|
||
|
}
|
||
|
//获取用户信息
|
||
|
userInfoURI := strings.Replace(provider.UserInfoURI, "USER-ID", userID, 1)
|
||
|
userInfoURI = userInfoURI + "&access_token=" + getAccessToken(provider.ClientID, provider.ClientSecret)
|
||
|
faceUser, err := getThirdPartyLoginUserBO(userInfoURI)
|
||
|
if err != nil {
|
||
|
return errors.New("获取用户信息失败"), user
|
||
|
}
|
||
|
user.UserID = userID
|
||
|
user.NickName = faceUser.UserName
|
||
|
user.Email = faceUser.Email
|
||
|
user.Avatar = faceUser.Picture.Data.URL
|
||
|
return nil, user
|
||
|
case "instagram":
|
||
|
resp, err1 := http.Get(provider.UserInfoURI + userID + fmt.Sprintf("?fields=%v&access_token=%v", "id,username", aceessToken))
|
||
|
if err1 != nil {
|
||
|
fmt.Println("Failed to get user info: ", err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
defer resp.Body.Close()
|
||
|
content, err1 := io.ReadAll(resp.Body)
|
||
|
if err1 != nil {
|
||
|
fmt.Println("Failed to read response body: ", err1.Error())
|
||
|
return
|
||
|
}
|
||
|
fmt.Println("Response body: ", string(content))
|
||
|
var googleUser response.TripartiteInstagram
|
||
|
err1 = json.Unmarshal(content, &googleUser)
|
||
|
if err1 != nil {
|
||
|
fmt.Println("Failed to read response body: ", err1.Error())
|
||
|
return
|
||
|
}
|
||
|
user.UserID = googleUser.ID
|
||
|
user.NickName = googleUser.Username
|
||
|
case "twitter":
|
||
|
user, err = getTwitterUserInfo(provider, aceessToken)
|
||
|
return err, user
|
||
|
case "google":
|
||
|
resp, err1 := http.Get("https://oauth2.googleapis.com/tokeninfo?id_token=" + aceessToken)
|
||
|
if err1 != nil {
|
||
|
fmt.Println("Failed to get user info: ", err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
defer resp.Body.Close()
|
||
|
content, err1 := io.ReadAll(resp.Body)
|
||
|
if err1 != nil {
|
||
|
fmt.Println("Failed to read response body: ", err1.Error())
|
||
|
return
|
||
|
}
|
||
|
var googleUser response.TripartiteGoogle
|
||
|
err1 = json.Unmarshal(content, &googleUser)
|
||
|
if err1 != nil {
|
||
|
fmt.Println("Failed to read response body: ", err1.Error())
|
||
|
return
|
||
|
}
|
||
|
user.UserID = googleUser.Sub
|
||
|
user.NickName = googleUser.Name
|
||
|
// user.Email = googleUser.Email
|
||
|
user.Avatar = googleUser.Picture
|
||
|
fmt.Println("Response body: ", string(content))
|
||
|
case "youtube":
|
||
|
resp, err1 := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + aceessToken)
|
||
|
fmt.Println("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + aceessToken)
|
||
|
if err1 != nil {
|
||
|
fmt.Println("Failed to get user info: ", err1.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
defer resp.Body.Close()
|
||
|
content, err1 := io.ReadAll(resp.Body)
|
||
|
if err1 != nil {
|
||
|
fmt.Println("Failed to read response body: ", err1.Error())
|
||
|
return
|
||
|
}
|
||
|
var googleUser response.TripartiteGoogle
|
||
|
err1 = json.Unmarshal(content, &googleUser)
|
||
|
if err1 != nil {
|
||
|
fmt.Println("Failed to read response body: ", err1.Error())
|
||
|
return
|
||
|
}
|
||
|
user.UserID = googleUser.ID
|
||
|
user.NickName = googleUser.Name
|
||
|
// user.Email = googleUser.Email
|
||
|
user.Avatar = googleUser.Picture
|
||
|
fmt.Println("Response body: ", string(content))
|
||
|
|
||
|
default:
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func DebugFacebookToken(accessToken, url string) (err error, userID string) {
|
||
|
// 构建请求
|
||
|
req, err := http.NewRequest("GET", url, nil)
|
||
|
if err != nil {
|
||
|
return err, ""
|
||
|
}
|
||
|
|
||
|
// 发送请求
|
||
|
client := http.Client{}
|
||
|
resp, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return err, ""
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
// 读取响应数据
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return err, ""
|
||
|
}
|
||
|
fmt.Println(string(body))
|
||
|
// 解析响应数据
|
||
|
var result = make(map[string]interface{})
|
||
|
if err := json.Unmarshal(body, &result); err != nil {
|
||
|
return err, ""
|
||
|
}
|
||
|
if _, ok := result["error"].(map[string]interface{}); ok {
|
||
|
errorMsg := result["error"].(map[string]interface{})["message"].(string)
|
||
|
return errors.New(errorMsg), ""
|
||
|
}
|
||
|
// 提取 access_token
|
||
|
if _, ok := result["data"].(map[string]interface{}); ok {
|
||
|
userID, ok := result["data"].(map[string]interface{})["user_id"].(string)
|
||
|
if !ok {
|
||
|
return errors.New("未能获取有效的user_id"), ""
|
||
|
}
|
||
|
return nil, userID
|
||
|
}
|
||
|
return errors.New("未能获取有效的user_id"), ""
|
||
|
}
|
||
|
|
||
|
func GetTiktokAccessToken(clientKey, clientSecret, code, redirectURI string) (string, error) {
|
||
|
// 构建请求体
|
||
|
data := url.Values{}
|
||
|
data.Set("client_key", clientKey)
|
||
|
data.Set("client_secret", clientSecret)
|
||
|
data.Set("code", code)
|
||
|
data.Set("grant_type", "authorization_code")
|
||
|
data.Set("redirect_uri", redirectURI)
|
||
|
|
||
|
// 构建请求
|
||
|
req, err := http.NewRequest("POST", "https://open.tiktokapis.com/v2/oauth/token", strings.NewReader(data.Encode()))
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
// 设置请求头
|
||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||
|
|
||
|
// 发送请求
|
||
|
client := http.Client{}
|
||
|
resp, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
// 读取响应数据
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
// 解析响应数据
|
||
|
var result map[string]interface{}
|
||
|
if err := json.Unmarshal(body, &result); err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
// 检查是否返回错误
|
||
|
if errorMsg, ok := result["error_description"].(string); ok {
|
||
|
return "", fmt.Errorf("获取 access token 失败:%s", errorMsg)
|
||
|
}
|
||
|
|
||
|
// 提取 access_token
|
||
|
accessToken, ok := result["access_token"].(string)
|
||
|
if !ok {
|
||
|
return "", fmt.Errorf("未能获取有效的 access token")
|
||
|
}
|
||
|
|
||
|
return accessToken, nil
|
||
|
}
|
||
|
|
||
|
func GetTwitterAccessToken(clientKey, clientSecret, code, redirectURI string) (string, error) {
|
||
|
// 构建请求体
|
||
|
data := url.Values{}
|
||
|
data.Set("code", code)
|
||
|
data.Set("grant_type", "authorization_code")
|
||
|
data.Set("client_id", clientKey)
|
||
|
data.Set("redirect_uri", redirectURI)
|
||
|
data.Set("code_verifier", "challenge")
|
||
|
|
||
|
req, err := http.NewRequest("POST", "https://api.twitter.com/2/oauth2/token", strings.NewReader(data.Encode()))
|
||
|
if err != nil {
|
||
|
// handle error
|
||
|
}
|
||
|
|
||
|
// 设置请求头
|
||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||
|
|
||
|
// 发送请求
|
||
|
client := http.Client{}
|
||
|
resp, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
// 读取响应数据
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
// 解析响应数据
|
||
|
var result map[string]interface{}
|
||
|
if err := json.Unmarshal(body, &result); err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
// 检查是否返回错误
|
||
|
if errorMsg, ok := result["error_description"].(string); ok {
|
||
|
return "", fmt.Errorf("获取 access token 失败:%s", errorMsg)
|
||
|
}
|
||
|
|
||
|
// 提取 access_token
|
||
|
accessToken, ok := result["access_token"].(string)
|
||
|
if !ok {
|
||
|
return "", fmt.Errorf("未能获取有效的 access token")
|
||
|
}
|
||
|
|
||
|
return accessToken, nil
|
||
|
}
|
||
|
|
||
|
func getThirdPartyLoginUserBO(url string) (model.FacebookUserInfo, error) {
|
||
|
resp, err := http.Get(url)
|
||
|
if err != nil {
|
||
|
return model.FacebookUserInfo{}, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||
|
return model.FacebookUserInfo{}, fmt.Errorf("Facebook请求查询用户接口失败,响应为%d", resp.StatusCode)
|
||
|
}
|
||
|
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return model.FacebookUserInfo{}, err
|
||
|
}
|
||
|
|
||
|
var userInfo model.FacebookUserInfo
|
||
|
err = json.Unmarshal(body, &userInfo)
|
||
|
if err != nil {
|
||
|
return model.FacebookUserInfo{}, err
|
||
|
}
|
||
|
|
||
|
return userInfo, nil
|
||
|
}
|
||
|
|
||
|
func getAccessToken(clientId string, clientSecret string) string {
|
||
|
url := "https://graph.facebook.com/oauth/access_token?client_id=" + clientId + "&client_secret=" + clientSecret + "&grant_type=client_credentials"
|
||
|
req, err := http.NewRequest("GET", url, nil)
|
||
|
if err != nil {
|
||
|
return ""
|
||
|
}
|
||
|
res, err := http.DefaultClient.Do(req)
|
||
|
if err != nil {
|
||
|
return ""
|
||
|
}
|
||
|
defer res.Body.Close()
|
||
|
body, err := io.ReadAll(res.Body)
|
||
|
if err != nil {
|
||
|
return ""
|
||
|
}
|
||
|
var result map[string]interface{}
|
||
|
if err := json.Unmarshal(body, &result); err != nil {
|
||
|
return ""
|
||
|
}
|
||
|
fmt.Println(string(body))
|
||
|
accessToken, ok := result["access_token"].(string)
|
||
|
if !ok {
|
||
|
return ""
|
||
|
}
|
||
|
return accessToken
|
||
|
// return url.QueryEscape(clientId + "|" + clientSecret)
|
||
|
}
|
||
|
|
||
|
func getUserInfo(token string) (*model.TiktokUser, error) {
|
||
|
url := "https://open.tiktokapis.com/v2/user/info/?fields=open_id,union_id,avatar_url,display_name"
|
||
|
req, err := http.NewRequest("GET", url, nil)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
req.Header.Add("Authorization", "Bearer "+token)
|
||
|
|
||
|
res, err := http.DefaultClient.Do(req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
defer res.Body.Close()
|
||
|
|
||
|
body, err := io.ReadAll(res.Body)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
var response response.TiktokResponse
|
||
|
err = json.Unmarshal(body, &response)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if response.Error.Code != "ok" {
|
||
|
return nil, fmt.Errorf("API error: %s", response.Error.Message)
|
||
|
}
|
||
|
|
||
|
return &response.Data.User, nil
|
||
|
}
|
||
|
|
||
|
func getUserSimple(ut, uuid string) (model.UserSimple, error) {
|
||
|
var (
|
||
|
err error
|
||
|
result model.UserSimple
|
||
|
)
|
||
|
err = global.MG_DB.Model(&model.User{}).Select("uuid,nick_name,avatar,phone,platform,tags").Where("`type` = ? AND uuid = ?", ut, uuid).Find(&result).Error
|
||
|
if err != nil {
|
||
|
return result, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func GetUserStatistics(uuid string) (err error, info response.UserStatistics) {
|
||
|
var (
|
||
|
user model.UserSimple
|
||
|
collectionTotal int64
|
||
|
)
|
||
|
err = global.MG_DB.Model(&model.User{}).Where("uuid=?", uuid).Find(&user).Error
|
||
|
if err != nil {
|
||
|
return errors.New("获取用户失败"), info
|
||
|
}
|
||
|
|
||
|
if user.Platform != "" {
|
||
|
var platforms []request.Platform
|
||
|
err = json.Unmarshal([]byte(user.Platform), &platforms)
|
||
|
for _, v := range platforms {
|
||
|
if v.IsAuth {
|
||
|
info.PlatformNum += 1
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
db := global.MG_DB.Model(&model.CollectionMission{}).Joins("INNER JOIN mission ON mission.id = collection_mission.mission_id").
|
||
|
Where("collection_mission.create_by = ?", uuid)
|
||
|
err = db.Count(&collectionTotal).Error
|
||
|
info.CollectionMissionNum = collectionTotal
|
||
|
|
||
|
return nil, info
|
||
|
}
|
||
|
|
||
|
func UpdateUserDetail(userID string, userInfo *request.UserDetail) error {
|
||
|
var (
|
||
|
err error
|
||
|
uMap = make(map[string]interface{})
|
||
|
)
|
||
|
switch userInfo.Type {
|
||
|
case "platform":
|
||
|
uMap["platform"] = userInfo.Platform
|
||
|
case "tags":
|
||
|
uMap["tags"] = userInfo.Tags
|
||
|
}
|
||
|
err = global.MG_DB.Model(&model.User{}).Where("uuid=?", userID).Updates(uMap).Error
|
||
|
if err != nil {
|
||
|
return errors.New("修改个人信息失败")
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func GetUserDetail(userID string) (err error, userInfo *model.UserSimple) {
|
||
|
var (
|
||
|
user model.UserSimple
|
||
|
dictData []model.SysDictData
|
||
|
)
|
||
|
err = global.MG_DB.Model(&model.User{}).Where("uuid=?", userID).Find(&user).Error
|
||
|
if err != nil {
|
||
|
return errors.New("获取用户失败"), nil
|
||
|
}
|
||
|
err = global.MG_DB.Model(&model.SysDictData{}).Where("type_code=?", "release_channel").Find(&dictData).Error
|
||
|
if err != nil {
|
||
|
return errors.New("获取用户失败"), nil
|
||
|
}
|
||
|
platformsC := make([]request.Platform, 0)
|
||
|
for i := 0; i < len(dictData); i++ {
|
||
|
platformsC = append(platformsC, request.Platform{
|
||
|
Platform: dictData[i].Value,
|
||
|
Label: dictData[i].Label,
|
||
|
})
|
||
|
}
|
||
|
if user.Platform != "" {
|
||
|
var platforms []request.Platform
|
||
|
err = json.Unmarshal([]byte(user.Platform), &platforms)
|
||
|
fmt.Println(err)
|
||
|
for i := 0; i < len(platforms); i++ {
|
||
|
for j := 0; j < len(platformsC); j++ {
|
||
|
if platforms[i].Platform == platformsC[j].Platform {
|
||
|
platformsC[j].IsAuth = platforms[i].IsAuth
|
||
|
platformsC[j].Url = platforms[i].Url
|
||
|
platformsC[j].Image = platforms[i].Image
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
user.Platforms = platformsC
|
||
|
} else {
|
||
|
user.Platforms = platformsC
|
||
|
}
|
||
|
return nil, &user
|
||
|
}
|
||
|
|
||
|
func getTwitterUserInfo(provider model.Provider, aceessToken string) (model.ProviderUser, error) {
|
||
|
var result response.TripartiteTwitter
|
||
|
var user model.ProviderUser
|
||
|
req, err := http.NewRequest("GET", provider.UserInfoURI, nil)
|
||
|
if err != nil {
|
||
|
// handle error
|
||
|
return user, err
|
||
|
}
|
||
|
|
||
|
req.Header.Add("Authorization", "Bearer "+aceessToken)
|
||
|
|
||
|
client := &http.Client{}
|
||
|
resp, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
// handle error
|
||
|
return user, err
|
||
|
}
|
||
|
defer resp.Body.Close()
|
||
|
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
// handle error
|
||
|
return user, err
|
||
|
}
|
||
|
fmt.Println("------twitter", string(body))
|
||
|
err = json.Unmarshal(body, &result)
|
||
|
if err != nil {
|
||
|
// handle error
|
||
|
fmt.Println("------twitter", err)
|
||
|
return user, err
|
||
|
}
|
||
|
user.UserID = result.Data.ID
|
||
|
user.NickName = result.Data.Name
|
||
|
return user, err
|
||
|
}
|
||
|
|
||
|
func UserLogOff(userID string) (err error) {
|
||
|
var (
|
||
|
uMap = make(map[string]interface{})
|
||
|
)
|
||
|
uMap["id_forbidden"] = 1
|
||
|
uMap["log_off_time"] = time.Now()
|
||
|
uMap["forbidden_time"] = time.Now()
|
||
|
uMap["forbidden_reason"] = "用户注销"
|
||
|
uMap["nick_name"] = "用户已注销"
|
||
|
uMap["avatar"] = "https://minio.sumweal.com/nft/221109/C82M980ZLumQCzt857yxur92iAsGdCc7271sDn8MPf666sk44V.png"
|
||
|
err = global.MG_DB.Model(&model.User{}).Where("uuid=?", userID).Updates(uMap).Error
|
||
|
if err != nil {
|
||
|
return errors.New("注销失败")
|
||
|
}
|
||
|
return nil
|
||
|
}
|