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.

82 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package service
import (
"context"
"math/rand"
pb "verifyCode/api/verifyCode"
)
type VerifyCodeService struct {
pb.UnimplementedVerifyCodeServer
}
// 构造器
func NewVerifyCodeService() *VerifyCodeService {
return &VerifyCodeService{}
}
func (s *VerifyCodeService) GetVerifyCode(ctx context.Context, req *pb.GetVerifyCodeRequest) (*pb.GetVerifyCodeReply, error) {
return &pb.GetVerifyCodeReply{
Code: RandCode(int(req.Length), req.Type),
}, nil
}
func RandCode(l int, r pb.TYPE) string {
switch r {
case pb.TYPE_DEFAULT:
fallthrough
case pb.TYPE_DIGIT:
return randcode("0123456789", l, 4)
case pb.TYPE_LETTRE:
return randcode("abcdefghigklmnopqrstuvwxyz", l, 5)
case pb.TYPE_MIXED:
return randcode("0123456789abcdefghigklmnopqrstuvwxyz", l, 6)
default:
return ""
}
}
// 随机数和新方法:优化实现
// 一次随机多次使用,分部分多次使用
func randcode(chars string, l, idxBits int) string {
//计算有效的二进制数基于char的长度
//推荐写死chars固定idxBits固定
//形成掩码。mask
//例如使用低六位00000000000111111
idxMask := 1<<idxBits - 1 //00001000000 - 1 = 00000111111
//63位可以使用多少次
idxMax := 63 / idxBits
//结果
result := make([]byte, l)
//生成随机数
//cache:随机位缓存
//remain:还可以用多少次
for i, cache, remain := 0, rand.Int63(), idxMax; i < l; {
if remain == 0 {
cache, remain = rand.Int63(), idxMax
}
//利用掩码获取有效位的随机数
if randIndex := int(cache & int64(idxMask)); randIndex < len(chars) {
result[i] = chars[randIndex]
i++
}
//使用下一组随即位
cache >>= idxBits
//减少一次使用次数
remain--
}
return string(result)
}
// 随机的核心方法:简单实现
//func randcode(chars string, l int) string {
// charsLen := len(chars)
// result := make([]byte, l)
// for i := 0; i < l; i++ {
// randIndex := rand.Intn(charsLen)
// result[i] = chars[randIndex]
// }
// return string(result)
//}