You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
3.4 KiB
160 lines
3.4 KiB
package utils
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"github.com/skip2/go-qrcode"
|
|
"hudongzhuanjia/logger"
|
|
"hudongzhuanjia/utils/define"
|
|
"io/ioutil"
|
|
"math/rand"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func Int(str string) int {
|
|
i, _ := strconv.Atoi(str)
|
|
return i
|
|
}
|
|
|
|
func Int64(str string) int64 {
|
|
i, _ := strconv.ParseInt(str, 10, 64)
|
|
return i
|
|
}
|
|
|
|
func Float64(str string) float64 {
|
|
f, _ := strconv.ParseFloat(strings.TrimSpace(str), 64)
|
|
return f
|
|
}
|
|
|
|
//除法,校验被除数为0时返回接口为0
|
|
// return one/two
|
|
func Except(one, two float64) float64 {
|
|
if two == 0 {
|
|
return 0
|
|
}
|
|
return one / two
|
|
}
|
|
|
|
//自定义小数的位数
|
|
func Float64CusDecimal(v float64, decimal int) float64 {
|
|
return Float64(fmt.Sprintf("%."+fmt.Sprintf("%d", decimal)+"f", v))
|
|
}
|
|
|
|
func Qrcode2Base64(url string) (string, error) {
|
|
qr, err := qrcode.Encode(url, qrcode.Medium, 256)
|
|
return fmt.Sprintf("data:image/png;base64,%s", base64.StdEncoding.EncodeToString(qr)), err
|
|
}
|
|
|
|
func GenH5Url(webName string, param map[string]interface{}) string {
|
|
path := fmt.Sprintf("%s/%s?", define.H5Host, webName)
|
|
for key, value := range param {
|
|
if key[len(key)-1] == '=' {
|
|
key = key[:len(key)-1]
|
|
}
|
|
path += fmt.Sprintf("%s=%v&", key, value)
|
|
}
|
|
lastChar := path[len(path)-1]
|
|
if lastChar == '&' || lastChar == '?' {
|
|
path = path[:len(path)-1]
|
|
}
|
|
return path
|
|
}
|
|
|
|
func GenH5Qrcode(WebName string, param map[string]interface{}) (string, error) {
|
|
path := GenH5Url(WebName, param)
|
|
return Qrcode2Base64(path)
|
|
}
|
|
|
|
func Gif2Base64(url string) (string, error) {
|
|
if strings.HasSuffix(url, ".gif") {
|
|
resp, err := http.Get(url)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
bys, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return fmt.Sprintf("data:image/gif;base64,%s", base64.StdEncoding.EncodeToString(bys)), err
|
|
}
|
|
return url, nil
|
|
}
|
|
|
|
func Sha1(b string) string {
|
|
h := sha1.New()
|
|
h.Write([]byte(b))
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
}
|
|
|
|
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 CountDownFormat(duration int64) string {
|
|
m := duration / 60 // 分钟
|
|
s := duration - m*60 // 剩余秒
|
|
h := m / 60 // 小时
|
|
m = m - h*60 // 剩余分钟
|
|
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
|
|
}
|
|
|
|
func RandomStr(length int) string {
|
|
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
bytes := []byte(str)
|
|
var result []byte
|
|
for i := 0; i < length; i++ {
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(i) + int64(time.Now().Nanosecond())))
|
|
result = append(result, bytes[r.Intn(len(bytes))])
|
|
}
|
|
return string(result)
|
|
}
|
|
|
|
func RandomInt(length int) int {
|
|
str := "0123456789"
|
|
bytes := []byte(str)
|
|
var result []byte
|
|
|
|
for i := 0; i < length; {
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano() + int64(i) + int64(time.Now().Nanosecond())))
|
|
b := bytes[r.Intn(len(bytes))]
|
|
if i == 0 && b == '0' {
|
|
continue
|
|
}
|
|
result = append(result, b)
|
|
i++
|
|
}
|
|
num, _ := strconv.Atoi(string(result))
|
|
return num
|
|
}
|
|
|
|
// 定时任务
|
|
func HandleTicker(ticker time.Duration, f func() error) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
logger.Error("定时任务错误panic原因", err)
|
|
}
|
|
HandleTicker(ticker, f)
|
|
}()
|
|
t := time.NewTicker(ticker)
|
|
for {
|
|
select {
|
|
case <-t.C:
|
|
err := f()
|
|
if err != nil {
|
|
logger.Error("定时任务错误原因", err)
|
|
}
|
|
}
|
|
}
|
|
}
|