蛋蛋星球-客户端
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_user_invitecode.go 1.7 KiB

3 일 전
3 일 전
3 일 전
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  5. "math/rand"
  6. "unicode"
  7. )
  8. func ReturnCode(l, types, num int) string {
  9. if num > 5 {
  10. return ""
  11. }
  12. //循环3次判断是否存在该邀请码
  13. var code string
  14. var (
  15. codes []string
  16. )
  17. for i := 0; i < 3; i++ {
  18. oneCode := GetRandomString(l, types)
  19. codes = append(codes, oneCode)
  20. }
  21. //判断是不是存在邀请码了
  22. userDb := implement.NewUserDb(db.Db)
  23. tmp, _ := userDb.UserFindByInviteCodes(codes...)
  24. //判断自定义是不是存在邀请码了
  25. customTmp, _ := userDb.UserFindByCustomInviteCodes(codes...)
  26. //循环生成的邀请码 判断tmp里有没有这个邀请码 如果邀请码没有就赋值 再判断是否存在 存在就清空
  27. for _, v := range codes {
  28. if code != "" { //如果存在并且数据库没有就跳过
  29. continue
  30. }
  31. code = v
  32. for _, v1 := range *tmp {
  33. //如果存在就清空
  34. if v1.SystemInviteCode == v {
  35. code = ""
  36. }
  37. }
  38. for _, v1 := range *customTmp {
  39. //如果存在就清空
  40. if v1.CustomInviteCode == v {
  41. code = ""
  42. }
  43. }
  44. }
  45. //如果都没有就继续加一位继续查
  46. if code == "" {
  47. return ReturnCode(l+1, types, num+1)
  48. }
  49. return code
  50. }
  51. // 随机生成指定位数的大写字母和数字的组合
  52. func GetRandomString(l, isLetter int) string {
  53. str := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  54. if isLetter != 1 {
  55. str = "0123456789"
  56. }
  57. strs := []rune(str)
  58. result := make([]rune, l)
  59. for i := range result {
  60. result[i] = strs[rand.Intn(len(strs))]
  61. }
  62. if IsLetter(string(result)) && isLetter == 1 {
  63. return GetRandomString(l, isLetter)
  64. }
  65. return string(result)
  66. }
  67. func IsLetter(s string) bool {
  68. for _, r := range s {
  69. if !unicode.IsLetter(r) {
  70. return false
  71. }
  72. }
  73. return true
  74. }