github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/common/util.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"math"
     7  	"math/rand"
     8  	"net/http"
     9  	"strconv"
    10  	"strings"
    11  	"time"
    12  )
    13  
    14  var (
    15  	chars = []string{
    16  		"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
    17  		"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
    18  		"k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
    19  		"u", "v", "w", "x", "y", "z", "A", "B", "C", "D",
    20  		"E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
    21  		"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
    22  		"Y", "Z", "~", "!", "@", "#", "$", "%", "^", "&",
    23  		"*", "(", ")", "-", "_", "=", "+", "[", "]", "{",
    24  		"}", "|", "<", ">", "?", "/", ".", ",", ";", ":"}
    25  
    26  	numberChars = []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
    27  )
    28  
    29  func Shuffle2(a []string) []string {
    30  	i := len(a) - 1
    31  	for i > 0 {
    32  		j := rand.Intn(i + 1)
    33  		a[i], a[j] = a[j], a[i]
    34  		i--
    35  	}
    36  	return a
    37  }
    38  
    39  func GetToken(n int) string {
    40  	if n < 1 {
    41  		return ""
    42  	}
    43  	var tokens []string
    44  	for i := 0; i < n; i++ {
    45  		tokens = append(tokens, chars[rand.Intn(90)]) // 90 是 Chars 的长度
    46  	}
    47  	return strings.Join(tokens, "")
    48  }
    49  
    50  // id 的第一位从 1 开始
    51  func GetID(n int) int {
    52  	if n < 1 {
    53  		return -1
    54  	}
    55  	min := math.Pow10(n - 1)
    56  	id := int(min) + rand.Intn(int(math.Pow10(n)-min))
    57  	return id
    58  }
    59  
    60  func HttpPost(url string, data string) ([]byte, error) {
    61  	resp, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data))
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	defer resp.Body.Close()
    66  	body, err := ioutil.ReadAll(resp.Body)
    67  	if err == nil {
    68  		return body, nil
    69  	}
    70  	return nil, err
    71  }
    72  func Atoi(str string) int {
    73  	i, _ := strconv.Atoi(str)
    74  	return i
    75  }
    76  func Decimal(value float64) float64 {
    77  	value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
    78  	return value
    79  }
    80  
    81  var todayCode = []string{
    82  	"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
    83  	"D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
    84  	"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
    85  }
    86  
    87  func GetTodayCode(n int) string {
    88  	newWords := ""
    89  	for i := 0; i < n; i++ {
    90  		newWords += todayCode[rand.Intn(len(todayCode))]
    91  	}
    92  	return newWords
    93  }
    94  
    95  func OneDay0ClockTimestamp(t time.Time) int64 {
    96  	return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix()
    97  }
    98  
    99  func TimeFormat() string {
   100  	return time.Now().Format("20060102")
   101  }