utils.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package common
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "fmt"
  6. "git.listensoft.net/tool/jspkit/common/lxhttp"
  7. "git.listensoft.net/tool/jspkit/common/variable"
  8. "git.listensoft.net/tool/jspkit/taxerr"
  9. "github.com/go-kratos/kratos/v2/log"
  10. "github.com/tidwall/gjson"
  11. "io"
  12. "net/url"
  13. "os"
  14. "regexp"
  15. "strings"
  16. )
  17. type LvName string
  18. const (
  19. CheckSuccess LvName = "申报成功" // 检查成功 ------ 检查完全通过
  20. CheckFail LvName = "检查失败" // 检查任务失败 ----- 网页打开失败等
  21. CheckOmit LvName = "有遗漏" // 检查有遗漏 ----- 未申报问题
  22. CheckAbnormal LvName = "有异常" //检查有异常 ----- 税款问题
  23. )
  24. func GeneratePath(suffix string) string {
  25. ps := os.PathSeparator
  26. //检查路径
  27. if !PathExists("captcha" + string(ps)) {
  28. _ = os.MkdirAll("captcha"+string(ps), os.ModePerm)
  29. }
  30. return "captcha" + string(ps) + GetTimestamp(13) + "." + suffix
  31. }
  32. func PathExists(path string) bool {
  33. _, err := os.Stat(path)
  34. if err == nil {
  35. return true
  36. }
  37. if os.IsNotExist(err) {
  38. return false
  39. }
  40. return false
  41. }
  42. // 返回一个32位md5加密后的字符串
  43. func Md5(data string) string {
  44. h := md5.New()
  45. h.Write([]byte(data))
  46. return hex.EncodeToString(h.Sum(nil))
  47. }
  48. /*
  49. 生成检查截图本地路径
  50. lv 图片对应的错误等级 用上面定义好的 Omit>Abnormal
  51. taxNo 税号
  52. period 账期
  53. location 截图位置,例如申报清册、申报结果查询、税款缴纳、缴费缴纳查询
  54. */
  55. func GenerateCheckImagePath(lv LvName, taxNo, period, location string) string {
  56. var path string
  57. path = "./data/check/" + taxNo + "/"
  58. if !PathExists(path) {
  59. os.MkdirAll(path, os.ModePerm)
  60. }
  61. return fmt.Sprintf(`%s-%s-%s-新版_%s_%s.png`, path+taxNo, period, GetTimestamp(13), location, lv)
  62. }
  63. func PostSbjt(taxno, period, path string) string {
  64. var params = map[string]string{
  65. "tax_no": taxno,
  66. "period": period,
  67. "type": "etax", //电子税务局
  68. }
  69. str, err := PostFile("https://file.listensoft.net/api/v1/uploadsbjt", map[string]string{
  70. "file": path,
  71. }, params)
  72. if err != nil {
  73. log.Warnf("【%s】文件上传失败", path)
  74. }
  75. return gjson.GetBytes(str, "path").String()
  76. }
  77. // 获取申报截图的path
  78. // taxNo 纳税人识别号 period 账期 taxType 任务名称
  79. func GetSbjtPath(taxNo, period string, taxType variable.TaskName) string {
  80. var path string
  81. path = "./data/" + taxNo + "/"
  82. if !PathExists(path) {
  83. os.MkdirAll(path, os.ModePerm)
  84. }
  85. path = path + period + string(taxType) + "_" + GetTimestamp(13) + ".png"
  86. return path
  87. }
  88. // 判断字符串是否全是英文
  89. func IsAllEnglish(s string) bool {
  90. // 检查字符串是否全部由ASCII字符组成
  91. for _, c := range s {
  92. if c > 127 {
  93. return false
  94. }
  95. }
  96. return true
  97. }
  98. // CreateFormReader 将 map 转换成 header
  99. func CreateFormReader(data map[string]string) io.Reader {
  100. form := url.Values{}
  101. for k, v := range data {
  102. form.Add(k, v)
  103. }
  104. return strings.NewReader(form.Encode())
  105. }
  106. func Unbracket(str string) string {
  107. str = strings.ReplaceAll(str, "(", "(")
  108. str = strings.ReplaceAll(str, ")", ")")
  109. str = strings.ReplaceAll(str, " ", "")
  110. return str
  111. }
  112. // 检查是否是手机号
  113. func CheckMobile(mobile string) bool {
  114. r := regexp.MustCompile(`^1[0-9]{10}$`)
  115. return r.Match([]byte(mobile))
  116. }
  117. func NewWebStuckTitle() *taxerr.SystemErr {
  118. return taxerr.NewSystemV3("电子税局页面卡顿", "系统将于30分钟后重试(可在\"通用设置\"关闭)")
  119. }
  120. // 查看设备是否在线
  121. func CheckEqmLive(tel string) bool {
  122. client := lxhttp.NewHttpClient()
  123. defer client.CloseIdleConnections()
  124. bys, err := lxhttp.POSTJson(client, "https://keep.jsptax.com"+"/api/v1/telDeviceStatus", map[string]string{"mobiles": tel}, map[string]string{})
  125. if err != nil {
  126. return false
  127. }
  128. if gjson.GetBytes(bys, `data.status`).Bool() {
  129. return true
  130. }
  131. return false
  132. }