1 1 month ago
parent
commit
d41063bee3
4 changed files with 314 additions and 0 deletions
  1. 39 0
      common/common.go
  2. 128 0
      common/converter.go
  3. 104 0
      common/period.go
  4. 43 0
      common/utils.go

+ 39 - 0
common/common.go

@@ -139,3 +139,42 @@ func GetUUid() string {
 	newID := uuid.New().String()
 	return strings.ReplaceAll(newID, "-", "")
 }
+
+// 拦截请求 for 代理
+func PageHijackReqForProxy(b *rod.Page, pattern string, ch chan []byte, c *http.Client) *rod.HijackRouter {
+	router := b.HijackRequests()
+	router.MustAdd(pattern, func(ctx *rod.Hijack) {
+		rod.Try(func() {
+			err := ctx.LoadResponse(c, true)
+			if err != nil {
+				return
+			}
+			ch <- []byte(ctx.Response.Body())
+			time.Sleep(time.Second * 5)
+			select {
+			case <-ch:
+				break
+			}
+		})
+	})
+	go router.Run()
+	return router
+}
+
+// 拦截请求
+func BrowserHijackReq(b *rod.Browser, pattern string, ch chan []byte) *rod.HijackRouter {
+	router := b.HijackRequests()
+	router.MustAdd(pattern, func(ctx *rod.Hijack) {
+		rod.Try(func() {
+			ctx.MustLoadResponse()
+			ch <- []byte(ctx.Response.Body())
+			time.Sleep(time.Second * 5)
+			select {
+			case <-ch:
+				break
+			}
+		})
+	})
+	go router.Run()
+	return router
+}

+ 128 - 0
common/converter.go

@@ -2,6 +2,8 @@ package common
 
 import (
 	"github.com/shopspring/decimal"
+	"math"
+	"reflect"
 	"strconv"
 	"strings"
 )
@@ -82,3 +84,129 @@ func FloatToIntStr(floatValue float64) string {
 func FloatAllToStr(f float64) string {
 	return strconv.FormatFloat(f, 'f', -1, 64)
 }
+
+func FormatAmt(n float64) string {
+	return strconv.FormatFloat(n, 'f', 2, 64) //格式化金额 保留两位小数(四舍五入)
+}
+
+func IsNumeric(s string) bool {
+	_, err := strconv.Atoi(s)
+	return err == nil
+}
+
+func DecimalDivFF(v1 float64, v2 float64) float64 {
+	r, _ := decimal.NewFromFloat(v1).Div(decimal.NewFromFloat(v2)).Float64()
+	return r
+}
+
+func Round2(value float64) float64 {
+	d := decimal.NewFromFloat(value)
+	//zaplog.LoggerS.Info(n.String())
+	f, _ := d.Round(2).Float64()
+	return f
+}
+
+// DecimalAddFF 精确相加 小数
+func DecimalAddFF(v1 float64, v2 float64) float64 {
+	r, _ := decimal.NewFromFloat(v1).Add(decimal.NewFromFloat(v2)).Float64()
+	return r
+}
+
+func InterfaceToFloat(unk interface{}) float64 {
+	var floatType = reflect.TypeOf(float64(0))
+	var stringType = reflect.TypeOf("")
+	switch i := unk.(type) {
+	case float64:
+		return i
+	case float32:
+		return float64(i)
+	case int64:
+		return float64(i)
+	case int32:
+		return float64(i)
+	case int:
+		return float64(i)
+	case uint64:
+		return float64(i)
+	case uint32:
+		return float64(i)
+	case uint:
+		return float64(i)
+	case string:
+		f, _ := strconv.ParseFloat(i, 64)
+		return f
+	default:
+		v := reflect.ValueOf(unk)
+		v = reflect.Indirect(v)
+		if v.Type().ConvertibleTo(floatType) {
+			fv := v.Convert(floatType)
+			return fv.Float()
+		} else if v.Type().ConvertibleTo(stringType) {
+			sv := v.Convert(stringType)
+			s := sv.String()
+			f, _ := strconv.ParseFloat(s, 64)
+			return f
+		} else {
+			return math.NaN()
+		}
+	}
+}
+
+// float64 to float64 去掉金额字符串后面的0
+func FloatToFloat(f float64) float64 {
+	ff, _ := strconv.ParseFloat(FloatToStr3(f), 64)
+	return ff
+}
+
+// string to Float
+func StrToFloat4(str string) interface{} {
+	if strings.Contains(str, `*`) {
+		return str
+	} else {
+		if strings.Contains(str, `%`) {
+			str = strings.ReplaceAll(str, `%`, ``)
+			str = TrimQff(str)
+			intValue, err := strconv.ParseFloat(str, 64)
+			if err != nil {
+				intValue = 0
+			}
+			d := decimal.NewFromFloat(intValue)
+			//zaplog.LoggerS.Info(n.String())
+			f, _ := d.Round(2).Float64()
+			return f / 100
+
+		} else {
+			str = TrimQff(str)
+			intValue, err := strconv.ParseFloat(str, 64)
+			if err != nil {
+				intValue = 0
+			}
+			d := decimal.NewFromFloat(intValue)
+			//zaplog.LoggerS.Info(n.String())
+			f, _ := d.Round(2).Float64()
+			return f
+		}
+
+	}
+
+}
+
+// float64 to string 去掉金额字符串后面的0
+func FloatToStr3(f float64) string {
+	ss := strconv.FormatFloat(f, 'f', 2, 64)
+	return doDecimal(ss)
+}
+
+func doDecimal(s string) string {
+	if strings.Index(s, ".") < 0 {
+		return s
+	}
+	l := len(s)
+	if strings.HasSuffix(s, "0") {
+		return doDecimal(s[0 : l-1])
+	}
+	if strings.HasSuffix(s, ".") {
+		return doDecimal(s[0 : l-1])
+	}
+	return s
+}

+ 104 - 0
common/period.go

@@ -1,7 +1,10 @@
 package common
 
 import (
+	"errors"
+	"github.com/go-kratos/kratos/v2/log"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -115,3 +118,104 @@ func GetCurrentPeriod() string { //仅本期报税使用
 	period := firstOfMonth.AddDate(0, -1, 0).Format("200601") // 当前账期
 	return period
 }
+
+func GetPeriod(f int) string {
+	t := time.Now()
+	year, month, _ := t.Date()
+	thisMonthFirstDay := time.Date(year, month, 1, 1, 1, 1, 1, t.Location())
+	a := thisMonthFirstDay.AddDate(0, f, 0).Format("200601")
+	return a
+}
+
+func GetNextPeriod(period string) string {
+	currentTime, _ := time.Parse("200601", period)
+	next := currentTime.AddDate(0, 1, 0)
+	return next.Format("200601")
+}
+
+// 传入账期区间返回中间的月份 fmtData 1 - 返回"200601" 2 -返回"2006-01"
+func PeriodBetweenStartEnd(start, end string, fmtData int) (err error, periods []string) {
+	if start == "" || end == "" {
+		return errors.New("时间错误"), periods
+	}
+	var startTime time.Time
+	var endTime time.Time
+
+	//格式化时间
+	if strings.Contains(start, "-") && strings.Contains(end, "-") {
+		timeTemplate := "2006-01"
+		startTime, _ = time.ParseInLocation(timeTemplate, start, time.Local)
+		endTime, _ = time.ParseInLocation(timeTemplate, end, time.Local)
+	} else if !strings.Contains(start, "-") && !strings.Contains(end, "-") {
+		timeTemplate := "200601"
+		startTime, _ = time.ParseInLocation(timeTemplate, start, time.Local)
+		endTime, _ = time.ParseInLocation(timeTemplate, end, time.Local)
+	} else {
+		return errors.New("传入参数格式错误"), periods
+	}
+
+	//结束日期包含本月
+	endTime = endTime.AddDate(0, 1, 0)
+
+	//判断结束时间在开始时间之后
+	if !endTime.After(startTime) {
+		return errors.New("结束时间在开始时间之前"), periods
+	}
+
+	for {
+		//如果时间相等 则跳出
+		if startTime.Equal(endTime) {
+			break
+		}
+		if fmtData == 1 {
+			periods = append(periods, startTime.Format("200601"))
+		}
+		if fmtData == 2 {
+			periods = append(periods, startTime.Format("2006-01"))
+		}
+		startTime = startTime.AddDate(0, 1, 0)
+	}
+	return
+}
+
+// 传入period 返回 最后一天 201902-》
+func GetLastDay(period string) time.Time {
+	//获取本地location
+	toBeCharge := period                                            //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
+	timeLayout := "200601"                                          //转化所需模板
+	loc, _ := time.LoadLocation("Local")                            //重要:获取时区
+	theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
+	sr := theTime.Unix()                                            //转化为时间戳 类型是int64
+	//zaplog.LoggerS.Info(theTime)                                            //打印输出theTime 2015-01-01 15:15:00 +0800 CST
+	//zaplog.LoggerS.Info(sr)                                                 //打印输出时间戳 1420041600
+
+	tm := time.Unix(sr, 0)
+	currentYear, currentMonth, _ := tm.Date()
+	currentLocation := tm.Location()
+	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
+	lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
+	//return firstOfMonth.Format("2006-01-02"), lastOfMonth.Format("2006-01-02")
+	log.Info(firstOfMonth, lastOfMonth)
+	return lastOfMonth
+}
+
+// 传入period 返回 第一天 201902-》
+func GetFirstDay(period string) time.Time {
+	//获取本地location
+	toBeCharge := period                                            //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写
+	timeLayout := "200601"                                          //转化所需模板
+	loc, _ := time.LoadLocation("Local")                            //重要:获取时区
+	theTime, _ := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型
+	sr := theTime.Unix()                                            //转化为时间戳 类型是int64
+	//zaplog.LoggerS.Info(theTime)                                            //打印输出theTime 2015-01-01 15:15:00 +0800 CST
+	//zaplog.LoggerS.Info(sr)                                                 //打印输出时间戳 1420041600
+
+	tm := time.Unix(sr, 0)
+	currentYear, currentMonth, _ := tm.Date()
+	currentLocation := tm.Location()
+	firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
+	lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
+	//return firstOfMonth.Format("2006-01-02"), lastOfMonth.Format("2006-01-02")
+	log.Info(firstOfMonth, lastOfMonth)
+	return firstOfMonth
+}

+ 43 - 0
common/utils.go

@@ -4,9 +4,13 @@ import (
 	"crypto/md5"
 	"encoding/hex"
 	"fmt"
+	"git.listensoft.net/tool/jspkit/common/variable"
 	"github.com/go-kratos/kratos/v2/log"
 	"github.com/tidwall/gjson"
+	"io"
+	"net/url"
 	"os"
+	"strings"
 )
 
 type LvName string
@@ -76,3 +80,42 @@ func PostSbjt(taxno, period, path string) string {
 	}
 	return gjson.GetBytes(str, "path").String()
 }
+
+// 获取申报截图的path
+// taxNo 纳税人识别号 period 账期 taxType 任务名称
+func GetSbjtPath(taxNo, period string, taxType variable.TaskName) string {
+	var path string
+	path = "./data/" + taxNo + "/"
+	if !PathExists(path) {
+		os.MkdirAll(path, os.ModePerm)
+	}
+	path = path + period + string(taxType) + "_" + GetTimestamp(13) + ".png"
+	return path
+}
+
+// 判断字符串是否全是英文
+func IsAllEnglish(s string) bool {
+	// 检查字符串是否全部由ASCII字符组成
+	for _, c := range s {
+		if c > 127 {
+			return false
+		}
+	}
+	return true
+}
+
+// CreateFormReader 将 map 转换成 header
+func CreateFormReader(data map[string]string) io.Reader {
+	form := url.Values{}
+	for k, v := range data {
+		form.Add(k, v)
+	}
+	return strings.NewReader(form.Encode())
+}
+
+func Unbracket(str string) string {
+	str = strings.ReplaceAll(str, "(", "(")
+	str = strings.ReplaceAll(str, ")", ")")
+	str = strings.ReplaceAll(str, " ", "")
+	return str
+}