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.
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 , t pb . TYPE ) string {
switch t {
case pb . TYPE_DEFAULT :
fallthrough
case pb . TYPE_DIGIT :
return randCode ( "0123456789" , l )
case pb . TYPE_LETTER :
return randCode ( "abcdefghijklnmopqrstuvwxyz" , l )
case pb . TYPE_MIXED :
return randCode ( "0123456789abcdefghijklnmopqrstuvwxyz" , l )
default :
}
return ""
}
// 核心
func randCode ( chars string , l int ) string {
charsLen := len ( chars )
result := make ( [ ] byte , l )
for i := 0 ; i < l ; i ++ {
//随机生成【0, n) 随机数
randIndex := rand . Intn ( charsLen )
result [ i ] = chars [ randIndex ]
}
rand . Intn ( len ( chars ) )
return string ( result )
}