附近小店
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

130 wiersze
3.5 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "applet/app/utils/cache"
  9. "applet/app/utils/logx"
  10. "errors"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. "strings"
  14. )
  15. // GetDefaultUser is 获取默认账号,uid =0 为系统默认账号,用于一些请求需要渠道id之类的东西
  16. func GetDefaultUser(c *gin.Context, token string) (*md.User, error) {
  17. user := new(md.User)
  18. if c.GetString("convert_url") == "1" { //转链接口
  19. goto DEFALUT
  20. } else {
  21. // Token 不为空时拿对应的用户数据
  22. if token != "" && strings.Contains(token, "Bearer") {
  23. user, err := CheckUser(c)
  24. if user == nil {
  25. return nil, errors.New("token is expired")
  26. }
  27. if err != nil {
  28. // 有报错自己拿默认用户
  29. goto DEFALUT
  30. }
  31. return user, nil
  32. }
  33. }
  34. DEFALUT:
  35. // 默认拿uid 等于0的用户数据
  36. profile, err := db.UserProfileFindByID(db.DBs[c.GetString("mid")], 0)
  37. if err != nil {
  38. return nil, err
  39. }
  40. info, err := db.UserFindByID(db.DBs[c.GetString("mid")], 0)
  41. if err != nil {
  42. return nil, err
  43. }
  44. ul, err := db.UserLevelInIDescByWeightLowWithOne(db.DBs[c.GetString("mid")])
  45. if err != nil {
  46. return nil, err
  47. }
  48. user.Info = info
  49. user.Profile = profile
  50. user.Level = ul
  51. return user, nil
  52. }
  53. func FastLoginUserExist(c *gin.Context, r *md.LoginResponse, requestBody *md.FastLoginRequestBody) error {
  54. // 每次登录都会刷新token
  55. var user = &model.User{}
  56. var err error
  57. if requestBody.Mobile != "" {
  58. user, err = db.UserFindByMobileAll(db.DBs[c.GetString("mid")], requestBody.Mobile)
  59. }
  60. if user == nil || user.Uid == 0 {
  61. return e.NewErr(400, "用户不存在")
  62. }
  63. if err != nil {
  64. return err
  65. }
  66. if user.DeleteAt == 1 {
  67. return e.NewErr(403053, "账号被删除,如有疑问请联系客服")
  68. }
  69. //判断用户是否虚拟账号 虚拟账号不能用手机号登陆
  70. if user.IsFake == 1 {
  71. return e.NewErr(400000, "该手机号无法登陆,请联系客服!")
  72. }
  73. //0未激活,1正常,2冻结
  74. switch user.State {
  75. case 0:
  76. return e.NewErr(403028, "no active")
  77. case 2:
  78. return e.NewErr(403029, "您的账号已被冻结")
  79. }
  80. // 生成jwt
  81. appName := db.SysCfgGet(c, "app_name")
  82. profile, err := db.UserProfileFindByID(db.DBs[c.GetString("mid")], user.Uid)
  83. if err != nil || profile == nil {
  84. _ = logx.Warn(err)
  85. return e.NewErr(400000, "数据不完整")
  86. }
  87. token := CheckUserToken(c, user, appName)
  88. r.Token = token
  89. r.UserID = utils.IntToStr(user.Uid)
  90. r.UserName = requestBody.Mobile
  91. r.Phone = requestBody.Mobile
  92. // 更新
  93. profile.ArkidToken = token
  94. _, err = db.UserProfileUpdate(db.DBs[c.GetString("mid")], user.Uid, profile)
  95. if err != nil {
  96. return logx.Warn(err)
  97. }
  98. // 写入缓存
  99. key := fmt.Sprintf("%s:token:%s", c.GetString("mid"), user.Username)
  100. _, err = cache.SetEx(key, token, 39528000) // 半年
  101. if err != nil {
  102. return logx.Warn(err)
  103. }
  104. return nil
  105. }
  106. func CheckUserToken(c *gin.Context, user *model.User, appName string) string {
  107. profile, err := db.UserProfileFindByID(db.DBs[c.GetString("mid")], user.Uid)
  108. if err != nil {
  109. e.OutErr(c, e.ERR_DB_ORM, err)
  110. return ""
  111. }
  112. token := profile.ArkidToken
  113. _, err = utils.ParseToken(token)
  114. if err != nil {
  115. token, _ = utils.GenToken(user.Uid, user.Username, user.Phone, appName, "", "")
  116. // 更新
  117. profile.ArkidToken = token
  118. _, err = db.UserProfileUpdate(db.DBs[c.GetString("mid")], user.Uid, profile)
  119. // 写入缓存
  120. key := fmt.Sprintf("%s:token:%s", c.GetString("mid"), user.Username)
  121. _, err = cache.SetEx(key, token, 39528000) // 半年
  122. }
  123. return token
  124. }