Refresh QRCODE

This commit is contained in:
807875765@qq.com
2022-04-18 18:25:39 +08:00
parent ec3265d38c
commit 8a17066463
2 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
package jdslogin
import (
"fmt"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/jx-callback/business/model/dao"
"github.com/tebeka/selenium"
"github.com/tebeka/selenium/chrome"
)
// RefreshQRCode Creat By 2022/04/18
// RefreshQRCode Author By Hang xu
// RefreshQRCode 用于爬取京东的二维码链接 并持久化进入数据库中
func RefreshQRCode() (retval string, err error) {
baseapi.SugarLogger.Debug("BEGIN JD SHOP QRCODE SPIDER---------------------------------------------------")
const (
seleniumPath = `/usr/bin/chromedriver`
port = 9515
)
opts := []selenium.ServiceOption{}
selenium.SetDebug(true)
service, err := selenium.NewChromeDriverService(seleniumPath, port, opts...)
if nil != err {
fmt.Println("start a chromedriver service falid", err.Error())
return
}
////server关闭之后chrome窗口也会关闭
defer service.Stop()
//链接本地的浏览器 chrome
caps := selenium.Capabilities{
//"browserName": "/Applications/Google Chrome Dev.app/Contents/MacOS/Google Chrome Dev",
"browserName": "/opt/google/chrome",
}
//禁止图片加载,加快渲染速度
imagCaps := map[string]interface{}{
"profile.managed_default_content_settings.images": 2,
}
chromeCaps := chrome.Capabilities{
Prefs: imagCaps,
Path: "/opt/google/chrome",
Args: []string{
//静默执行请求
"--headless", // 设置Chrome无头模式在linux下运行需要设置这个参数否则会报错
"--no-sandbox",
"--user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36", // 模拟user-agent防反爬
"--Host=porder.shop.jd.com",
"--Connection=keep-alive",
},
}
//以上是设置浏览器参数
caps.AddChrome(chromeCaps)
url := "https://porder.shop.jd.com/order/orderlist/allOrders"
w_b1, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
if err != nil {
fmt.Println("connect to the webDriver faild", err.Error())
return
}
err = w_b1.Get(url)
pageSource, err := w_b1.PageSource()
baseapi.SugarLogger.Debug("pageSource", pageSource)
qr, err := w_b1.FindElement("id", "js-login-qrcode")
qrStr, err := qr.GetAttribute("src")
baseapi.SugarLogger.Debug("qrStr:", qrStr) //当前qrcode sample:https://passport.shop.jd.com/login/json/qrcode_show.action?_timeStr=165026591404449b823cc58684d948d0e0ca8351adf64
// 进行存库操作
// save to database 存入 new_config表中
db := dao.GetDB()
txDB, _ := dao.Begin(db)
defer func() {
if r := recover(); r != nil {
dao.Rollback(db, txDB)
panic(r)
}
}()
configList, err := dao.QueryConfigs(db, "jdsQRcode", "Cookie", "")
if err != nil {
dao.Rollback(db, txDB)
return "", err
}
if _, err = dao.UpdateEntityLogically(db, configList[0], map[string]interface{}{
"Value": qrStr,
}, "", nil); err != nil {
dao.Rollback(db, txDB)
return "", err
}
baseapi.SugarLogger.Debug("THE JD SHOP QRCODE SPIDER THE END---------------------------------------------")
return "", nil
}

View File

@@ -2,6 +2,7 @@ package misc
import (
"fmt"
"git.rosy.net.cn/jx-callback/business/jxcallback/scheduler/jdslogin"
"git.rosy.net.cn/jx-callback/business/partner/delivery"
"sync"
"time"
@@ -351,6 +352,9 @@ func Init() {
/* ScheduleTimerFuncByInterval(func() {
jcq.JCQSchedule()
}, 10*time.Second, 10*time.Second)*/
ScheduleTimerFuncByInterval(func() {
jdslogin.RefreshQRCode()
}, 10*time.Second, 10*time.Minute)
ScheduleTimerFunc("AutoSaleStoreSku", func() {
cms.AutoSaleStoreSku(jxcontext.AdminCtx, nil, false)
}, autoSaleStoreSkuTimeList)