123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package utils
- import (
- "bytes"
- "crypto/sha256"
- "encoding/hex"
- "fmt"
- "github.com/skip2/go-qrcode"
- "github.com/segmentio/ksuid"
- "math/rand"
- "os"
- "strconv"
- "strings"
- "time"
- )
- type Error struct {
- Code int `json:"code"`
- Desc string `json:"desc"`
- }
- /**
- 生成一个随机的不重复的用户ID
- */
- func GenUUID() string {
- uuid, _ := ksuid.NewRandomWithTime(time.Now())
- return uuid.String()
- }
- /**
- 隐藏手机号码中间4位
- */
- func KeepSecretForMobile(mobile string) string {
- if len(mobile) > 0 {
- var sqlBuf bytes.Buffer
- sqlBuf.WriteString(mobile[0:3])
- sqlBuf.WriteString("****")
- sqlBuf.WriteString(mobile[7:11])
- return sqlBuf.String()
- }
- return ""
- }
- /**
- 生成一个随机的不重复的数字
- */
- func GenUserIdentifier() string {
- random := rand.New(rand.NewSource(time.Now().UnixNano()))
- return fmt.Sprintf("%08v", random.Int31n(100000000))
- }
- /**
- 产生随机验证码
- */
- func GenRandomCode() string {
- random := rand.New(rand.NewSource(time.Now().UnixNano()))
- return fmt.Sprintf("%06v", random.Int31n(1000000))
- }
- /**
- 用户头像处理
- */
- func CompleteAvatar(avatar, defaultAvatar string) string {
- if len(avatar) > 0 && (strings.HasPrefix(avatar, "http://") || strings.HasPrefix(avatar, "https://")) {
- return avatar
- }
- return defaultAvatar
- }
- /**
- 生成二维码
- */
- func main() {
- qrcode.WriteFile("http://www.flysnow.org/",qrcode.Medium,256,"./blog_qrcode.png")
- }
- // 如果不存在指定的目录, 则直接创建一个
- func IsNotExistMkDirAll(path string) error {
- _, err := os.Stat(path)
- if notExist := os.IsNotExist(err); notExist == true {
- err := os.MkdirAll(path, os.ModePerm)
- if err != nil {
- return err
- }
- }
- return nil
- }
- /**
- 随机文件名, 防止文件名重复
- */
- func RandImageName(len int) string {
- b := make([]byte, len)
- rand.Read(b)
- return fmt.Sprintf("%x", b) + strconv.FormatInt(time.Now().Unix(), 10)
- }
- /**
- 生成签名Sig
- */
- func CalculateSigForTempl(appkey string, random string, mobiles []string, time int64) string {
- cnt := len(mobiles)
- if cnt == 0 {
- return ""
- }
- var buffer bytes.Buffer
- buffer.WriteString(mobiles[0])
- for i := 1; i < cnt; i++ {
- buffer.WriteString(",")
- buffer.WriteString(mobiles[i])
- }
- str := "appkey=" + appkey + "&random=" + random + "&time=" + strconv.FormatInt(time, 10) + "&mobile=" + buffer.String()
- hash := sha256.New()
- hash.Write([]byte(str))
- hashInBytes := hash.Sum(nil)
- return hex.EncodeToString(hashInBytes)
- }
- func Interface2String(inter interface{}) string {
- switch inter.(type) {
- case string:
- return inter.(string)
- case int:
- return strconv.Itoa(inter.(int))
- case int64:
- return strconv.FormatInt(inter.(int64), 10)
- case float64:
- return strconv.FormatFloat(inter.(float64), 'f', -1, 64)
- case bool:
- return strconv.FormatBool(inter.(bool))
- }
- return ""
- }
|