common.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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/models"
  9. "git.listensoft.net/tool/jspkit/common/variable"
  10. "git.listensoft.net/tool/jspkit/logger"
  11. "git.listensoft.net/tool/jspkit/taxerr"
  12. "github.com/go-kratos/kratos/v2/log"
  13. "github.com/go-rod/rod"
  14. "github.com/google/uuid"
  15. "go.uber.org/zap"
  16. "io"
  17. "mime/multipart"
  18. "net/http"
  19. "os"
  20. "strings"
  21. "time"
  22. )
  23. var ExcelServiceUrl = `http://47.105.103.181:4000/excel` // 新版解析excel服务
  24. var InfoCscsts bool = false
  25. var ComInfo = models.CompanyInfo{}
  26. const (
  27. ClickTimeOut = 5 * time.Second
  28. LoadTimeout = 20 * time.Second
  29. )
  30. // 手机号锁 time 锁定多长时间 传-1解锁
  31. func AddTelLockerX2(tel string, secend string) {
  32. c := lxhttp.NewHttpClient()
  33. bytes, _ := lxhttp.Get(c, variable.TaxTaskURL+"/api/v1/saveTelLock?tel="+tel+"&time="+secend)
  34. logger.Info("TelLock: " + string(bytes) + secend + "s")
  35. }
  36. func AddTelLockerX2Req(tel string, secend string, reqs string) {
  37. c := lxhttp.NewHttpClient()
  38. uri := variable.TaxTaskURL + "/api/v1/saveTelLock?tel=" + tel + "&time=" + secend
  39. if reqs != "" {
  40. uri += "&reqNos=" + reqs
  41. }
  42. bytes, _ := lxhttp.Get(c, uri)
  43. logger.Info(tel + " TelLock: " + string(bytes) + " " + secend + "s")
  44. }
  45. func HandleError(ctx context.Context, err error) error {
  46. if err == nil {
  47. return nil
  48. }
  49. uuid := ""
  50. _ = rod.Try(func() {
  51. if ctx != nil {
  52. uuid = ctx.Value(variable.UUID).(string)
  53. }
  54. })
  55. var usererr *taxerr.UserErr
  56. var taxerr2 *taxerr.SystemErr
  57. if errors.As(err, &taxerr2) {
  58. logger.Info(uuid, " ", taxerr2.Error())
  59. if !strings.Contains(taxerr2.Error(), "[异常]:") {
  60. return errors.New("[异常]:" + taxerr2.Error())
  61. } else {
  62. return errors.New(taxerr2.Error())
  63. }
  64. } else if errors.As(err, &usererr) {
  65. logger.Info(uuid, " ", usererr.Error())
  66. if !strings.Contains(usererr.Error(), "[错误]:") {
  67. return errors.New("[错误]:" + usererr.Error())
  68. } else {
  69. return errors.New(usererr.Error())
  70. }
  71. } else if err != nil {
  72. log.Error(uuid, " ", err.Error())
  73. return taxerr.NewWebStuckTitle(true)
  74. }
  75. return nil
  76. }
  77. // url 路径 filename 文件的上传参数
  78. func PostFile(url string, files map[string]string, ext map[string]string) ([]byte, error) {
  79. res := []byte{}
  80. //创建一个模拟的form中的一个选项,这个form项现在是空的
  81. bodyBuf := &bytes.Buffer{}
  82. bodyWriter := multipart.NewWriter(bodyBuf)
  83. for k, v := range files {
  84. //打开文件句柄操作
  85. file, err := os.Open(v)
  86. if err != nil {
  87. //zaplog.LoggerS.Info("error opening file")
  88. return res, err
  89. }
  90. defer file.Close()
  91. //相当于现在还没选择文件, form项里选择文件的选项
  92. fileWriter, err := bodyWriter.CreateFormFile(k, file.Name())
  93. if err != nil {
  94. //zaplog.LoggerS.Info("error writing to buffer")
  95. return res, err
  96. }
  97. //iocopy 这里相当于选择了文件,将文件放到form中
  98. _, err = io.Copy(fileWriter, file)
  99. if err != nil {
  100. return res, err
  101. }
  102. }
  103. //获取上传文件的类型,multipart/form-data; boundary=...
  104. contentType := bodyWriter.FormDataContentType()
  105. //上传的其他参数
  106. for key, val := range ext {
  107. _ = bodyWriter.WriteField(key, val)
  108. }
  109. //这个很关键,必须这样写关闭,不能使用defer关闭,不然会导致错误
  110. bodyWriter.Close()
  111. // 忽略证书
  112. c := http.Client{
  113. Transport: &http.Transport{
  114. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  115. },
  116. Timeout: time.Second * 60,
  117. }
  118. c.CloseIdleConnections()
  119. //发送post请求到服务端
  120. resp, err := c.Post(url, contentType, bodyBuf)
  121. if err != nil {
  122. return res, err
  123. }
  124. defer resp.Body.Close()
  125. return io.ReadAll(resp.Body)
  126. }
  127. func GetBaseUrl(p *rod.Page) string {
  128. URL := p.MustInfo().URL
  129. baseUrl := "https://" + URL[8:][:strings.Index(URL[8:], "/")]
  130. return baseUrl
  131. }
  132. func GetUUid() string {
  133. // 创建新的UUID
  134. newID := uuid.New().String()
  135. return strings.ReplaceAll(newID, "-", "")
  136. }
  137. // 拦截请求 for 代理
  138. func PageHijackReqForProxy(b *rod.Page, pattern string, ch chan []byte, c *http.Client) *rod.HijackRouter {
  139. router := b.HijackRequests()
  140. router.MustAdd(pattern, func(ctx *rod.Hijack) {
  141. rod.Try(func() {
  142. err := ctx.LoadResponse(c, true)
  143. if err != nil {
  144. return
  145. }
  146. ch <- []byte(ctx.Response.Body())
  147. time.Sleep(time.Second * 5)
  148. select {
  149. case <-ch:
  150. break
  151. }
  152. })
  153. })
  154. go router.Run()
  155. return router
  156. }
  157. // 拦截请求
  158. func BrowserHijackReq(b *rod.Browser, pattern string, ch chan []byte) *rod.HijackRouter {
  159. router := b.HijackRequests()
  160. router.MustAdd(pattern, func(ctx *rod.Hijack) {
  161. rod.Try(func() {
  162. ctx.MustLoadResponse()
  163. ch <- []byte(ctx.Response.Body())
  164. time.Sleep(time.Second * 5)
  165. select {
  166. case <-ch:
  167. break
  168. }
  169. })
  170. })
  171. go router.Run()
  172. return router
  173. }
  174. // DeleteTpassCookie 失效当前cookie
  175. func DeleteTpassCookie(info models.CompanyInfo) {
  176. //江西新版登录和旧版的tpass,分开来存储,防止新版登录的公司拿到旧版登录的tapss
  177. tsessionKey := info.Tel
  178. if !strings.Contains(tsessionKey, "_"+info.Area) {
  179. tsessionKey += "_" + info.Area
  180. }
  181. if strings.Contains(info.Dlfs, "代理") {
  182. tsessionKey += "Agent"
  183. }
  184. if (info.Area == "jiangxi" || info.Area == "heilongjiang" || info.Area == "guizhou" || info.Area == "chongqing") && info.Dlfs != "新版登录" {
  185. tsessionKey += "Other"
  186. }
  187. tsessionKey += "_tpass"
  188. client := lxhttp.NewHttpClient()
  189. body := map[string]string{
  190. "tsessionKey": tsessionKey,
  191. "pwd": info.Zzrmm,
  192. }
  193. logger.Info("req", zap.Any("body", body))
  194. bys, err := lxhttp.POSTJson(client, variable.SessionKeepURL+"/api/v1/session/TSession/delete", body, map[string]string{})
  195. if err != nil {
  196. logger.Error(err.Error())
  197. }
  198. logger.Info("删除key:" + tsessionKey + "结果: " + string(bys))
  199. }
  200. type SessionInfo struct {
  201. URL string //页面URL API接口地址
  202. Selector string //选择器 API返回结果路径
  203. SelectorValue string //每个选择器对应的值 API对应的企业值
  204. Cookies string //cookies
  205. Area variable.Area
  206. Api bool
  207. ApiURL string
  208. ApiMethod string
  209. ApiParam string
  210. ApiHeader map[string]string
  211. Valid int //新版登录的cookie是否可用,仅获取时赋值
  212. }