common.go 7.8 KB

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