|
@@ -13,7 +13,6 @@ import (
|
|
|
"github.com/lin-jim-leon/kuaishou/open/user"
|
|
|
"log"
|
|
|
"strings"
|
|
|
- "sync"
|
|
|
"time"
|
|
|
"youngmini_server/app/model/youngee_talent_model"
|
|
|
"youngmini_server/app/service/youngee_sectask_service"
|
|
@@ -35,22 +34,8 @@ const (
|
|
|
//SignSecret1 = "bf6393dce0a2b669ee348bebb837b0da"
|
|
|
)
|
|
|
|
|
|
-// 定义全局变量保存上下文
|
|
|
-var ctx context.Context
|
|
|
-var cancel context.CancelFunc
|
|
|
-var once sync.Once // 确保上下文只被初始化一次
|
|
|
-
|
|
|
-// 初始化 chromedp 上下文的函数
|
|
|
-func initChromedpContext() {
|
|
|
- once.Do(func() {
|
|
|
- // 初始化 chromedp 上下文
|
|
|
- ctx, cancel = chromedp.NewContext(context.Background())
|
|
|
- fmt.Println("Chromedp 上下文已初始化")
|
|
|
- })
|
|
|
-}
|
|
|
-
|
|
|
// 清理上下文的函数,用于关闭浏览器和取消上下文
|
|
|
-func CleanupChromedpContext() {
|
|
|
+func CleanupChromedpContext(cancel context.CancelFunc) {
|
|
|
if cancel != nil {
|
|
|
cancel()
|
|
|
fmt.Println("Chromedp 上下文已关闭")
|
|
@@ -68,13 +53,15 @@ func GetQrcode(r *ghttp.Request) *TalentHttpResult {
|
|
|
startTime := time.Now()
|
|
|
// 创建一个新的上下文
|
|
|
// 确保 chromedp 上下文已初始化
|
|
|
- initChromedpContext()
|
|
|
- //ctx, cancel := chromedp.NewContext(context.Background())
|
|
|
+ //initChromedpContext()
|
|
|
+ ctx, cancel := chromedp.NewContext(context.Background()) //每个请求有独立的上下文,而不是共享的
|
|
|
+ fmt.Println("上下文初始化成功")
|
|
|
+
|
|
|
// 构建包含 tid 的 urlstr
|
|
|
var urlstr string
|
|
|
typePlatform := r.GetInt("platform_id")
|
|
|
if typePlatform == 8 { //快手平台
|
|
|
- urlstr = fmt.Sprintf("https://open.kuaishou.com/oauth2/connect?state=%s&app_id=ks671599294546520767&redirect_uri=https://younggee.com/kuaishouVideoVideo&scope=user_video_info,user_info&response_type=code", tid)
|
|
|
+ urlstr = fmt.Sprintf("https://open.kuaishou.com/oauth2/connect?state=%s&app_id=ks671599294546520767&redirect_uri=https://younggee.com/kuaishouVideo&scope=user_video_info,user_info&response_type=code", tid)
|
|
|
} else if typePlatform == 2 { //抖音平台
|
|
|
urlstr = fmt.Sprintf("https://open.douyin.com/platform/oauth/connect/?state=%s&client_key=awi77xl5kpl16hmi&response_type=code&scope=user_info,data.external.user,video.list.bind&redirect_uri=https://www.younggee.com/douyinauth", tid)
|
|
|
} else {
|
|
@@ -109,7 +96,13 @@ func GetQrcode(r *ghttp.Request) *TalentHttpResult {
|
|
|
println("ok----->", ok)
|
|
|
println("url----->", urlstr)
|
|
|
fmt.Println("代码运行时间:", time.Since(startTime))
|
|
|
+ // 设置定时器,100秒后关闭上下文
|
|
|
+ go func() {
|
|
|
+ <-time.After(100 * time.Second)
|
|
|
+ CleanupChromedpContext(cancel)
|
|
|
+ }()
|
|
|
return &TalentHttpResult{Code: 0, Msg: "success", Data: text}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
func GetKsEcomQrcode(r *ghttp.Request) *TalentHttpResult {
|
|
@@ -121,13 +114,14 @@ func GetKsEcomQrcode(r *ghttp.Request) *TalentHttpResult {
|
|
|
}
|
|
|
// 记录开始时间
|
|
|
startTime := time.Now()
|
|
|
- // 确保上下文只初始化一次
|
|
|
- once.Do(func() {
|
|
|
- ctx, _ = chromedp.NewContext(context.Background())
|
|
|
- fmt.Println("Chromedp 上下文已初始化")
|
|
|
- })
|
|
|
// 创建一个新的上下文
|
|
|
- //ctx, _ := chromedp.NewContext(context.Background())
|
|
|
+ ctx, cancel := chromedp.NewContext(context.Background())
|
|
|
+ //ctx, cancel := chromedp.NewContext(context.Background(), chromedp.WithDebugf(log.Printf))
|
|
|
+ // 设置定时器,100秒后关闭上下文
|
|
|
+ //go func() {
|
|
|
+ // <-time.After(100 * time.Second)
|
|
|
+ // CleanupChromedpContext(cancel)
|
|
|
+ //}()
|
|
|
//需要访问的网址,无state,扫码后跳转到订购页面
|
|
|
urlstr := fmt.Sprintf("https://login.kwaixiaodian.com/?biz=fuwu&redirect_url=https://fuwu.kwaixiaodian.com/new/detail?id=22328015871939")
|
|
|
// 执行任务
|
|
@@ -148,19 +142,15 @@ func GetKsEcomQrcode(r *ghttp.Request) *TalentHttpResult {
|
|
|
if err != nil {
|
|
|
fmt.Println("errInfo---", err.Error())
|
|
|
}
|
|
|
- println("text----->", text)
|
|
|
- println("ok----->", ok)
|
|
|
- println("url----->", urlstr)
|
|
|
fmt.Println("代码运行时间:", time.Since(startTime))
|
|
|
// 返回二维码的操作已经完成,后续操作(go fun)在后台继续进行
|
|
|
// 启动后台 goroutine 监听重定向并处理
|
|
|
- go HandleAfterRedirect(r)
|
|
|
-
|
|
|
+ go HandleAfterRedirect(r, ctx, cancel)
|
|
|
return &TalentHttpResult{Code: 0, Msg: "二维码已生成", Data: text}
|
|
|
}
|
|
|
|
|
|
// 此函数执行完毕后
|
|
|
-func HandleAfterRedirect(r *ghttp.Request) *TalentHttpResult {
|
|
|
+func HandleAfterRedirect(r *ghttp.Request, ctx context.Context, cancel context.CancelFunc) *TalentHttpResult {
|
|
|
fmt.Println("HandleAfterRedirect-----")
|
|
|
//达人id获取
|
|
|
tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
|
|
@@ -168,26 +158,31 @@ func HandleAfterRedirect(r *ghttp.Request) *TalentHttpResult {
|
|
|
var isChecked1, isChecked2 bool
|
|
|
//var isChecked1 bool
|
|
|
var currentURL string
|
|
|
+ var subscribeURL string
|
|
|
+
|
|
|
authURL := fmt.Sprintf("https://open.kwaixiaodian.com/oauth/authorize?app_id=ks651333097154138217&redirectUri=https://younggee.com/kuaishouauth&scope=merchant_distribution,merchant_refund,merchant_item,merchant_order,user_info,merchant_servicemarket,merchant_user,merchant_logistics&state=%s", tid)
|
|
|
// 执行任务
|
|
|
+ fmt.Println("扫码跳转成功,正在提交订单...")
|
|
|
err = chromedp.Run(ctx, chromedp.Tasks{
|
|
|
// 等待用户扫码并跳转后,点击“立即购买”按钮,在这识别用户是否扫码完成
|
|
|
- chromedp.WaitVisible(`#main_root > div > div.yPXHTHq9cx_6vEhWohw_ > div.Jb15b0XMeXvmyWfEfrvq > div.m5IIGVbmvqsfZdb00GFO > div.JktibAFTihy_wzOaJXHl > div.Cbz2c2xjrwvEPJnVK4iA > div.KY1LeQ_L_xVgT9RzkLow.cgxWbYdA4fs28QuNN36F > button`),
|
|
|
- chromedp.Click(`#main_root > div > div.yPXHTHq9cx_6vEhWohw_ > div.Jb15b0XMeXvmyWfEfrvq > div.m5IIGVbmvqsfZdb00GFO > div.JktibAFTihy_wzOaJXHl > div.Cbz2c2xjrwvEPJnVK4iA > div.KY1LeQ_L_xVgT9RzkLow.cgxWbYdA4fs28QuNN36F > button`),
|
|
|
+ chromedp.WaitVisible(`#main_root > div > div.yPXHTHq9cx_6vEhWohw_ > div.Jb15b0XMeXvmyWfEfrvq > div.m5IIGVbmvqsfZdb00GFO > div.JktibAFTihy_wzOaJXHl > div.Cbz2c2xjrwvEPJnVK4iA > div.KY1LeQ_L_xVgT9RzkLow.cgxWbYdA4fs28QuNN36F > button`, chromedp.ByQuery),
|
|
|
+ chromedp.Click(`#main_root > div > div.yPXHTHq9cx_6vEhWohw_ > div.Jb15b0XMeXvmyWfEfrvq > div.m5IIGVbmvqsfZdb00GFO > div.JktibAFTihy_wzOaJXHl > div.Cbz2c2xjrwvEPJnVK4iA > div.KY1LeQ_L_xVgT9RzkLow.cgxWbYdA4fs28QuNN36F > button`, chromedp.ByQuery),
|
|
|
// 等待第一个 checkbox 元素可见
|
|
|
- chromedp.WaitVisible(`#app > div > section > main > div.cart-container > div.price-content > div.checkbox > label > span.ant-checkbox > input`),
|
|
|
-
|
|
|
+ chromedp.WaitVisible(`#app > div > section > main > div.cart-container > div.price-content > div.checkbox > label > span.ant-checkbox > input`, chromedp.ByQuery),
|
|
|
// 检查第一个 checkbox 是否已选中
|
|
|
chromedp.EvaluateAsDevTools(`document.querySelector('#app > div > section > main > div.cart-container > div.price-content > div.checkbox > label > span.ant-checkbox > input').checked`, &isChecked1),
|
|
|
+ chromedp.Location(&subscribeURL),
|
|
|
})
|
|
|
+
|
|
|
if err != nil {
|
|
|
log.Fatalf("任务1执行失败:%v", err)
|
|
|
}
|
|
|
- fmt.Println("*****")
|
|
|
- // 如果第一个 checkbox 没有选中,则点击选中
|
|
|
+ fmt.Println("订阅页面---->", subscribeURL)
|
|
|
+
|
|
|
if !isChecked1 {
|
|
|
fmt.Println("第一个 Checkbox 未选中。现在选中...")
|
|
|
- err = chromedp.Run(ctx, chromedp.Click(`#app > div > section > main > div.cart-container > div.price-content > div.checkbox > label > span.ant-checkbox > input`))
|
|
|
+ err = chromedp.Run(ctx, chromedp.Click(`#app > div > section > main > div.cart-container > div.price-content > div.checkbox > label > span.ant-checkbox > input`, chromedp.ByQuery))
|
|
|
+ fmt.Println("选中成功")
|
|
|
if err != nil {
|
|
|
log.Fatalf("点击第一个 checkbox 失败:%v", err)
|
|
|
}
|
|
@@ -195,49 +190,86 @@ func HandleAfterRedirect(r *ghttp.Request) *TalentHttpResult {
|
|
|
fmt.Println("第一个 Checkbox 已经选中。")
|
|
|
}
|
|
|
|
|
|
+ // 确认订单 拆分
|
|
|
+ fmt.Println("这一步进行服务的订阅...")
|
|
|
err = chromedp.Run(ctx, chromedp.Tasks{
|
|
|
- // 点击提交订单按钮
|
|
|
- chromedp.Click(`#app > div > section > main > div.cart-container > div.price-content > div.checkout > button`),
|
|
|
- chromedp.Sleep(2 * time.Second),
|
|
|
- //服务已购买,进行授权
|
|
|
+ chromedp.Click(`#app > div > section > main > div.cart-container > div.price-content > div.checkout > button`, chromedp.ByQuery),
|
|
|
chromedp.Navigate(authURL),
|
|
|
- chromedp.Sleep(5 * time.Second),
|
|
|
+ chromedp.Location(¤tURL),
|
|
|
})
|
|
|
- //获取url
|
|
|
+ //出现点击服务订阅,失败的问题,等不到下一个页面的出现
|
|
|
+
|
|
|
+ //// Step 1: 点击按钮
|
|
|
+ //fmt.Println("1")
|
|
|
+ //err = chromedp.Run(ctx, chromedp.Tasks{
|
|
|
+ // chromedp.Click(`#app > div > section > main > div.cart-container > div.price-content > div.checkout > button`, chromedp.ByQuery), // 点击按钮
|
|
|
+ //})
|
|
|
+ //fmt.Println("2")
|
|
|
+ //if err != nil {
|
|
|
+ // log.Fatalf("获取 afterbuyURL 失败: %v", err)
|
|
|
+ //}
|
|
|
+ // Step 4: 跳转到授权页面
|
|
|
+ //err = chromedp.Run(ctx, chromedp.WaitVisible(`#app > div > section > main > div.paid-container > div.title`, chromedp.ByQuery))
|
|
|
+ //if err != nil {
|
|
|
+ // log.Fatalf("跳转到授权页面失败:%v", err)
|
|
|
+ //}
|
|
|
+ //fmt.Println("3")
|
|
|
+
|
|
|
+ // Step 4: 跳转到授权页面
|
|
|
+ //err = chromedp.Run(ctx, chromedp.Navigate(authURL))
|
|
|
+ //if err != nil {
|
|
|
+ // log.Fatalf("跳转到授权页面失败:%v", err)
|
|
|
+ //}
|
|
|
+
|
|
|
+ // Step 5: 获取授权页面 URL
|
|
|
err = chromedp.Run(ctx, chromedp.Location(¤tURL))
|
|
|
+ if err != nil {
|
|
|
+ log.Fatalf("获取 currentURL 失败:%v", err)
|
|
|
+ }
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("提交订单失败---", err.Error())
|
|
|
+ }
|
|
|
+ fmt.Println("当前授权地址url---》:", currentURL)
|
|
|
+ fmt.Println("服务的订阅成功...")
|
|
|
+ //提交订单需要时间处理
|
|
|
+ //time.Sleep(3 * time.Second)
|
|
|
+ //跳转到授权页面
|
|
|
+ //err = chromedp.Run(ctx, chromedp.Tasks{
|
|
|
+ // chromedp.Navigate(authURL),
|
|
|
+ // chromedp.Location(¤tURL),
|
|
|
+ //})
|
|
|
//期望含code
|
|
|
- fmt.Println("currentURL-----:", currentURL)
|
|
|
-
|
|
|
- // 检查当前 URL 是否包含指定字符串 "https://open.kwaixiaodian.com/oauth/authorize"
|
|
|
+ //不管是否订购过 , 肯定会包含 ,需要手动点击则点击, 不需要手动点击直接授权成功
|
|
|
if strings.Contains(currentURL, "https://open.kwaixiaodian.com/oauth/authorize") {
|
|
|
- // 等待第二个 checkbox 元素可见
|
|
|
+ log.Println("into https://open.kwaixiaodian.com/oauth/authorize")
|
|
|
err = chromedp.Run(ctx, chromedp.WaitVisible(`#root > div > div > div > div > div.flex.f-jc-c.UcUNDIrRp1R3a9EnEbQE > div > div.f-1.f-fs-0.Zcms9k6vZGxKICWMIrdG > div.wNrvV7C_9L5FB2oxmyLe > div > div:nth-child(2) > label > span.ant-checkbox > input`, chromedp.ByQuery))
|
|
|
+ if err != nil {
|
|
|
+ log.Println("WaitVisible执行失败:%v", err)
|
|
|
+ }
|
|
|
+ log.Println("等待第二个 checkbox 元素可见")
|
|
|
// 检查第二个 checkbox 获得选中情况
|
|
|
err = chromedp.Run(ctx, chromedp.EvaluateAsDevTools(`document.querySelector('#root > div > div > div > div > div.flex.f-jc-c.UcUNDIrRp1R3a9EnEbQE > div > div.f-1.f-fs-0.Zcms9k6vZGxKICWMIrdG > div.wNrvV7C_9L5FB2oxmyLe > div > div:nth-child(2) > label > span.ant-checkbox > input').checked`, &isChecked2))
|
|
|
if err != nil {
|
|
|
- log.Fatalf("检查第二个 checkbox 状态失败:%v", err)
|
|
|
+ log.Println("EvaluateAsDevTools执行失败:%v", err)
|
|
|
}
|
|
|
-
|
|
|
+ log.Println("检查第二个 checkbox 获得选中情况")
|
|
|
// 如果第二个 checkbox 没有选中,则点击选中
|
|
|
if !isChecked2 {
|
|
|
//选中同意
|
|
|
- err = chromedp.Run(ctx, chromedp.Click(`#root > div > div > div > div > div.flex.f-jc-c.UcUNDIrRp1R3a9EnEbQE > div > div.f-1.f-fs-0.Zcms9k6vZGxKICWMIrdG > div.wNrvV7C_9L5FB2oxmyLe > div > div:nth-child(2) > label > span.ant-checkbox > input`))
|
|
|
+ err = chromedp.Run(ctx, chromedp.Click(`#root > div > div > div > div > div.flex.f-jc-c.UcUNDIrRp1R3a9EnEbQE > div > div.f-1.f-fs-0.Zcms9k6vZGxKICWMIrdG > div.wNrvV7C_9L5FB2oxmyLe > div > div:nth-child(2) > label > span.ant-checkbox > input`, chromedp.ByQuery))
|
|
|
//点击授权按钮,肯定重定向过去了
|
|
|
} else {
|
|
|
fmt.Println("第二个 Checkbox 已经选中。")
|
|
|
}
|
|
|
//点击后授权成功
|
|
|
- err = chromedp.Run(ctx, chromedp.Click(`#root > div > div > div > div > div.flex.f-jc-c.UcUNDIrRp1R3a9EnEbQE > div > div.f-1.f-fs-0.Zcms9k6vZGxKICWMIrdG > div.wNrvV7C_9L5FB2oxmyLe > button`))
|
|
|
+ err = chromedp.Run(ctx, chromedp.Click(`#root > div > div > div > div > div.flex.f-jc-c.UcUNDIrRp1R3a9EnEbQE > div > div.f-1.f-fs-0.Zcms9k6vZGxKICWMIrdG > div.wNrvV7C_9L5FB2oxmyLe > button`, chromedp.ByQuery))
|
|
|
+ //等待跳转并且授权
|
|
|
+ time.Sleep(10 * time.Second)
|
|
|
+ CleanupChromedpContext(cancel)
|
|
|
fmt.Println("ok")
|
|
|
-
|
|
|
- } else {
|
|
|
- fmt.Println("已经被后端API捕捉。")
|
|
|
- fmt.Println("currentURL-----:", currentURL)
|
|
|
}
|
|
|
-
|
|
|
// 输出执行完成的日志
|
|
|
fmt.Println("授权完成")
|
|
|
-
|
|
|
return &TalentHttpResult{Code: 0, Msg: "已重定向到kuaishouauth接口", Data: nil}
|
|
|
}
|
|
|
|
|
@@ -272,101 +304,108 @@ func CheckAccount(r *ghttp.Request) *TalentHttpResult {
|
|
|
}
|
|
|
|
|
|
// / 抖音二维码绑定,轮询此接口,
|
|
|
-func CheckDyNewAccount(r *ghttp.Request) *TalentHttpResult {
|
|
|
- //check到了更新时间在两秒内的插入的数据。则说明已绑定。弹窗消失。
|
|
|
- tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
|
|
|
- redisDyBindStatusKey := fmt.Sprintf("DyBind:%s", tid)
|
|
|
- bindStatus, err := g.Redis().DoVar("GET", redisDyBindStatusKey)
|
|
|
- if bindStatus.String() == "bound_to_other" {
|
|
|
- return &TalentHttpResult{Code: -1, Msg: "此账号已被其他达人绑定", Data: nil}
|
|
|
- }
|
|
|
- if err != nil {
|
|
|
- return &TalentHttpResult{Code: -1, Msg: "Get talent id failed"}
|
|
|
- }
|
|
|
- // 获取当前时间
|
|
|
- currentTime := gtime.Now()
|
|
|
- // 查询数据库中符合条件的所有记录
|
|
|
- var userInfos []youngee_talent_model.KuaishouUserInfo
|
|
|
- err = g.DB().Model("platform_kuaishou_user_info").
|
|
|
- Where("talent_id = ?", tid).
|
|
|
- Scan(&userInfos)
|
|
|
- if err != nil {
|
|
|
- return &TalentHttpResult{Code: -4, Msg: "Database query failed"}
|
|
|
- }
|
|
|
-
|
|
|
- if len(userInfos) == 0 {
|
|
|
- // 如果没有符合条件的数据,返回失败
|
|
|
- return &TalentHttpResult{Code: -5, Msg: "No valid data found"}
|
|
|
- }
|
|
|
-
|
|
|
- // 找到 CreateTime 最新的记录
|
|
|
- latestUserInfo := userInfos[0]
|
|
|
- for _, info := range userInfos[1:] {
|
|
|
- if info.CreateTime.After(latestUserInfo.CreateTime) {
|
|
|
- latestUserInfo = info
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 计算创建时间与当前时间的差值
|
|
|
- timeDiff := currentTime.Sub(latestUserInfo.CreateTime)
|
|
|
- if timeDiff <= 3*time.Second {
|
|
|
- return &TalentHttpResult{Code: 0, Msg: "success"}
|
|
|
- }
|
|
|
-
|
|
|
- // 超过时间差,返回失败
|
|
|
- return &TalentHttpResult{Code: -6, Msg: "Time difference exceeds 3 seconds"}
|
|
|
-}
|
|
|
+//func CheckDyNewAccount(r *ghttp.Request) *TalentHttpResult {
|
|
|
+// //check到了更新时间在两秒内的插入的数据。则说明已绑定。弹窗消失。
|
|
|
+// tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
|
|
|
+// redisDyBindStatusKey := fmt.Sprintf("Bind:%s", tid)
|
|
|
+// bindStatus, err := g.Redis().DoVar("GET", redisDyBindStatusKey)
|
|
|
+// if bindStatus.String() == "bound_to_other" {
|
|
|
+// return &TalentHttpResult{Code: -1, Msg: "此账号已被其他达人绑定", Data: nil}
|
|
|
+// }
|
|
|
+// if err != nil {
|
|
|
+// return &TalentHttpResult{Code: -1, Msg: "Get talent id failed"}
|
|
|
+// }
|
|
|
+// // 获取当前时间
|
|
|
+// currentTime := gtime.Now()
|
|
|
+// // 查询数据库中符合条件的所有记录
|
|
|
+// var userInfos []youngee_talent_model.KuaishouUserInfo
|
|
|
+// err = g.DB().Model("platform_kuaishou_user_info").
|
|
|
+// Where("talent_id = ?", tid).
|
|
|
+// Scan(&userInfos)
|
|
|
+// if err != nil {
|
|
|
+// return &TalentHttpResult{Code: -4, Msg: "Database query failed"}
|
|
|
+// }
|
|
|
+//
|
|
|
+// if len(userInfos) == 0 {
|
|
|
+// // 如果没有符合条件的数据,返回失败
|
|
|
+// return &TalentHttpResult{Code: -5, Msg: "No valid data found"}
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 找到 CreateTime 最新的记录
|
|
|
+// latestUserInfo := userInfos[0]
|
|
|
+// for _, info := range userInfos[1:] {
|
|
|
+// if info.CreateTime.After(latestUserInfo.CreateTime) {
|
|
|
+// latestUserInfo = info
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 计算创建时间与当前时间的差值
|
|
|
+// timeDiff := currentTime.Sub(latestUserInfo.CreateTime)
|
|
|
+// if timeDiff <= 3*time.Second {
|
|
|
+// return &TalentHttpResult{Code: 0, Msg: "success"}
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 超过时间差,返回失败
|
|
|
+// return &TalentHttpResult{Code: -6, Msg: "Time difference exceeds 3 seconds"}
|
|
|
+//}
|
|
|
|
|
|
// 用于轮询,检查数据库表中是否有达人对应的数据,有就删掉,/kuaishouath中会插入。
|
|
|
-func CheckKsNewAccount(r *ghttp.Request) *TalentHttpResult {
|
|
|
+func CheckNewAccount(r *ghttp.Request) *TalentHttpResult {
|
|
|
//check到了更新时间在两秒内的插入的数据。则说明已绑定。弹窗消失。
|
|
|
- tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
|
|
|
- redisKsBindStatusKey := fmt.Sprintf("KsBind:%s", tid)
|
|
|
- bindStatus, err := g.Redis().DoVar("GET", redisKsBindStatusKey)
|
|
|
- //if bindStatus == nil {
|
|
|
- //
|
|
|
- //}
|
|
|
- if bindStatus != nil && bindStatus.String() == "bound_to_other" {
|
|
|
- return &TalentHttpResult{Code: -1, Msg: "此快手账号已被其他达人绑定", Data: nil}
|
|
|
- }
|
|
|
- if err != nil {
|
|
|
- return &TalentHttpResult{Code: -3, Msg: "Get talent id failed"}
|
|
|
- }
|
|
|
+ tid, _ := utils.SessionTalentInfo.GetTalentIdFromSession(r)
|
|
|
+ platformId := r.GetQueryInt("platform_id", 0)
|
|
|
+ if platformId == 0 {
|
|
|
+ return &TalentHttpResult{Code: -1, Msg: "参数格式有误", Data: nil}
|
|
|
+ }
|
|
|
+ if platformId == 4 || platformId == 8 || platformId == 2 {
|
|
|
+ redisKsBindStatusKey := fmt.Sprintf("Bind:%s", tid)
|
|
|
+ bindStatus, err := g.Redis().DoVar("GET", redisKsBindStatusKey)
|
|
|
+ if bindStatus != nil && bindStatus.String() == "bound_to_other" {
|
|
|
+ return &TalentHttpResult{Code: -1, Msg: "此快手账号已被其他达人绑定", Data: nil}
|
|
|
+ }
|
|
|
+ if err != nil {
|
|
|
+ return &TalentHttpResult{Code: -3, Msg: "Get talent id failed"}
|
|
|
+ }
|
|
|
|
|
|
- // 获取当前时间
|
|
|
- currentTime := gtime.Now()
|
|
|
- // 查询数据库中符合条件的所有记录
|
|
|
- var userInfos []youngee_talent_model.KuaishouUserInfo
|
|
|
- err = g.DB().Model("platform_kuaishou_user_info").
|
|
|
- Where("talent_id = ?", tid).
|
|
|
- Scan(&userInfos)
|
|
|
- if err != nil {
|
|
|
- return &TalentHttpResult{Code: -4, Msg: "Database query failed"}
|
|
|
- }
|
|
|
+ // 获取当前时间
|
|
|
+ currentTime := gtime.Now()
|
|
|
+ // 查询数据库中符合条件的所有记录
|
|
|
+ var userInfos []youngee_talent_model.KuaishouUserInfo
|
|
|
+ err = g.DB().Model("platform_kuaishou_user_info").
|
|
|
+ Where("talent_id = ? AND platform_id=?", tid, platformId).
|
|
|
+ Scan(&userInfos)
|
|
|
+ if err != nil {
|
|
|
+ return &TalentHttpResult{Code: -4, Msg: "Database query failed"}
|
|
|
+ }
|
|
|
|
|
|
- //当前达人没有绑定过账号
|
|
|
- if len(userInfos) == 0 {
|
|
|
- // 如果没有符合条件的数据,返回失败
|
|
|
- // 如果没有符合条件的数据,返回继续轮询的提示
|
|
|
- return &TalentHttpResult{Code: -5, Msg: "没有数据,继续轮询"}
|
|
|
- }
|
|
|
+ //当前达人没有绑定过账号
|
|
|
+ if len(userInfos) == 0 {
|
|
|
+ // 如果没有符合条件的数据,返回失败
|
|
|
+ // 如果没有符合条件的数据,返回继续轮询的提示
|
|
|
+ return &TalentHttpResult{Code: -5, Msg: "没有数据,继续轮询"}
|
|
|
+ }
|
|
|
|
|
|
- // 找到 CreateTime 最新的记录
|
|
|
- latestUserInfo := userInfos[0]
|
|
|
- for _, info := range userInfos[1:] {
|
|
|
- if info.CreateTime.After(latestUserInfo.CreateTime) {
|
|
|
- latestUserInfo = info
|
|
|
+ // 找到 CreateTime 最新的记录
|
|
|
+ latestUserInfo := userInfos[0]
|
|
|
+ for _, info := range userInfos[1:] {
|
|
|
+ if info.CreateTime.After(latestUserInfo.CreateTime) {
|
|
|
+ latestUserInfo = info
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- // 计算创建时间与当前时间的差值
|
|
|
- timeDiff := currentTime.Sub(latestUserInfo.CreateTime)
|
|
|
- if timeDiff <= 3*time.Second {
|
|
|
- return &TalentHttpResult{Code: 0, Msg: "授权完成,数据已入库"}
|
|
|
+ // 计算创建时间与当前时间的差值
|
|
|
+ timeDiff := currentTime.Sub(latestUserInfo.CreateTime)
|
|
|
+ if timeDiff <= 5*time.Second {
|
|
|
+ return &TalentHttpResult{Code: 0, Msg: "授权完成,数据已入库"}
|
|
|
+ }
|
|
|
+
|
|
|
+ // 超过时间差,返回失败
|
|
|
+ return &TalentHttpResult{Code: -2, Msg: "数据还没进来,继续轮训"}
|
|
|
+
|
|
|
+ } else {
|
|
|
+ return &TalentHttpResult{Code: -2, Msg: "platform_id只能是2,4,8"}
|
|
|
}
|
|
|
|
|
|
- // 超过时间差,返回失败
|
|
|
- return &TalentHttpResult{Code: -2, Msg: "数据还没进来,继续轮训"}
|
|
|
}
|
|
|
|
|
|
func CheckTokenExp(openId string, platformId int, talentId string) int {
|
|
@@ -503,6 +542,7 @@ func AddWindowKuaishouList(r *ghttp.Request) *TalentHttpResult {
|
|
|
for _, record := range resInfo {
|
|
|
// 调用 CheckKuaishouTokenExp 函数,检查 openId 是否过期
|
|
|
expired := CheckKuaishouTokenExp(record.OpenId)
|
|
|
+ fmt.Println()
|
|
|
// 将过期检查结果赋值给 expired 属性 ,过期则不能被选中用于加入橱窗
|
|
|
record.Expired = expired
|
|
|
}
|
|
@@ -511,10 +551,9 @@ func AddWindowKuaishouList(r *ghttp.Request) *TalentHttpResult {
|
|
|
|
|
|
func SignUpSecKuaishouList(r *ghttp.Request) *TalentHttpResult {
|
|
|
selectionId := r.GetString("selection_id")
|
|
|
- //领样条件
|
|
|
- //todo
|
|
|
- var recruitStrategies []*youngee_talent_model.FreeStrategy
|
|
|
- err := g.DB().Model("free_strategy").Where("selection_id = ?", selectionId).Scan(&recruitStrategies)
|
|
|
+ //判断是否满足领样条件
|
|
|
+ var freeStrategies []*youngee_talent_model.FreeStrategy
|
|
|
+ err := g.DB().Model("free_strategy").Where("selection_id = ?", selectionId).Scan(&freeStrategies)
|
|
|
|
|
|
var results []*youngee_talent_model.KuaishouUserInfo
|
|
|
tid, _ := utils.SessionTalentInfo.GetTalentIdFromSession(r)
|
|
@@ -556,16 +595,28 @@ func SignUpSecKuaishouList(r *ghttp.Request) *TalentHttpResult {
|
|
|
// 将过期检查结果赋值给 expired 属性 ,过期则不能被选中用于加入橱窗
|
|
|
record.Expired = expired
|
|
|
//是否报名
|
|
|
- isSignResult, err := g.DB().Model("younggee_sec_task_info").Where("selection_id=? AND open_id=? talent_id=? sample_mode=?", selectionId, record.OpenId, tid, 1).One()
|
|
|
+ isSignResult, err := g.DB().Model("younggee_sec_task_info").Where("selection_id=? AND open_id=? AND talent_id=? AND sample_mode=?", selectionId, record.OpenId, tid, 1).One()
|
|
|
// 根据查询结果设置 IsSign 属性
|
|
|
if err != nil || isSignResult.IsEmpty() {
|
|
|
record.IsSign = 0 // 没有查到数据,设置为 0
|
|
|
} else {
|
|
|
record.IsSign = 1 // 查到数据,设置为 1
|
|
|
}
|
|
|
- //fanNum := record.Fan
|
|
|
- //saleNum30 := record.SaleNum30day
|
|
|
- //todo
|
|
|
+ //
|
|
|
+ fanNum := record.Fan
|
|
|
+ fmt.Println("我的粉丝数", fanNum)
|
|
|
+ saleNum30 := record.SaleNum30day
|
|
|
+ for _, strategy := range freeStrategies {
|
|
|
+ // 判断粉丝数是否满足当前策略的要求
|
|
|
+ if fanNum >= strategy.FollowersLow && saleNum30 >= strategy.SaleNum {
|
|
|
+ // 如果粉丝数满足策略的范围,设置 is_fit 为 1
|
|
|
+ record.IsOk = 1
|
|
|
+ break
|
|
|
+ } else {
|
|
|
+ // 否则设置 is_fit 为 0
|
|
|
+ record.IsOk = 0
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
return &TalentHttpResult{Code: 0, Msg: "列表展示成功", Data: resInfo}
|
|
@@ -580,29 +631,35 @@ func AddWindowWithKsAccount(r *ghttp.Request) *TalentHttpResult {
|
|
|
err := r.ParseForm(&signSecTaskReq)
|
|
|
|
|
|
if err != nil {
|
|
|
- fmt.Println("err--", err.Error())
|
|
|
return &TalentHttpResult{Code: -1, Msg: "POST参数解析失败", Data: err.Error()}
|
|
|
}
|
|
|
|
|
|
- value, err := g.DB().Model("platform_kuaishou_user_info").Fields("access_token").Where("talent_id=? AND open_id = ?", tid, signSecTaskReq.OpenId).Value()
|
|
|
+ value, err := g.DB().Model("platform_kuaishou_user_info").Fields("access_token").Where("talent_id=? AND open_id = ? AND platform_id = 4 ", tid, signSecTaskReq.OpenId).Value()
|
|
|
if err != nil {
|
|
|
fmt.Println("query db fail")
|
|
|
}
|
|
|
accessToken := value.String()
|
|
|
- pIdSlice := []string{signSecTaskReq.ProductId}
|
|
|
+ fmt.Println("access_token:--->", accessToken)
|
|
|
+ pIdSlice := []string{signSecTaskReq.KSProductId}
|
|
|
httpResult := youngee_sectask_service.IsCreateSecTask(r)
|
|
|
+ fmt.Println("httpResult--->", httpResult)
|
|
|
|
|
|
if httpResult.Code == 1 { //存在报名信息,仅添加橱窗
|
|
|
+ fmt.Println("大人id", tid)
|
|
|
+ fmt.Println("accessToken--->", accessToken)
|
|
|
_, err := merchant.AddItemsToShelf(ClientKey, SignSecret, accessToken, pIdSlice)
|
|
|
+
|
|
|
if err != nil {
|
|
|
//表示添加失敗,沒有開通
|
|
|
- return &TalentHttpResult{Code: -1, Msg: "快手未開通橱窗、商品不存在或已下线", Data: nil}
|
|
|
+ fmt.Println("假如橱窗错误信息为---", err.Error())
|
|
|
+ return &TalentHttpResult{Code: -1, Msg: "存在报名信息,快手未開通橱窗、商品不存在或已下线", Data: err.Error()}
|
|
|
}
|
|
|
// 查询成功,返回成功结果和数据
|
|
|
return &TalentHttpResult{Code: 0, Msg: "无需创建任务,加入橱窗成功", Data: nil}
|
|
|
} else { //不存在报名信息
|
|
|
//创建并插入报名信息
|
|
|
code := youngee_sectask_service.SignUpSecTaskFromWindow(r).Code
|
|
|
+ fmt.Println("滴滴答答啦放假啦", code)
|
|
|
if code != 0 {
|
|
|
return &TalentHttpResult{Code: -1, Msg: "创建报名信息失败", Data: nil}
|
|
|
}
|
|
@@ -610,7 +667,7 @@ func AddWindowWithKsAccount(r *ghttp.Request) *TalentHttpResult {
|
|
|
_, err := merchant.AddItemsToShelf(ClientKey, SignSecret, accessToken, pIdSlice)
|
|
|
if err != nil {
|
|
|
//表示添加失敗,沒有開通
|
|
|
- return &TalentHttpResult{Code: -1, Msg: "快手未開通橱窗、商品不存在或已下线", Data: nil}
|
|
|
+ return &TalentHttpResult{Code: -1, Msg: "不存在报名信息,快手未開通橱窗、商品不存在或已下线", Data: err.Error()}
|
|
|
}
|
|
|
//报名人数加一
|
|
|
_, err = g.DB().Model("younggee_selection_info").
|
|
@@ -903,14 +960,14 @@ func GetKuaishouList(r *ghttp.Request) *TalentHttpResult {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
// 构建 UserInfo 列表 参数分别表示初始长度和容量
|
|
|
userInfoList := make([]*youngee_talent_model.KuaishouUserInfo, 0, len(userInfoMap))
|
|
|
for _, info := range userInfoMap {
|
|
|
+ expStatus := CheckKuaishouTokenExp(info.OpenId)
|
|
|
+ info.Status = expStatus
|
|
|
//将 info 添加到 userInfoList 列表的末尾。
|
|
|
userInfoList = append(userInfoList, info)
|
|
|
}
|
|
|
-
|
|
|
// 计算不同的 open_id 的个数
|
|
|
uniqueOpenIDCount := len(openIDSet)
|
|
|
|
|
@@ -934,9 +991,9 @@ func CheckKuaishouTokenExp(openId string) int {
|
|
|
return -1
|
|
|
}
|
|
|
AccessToken := key.String()
|
|
|
- _, err = douyinUser.GetUserInfo(openId, AccessToken)
|
|
|
+ _, err = user.GetUserinfo(ClientKey, AccessToken)
|
|
|
if err != nil { //过期
|
|
|
- fmt.Println("平台accessToken过期")
|
|
|
+ fmt.Println("平台accessToken过期", err.Error())
|
|
|
return 1
|
|
|
} else {
|
|
|
fmt.Println("未过期")
|
|
@@ -966,23 +1023,20 @@ func GetDouyinList(r *ghttp.Request) *TalentHttpResult {
|
|
|
// 获取达人对应的平台为2的列表,含基本信息
|
|
|
tid, _ := utils.SessionTalentInfo.GetTalentIdFromSession(r)
|
|
|
userInfo := &youngee_talent_model.KuaishouUserInfo{}
|
|
|
- results, err := g.DB().Model(userInfo).Where("talent_id = ?", tid).
|
|
|
- Where("platform_Id IN (?)", 2).
|
|
|
+ results, err := g.DB().Model(userInfo).Where("talent_id = ? AND platform_Id = ? ", tid, 2).
|
|
|
All()
|
|
|
if err != nil {
|
|
|
fmt.Println("Error querying ", err)
|
|
|
return &TalentHttpResult{Code: 0, Msg: "查询抖音列表失败", Data: nil}
|
|
|
}
|
|
|
-
|
|
|
// 创建一个存储用户信息的切片
|
|
|
- updatedUserInfoList := []*youngee_talent_model.KuaishouUserInfo{}
|
|
|
+ UserInfoList := []*youngee_talent_model.KuaishouUserInfo{}
|
|
|
|
|
|
// 遍历列表,判断是否过期并更新 Expired 属性
|
|
|
for _, record := range results.List() {
|
|
|
openID := gconv.String(record["open_id"])
|
|
|
// 调用 CheckDouyinTokenExp 函数,获取过期状态 0/1
|
|
|
expiredStatus := CheckDouyinTokenExp(openID)
|
|
|
-
|
|
|
// 更新 record 中的 Expired 字段值
|
|
|
record["Expired"] = expiredStatus
|
|
|
err := gconv.Struct(record, userInfo)
|
|
@@ -991,17 +1045,31 @@ func GetDouyinList(r *ghttp.Request) *TalentHttpResult {
|
|
|
} // 将 map 转换为 KuaishouUserInfo 结构体
|
|
|
|
|
|
// 添加到用户信息列表
|
|
|
- updatedUserInfoList = append(updatedUserInfoList, userInfo)
|
|
|
-
|
|
|
- // 如果需要将更新后的 record 保存到数据库中,请取消注释以下代码
|
|
|
- // if _, err := g.DB().Model(userInfo).Where("open_id = ?", openID).Data(record).Update(); err != nil {
|
|
|
- // fmt.Println("Error updating Expired status: ", err)
|
|
|
- // }
|
|
|
+ UserInfoList = append(UserInfoList, userInfo)
|
|
|
}
|
|
|
// 返回 DYListResult 类型
|
|
|
return &TalentHttpResult{Code: 1, Msg: "返回抖音列表成功", Data: &youngee_talent_model.DYListResult{
|
|
|
- Count: len(updatedUserInfoList),
|
|
|
- UserInfo: updatedUserInfoList,
|
|
|
+ Count: len(UserInfoList),
|
|
|
+ UserInfo: UserInfoList,
|
|
|
+ }}
|
|
|
+}
|
|
|
+
|
|
|
+func GetMyAccountList(r *ghttp.Request) *TalentHttpResult {
|
|
|
+ // 获取达人对应的平台为2的列表,含基本信息
|
|
|
+ tid, _ := utils.SessionTalentInfo.GetTalentIdFromSession(r)
|
|
|
+ pId := r.GetQueryInt("platform_id", 0)
|
|
|
+ fmt.Println("PlatformId:", pId)
|
|
|
+ if pId != 1 && pId != 3 && pId != 5 {
|
|
|
+ return &TalentHttpResult{Code: -1, Msg: "platform_id无效"}
|
|
|
+ }
|
|
|
+ AccountList := []*youngee_talent_model.KuaishouUserInfo{}
|
|
|
+ g.Model("platform_kuaishou_user_info").Where("talent_id=? AND platform_id=? AND is_delete = ?", tid, pId, 0).Scan(&AccountList)
|
|
|
+ if len(AccountList) == 0 {
|
|
|
+ return &TalentHttpResult{Code: 0, Msg: "无内容", Data: nil}
|
|
|
+ }
|
|
|
+ return &TalentHttpResult{Code: 1, Msg: "返回列表成功", Data: &youngee_talent_model.AccountListResult{
|
|
|
+ Count: len(AccountList),
|
|
|
+ UserInfo: AccountList,
|
|
|
}}
|
|
|
}
|
|
|
|