common.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package common
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/tls"
  6. "errors"
  7. "git.listensoft.net/tool/jspkit/common/lxhttp"
  8. "git.listensoft.net/tool/jspkit/common/variable"
  9. "git.listensoft.net/tool/jspkit/logger"
  10. "git.listensoft.net/tool/jspkit/taxerr"
  11. "github.com/go-kratos/kratos/v2/log"
  12. "github.com/go-rod/rod"
  13. "github.com/google/uuid"
  14. "io"
  15. "mime/multipart"
  16. "net/http"
  17. "os"
  18. "strings"
  19. "time"
  20. )
  21. var ExcelServiceUrl = `http://47.105.103.181:4000/excel` // 新版解析excel服务
  22. const (
  23. ClickTimeOut = 5 * time.Second
  24. LoadTimeout = 20 * time.Second
  25. )
  26. // 手机号锁 time 锁定多长时间 传-1解锁
  27. func AddTelLockerX2(tel string, secend string) {
  28. c := lxhttp.NewHttpClient()
  29. bytes, _ := lxhttp.Get(c, variable.TaxTaskURL+"/api/v1/saveTelLock?tel="+tel+"&time="+secend)
  30. logger.Info("TelLock: " + string(bytes) + secend + "s")
  31. }
  32. func AddTelLockerX2Req(tel string, secend string, reqs string) {
  33. c := lxhttp.NewHttpClient()
  34. uri := variable.TaxTaskURL + "/api/v1/saveTelLock?tel=" + tel + "&time=" + secend
  35. if reqs != "" {
  36. uri += "&reqNos=" + reqs
  37. }
  38. bytes, _ := lxhttp.Get(c, uri)
  39. logger.Info(tel + " TelLock: " + string(bytes) + " " + secend + "s")
  40. }
  41. func HandleError(ctx context.Context, err error) error {
  42. if err == nil {
  43. return nil
  44. }
  45. uuid := ""
  46. _ = rod.Try(func() {
  47. if ctx != nil {
  48. uuid = ctx.Value(variable.UUID).(string)
  49. }
  50. })
  51. var usererr *taxerr.UserErr
  52. var taxerr2 *taxerr.SystemErr
  53. if errors.As(err, &taxerr2) {
  54. log.Info(uuid, " ", taxerr2.Error())
  55. if !strings.Contains(taxerr2.Error(), "[异常]:") {
  56. return errors.New("[异常]:" + taxerr2.Error())
  57. } else {
  58. return errors.New(taxerr2.Error())
  59. }
  60. } else if errors.As(err, &usererr) {
  61. log.Info(uuid, " ", usererr.Error())
  62. if !strings.Contains(usererr.Error(), "[错误]:") {
  63. return errors.New("[错误]:" + usererr.Error())
  64. } else {
  65. return errors.New(usererr.Error())
  66. }
  67. } else if err != nil {
  68. log.Error(uuid, " ", err.Error())
  69. return taxerr.NewWebStuckTitle(true)
  70. }
  71. return nil
  72. }
  73. // url 路径 filename 文件的上传参数
  74. func PostFile(url string, files map[string]string, ext map[string]string) ([]byte, error) {
  75. res := []byte{}
  76. //创建一个模拟的form中的一个选项,这个form项现在是空的
  77. bodyBuf := &bytes.Buffer{}
  78. bodyWriter := multipart.NewWriter(bodyBuf)
  79. for k, v := range files {
  80. //打开文件句柄操作
  81. file, err := os.Open(v)
  82. if err != nil {
  83. //zaplog.LoggerS.Info("error opening file")
  84. return res, err
  85. }
  86. defer file.Close()
  87. //相当于现在还没选择文件, form项里选择文件的选项
  88. fileWriter, err := bodyWriter.CreateFormFile(k, file.Name())
  89. if err != nil {
  90. //zaplog.LoggerS.Info("error writing to buffer")
  91. return res, err
  92. }
  93. //iocopy 这里相当于选择了文件,将文件放到form中
  94. _, err = io.Copy(fileWriter, file)
  95. if err != nil {
  96. return res, err
  97. }
  98. }
  99. //获取上传文件的类型,multipart/form-data; boundary=...
  100. contentType := bodyWriter.FormDataContentType()
  101. //上传的其他参数
  102. for key, val := range ext {
  103. _ = bodyWriter.WriteField(key, val)
  104. }
  105. //这个很关键,必须这样写关闭,不能使用defer关闭,不然会导致错误
  106. bodyWriter.Close()
  107. // 忽略证书
  108. c := http.Client{
  109. Transport: &http.Transport{
  110. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  111. },
  112. Timeout: time.Second * 60,
  113. }
  114. c.CloseIdleConnections()
  115. //发送post请求到服务端
  116. resp, err := c.Post(url, contentType, bodyBuf)
  117. if err != nil {
  118. return res, err
  119. }
  120. defer resp.Body.Close()
  121. return io.ReadAll(resp.Body)
  122. }
  123. func GetBaseUrl(p *rod.Page) string {
  124. URL := p.MustInfo().URL
  125. baseUrl := "https://" + URL[8:][:strings.Index(URL[8:], "/")]
  126. return baseUrl
  127. }
  128. func GetUUid() string {
  129. // 创建新的UUID
  130. newID := uuid.New().String()
  131. return strings.ReplaceAll(newID, "-", "")
  132. }
  133. // 拦截请求 for 代理
  134. func PageHijackReqForProxy(b *rod.Page, pattern string, ch chan []byte, c *http.Client) *rod.HijackRouter {
  135. router := b.HijackRequests()
  136. router.MustAdd(pattern, func(ctx *rod.Hijack) {
  137. rod.Try(func() {
  138. err := ctx.LoadResponse(c, true)
  139. if err != nil {
  140. return
  141. }
  142. ch <- []byte(ctx.Response.Body())
  143. time.Sleep(time.Second * 5)
  144. select {
  145. case <-ch:
  146. break
  147. }
  148. })
  149. })
  150. go router.Run()
  151. return router
  152. }
  153. // 拦截请求
  154. func BrowserHijackReq(b *rod.Browser, pattern string, ch chan []byte) *rod.HijackRouter {
  155. router := b.HijackRequests()
  156. router.MustAdd(pattern, func(ctx *rod.Hijack) {
  157. rod.Try(func() {
  158. ctx.MustLoadResponse()
  159. ch <- []byte(ctx.Response.Body())
  160. time.Sleep(time.Second * 5)
  161. select {
  162. case <-ch:
  163. break
  164. }
  165. })
  166. })
  167. go router.Run()
  168. return router
  169. }