123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- // Package fenwei
- // @Author gmy 2024/8/20 14:47:00
- package fenwei
- import (
- "bytes"
- "context"
- "encoding/json"
- "eta/eta_data_analysis/models"
- "eta/eta_data_analysis/services"
- "eta/eta_data_analysis/utils"
- "fmt"
- "github.com/chromedp/cdproto/network"
- "github.com/chromedp/chromedp"
- "io"
- "log"
- "net/http"
- "sync"
- "time"
- )
- type DataProcessor interface {
- FetchAndProcess() error
- GenerateRequestParams(string) map[string]string
- }
- type BaseProcessor struct {
- URL string
- }
- var (
- authorization string
- authLock sync.RWMutex
- )
- func (p *BaseProcessor) FetchAndProcess() error {
- // 获取当前时间 yyyy-MM-dd
- now := time.Now()
- currentTime := now.Format(utils.FormatDateUnSpace)
- // 请求参数
- params := p.GenerateRequestParams(currentTime)
- req, err := http.NewRequest("POST", p.URL, bytes.NewBufferString(params["params"]))
- if err != nil {
- return err
- }
- // 设置请求头
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
- authLock.RLock()
- req.Header.Set("Authorization", authorization)
- authLock.RUnlock()
- 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
- }
- if checkResp(string(body)) {
- authLock.Lock()
- // 登录获取Authorization
- authorization, err = getAuthorizationByChrome()
- if err != nil {
- authLock.Unlock()
- return err
- }
- authLock.Unlock()
- // 重新请求
- req.Header.Set("Authorization", authorization)
- resp, err = client.Do(req)
- if err != nil {
- return err
- }
- }
- // 数据处理
- response, err := p.ProcessResponse(string(body))
- if err != nil {
- return err
- }
- utils.FileLog.Info(fmt.Sprintf("response: %v", response))
- // 请求lib应用入库
- paramsLib := make(map[string]interface{})
- paramsLib["List"] = response
- paramsLib["TerminalCode"] = utils.TerminalCode
- services.PostEdbLib(paramsLib, utils.LIB_ROUTE_FENWEI_NET_DATA_HANDLE)
- return nil
- }
- // resp响应参数检测 code or message 判断是否需要重新登录
- func checkResp(resp string) bool {
- if resp == "" {
- return true
- }
- var responseObj models.Response
- err := json.Unmarshal([]byte(resp), &responseObj)
- if err != nil {
- return false
- }
- if responseObj.Code != 200 || responseObj.Message != "成功!" {
- return true
- }
- return false
- }
- // GenerateRequestParams 让子类来实现这个方法
- func (p *BaseProcessor) GenerateRequestParams(string) map[string]string {
- return map[string]string{}
- }
- func (p *BaseProcessor) ProcessResponse(data string) ([]models.FenWeiNetIndexInfo, error) {
- return nil, nil
- }
- // GetAuthorizationByChrome 获取Authorization
- func getAuthorizationByChrome() (authorization string, err error) {
- // 读取Cookie
- if utils.FenweiNetUseName == "" {
- err = fmt.Errorf("汾渭账号未设置")
- return
- }
- if utils.FenweiNetPassword == "" {
- err = fmt.Errorf("汾渭密码未设置")
- return
- }
- opts := append(
- chromedp.DefaultExecAllocatorOptions[:],
- chromedp.Flag("headless", false),
- )
- allocCtx, cancel1 := chromedp.NewExecAllocator(context.Background(), opts...)
- defer cancel1()
- // 创建chrome实例
- ctx, cancel2 := chromedp.NewContext(
- allocCtx,
- chromedp.WithLogf(log.Printf),
- )
- defer cancel2()
- err = chromedp.Run(ctx,
- chromedp.Navigate(`https://www.sxcoal.com/`),
- chromedp.Click(`.pc_content__jO_mq`, chromedp.ByQuery),
- chromedp.Sleep(2*time.Second),
- chromedp.SetValue(`div.Sign_username__7eYwE input[type="text"]`, utils.FenweiNetUseName, chromedp.ByQuery),
- chromedp.SetValue(`div.Sign_password__dwxMn input[type="password"]`, utils.FenweiNetPassword, chromedp.ByQuery),
- chromedp.Sleep(2*time.Second),
- // 登录
- chromedp.Click(`.Button_text__D25sy`, chromedp.ByQuery),
- chromedp.ActionFunc(func(ctx context.Context) error {
- /*cookies, err := network.GetCookies().Do(ctx)
- if err != nil {
- return err
- }
- for _, v := range cookies {
- cookieStr = cookieStr + v.Name + "=" + v.Value + ";"
- }
- utils.FileLog.Info("header cookie:" + cookieStr)*/
- // 监听并处理请求事件
- chromedp.ListenTarget(ctx, func(ev interface{}) {
- if ev, ok := ev.(*network.EventRequestWillBeSent); ok {
- for _, header := range ev.Request.Headers {
- if authHeader, ok := header.(string); ok && authHeader == "Authorization" {
- authorization = authHeader
- utils.FileLog.Info("Authorization header found: " + authorization)
- }
- }
- }
- })
- return nil
- }),
- )
- return
- }
|