base_from_hisugar.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package base_from_hisugar
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta/eta_data_analysis/models"
  6. "eta/eta_data_analysis/utils"
  7. "fmt"
  8. "github.com/chromedp/cdproto/network"
  9. "github.com/chromedp/chromedp"
  10. "io"
  11. "log"
  12. "net/http"
  13. "strings"
  14. "time"
  15. )
  16. // 泛糖科技
  17. func HisugarLogin() (cookie string, err error) {
  18. opts := append(
  19. chromedp.DefaultExecAllocatorOptions[:],
  20. chromedp.Flag("headless", false),
  21. )
  22. allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
  23. defer cancel()
  24. // 创建chrome实例
  25. ctx, cancel := chromedp.NewContext(
  26. allocCtx,
  27. chromedp.WithLogf(log.Printf),
  28. )
  29. defer cancel()
  30. err = chromedp.Run(ctx,
  31. chromedp.Navigate(`http://222.84.157.44:12880/tempo/login.jsp`),
  32. chromedp.WaitVisible(`.tempo-logon-form`, chromedp.ByQuery), // 等待登录表单可见
  33. chromedp.SetValue(`input[name="userName"]`, utils.HisugarAccount, chromedp.ByQuery),
  34. chromedp.SetValue(`input[name="password"]`, utils.HisugarPassword, chromedp.ByQuery),
  35. chromedp.Sleep(1*time.Second),
  36. chromedp.Click(`.login-button`, chromedp.ByQuery),
  37. chromedp.Sleep(5*time.Second),
  38. )
  39. var cookies []*network.Cookie
  40. // 运行任务以获取 cookies
  41. err = chromedp.Run(ctx,
  42. chromedp.Navigate(`http://222.84.157.44:12880/tempo/customportal/index.html#`),
  43. chromedp.ActionFunc(func(ctx context.Context) error {
  44. var err error
  45. cookies, err = network.GetCookies().Do(ctx)
  46. if err != nil {
  47. return err
  48. }
  49. for _, c := range cookies {
  50. cookie += fmt.Sprintf("%s=%s;", c.Name, c.Value)
  51. log.Printf("Name: %s, Value: %s, Domain: %s\n", c.Name, c.Value, c.Domain)
  52. fmt.Println(c)
  53. }
  54. return nil
  55. }),
  56. )
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. return
  61. }
  62. type HisugarResponseData struct {
  63. Userdata map[string]interface{} `json:"userdata"`
  64. Total int `json:"total"`
  65. Records int `json:"records"`
  66. DicData interface{} `json:"dicData"`
  67. Page int `json:"page"`
  68. Rows []Row1 `json:"rows"`
  69. _process interface{} `json:"_process"`
  70. SQLDataSetParams interface{} `json:"sqlDataSetParams"`
  71. }
  72. type Row struct {
  73. C1357941685__tablecol_ string `json:"C1357941685__tablecol_"`
  74. C1357941686__tablecol_ string `json:"C1357941686__tablecol_"`
  75. C1357941687__tablecol_ string `json:"C1357941687__tablecol_"`
  76. C1357941688__tablecol_ string `json:"C1357941688__tablecol_"`
  77. C1357941689__tablecol_ string `json:"C1357941689__tablecol_"`
  78. CC2034405405__tablecol_ string `json:"CC2034405405__tablecol_"`
  79. CC2034405406__tablecol_ string `json:"CC2034405406__tablecol_"`
  80. CC2034405407__tablecol_ string `json:"CC2034405407__tablecol_"`
  81. CC2034405408__tablecol_ string `json:"CC2034405408__tablecol_"`
  82. CC2034405409__tablecol_ string `json:"CC2034405409__tablecol_"`
  83. CC2034405410__tablecol_ string `json:"CC2034405410__tablecol_"`
  84. CC2034405411__tablecol_ string `json:"CC2034405411__tablecol_"`
  85. CC2034405412__tablecol_ string `json:"CC2034405412__tablecol_"`
  86. CC2034405413__tablecol_ string `json:"CC2034405413__tablecol_"`
  87. }
  88. type Process struct {
  89. Label string `json:"label"`
  90. ExecuteContent string `json:"executeContent"`
  91. BeforeExecuteTime int64 `json:"beforeExecuteTime"`
  92. AfterExecuteTime int64 `json:"afterExecuteTime"`
  93. InnerTimes map[string]int64 `json:"innerTimes"`
  94. ElapsedTime int64 `json:"elapsedTime"`
  95. }
  96. type Row1 struct {
  97. C1348804802__tablecol_ string `json:"C1348804802__tablecol_"`
  98. C1348804803__tablecol_ string `json:"C1348804803__tablecol_"`
  99. C1348804804__tablecol_ string `json:"C1348804804__tablecol_"`
  100. C1348804805__tablecol_ string `json:"C1348804805__tablecol_"`
  101. CC787774154__tablecol_ string `json:"CC787774154__tablecol_"`
  102. CC787774155__tablecol_ string `json:"CC787774155__tablecol_"`
  103. CC787774156__tablecol_ string `json:"CC787774156__tablecol_"`
  104. CC787774157__tablecol_ string `json:"CC787774157__tablecol_"`
  105. CC787774158__tablecol_ string `json:"CC787774158__tablecol_"`
  106. CC787774159__tablecol_ string `json:"CC787774159__tablecol_"`
  107. CC787774160__tablecol_ string `json:"CC787774160__tablecol_"`
  108. CC787774161__tablecol_ string `json:"CC787774161__tablecol_"`
  109. CC787774162__tablecol_ string `json:"CC787774162__tablecol_"`
  110. }
  111. type HisugarResponseDataSimple struct {
  112. Total int `json:"total"`
  113. Records int `json:"records"`
  114. Page int `json:"page"`
  115. Rows []map[string]string `json:"rows"`
  116. }
  117. func PostHandleHisugar(indexList []*models.BaseFromHisugarIndex) (err error) {
  118. params := make(map[string]interface{})
  119. params["List"] = indexList
  120. result, e := postEdbLib(params, utils.LIB_ROUTE_Hisugar_TABLE_HANDLE)
  121. if e != nil {
  122. b, _ := json.Marshal(params)
  123. fmt.Printf("postEdbLib err: %v, params: %s\n", e, string(b))
  124. utils.FileLog.Info(fmt.Sprintf("postEdbLib err: %v, params: %s", e, string(b)))
  125. return
  126. }
  127. resp := new(models.BaseEdbLibResponse)
  128. if e = json.Unmarshal(result, &resp); e != nil {
  129. fmt.Printf("json.Unmarshal err: %v\n", e)
  130. utils.FileLog.Info(fmt.Sprintf("json.Unmarshal err: %v", e))
  131. return
  132. }
  133. if resp.Ret != 200 {
  134. fmt.Printf("Msg: %s, ErrMsg: %s\n", resp.Msg, resp.ErrMsg)
  135. utils.FileLog.Info(fmt.Sprintf("Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg))
  136. return
  137. }
  138. return
  139. }
  140. // postEdbLib 调用指标接口
  141. func postEdbLib(param map[string]interface{}, method string) (result []byte, err error) {
  142. postUrl := utils.EDB_LIB_URL + method
  143. postData, err := json.Marshal(param)
  144. if err != nil {
  145. return
  146. }
  147. result, err = httpPost(postUrl, string(postData), "application/json")
  148. if err != nil {
  149. return
  150. }
  151. return
  152. }
  153. // httpPost HTTP请求
  154. func httpPost(url, postData string, params ...string) ([]byte, error) {
  155. fmt.Println("httpPost Url:" + url)
  156. body := io.NopCloser(strings.NewReader(postData))
  157. client := &http.Client{}
  158. req, err := http.NewRequest("POST", url, body)
  159. if err != nil {
  160. return nil, err
  161. }
  162. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  163. if len(params) > 0 && params[0] != "" {
  164. contentType = params[0]
  165. }
  166. req.Header.Set("Content-Type", contentType)
  167. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  168. resp, err := client.Do(req)
  169. if err != nil {
  170. fmt.Println("client.Do err:" + err.Error())
  171. return nil, err
  172. }
  173. defer func() {
  174. _ = resp.Body.Close()
  175. }()
  176. b, err := io.ReadAll(resp.Body)
  177. if err != nil {
  178. fmt.Println("httpPost:" + string(b))
  179. }
  180. return b, err
  181. }