github.com/Go-To-Byte/DouSheng/user_center@v0.0.0-20230524130918-ad531c1a3f6a/common/utils/token.go (about) 1 // @Author: Ciusyan 2023/2/6 2 package utils 3 4 import ( 5 "github.com/gin-gonic/gin" 6 "math/rand" 7 "strings" 8 "time" 9 10 "github.com/Go-To-Byte/DouSheng/dou_kit/constant" 11 ) 12 13 // MakeBearer 生成Base64的字符串 14 func MakeBearer(length int) string { 15 charList := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 16 t := make([]string, length) 17 rand.Seed(time.Now().UnixNano() + int64(length) + rand.Int63n(10000)) 18 for i := 0; i < length; i++ { 19 rn := rand.Intn(len(charList)) 20 w := charList[rn : rn+1] 21 t = append(t, w) 22 } 23 24 return strings.Join(t, "") 25 } 26 27 // GetToken 从gin的Ctx种获取Token 28 func GetToken(ctx *gin.Context) string { 29 30 // 1、从 header 中获取 31 // ...我们这里都是从参数中获取的 32 33 // 2、从query string 中获取 34 tk := ctx.Query(constant.REQUEST_TOKEN) 35 if tk != "" { 36 return tk 37 } 38 39 // 3、从 body 中获取 40 tk = ctx.PostForm(constant.REQUEST_TOKEN) 41 if tk != "" { 42 return tk 43 } 44 45 // 4、都没有,就返回 "" 46 return "" 47 }