|
|
- package ordercalc
-
- import (
- "math/rand"
- "strconv"
- "strings"
- "time"
- )
-
- /**
- * 订单号规则
- * id后4位(随机4位)+当前时间(10位)
- * 2020/08/10
- */
- func NewOrderSn(user_id string) string {
- var prefix string
- if user_id != "" {
- if len(user_id) > 4 {
- prefix = user_id[len(user_id)-4:] //截取后4位
- } else {
- prefix = user_id
- }
- } else {
- //随机4位
- str := "123456789"
- bytes := []byte(str)
- result := []byte{}
- r := rand.New(rand.NewSource(time.Now().UnixNano()))
- for i := 0; i < 4; i++ {
- result = append(result, bytes[r.Intn(len(bytes))])
- }
- prefix = string(result)
- }
-
- //拼接
- var build strings.Builder
- build.WriteString(prefix)
- build.WriteString(strconv.FormatInt(time.Now().Unix(), 10)) //当前时间
-
- return build.String()
-
- }
-
- /**
- * 核销码规则
- * 随机2位+当前时间(6位)
- * 2020/08/10
- */
- func VerifyNumber() string {
- var prefix string
-
- //随机4位
- str := "123456789"
- bytes := []byte(str)
- result := []byte{}
- r := rand.New(rand.NewSource(time.Now().UnixNano()))
- for i := 0; i < 1; i++ {
- result = append(result, bytes[r.Intn(len(bytes))])
- }
- prefix = string(result)
-
- //拼接
- var build strings.Builder
- build.WriteString(prefix)
- build.WriteString(strconv.FormatInt(time.Now().UnixNano(), 10)[10:17]) //当前时间
-
- return build.String()
-
- }
|