utils.go 4.4 KB

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