passport.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package controllers
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "gopkg"
  6. "lzyd-user-api/common"
  7. "lzyd-user-api/models"
  8. "lzyd-user-api/services"
  9. "lzyd-user-api/utils"
  10. "time"
  11. )
  12. // @tags Passport
  13. // @Description 登录注册
  14. // @Summary 登录注册
  15. // @Accept json
  16. // @Produce json
  17. // @Param body body models.QuickLoginReq true "快捷登录请求内容"
  18. // @Success 200 {string} json "{"code":200,"message":"success","data":{"token":"","user":{}}}"
  19. // @Router /passport/quickLogin [post]
  20. // @Security ApiKeyAuth
  21. func QuickLogin(c *gin.Context) {
  22. var req models.QuickLoginReq
  23. err := c.ShouldBindJSON(&req)
  24. if err != nil {
  25. panic(err)
  26. }
  27. tokenStr := c.GetHeader("x-app-id")
  28. if tokenStr != "lzyd" {
  29. common.GenMessageResp(c, common.InvalidParametes, "非法操作", nil)
  30. return
  31. }
  32. //common.LogInfo("")
  33. //common.LogInfo(fmt.Sprintf("-------- [%s]快捷登录START --------", req.MobilePhone))
  34. if len(req.MobilePhone) == 0 || len(req.ValidCode) == 0 {
  35. common.GenResp(c, common.InvalidParametes, "无效的参数", nil)
  36. return
  37. }
  38. if len(req.MobilePhone) != 11 {
  39. common.GenResp(c, common.MobileFormatError, "手机号码格式有误", nil)
  40. return
  41. }
  42. var (
  43. model models.UserInfo
  44. queryStat int
  45. code = common.Success
  46. )
  47. userSms := services.GetValidSmsUser(common.DB, req.MobilePhone)
  48. SmsTypeLogin := common.LoginTypeMobile
  49. if len(userSms.MobilePhone) == 0 {
  50. SmsTypeLogin = common.LoginTypeMobile
  51. }
  52. sms := services.GetValidSms(common.DB, req.MobilePhone, req.ValidCode, SmsTypeLogin)
  53. if sms.SmsId == 0 {
  54. code = common.SmsCodeInvalid
  55. } else {
  56. if time.Now().Unix()-common.SmsExpiredSeconds > sms.CreateAt {
  57. fmt.Println(time.Now().Unix()-common.SmsExpiredSeconds,sms.CreateAt)
  58. code = common.SmsCodeExpired
  59. } else {
  60. model, queryStat = services.LoginWithMobileOnly(req.MobilePhone)
  61. fmt.Println("LoginWithMobileOnly",model, queryStat)
  62. switch queryStat {
  63. case common.QueryStateSuccess:
  64. if err := model.Login(common.DB); err != nil {
  65. code = common.DbInsertErr
  66. }
  67. case common.QueryStateNone:
  68. model.UserId = gopkg.GenUUID()
  69. model.RealName = utils.KeepSecretForMobile(req.MobilePhone)
  70. model.UserName = services.UniqueName(model.UserName)
  71. model.UserAvatar = common.SystemInfo.Avatar
  72. model.Gender = common.GenderUnknow
  73. model.MobilePhone = req.MobilePhone
  74. model.IsDelete = common.GenderUnknow
  75. model.AppId = tokenStr
  76. //model.Password = utils.DESEncryptString("888888", true) /// 设置默认密码`888888`
  77. //common.LogInfo(fmt.Sprintf("没查到记录,插入记录: %v", model))
  78. if err := model.Register(common.DB); err != nil {
  79. code = common.DbInsertErr
  80. }
  81. }
  82. }
  83. }
  84. if code != common.Success {
  85. msg := ""
  86. if code == common.SmsCodeInvalid {
  87. msg = "无效的短信验证码"
  88. } else if code == common.DbInsertErr {
  89. msg = "插入记录时发生错误"
  90. } else if code == common.SmsCodeExpired {
  91. msg = "验证码已失效"
  92. } else {
  93. msg = "未知错误"
  94. }
  95. common.GenResp(c, -1, msg, nil)
  96. return
  97. }
  98. //common.LogInfo(fmt.Sprintf("-------- [%s]快捷登录END --------", req.MobilePhone))
  99. // 返回结果处理
  100. var ret = models.UserResp{
  101. Id: model.Id,
  102. UserId: model.UserId,
  103. UserName: model.UserName,
  104. MobilePhone: model.MobilePhone,
  105. UserAvatar: model.UserAvatar,
  106. Gender: model.Gender,
  107. Birthday: model.Birthday,
  108. Area: model.Area,
  109. RegisterTime: model.RegisterTime,
  110. AppId: model.AppId,
  111. }
  112. common.GenResp(c, code, "SUCCESS", ret)
  113. }