common.go 8.5 KB

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