This commit is contained in:
zhangwenjian committed 2020-09-29 08:41:20 +08:00
1 parent 677cdb7821
commit bc648c3f2f
60 files changed
+2319 -550

No files matched your search

+10 -113
View File
@@ -1,10 +1,10 @@
package tools
import (
"archive/zip"
"bufio"
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"log"
@@ -14,17 +14,6 @@ import (
"time"
)
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func PathCreate(dir string) error {
return os.MkdirAll(dir, os.ModePerm)
}
@@ -38,88 +27,9 @@ func FileCreate(content bytes.Buffer, name string) {
if err != nil {
log.Println(err)
}
//for i := 0; i < len(content); i++ {
// //写入byte的slice数据
// file.Write(content)
// //写入字符串
// //
//}
file.Close()
}
func PathRemove(name string) {
err := os.RemoveAll(name)
if err != nil {
log.Println(err)
}
}
func FileRemove(name string) {
err := os.Remove(name)
if err != nil {
log.Println(err)
}
}
func FileZip(dst, src string, notContPath string) (err error) {
//创建准备写入的文件
fw, err := os.Create(dst)
defer fw.Close()
if err != nil {
return err
}
// 通过 fw 来创建 zip.Write
zw := zip.NewWriter(fw)
defer func() {
// 检测一下是否成功关闭
if err := zw.Close(); err != nil {
log.Fatalln(err)
}
}()
return filepath.Walk(src, func(path string, fi os.FileInfo, errBack error) (err error) {
if errBack != nil {
return errBack
}
fh, err := zip.FileInfoHeader(fi)
if err != nil {
return
}
fh.Name = strings.TrimPrefix(path, string(filepath.Separator))
if fi.IsDir() {
fh.Name += "/"
}
fh.Name = strings.Replace(fh.Name, notContPath, "", -1)
w, err := zw.CreateHeader(fh)
if err != nil {
return
}
if !fh.Mode().IsRegular() {
return nil
}
fr, err := os.Open(path)
defer fr.Close()
if err != nil {
return
}
n, err := io.Copy(w, fr)
if err != nil {
return
}
log.Printf("成功压缩文件: %s, 共写入了 %d 个字符的数据\n", path, n)
return nil
})
}
type ReplaceHelper struct {
Root string //路径
OldText string //需要替换的文本
@@ -165,28 +75,6 @@ func (h ReplaceHelper) walkCallback(path string, f os.FileInfo, err error) error
return err
}
func FileMonitoring(filePth string, hookfn func([]byte)) {
f, err := os.Open(filePth)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
rd := bufio.NewReader(f)
f.Seek(0, 2)
for {
line, err := rd.ReadBytes('\n')
// 如果是文件末尾不返回
if err == io.EOF {
time.Sleep(500 * time.Millisecond)
continue
} else if err != nil {
log.Fatalln(err)
}
go hookfn(line)
}
}
func FileMonitoringById(ctx context.Context, filePth string, id string, group string, hookfn func(context.Context, string, string, []byte)) {
f, err := os.Open(filePth)
if err != nil {
@@ -221,3 +109,12 @@ func GetFileSize(filename string) int64 {
})
return result
}
//获取当前路径,比如:E:/abc/data/test
func GetCurrentPath() string {
dir, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
return strings.Replace(dir, "\\", "/", -1)
}
-15
View File
@@ -1,15 +0,0 @@
package tools
import (
"math"
"strconv"
)
func Float64ToString(e float64) string {
return strconv.FormatFloat(e, 'E', -1, 64)
}
func Round(f float64, n int) float64 {
pow10_n := math.Pow10(n)
return math.Trunc((f+0.5/pow10_n)*pow10_n) / pow10_n // TODO +0.5 是为了四舍五入,如果不希望这样去掉这个
}
+13 -1
View File
@@ -1,7 +1,19 @@
package tools
import "strconv"
import (
"math"
"strconv"
)
func IntToString(e int) string {
return strconv.Itoa(e)
}
func Int64ToString(e int64) string {
return strconv.FormatInt(e, 10)
}
func Round(f float64, n int) float64 {
pow10_n := math.Pow10(n)
return math.Trunc((f+0.5/pow10_n)*pow10_n) / pow10_n // TODO +0.5 是为了四舍五入,如果不希望这样去掉这个
}
-7
View File
@@ -1,7 +0,0 @@
package tools
import "strconv"
func Int64ToString(e int64) string {
return strconv.FormatInt(e, 10)
}
-16
View File
@@ -1,16 +0,0 @@
package tools
import (
"fmt"
"os"
"strings"
)
//获取当前路径,比如:E:/abc/data/test
func GetCurrentPath() string {
dir, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
return strings.Replace(dir, "\\", "/", -1)
}
+1 -51
View File
@@ -2,26 +2,14 @@ package tools
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
func StringToInt64(e string) (int64, error) {
return strconv.ParseInt(e, 10, 64)
}
func StringToInt(e string) (int, error) {
return strconv.Atoi(e)
}
func StringToBool(e string) (bool, error) {
return strconv.ParseBool(e)
}
func GetCurrentTimeStr() string {
return time.Now().Format("2006-01-02 15:04:05")
}
@@ -30,48 +18,10 @@ func GetCurrentTime() time.Time {
return time.Now()
}
func FormatTimeStr(timeStr string) (string, error) {
loc, _ := time.LoadLocation("Local")
theTime, err := time.ParseInLocation("2006-01-02T15:04:05.000Z", timeStr, loc)
return theTime.Format("2006/01/02 15:04:05"), err
}
func StructToJsonStr(e interface{}) (string, error) {
if b, err := json.Marshal(e); err == nil {
return string(b), err
} else {
return "", err
}
}
func GetBodyString(c *gin.Context) (string, error) {
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
fmt.Printf("read body err, %v\n", err)
return string(body), nil
} else {
return "", err
}
}
func JsonStrToMap(e string) (map[string]interface{}, error) {
var dict map[string]interface{}
if err := json.Unmarshal([]byte(e), &dict); err == nil {
return dict, err
} else {
return nil, err
}
}
func StructToMap(data interface{}) (map[string]interface{}, error) {
dataBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
mapData := make(map[string]interface{})
err = json.Unmarshal(dataBytes, &mapData)
if err != nil {
return nil, err
}
return mapData, nil
}
}
-6
View File
@@ -15,12 +15,6 @@ type Ids struct {
Ids []int
}
func GetDeleteIds(c *gin.Context) []int {
var ids = new(Ids)
c.Bind(ids)
return ids.Ids
}
func IdsStrToIdsIntGroupStr(keys string) []int {
IDS := make([]int, 0)
ids := strings.Split(keys, ",")
-10
View File
@@ -14,16 +14,6 @@ import (
"golang.org/x/crypto/bcrypt"
)
// 不建议使用的方法(即将过时)
// Deprecated method (out of date)
func StrToInt(err error, index string) int {
result, err := strconv.Atoi(index)
if err != nil {
HasError(err, "string to int error"+err.Error(), -1)
}
return result
}
func CompareHashAndPassword(e string, p string) (bool, error) {
err := bcrypt.CompareHashAndPassword([]byte(e), []byte(p))
if err != nil {