附近小店
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.

svc_user_invitecode.go 2.8 KiB

1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/md"
  6. "applet/app/utils"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "math/rand"
  10. "unicode"
  11. )
  12. // 检测获取邀请码
  13. func UserInviteCode(c *gin.Context, user *md.User) string {
  14. //已经有邀请码了就中断
  15. if user.Profile.InviteCode != "" {
  16. return user.Profile.InviteCode
  17. }
  18. var code string
  19. //读取后台配置判断是数字还是 数字+字母
  20. inviteData := db.SysCfgFind(c, "app_invite_length", "app_invite_type")
  21. l := utils.StrToInt(inviteData["app_invite_length"])
  22. types := utils.StrToInt(inviteData["app_invite_type"])
  23. ////匹配最新的用户的邀请码长度
  24. //newUser, _ := db.UserProfileOrderByNew(db.DBs[c.GetString("mid")])
  25. //if newUser.InviteCode != "" {
  26. // l = len(newUser.InviteCode)
  27. //}
  28. //读取code
  29. code = returnCode(c, l, types, 0)
  30. //查询用户信息
  31. userProfile, err := db.UserProfileFindByID(db.DBs[c.GetString("mid")], user.Info.Uid)
  32. if err != nil {
  33. e.OutErr(c, e.ERR_DB_ORM, err)
  34. return ""
  35. }
  36. //写入code
  37. userProfile.InviteCode = code
  38. fmt.Println(userProfile)
  39. db.UserProfileUpdate(db.DBs[c.GetString("mid")], user.Info.Uid, userProfile)
  40. return code
  41. }
  42. func returnCode(c *gin.Context, l, types, num int) string {
  43. if num > 5 {
  44. return ""
  45. }
  46. //循环3次判断是否存在该邀请码
  47. var code string
  48. var (
  49. codes []string
  50. )
  51. for i := 0; i < 3; i++ {
  52. oneCode := GetRandomString(l, types)
  53. codes = append(codes, oneCode)
  54. }
  55. //判断是不是存在邀请码了
  56. tmp, _ := db.UserProfileFindByInviteCodes(db.DBs[c.GetString("mid")], codes...)
  57. //判断自定义是不是存在邀请码了
  58. customTmp, _ := db.UserProfileFindByCustomInviteCodes(db.DBs[c.GetString("mid")], codes...)
  59. //循环生成的邀请码 判断tmp里有没有这个邀请码 如果邀请码没有就赋值 再判断是否存在 存在就清空
  60. for _, v := range codes {
  61. if code != "" { //如果存在并且数据库没有就跳过
  62. continue
  63. }
  64. code = v
  65. for _, v1 := range *tmp {
  66. //如果存在就清空
  67. if v1.InviteCode == v {
  68. code = ""
  69. }
  70. }
  71. for _, v1 := range *customTmp {
  72. //如果存在就清空
  73. if v1.CustomInviteCode == v {
  74. code = ""
  75. }
  76. }
  77. }
  78. //如果都没有就继续加一位继续查
  79. if code == "" {
  80. return returnCode(c, l+1, types, num+1)
  81. }
  82. return code
  83. }
  84. // 随机生成指定位数的大写字母和数字的组合
  85. func GetRandomString(l, isLetter int) string {
  86. str := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  87. if isLetter != 1 {
  88. str = "0123456789"
  89. }
  90. strs := []rune(str)
  91. result := make([]rune, l)
  92. for i := range result {
  93. result[i] = strs[rand.Intn(len(strs))]
  94. }
  95. fmt.Println(result)
  96. if IsLetter(string(result)) && isLetter == 1 {
  97. return GetRandomString(l, isLetter)
  98. }
  99. return string(result)
  100. }
  101. func IsLetter(s string) bool {
  102. for _, r := range s {
  103. if !unicode.IsLetter(r) {
  104. return false
  105. }
  106. }
  107. return true
  108. }