common.go 7.9 KB

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