Browse Source

补充银行方法

1 2 weeks ago
parent
commit
8a202077ff
5 changed files with 198 additions and 0 deletions
  1. 40 0
      common/common.go
  2. 14 0
      common/file.go
  3. 17 0
      common/period.go
  4. 76 0
      common/rod_utils.go
  5. 51 0
      common/utils.go

+ 40 - 0
common/common.go

@@ -81,6 +81,46 @@ func HandleError(ctx context.Context, err error) error {
 	return nil
 }
 
+func HandleErrorBank(ctx context.Context, err error) error {
+	if err == nil {
+		return nil
+	}
+	uuid := GetUUid()
+	if ctx != nil {
+		uuid = ctx.Value(variable.UUID).(string)
+	}
+	var usererr *taxerr.UserErr
+	var taxerr *taxerr.SystemErr
+	if errors.As(err, &taxerr) {
+		logger.Info(uuid, " ", taxerr.Error())
+		if !strings.Contains(taxerr.Error(), "[异常]:") {
+			return errors.New("[异常]:" + taxerr.Error())
+		} else {
+			return errors.New(taxerr.Error())
+		}
+	} else if errors.As(err, &usererr) {
+		logger.Info(uuid, " ", usererr.Error())
+		if !strings.Contains(usererr.Error(), "[错误]:") {
+			return errors.New("[错误]:" + usererr.Error())
+		} else {
+			return errors.New(usererr.Error())
+		}
+	} else if err != nil {
+		logger.Error(uuid, " ", err.Error())
+		return BankStuckTitle()
+	}
+	return nil
+}
+
+// 银行卡顿统一提示
+func BankStuckTitle() *taxerr.SystemErr {
+	if ComInfo.Cscsts {
+		return taxerr.NewSystemV3("银行页面卡顿", "系统将于30分钟后重试(可在\"通用设置\"关闭)")
+	} else {
+		return taxerr.NewSystemV3("银行页面卡顿", "请稍后重试(可在\"通用设置\"配置自动重试)")
+	}
+}
+
 // url 路径 filename 文件的上传参数
 func PostFile(url string, files map[string]string, ext map[string]string) ([]byte, error) {
 	res := []byte{}

+ 14 - 0
common/file.go

@@ -3,6 +3,7 @@ package common
 import (
 	"encoding/json"
 	"github.com/go-rod/rod/lib/utils"
+	"github.com/tidwall/gjson"
 	"os"
 	"strings"
 )
@@ -19,6 +20,19 @@ func GetCollectPDFPath(district, taxNo, taxTape string) (string, error) {
 	return path, err
 }
 
+// name pdf文件名
+// path 文件路径
+func PostBankPdf(name, suffix, path string) string {
+	var params = map[string]string{
+		"path": "/mnt/bank/banks/",
+		"name": name + "." + suffix,
+	}
+	str, _ := PostFile("https://public.listensoft.net/api/uploadFile", map[string]string{
+		"upFile": path,
+	}, params)
+	return gjson.GetBytes(str, "data.url").String()
+}
+
 // PDFtoStrArrayPython pdf转二维数组 python服务
 func PDFtoStrArrayPython(path string) ([][]string, error) {
 	var code [][]string

+ 17 - 0
common/period.go

@@ -2,6 +2,7 @@ package common
 
 import (
 	"errors"
+	"fmt"
 	"git.listensoft.net/tool/jspkit/logger"
 	"math"
 	"strconv"
@@ -292,3 +293,19 @@ func getCurrentPeriod(period string) string {
 	period = parse.AddDate(0, -1, 0).Format("200601") // 获取上月
 	return period
 }
+
+func GetFirstAndLastDayOfPeriod(period string) (string, string) {
+	t, _ := time.Parse("200601", period)
+	firstDay := t.AddDate(0, 0, 0)
+	lastDay := t.AddDate(0, 1, -1)
+	return firstDay.Format("2006-01-02"), lastDay.Format("2006-01-02")
+}
+
+// DateFormatter 将YYYYMMDD格式改为YYYY-MM-DD格式
+func DateFormatter(date string) string {
+	if len(date) != 8 {
+		return "" // 返回空字符串表示输入格式不正确
+	}
+
+	return fmt.Sprintf("%s-%s-%s", date[:4], date[4:6], date[6:])
+}

+ 76 - 0
common/rod_utils.go

@@ -3,6 +3,8 @@ package common
 import (
 	"errors"
 	"git.listensoft.net/tool/jspkit/common/lxrod"
+	"git.listensoft.net/tool/jspkit/common/models"
+	"git.listensoft.net/tool/jspkit/common/watermark"
 	"git.listensoft.net/tool/jspkit/logger"
 	"git.listensoft.net/tool/jspkit/taxerr"
 	"github.com/go-rod/rod"
@@ -10,6 +12,7 @@ import (
 	"github.com/go-rod/rod/lib/proto"
 	"github.com/go-rod/rod/lib/utils"
 	"go.uber.org/zap"
+	"os"
 	"strings"
 	"time"
 )
@@ -350,3 +353,76 @@ func WaitHasX(page *rod.Page, xpath string) (x bool) {
 	}
 	return x
 }
+
+func SaveErrImg(p *rod.Page, info models.CompanyInfo) string {
+	var check_path string
+	check_path = "./data/sbImg/" + info.TaxNo + "/"
+	if !PathExists(check_path) {
+		os.MkdirAll(check_path, os.ModePerm)
+	}
+	check_path = check_path + info.Period + "-" + time.Now().Format("2006-01-02-15-04-05") + ".png"
+	check_path2 := ""
+	_ = rod.Try(func() {
+		p.Timeout(ClickTimeOut).MustScreenshot(check_path)
+		watermark.Add(check_path)
+		check_path2 = PostSbjt(info.TaxNo, info.Period, check_path)
+		os.Remove(check_path)
+	})
+	return check_path2
+}
+
+// InputWithTimeout 规定时间内填入输入值,默认5s
+func InputWithTimeout(p *rod.Page, selector, inputVal string, timeout ...int64) {
+	var t int64
+	if len(timeout) == 0 {
+		t = 5
+	} else {
+		t = timeout[0]
+	}
+	p.Timeout(time.Duration(t) * time.Second).MustSearch(selector).MustSelectAllText().MustInput(inputVal)
+}
+
+func SaveErrImgTask(p *rod.Page, info *models.TaxTask) string {
+	var check_path string
+	check_path = "./data/sbImg/" + info.TaxNo + "/"
+	if !PathExists(check_path) {
+		os.MkdirAll(check_path, os.ModePerm)
+	}
+	check_path = check_path + info.Period + "-" + time.Now().Format("2006-01-02-15-04-05") + ".png"
+	var check_path2 string
+	_ = rod.Try(func() {
+		p.Timeout(ClickTimeOut).MustScreenshot(check_path)
+		watermark.Add(check_path)
+		check_path2 = PostSbjt(info.TaxNo, info.Period, check_path)
+		os.Remove(check_path)
+	})
+	return check_path2
+}
+
+// 使用键盘模拟输入
+func InputStrNew(page *rod.Page, el *rod.Element, str string) {
+	el.MustScrollIntoView()
+	box := el.MustShape().Box()
+	Click(page, box.X+(box.Width/2), box.Y+(box.Height/2))
+	time.Sleep(time.Millisecond * 100)
+	selectAll2(page)
+	time.Sleep(time.Millisecond * 100)
+	_ = page.Keyboard.Type(input.Delete)
+	_ = page.Keyboard.Type(input.Backspace)
+	time.Sleep(time.Millisecond * 100)
+	_ = page.Keyboard.Type(input.Backspace)
+	for _, v := range str {
+		_ = page.Keyboard.Type(input.Key(v))
+		time.Sleep(time.Millisecond * 100)
+	}
+}
+
+func Click(page *rod.Page, x, y float64) {
+	page.Mouse.MustMoveTo(x, y)
+	time.Sleep(time.Millisecond * 100)
+	page.Mouse.MustDown("left")
+	page.Mouse.MustUp("left")
+}
+func selectAll2(page *rod.Page) {
+	page.Keyboard.MustType(input.ControlLeft).MustType('a').MustType('a').MustType(input.ControlLeft)
+}

+ 51 - 0
common/utils.go

@@ -12,11 +12,13 @@ import (
 	"github.com/go-kratos/kratos/v2/log"
 	"github.com/tidwall/gjson"
 	"io"
+	"math/rand"
 	"net/url"
 	"os"
 	"reflect"
 	"regexp"
 	"strings"
+	"time"
 )
 
 type LvName string
@@ -200,3 +202,52 @@ func GetTelTaskStatus(tel string) (bool, error) {
 		return false, nil
 	}
 }
+
+var rng = rand.New(rand.NewSource(time.Now().UnixMicro()))
+
+// GenTracks 函数生成移动轨迹。先做加速运动,再做减速运动。
+//
+//	设滑块滑动的加速度为 a;当前速度为 v;初速度为 v0;位移为 x;所需时间为 t。
+//	根据加速度物理公式,它们满足以下关系:
+//	v = v0 + a * t
+//	x = v0 * t + 0.5 * a * t * t
+//
+// 参考:
+//
+// https://python3webspider.cuiqingcai.com/8.2-ji-yan-hua-dong-yan-zheng-ma-shi-bie
+func GenTracks(distance float64) []float64 {
+	var track = make([]float64, 0)     // 移动轨迹
+	var current float64 = 0            // 当前位移
+	var mid float64 = distance * 4 / 5 // 减速阈值
+	var t float64 = 0.15               // 计算间隔
+	var v float64 = 0                  // 初速度
+	var a1 = rng.Float64()*6 + 2       // 正加速度 [2,8)
+	var a2 = -a1 - 2                   // 负加速度 (-10,-4]
+
+	for current < distance {
+		// 加速度
+		var a float64
+		if current < mid {
+			a = a1
+		} else {
+			a = -a2
+		}
+		// 初速度 v0
+		v0 := v
+		// 当前速度 v = v0 + at
+		v = v0 + a*t
+		// 移动距离 x = v0t + 1/2 * a * t^2
+		x := v0*t + 1.0/2.0*a*t*t
+
+		current += x
+		track = append(track, x)
+	}
+
+	// 修正超标值
+	track = append(track, distance-current)
+	return track
+}
+
+func Trim(str interface{}) string {
+	return strings.TrimSpace(fmt.Sprintf("%s", str))
+}