common.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. func HandleErrorBank(ctx context.Context, err error) error {
  78. if err == nil {
  79. return nil
  80. }
  81. uuid := GetUUid()
  82. if ctx != nil {
  83. uuid = ctx.Value(variable.UUID).(string)
  84. }
  85. var usererr *taxerr.UserErr
  86. var taxerr *taxerr.SystemErr
  87. if errors.As(err, &taxerr) {
  88. logger.Info(uuid, " ", taxerr.Error())
  89. if !strings.Contains(taxerr.Error(), "[异常]:") {
  90. return errors.New("[异常]:" + taxerr.Error())
  91. } else {
  92. return errors.New(taxerr.Error())
  93. }
  94. } else if errors.As(err, &usererr) {
  95. logger.Info(uuid, " ", usererr.Error())
  96. if !strings.Contains(usererr.Error(), "[错误]:") {
  97. return errors.New("[错误]:" + usererr.Error())
  98. } else {
  99. return errors.New(usererr.Error())
  100. }
  101. } else if err != nil {
  102. logger.Error(uuid, " ", err.Error())
  103. return BankStuckTitle()
  104. }
  105. return nil
  106. }
  107. // 银行卡顿统一提示
  108. func BankStuckTitle() *taxerr.SystemErr {
  109. if ComInfo.Cscsts {
  110. return taxerr.NewSystemV3("银行页面卡顿", "系统将于30分钟后重试(可在\"通用设置\"关闭)")
  111. } else {
  112. return taxerr.NewSystemV3("银行页面卡顿", "请稍后重试(可在\"通用设置\"配置自动重试)")
  113. }
  114. }
  115. // url 路径 filename 文件的上传参数
  116. func PostFile(url string, files map[string]string, ext map[string]string) ([]byte, error) {
  117. res := []byte{}
  118. //创建一个模拟的form中的一个选项,这个form项现在是空的
  119. bodyBuf := &bytes.Buffer{}
  120. bodyWriter := multipart.NewWriter(bodyBuf)
  121. for k, v := range files {
  122. //打开文件句柄操作
  123. file, err := os.Open(v)
  124. if err != nil {
  125. //zaplog.LoggerS.Info("error opening file")
  126. return res, err
  127. }
  128. defer file.Close()
  129. //相当于现在还没选择文件, form项里选择文件的选项
  130. fileWriter, err := bodyWriter.CreateFormFile(k, file.Name())
  131. if err != nil {
  132. //zaplog.LoggerS.Info("error writing to buffer")
  133. return res, err
  134. }
  135. //iocopy 这里相当于选择了文件,将文件放到form中
  136. _, err = io.Copy(fileWriter, file)
  137. if err != nil {
  138. return res, err
  139. }
  140. }
  141. //获取上传文件的类型,multipart/form-data; boundary=...
  142. contentType := bodyWriter.FormDataContentType()
  143. //上传的其他参数
  144. for key, val := range ext {
  145. _ = bodyWriter.WriteField(key, val)
  146. }
  147. //这个很关键,必须这样写关闭,不能使用defer关闭,不然会导致错误
  148. bodyWriter.Close()
  149. // 忽略证书
  150. c := http.Client{
  151. Transport: &http.Transport{
  152. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  153. },
  154. Timeout: time.Second * 60,
  155. }
  156. c.CloseIdleConnections()
  157. //发送post请求到服务端
  158. resp, err := c.Post(url, contentType, bodyBuf)
  159. if err != nil {
  160. return res, err
  161. }
  162. defer resp.Body.Close()
  163. return io.ReadAll(resp.Body)
  164. }
  165. func GetBaseUrl(p *rod.Page) string {
  166. URL := p.MustInfo().URL
  167. baseUrl := "https://" + URL[8:][:strings.Index(URL[8:], "/")]
  168. return baseUrl
  169. }
  170. func GetUUid() string {
  171. // 创建新的UUID
  172. newID := uuid.New().String()
  173. return strings.ReplaceAll(newID, "-", "")
  174. }
  175. // 拦截请求 for 代理
  176. func PageHijackReqForProxy(b *rod.Page, pattern string, ch chan []byte, c *http.Client) *rod.HijackRouter {
  177. router := b.HijackRequests()
  178. router.MustAdd(pattern, func(ctx *rod.Hijack) {
  179. rod.Try(func() {
  180. err := ctx.LoadResponse(c, true)
  181. if err != nil {
  182. return
  183. }
  184. ch <- []byte(ctx.Response.Body())
  185. time.Sleep(time.Second * 5)
  186. select {
  187. case <-ch:
  188. break
  189. }
  190. })
  191. })
  192. go router.Run()
  193. return router
  194. }
  195. // 拦截请求
  196. func BrowserHijackReq(b *rod.Browser, pattern string, ch chan []byte) *rod.HijackRouter {
  197. router := b.HijackRequests()
  198. router.MustAdd(pattern, func(ctx *rod.Hijack) {
  199. rod.Try(func() {
  200. ctx.MustLoadResponse()
  201. ch <- []byte(ctx.Response.Body())
  202. time.Sleep(time.Second * 5)
  203. select {
  204. case <-ch:
  205. break
  206. }
  207. })
  208. })
  209. go router.Run()
  210. return router
  211. }
  212. // DeleteTpassCookie 失效当前cookie
  213. func DeleteTpassCookie(info models.CompanyInfo) {
  214. //江西新版登录和旧版的tpass,分开来存储,防止新版登录的公司拿到旧版登录的tapss
  215. tsessionKey := info.Tel
  216. if !strings.Contains(tsessionKey, "_"+info.Area) {
  217. tsessionKey += "_" + info.Area
  218. }
  219. if strings.Contains(info.Dlfs, "代理") {
  220. tsessionKey += "Agent"
  221. }
  222. if (info.Area == "jiangxi" || info.Area == "heilongjiang" || info.Area == "guizhou" || info.Area == "chongqing") && info.Dlfs != "新版登录" {
  223. tsessionKey += "Other"
  224. }
  225. tsessionKey += "_tpass"
  226. client := lxhttp.NewHttpClient()
  227. body := map[string]string{
  228. "tsessionKey": tsessionKey,
  229. "pwd": info.Zzrmm,
  230. }
  231. logger.Info("req", zap.Any("body", body))
  232. bys, err := lxhttp.POSTJson(client, variable.SessionKeepURL+"/api/v1/session/TSession/delete", body, map[string]string{})
  233. if err != nil {
  234. logger.Error(err.Error())
  235. }
  236. logger.Info("删除key:" + tsessionKey + "结果: " + string(bys))
  237. }
  238. type SessionInfo struct {
  239. URL string //页面URL API接口地址
  240. Selector string //选择器 API返回结果路径
  241. SelectorValue string //每个选择器对应的值 API对应的企业值
  242. Cookies string //cookies
  243. Area variable.Area
  244. Api bool
  245. ApiURL string
  246. ApiMethod string
  247. ApiParam string
  248. ApiHeader map[string]string
  249. Valid int //新版登录的cookie是否可用,仅获取时赋值
  250. }