-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
148 lines (135 loc) · 3.24 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gutil
import (
"fmt"
"image"
"math"
"regexp"
"strconv"
"unicode/utf8"
xhttp "github.com/swxctx/gutil/http"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
/*
input: [1,2,3,4]
output: ?,?,?,? [1 2 3 4]
*/
// GetSQLPlaceholder sql参数拼接
func GetSQLPlaceholder(list []string, params []interface{}) (s string, paramList []interface{}) {
n := len(list)
for i := 0; i < len(list); i++ {
s += "?"
if i < n-1 {
s += ","
}
paramList = append(paramList, list[i])
}
return
}
/*
intput: 1,2
output: 2
*/
// IntMax get max
func IntMax(i1, i2 int) int {
if i1 > i2 {
return i1
}
return i2
}
// RemoveElementToint32 移除数组内的指定元素
func RemoveElementToint32(list []int32, value int32) []int32 {
var (
result = make([]int32, 0)
)
index := 0
endIndex := len(list) - 1
for i, s := range list {
if s == value {
result = append(result, list[index:i]...)
index = i + 1
} else if i == endIndex {
result = append(result, list[index:endIndex+1]...)
}
}
return result
}
// CheckLongitudeAndLatitude 校验经纬度
func CheckLongitudeAndLatitude(longitude, latitude float64) bool {
// 校验经度范围+-(0-180),小数位不大于20位
longitudeStr := strconv.FormatFloat(longitude, 'f', -1, 64)
reg := regexp.MustCompile(`^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,20})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,20}|180)$`)
if !reg.MatchString(longitudeStr) {
return false
}
// 校验纬度范围+-(0-90),小数位不大于20位
latitudeStr := strconv.FormatFloat(latitude, 'f', -1, 64)
reg = regexp.MustCompile(`^(\-|\+)?([0-8]?\d{1}\.\d{0,20}|90\.0{0,20}|[0-8]?\d{1}|90)$`)
if !reg.MatchString(latitudeStr) {
return false
}
return true
}
// EarthDistance 经纬度距离计算(返回m)
func EarthDistance(lat1, lng1, lat2, lng2 float64) float64 {
radius := float64(6371000)
rad := math.Pi / 180.0
lat1 = lat1 * rad
lng1 = lng1 * rad
lat2 = lat2 * rad
lng2 = lng2 * rad
theta := lng2 - lng1
dist := math.Acos(math.Sin(lat1)*math.Sin(lat2) + math.Cos(lat1)*math.Cos(lat2)*math.Cos(theta))
return dist * radius
}
// GetXYDistance 计算平面内两点间距离
func GetXYDistance(x1, y1, x2, y2 float64) float64 {
if x1 == x2 && y1 == y2 {
return 0
}
return math.Sqrt(math.Pow(x1-x2, 2) + math.Pow(y1-y2, 2))
}
// ToGBK
func ToGBK(s string) (string, error) {
enc := simplifiedchinese.GBK
trans := enc.NewEncoder()
r, _, err := transform.String(trans, s)
return r, err
}
// FromGBK
func FromGBK(s string) (string, error) {
enc := simplifiedchinese.GBK
trans := enc.NewDecoder()
r, _, err := transform.String(trans, s)
return r, err
}
// GetImageSize 图片(在线url)宽和高
func GetImageSize(url string) (h, w int, err error) {
resp, _, err := xhttp.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
icfg, _, err := image.DecodeConfig(resp.Body)
if err != nil {
return
}
return icfg.Height, icfg.Width, nil
}
// FilterEmoji 过滤 emoji 表情
func FilterEmoji(content string) string {
var (
newContent string
)
for _, value := range content {
_, size := utf8.DecodeRuneInString(string(value))
if size <= 3 {
f := fmt.Sprintf("%#U", value)
if f == "U+200D" {
continue
}
newContent += string(value)
}
}
return newContent
}