package base_from_hisugar import ( "context" "encoding/json" "eta/eta_data_analysis/models" "eta/eta_data_analysis/utils" "fmt" "github.com/chromedp/cdproto/network" "github.com/chromedp/chromedp" "io" "log" "net/http" "strings" "time" ) // 泛糖科技 func HisugarLogin() (cookie string, err error) { opts := append( chromedp.DefaultExecAllocatorOptions[:], chromedp.Flag("headless", false), ) allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...) defer cancel() // 创建chrome实例 ctx, cancel := chromedp.NewContext( allocCtx, chromedp.WithLogf(log.Printf), ) defer cancel() err = chromedp.Run(ctx, chromedp.Navigate(`http://222.84.157.44:12880/tempo/login.jsp`), chromedp.WaitVisible(`.tempo-logon-form`, chromedp.ByQuery), // 等待登录表单可见 chromedp.SetValue(`input[name="userName"]`, utils.HisugarAccount, chromedp.ByQuery), chromedp.SetValue(`input[name="password"]`, utils.HisugarPassword, chromedp.ByQuery), chromedp.Sleep(1*time.Second), chromedp.Click(`.login-button`, chromedp.ByQuery), chromedp.Sleep(5*time.Second), ) var cookies []*network.Cookie // 运行任务以获取 cookies err = chromedp.Run(ctx, chromedp.Navigate(`http://222.84.157.44:12880/tempo/customportal/index.html#`), chromedp.ActionFunc(func(ctx context.Context) error { var err error cookies, err = network.GetCookies().Do(ctx) if err != nil { return err } for _, c := range cookies { cookie += fmt.Sprintf("%s=%s;", c.Name, c.Value) log.Printf("Name: %s, Value: %s, Domain: %s\n", c.Name, c.Value, c.Domain) fmt.Println(c) } return nil }), ) if err != nil { log.Fatal(err) } return } type HisugarResponseData struct { Userdata map[string]interface{} `json:"userdata"` Total int `json:"total"` Records int `json:"records"` DicData interface{} `json:"dicData"` Page int `json:"page"` Rows []Row1 `json:"rows"` _process interface{} `json:"_process"` SQLDataSetParams interface{} `json:"sqlDataSetParams"` } type Row struct { C1357941685__tablecol_ string `json:"C1357941685__tablecol_"` C1357941686__tablecol_ string `json:"C1357941686__tablecol_"` C1357941687__tablecol_ string `json:"C1357941687__tablecol_"` C1357941688__tablecol_ string `json:"C1357941688__tablecol_"` C1357941689__tablecol_ string `json:"C1357941689__tablecol_"` CC2034405405__tablecol_ string `json:"CC2034405405__tablecol_"` CC2034405406__tablecol_ string `json:"CC2034405406__tablecol_"` CC2034405407__tablecol_ string `json:"CC2034405407__tablecol_"` CC2034405408__tablecol_ string `json:"CC2034405408__tablecol_"` CC2034405409__tablecol_ string `json:"CC2034405409__tablecol_"` CC2034405410__tablecol_ string `json:"CC2034405410__tablecol_"` CC2034405411__tablecol_ string `json:"CC2034405411__tablecol_"` CC2034405412__tablecol_ string `json:"CC2034405412__tablecol_"` CC2034405413__tablecol_ string `json:"CC2034405413__tablecol_"` } type Process struct { Label string `json:"label"` ExecuteContent string `json:"executeContent"` BeforeExecuteTime int64 `json:"beforeExecuteTime"` AfterExecuteTime int64 `json:"afterExecuteTime"` InnerTimes map[string]int64 `json:"innerTimes"` ElapsedTime int64 `json:"elapsedTime"` } type Row1 struct { C1348804802__tablecol_ string `json:"C1348804802__tablecol_"` C1348804803__tablecol_ string `json:"C1348804803__tablecol_"` C1348804804__tablecol_ string `json:"C1348804804__tablecol_"` C1348804805__tablecol_ string `json:"C1348804805__tablecol_"` CC787774154__tablecol_ string `json:"CC787774154__tablecol_"` CC787774155__tablecol_ string `json:"CC787774155__tablecol_"` CC787774156__tablecol_ string `json:"CC787774156__tablecol_"` CC787774157__tablecol_ string `json:"CC787774157__tablecol_"` CC787774158__tablecol_ string `json:"CC787774158__tablecol_"` CC787774159__tablecol_ string `json:"CC787774159__tablecol_"` CC787774160__tablecol_ string `json:"CC787774160__tablecol_"` CC787774161__tablecol_ string `json:"CC787774161__tablecol_"` CC787774162__tablecol_ string `json:"CC787774162__tablecol_"` } type HisugarResponseDataSimple struct { Total int `json:"total"` Records int `json:"records"` Page int `json:"page"` Rows []map[string]string `json:"rows"` } func PostHandleHisugar(indexList []*models.BaseFromHisugarIndex) (err error) { params := make(map[string]interface{}) params["List"] = indexList result, e := postEdbLib(params, utils.LIB_ROUTE_Hisugar_TABLE_HANDLE) if e != nil { b, _ := json.Marshal(params) fmt.Printf("postEdbLib err: %v, params: %s\n", e, string(b)) utils.FileLog.Info(fmt.Sprintf("postEdbLib err: %v, params: %s", e, string(b))) return } resp := new(models.BaseEdbLibResponse) if e = json.Unmarshal(result, &resp); e != nil { fmt.Printf("json.Unmarshal err: %v\n", e) utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %v", e)) return } if resp.Ret != 200 { fmt.Printf("Msg: %s, ErrMsg: %s\n", resp.Msg, resp.ErrMsg) utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg)) return } return } // postEdbLib 调用指标接口 func postEdbLib(param map[string]interface{}, method string) (result []byte, err error) { postUrl := utils.EDB_LIB_URL + method postData, err := json.Marshal(param) if err != nil { return } result, err = httpPost(postUrl, string(postData), "application/json") if err != nil { return } return } // httpPost HTTP请求 func httpPost(url, postData string, params ...string) ([]byte, error) { fmt.Println("httpPost Url:" + url) body := io.NopCloser(strings.NewReader(postData)) client := &http.Client{} req, err := http.NewRequest("POST", url, body) if err != nil { return nil, err } contentType := "application/x-www-form-urlencoded;charset=utf-8" if len(params) > 0 && params[0] != "" { contentType = params[0] } req.Header.Set("Content-Type", contentType) req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY)) resp, err := client.Do(req) if err != nil { fmt.Println("client.Do err:" + err.Error()) return nil, err } defer func() { _ = resp.Body.Close() }() b, err := io.ReadAll(resp.Body) if err != nil { fmt.Println("httpPost:" + string(b)) } return b, err }