utils.go 3.3 KB

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