123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package controllers
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "gopkg"
- "lzyd-user-api/common"
- "lzyd-user-api/models"
- "lzyd-user-api/services"
- "lzyd-user-api/utils"
- "time"
- )
- // @tags Passport
- // @Description 登录注册
- // @Summary 登录注册
- // @Accept json
- // @Produce json
- // @Param body body models.QuickLoginReq true "快捷登录请求内容"
- // @Success 200 {string} json "{"code":200,"message":"success","data":{"token":"","user":{}}}"
- // @Router /passport/quickLogin [post]
- // @Security ApiKeyAuth
- func QuickLogin(c *gin.Context) {
- var req models.QuickLoginReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- panic(err)
- }
- tokenStr := c.GetHeader("x-app-id")
- if tokenStr != "lzyd" {
- common.GenMessageResp(c, common.InvalidParametes, "非法操作", nil)
- return
- }
- //common.LogInfo("")
- //common.LogInfo(fmt.Sprintf("-------- [%s]快捷登录START --------", req.MobilePhone))
- if len(req.MobilePhone) == 0 || len(req.ValidCode) == 0 {
- common.GenResp(c, common.InvalidParametes, "无效的参数", nil)
- return
- }
- if len(req.MobilePhone) != 11 {
- common.GenResp(c, common.MobileFormatError, "手机号码格式有误", nil)
- return
- }
- var (
- model models.UserInfo
- queryStat int
- code = common.Success
- )
- userSms := services.GetValidSmsUser(common.DB, req.MobilePhone)
- SmsTypeLogin := common.LoginTypeMobile
- if len(userSms.MobilePhone) == 0 {
- SmsTypeLogin = common.LoginTypeMobile
- }
- sms := services.GetValidSms(common.DB, req.MobilePhone, req.ValidCode, SmsTypeLogin)
- if sms.SmsId == 0 {
- code = common.SmsCodeInvalid
- } else {
- if time.Now().Unix()-common.SmsExpiredSeconds > sms.CreateAt {
- fmt.Println(time.Now().Unix()-common.SmsExpiredSeconds,sms.CreateAt)
- code = common.SmsCodeExpired
- } else {
- model, queryStat = services.LoginWithMobileOnly(req.MobilePhone)
- fmt.Println("LoginWithMobileOnly",model, queryStat)
- switch queryStat {
- case common.QueryStateSuccess:
- if err := model.Login(common.DB); err != nil {
- code = common.DbInsertErr
- }
- case common.QueryStateNone:
- model.UserId = gopkg.GenUUID()
- model.RealName = utils.KeepSecretForMobile(req.MobilePhone)
- model.UserName = services.UniqueName(model.UserName)
- model.UserAvatar = common.SystemInfo.Avatar
- model.Gender = common.GenderUnknow
- model.MobilePhone = req.MobilePhone
- model.IsDelete = common.GenderUnknow
- model.AppId = tokenStr
- //model.Password = utils.DESEncryptString("888888", true) /// 设置默认密码`888888`
- //common.LogInfo(fmt.Sprintf("没查到记录,插入记录: %v", model))
- if err := model.Register(common.DB); err != nil {
- code = common.DbInsertErr
- }
- }
- }
- }
- if code != common.Success {
- msg := ""
- if code == common.SmsCodeInvalid {
- msg = "无效的短信验证码"
- } else if code == common.DbInsertErr {
- msg = "插入记录时发生错误"
- } else if code == common.SmsCodeExpired {
- msg = "验证码已失效"
- } else {
- msg = "未知错误"
- }
- common.GenResp(c, -1, msg, nil)
- return
- }
- //common.LogInfo(fmt.Sprintf("-------- [%s]快捷登录END --------", req.MobilePhone))
- // 返回结果处理
- var ret = models.UserResp{
- Id: model.Id,
- UserId: model.UserId,
- UserName: model.UserName,
- MobilePhone: model.MobilePhone,
- UserAvatar: model.UserAvatar,
- Gender: model.Gender,
- Birthday: model.Birthday,
- Area: model.Area,
- RegisterTime: model.RegisterTime,
- AppId: model.AppId,
- }
- common.GenResp(c, code, "SUCCESS", ret)
- }
|