|
- package svc
-
- import (
- "applet/app/db"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- "math/rand"
- "unicode"
- )
-
- func ReturnCode(l, types, num int) string {
- if num > 5 {
- return ""
- }
- //循环3次判断是否存在该邀请码
- var code string
- var (
- codes []string
- )
- for i := 0; i < 3; i++ {
- oneCode := GetRandomString(l, types)
- codes = append(codes, oneCode)
- }
-
- //判断是不是存在邀请码了
- userDb := implement.NewUserDb(db.Db)
- tmp, _ := userDb.UserFindByInviteCodes(codes...)
- //判断自定义是不是存在邀请码了
- customTmp, _ := userDb.UserFindByCustomInviteCodes(codes...)
-
- //循环生成的邀请码 判断tmp里有没有这个邀请码 如果邀请码没有就赋值 再判断是否存在 存在就清空
- for _, v := range codes {
- if code != "" { //如果存在并且数据库没有就跳过
- continue
- }
- code = v
- for _, v1 := range *tmp {
- //如果存在就清空
- if v1.SystemInviteCode == v {
- code = ""
- }
- }
- for _, v1 := range *customTmp {
- //如果存在就清空
- if v1.CustomInviteCode == v {
- code = ""
- }
- }
- }
- //如果都没有就继续加一位继续查
- if code == "" {
- return ReturnCode(l+1, types, num+1)
- }
- return code
- }
-
- // 随机生成指定位数的大写字母和数字的组合
- func GetRandomString(l, isLetter int) string {
- str := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- if isLetter != 1 {
- str = "0123456789"
- }
- strs := []rune(str)
- result := make([]rune, l)
- for i := range result {
- result[i] = strs[rand.Intn(len(strs))]
- }
- if IsLetter(string(result)) && isLetter == 1 {
- return GetRandomString(l, isLetter)
- }
- return string(result)
- }
-
- func IsLetter(s string) bool {
- for _, r := range s {
- if !unicode.IsLetter(r) {
- return false
- }
- }
- return true
- }
|