蛋蛋星球-客户端
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.
 
 
 
 
 

71 lines
1.4 KiB

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