-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
73 lines (64 loc) · 1.78 KB
/
util.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
package coolCaptcha
import (
"bytes"
"encoding/base64"
"errors"
"image"
"image/gif"
"image/png"
"math/rand"
"regexp"
"github.com/samber/lo"
)
// randomFloat64
// @Description: Take a random number from the smallest and largest ranges
// @param min
// @param max
// @return float64
func randomFloat64(min, max float64) float64 {
return rand.Float64()*(max-min) + min
}
// convertImageToBase64
// @Description: convert image.Image to image base64 data
// @param imageData
// @return imageBase64Data
// @return err
func convertImageToBase64(imageData image.Image) (imageBase64Data string, err error) {
writer := new(bytes.Buffer)
err = png.Encode(writer, imageData)
if err != nil {
return
}
imageBase64Data = "data:image/png;base64," + base64.StdEncoding.EncodeToString(writer.Bytes())
return
}
// convertImagGifToBase64
// @Description: convert image.Image to image base64 data
// @param imageData
// @return imageBase64Data
// @return err
func convertGifToBase64(imageData *gif.GIF) (gifBase64Data string, err error) {
writer := new(bytes.Buffer)
err = gif.EncodeAll(writer, imageData)
if err != nil {
return
}
gifBase64Data = "data:image/gif;base64," + base64.StdEncoding.EncodeToString(writer.Bytes())
return
}
// CheckCustomCodeFormat
// @Description: Detect the format of the code
// @param code
// @return err
func checkCustomCodeFormat(code string) (err error) {
reg := `^[a-zA-Z\d]{4}$`
rgx := regexp.MustCompile(reg)
if !rgx.MatchString(code) {
err = errors.New("the custom code is malformed, only English letters and numbers are supported, and the length is 4")
return
}
return
}
func genRandomPoint(originNum float64, coefficient float64) float64 {
return lo.If(rand.Intn(2) == 1, originNum+(coefficient*rand.Float64())).Else(originNum - (coefficient * rand.Float64()))
}