蛋蛋星球 后台端
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.
 
 
 
 

211 lines
7.7 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/utils"
  5. "applet/app/utils/logx"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "math/rand"
  11. "time"
  12. "unicode"
  13. )
  14. // 简单的recover
  15. func Rev() {
  16. if err := recover(); err != nil {
  17. _ = logx.Error(err)
  18. }
  19. }
  20. func ReturnCode(l, types, num int) string {
  21. if num > 5 {
  22. return ""
  23. }
  24. //循环3次判断是否存在该邀请码
  25. var code string
  26. var (
  27. codes []string
  28. )
  29. for i := 0; i < 3; i++ {
  30. oneCode := GetRandomString(l, types)
  31. codes = append(codes, oneCode)
  32. }
  33. //判断是不是存在邀请码了
  34. userDb := implement.NewUserDb(db.Db)
  35. tmp, _ := userDb.UserFindByInviteCodes(codes...)
  36. //判断自定义是不是存在邀请码了
  37. customTmp, _ := userDb.UserFindByCustomInviteCodes(codes...)
  38. //循环生成的邀请码 判断tmp里有没有这个邀请码 如果邀请码没有就赋值 再判断是否存在 存在就清空
  39. repeat := []string{"46ZQP3", "YJ61CV", "FBBD6P", "K2ZK47", "S46ZQP", "8QZEN6", "X1K9JI", "RY2HUG", "FQJOF6", "3HFFVH", "ALRC49", "1I6DBA", "RT537I", "R8Z95Z", "ADT33R", "9Z5N3C", "AMLIB0", "4FJ2HM", "1P1186", "373V3E", "IGCL3W", "RY4N0T", "12B5VA", "G7BKRS", "NBQWY3", "5C9RGD", "0SQSS7", "OARC7F", "O9RQKR", "LV6SMZ", "RY929H", "2CGLA5", "6WG3W7", "2OCXXJ", "Z74D5E", "MZT1BE", "5KU4Z9", "JMY2OA", "TVO9LM", "MX4ORM", "AFQ60J", "B2LLUQ", "S66KH5", "CMO6XX", "9JNH3G", "I5KSZL", "V3QMWG", "NYOS6I", "SCCX47", "IE250I", "95LT56", "OSJ7VD", "UWW8JU", "GPQUP3", "9YJ0CI", "2JA8YT", "Z5MOM4", "B41PBO", "7QUTOH", "FN3N4F", "639NNI", "9GDNEZ", "6BLIIS", "MHKK2I", "W05VR0", "QNI24A", "6Z13ER", "SU8VQL", "H0JKDS", "P6X6XI", "EXQ8P1", "1NZJBI", "HJI1GT", "M6OLIZ", "66WS8X", "XXS04V", "VNR363", "FD9CRH", "40TCCD", "QNWZR1", "6EAD47", "1HK0KF", "AR3IY5", "F1B3GM", "Q479YA", "F7D198", "VD7FXV", "8I7WF4", "SUO254", "VTH32H", "SLHLA5", "7TFEFZ", "73L091", "3CEYZ2", "20ZIY5", "4Q6JIK", "P4K0NX", "M04OUK", "3KUZSH", "YZG4HN", "A5LE91", "CDJQO4", "5XTSKP", "3KOR4B", "S69MBU", "881EYC", "KDZB13", "K22C83", "M08M6A", "31OY42", "8FHO8O", "P7AE2M", "W6ADG4", "DMAGW9", "AV653E", "9NEFFX", "P8E8LX", "1PINC4", "1Y2A0B", "HD1BPN", "S3W9WB", "Q9I0XO", "75K0KL", "Y72G14", "KL8HAX", "JS41K1", "G4L34B", "RV8KBP", "VG2S5B", "V6ZP51", "S5DWD8", "J24KTU", "N7IFG8", "4Z94WX", "4TOK2G", "8COMEX", "OO2M08", "IQDA9G", "V1Y8HC", "UBE4KZ", "C6JVML", "89DHLX", "PIW413", "UAJ4KB", "SPEQB9", "7CJDW8", "E2Q8MW", "M1PDC3", "1C7QIR", "O122E4", "F735IK", "4GGZU3", "4CW5N8", "ISCC02", "7WB4ET", "7PQQK4", "FSKO12", "6VREUP", "Q21NUE", "KOK7GQ", "J2U1U9", "TJ3O19", "99SWY1", "46SO3B", "NO5HE5", "J2N5GU", "2ADNIW", "K014LU", "UDC2RY", "DSGQ3O", "579A6Z", "MG3H08", "WXZL8M", "MG8PG7", "1JKUF8", "ITP4WQ", "6FAOU3", "00GTZQ", "XYUYO4", "GS9YJ6", "KQ7UWT", "7981LY", "J7BGGU", "XMJEB9", "E58TS4", "IL82FA", "P3RR5Z", "5RYDW0", "GCU8OJ", "12YH73", "K8LF5L", "Z79K5R", "KHX1BY", "7K3YSL", "WM9OH5", "0DL3OH", "KKLQJ1", "XPROG1", "OB66GR", "7ZYCNE", "L6010Q", "9KNBJC", "H0A6Z2", "VC5X88", "MAZKT8", "FZTL3Q", "T58EF4", "G7A2PB", "K19Y5W", "39842R", "UIIX8U", "GY5D0Y", "3723U5", "5UZPDO", "74RTB0", "5J9ZQ3", "24S3YM", "UZ7CF8", "J8FYO6", "MF7AKW", "OMY3T6", "X0F7YO", "NCGTM2", "MZNPO3", "ROBSR6", "67CHNX", "Y1KTV2", "PKZ925", "429OSC", "WB8TRY", "7ZWUQ4", "F8HQGC", "5NG2HI", "RTRQ9W", "4EH1GI", "VSQB15", "34ZRJS", "ENO4TN", "MW0P2T", "IR298E", "3ZEH1K", "NRHKD4", "EVL5DO", "A17WRT", "1SGNB7", "28YJXV", "DQEQ8G", "X4ZI02", "E4VXT8", "J7AV62", "N3BNH8", "IVUX2P", "Y5KOXS", "SRT4X1", "FBY12A", "5HMYIR", "ONCKZ7", "0D84XJ", "BX9YZ2", "SIS6RC", "ZT7TFB", "TRPBB9", "V7QMAH"}
  40. for _, v := range codes {
  41. if utils.InArr(v, repeat) { //出问题的不给用了
  42. continue
  43. }
  44. if code != "" { //如果存在并且数据库没有就跳过
  45. continue
  46. }
  47. code = v
  48. for _, v1 := range *tmp {
  49. //如果存在就清空
  50. if v1.SystemInviteCode == v {
  51. code = ""
  52. }
  53. }
  54. for _, v1 := range *customTmp {
  55. //如果存在就清空
  56. if v1.CustomInviteCode == v {
  57. code = ""
  58. }
  59. }
  60. }
  61. //如果都没有就继续加一位继续查
  62. if code == "" {
  63. return ReturnCode(l+1, types, num+1)
  64. }
  65. return code
  66. }
  67. // 随机生成指定位数的大写字母和数字的组合
  68. func GetRandomString(l, isLetter int) string {
  69. str := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  70. if isLetter != 1 {
  71. str = "0123456789"
  72. }
  73. strs := []rune(str)
  74. result := make([]rune, l)
  75. for i := range result {
  76. result[i] = strs[rand.Intn(len(strs))]
  77. }
  78. if IsLetter(string(result)) && isLetter == 1 {
  79. return GetRandomString(l, isLetter)
  80. }
  81. return string(result)
  82. }
  83. func IsLetter(s string) bool {
  84. for _, r := range s {
  85. if !unicode.IsLetter(r) {
  86. return false
  87. }
  88. }
  89. return true
  90. }
  91. // RoutineInsertUserRelate is 协程 当关联上级用户时,需要查询当前用户的所有关联下级,并新增关联上级与当前用户下级关系
  92. func RoutineInsertUserRelate(puid, uid int64, addlv int) {
  93. userRelateDb := implement.NewUserRelateDb(db.Db)
  94. defer Rev()
  95. urs, err := userRelateDb.UserRelatesByPuid(uid, 0, 0)
  96. if err != nil {
  97. logx.Warn(err)
  98. }
  99. // fmt.Println(*urs)
  100. for _, item := range *urs {
  101. _, err = userRelateDb.UserRelateInsert(
  102. &model.UserRelate{
  103. ParentUid: puid,
  104. Uid: item.Uid,
  105. Level: item.Level + addlv,
  106. InviteTime: time.Now().Format("2006-01-02 15:04:05"),
  107. })
  108. if err != nil {
  109. continue
  110. }
  111. logx.Info(fmt.Sprintf("关联pid(%v) -> uid(%v) ,lv:%v", puid, item.Uid, item.Level+addlv))
  112. }
  113. }
  114. // RoutineMultiRelate is 多级关联
  115. func RoutineMultiRelate(pid, uid int64, lv int) {
  116. userDb := implement.NewUserDb(db.Db)
  117. userRelateDb := implement.NewUserRelateDb(db.Db)
  118. defer Rev()
  119. for {
  120. if pid == 0 {
  121. break
  122. }
  123. m, err := userDb.GetUser(pid)
  124. if err != nil || m == nil {
  125. logx.Warn(err)
  126. break
  127. }
  128. if m.ParentUid == 0 {
  129. break
  130. }
  131. lv++
  132. ur := new(model.UserRelate)
  133. ur.ParentUid = m.ParentUid
  134. ur.Uid = uid
  135. ur.Level = lv
  136. ur.InviteTime = time.Now().Format("2006-01-02 15:04:05")
  137. _, err = userRelateDb.UserRelateInsert(ur)
  138. if err != nil {
  139. logx.Warn(err)
  140. break
  141. }
  142. // 还要关联当前的用户的所有下级,注意关联等级
  143. RoutineInsertUserRelate(m.ParentUid, uid, lv)
  144. // 下级关联上上级
  145. // 继续查询
  146. logx.Info(fmt.Sprintf("关联pid(%v) -> uid(%v),lv:%v", ur.ParentUid, ur.Uid, lv))
  147. logx.Info("继续查询")
  148. pid = m.ParentUid
  149. }
  150. }
  151. func UserImeiAdd(c *gin.Context, uid int64) {
  152. var tmp model.UserImei
  153. get, _ := db.Db.Where("uid=? and imei=? and ip=?", uid, c.GetHeader("imei"), utils.GetIP(c.Request)).Get(&tmp)
  154. if get == false {
  155. var data = model.UserImei{
  156. Uid: int(uid),
  157. Imei: c.GetHeader("imei"),
  158. CreateAt: time.Now(),
  159. UpdateAt: time.Now(),
  160. Ip: utils.GetIP(c.Request),
  161. }
  162. db.Db.Insert(&data)
  163. } else {
  164. tmp.UpdateAt = time.Now()
  165. db.Db.Where("id=?", tmp.Id).Cols("update_at").Update(&tmp)
  166. }
  167. NewUserDb := implement.NewUserDb(db.Db)
  168. user, _ := NewUserDb.GetUser(uid)
  169. if user != nil {
  170. user.LastLoginAt = time.Now().Format("2006-01-02 15:04:05")
  171. user.LastLoginIp = utils.GetIP(c.Request)
  172. db.Db.Where("id=?", user.Id).Cols("last_login_at,last_login_ip").Update(user)
  173. UpdateUserTime(user.Id, "login")
  174. }
  175. }
  176. func UpdateUserTime(uid int64, types string) {
  177. count, _ := db.Db.Where("uid=?", uid).Count(&model.UserNoticeTime{})
  178. if count == 0 {
  179. tmp := &model.UserNoticeTime{Uid: int(uid)}
  180. if types == "login" {
  181. tmp.LoginTime = int(time.Now().Unix())
  182. } else {
  183. tmp.SignTime = int(time.Now().Unix())
  184. }
  185. db.Db.Insert(tmp)
  186. } else {
  187. tmp := &model.UserNoticeTime{}
  188. str := ""
  189. if types == "login" {
  190. str = "login_time"
  191. tmp.LoginTime = int(time.Now().Unix())
  192. } else {
  193. str = "sign_time"
  194. tmp.SignTime = int(time.Now().Unix())
  195. }
  196. db.Db.Where("uid=?", uid).Cols(str).Update(tmp)
  197. }
  198. }