hpds_jkw_web/pkg/utils/utils.go

71 lines
1.7 KiB
Go
Raw Normal View History

2023-01-06 10:09:23 +08:00
package utils
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
2023-07-10 15:28:20 +08:00
"fmt"
"math"
2023-01-06 10:09:23 +08:00
"math/rand"
2023-07-10 15:28:20 +08:00
"strconv"
"strings"
2023-01-06 10:09:23 +08:00
"time"
)
/*
RandomString 产生随机数
- size 随机码的位数
- kind 0 // 纯数字
1 // 小写字母
2 // 大写字母
3 // 数字、大小写字母
*/
func RandomString(size int, kind int) string {
iKind, kinds, rsBytes := kind, [][]int{[]int{10, 48}, []int{26, 97}, []int{26, 65}}, make([]byte, size)
isAll := kind > 2 || kind < 0
rand.Seed(time.Now().UnixNano())
for i := 0; i < size; i++ {
if isAll { // random iKind
iKind = rand.Intn(3)
}
scope, base := kinds[iKind][0], kinds[iKind][1]
rsBytes[i] = uint8(base + rand.Intn(scope))
}
return string(rsBytes)
}
func GetUserSha1Pass(pass, salt string) string {
key := []byte(salt)
mac := hmac.New(sha1.New, key)
mac.Write([]byte(pass))
//进行base64编码
res := base64.StdEncoding.EncodeToString(mac.Sum(nil))
return res
}
2023-07-10 15:28:20 +08:00
// GetMilepost 里程桩加减里程,返回里程桩
func GetMilepost(start, num, upDown string) string {
arr := strings.Split(start, "+")
var (
kilometre, meter, milepost, counter, res, resMilepost, resMeter float64
)
if len(arr) == 1 {
meter = 0
} else {
meter, _ = strconv.ParseFloat(arr[1], 64)
}
str := strings.Replace(arr[0], "k", "", -1)
str = strings.Replace(str, "K", "", -1)
kilometre, _ = strconv.ParseFloat(str, 64)
milepost = kilometre + meter/1000
counter, _ = strconv.ParseFloat(num, 64)
if upDown == "D" {
res = milepost - counter
} else {
res = milepost + counter
}
resMilepost = math.Floor(res)
resMeter = (res - resMilepost) * 100
return fmt.Sprintf("K%d+%.2f", int(resMilepost), resMeter)
}