Browse Source

手机号邮箱登录

ziwen 2 years ago
parent
commit
d53411a445

+ 215 - 0
controller/pc/pc.go

@@ -8,9 +8,11 @@ import (
 	pcModels "hongze/hongze_yb/models/response/pc"
 	"hongze/hongze_yb/models/tables/customer_comment"
 	"hongze/hongze_yb/models/tables/rddp/classify"
+	"hongze/hongze_yb/models/tables/rddp/msg_code"
 	"hongze/hongze_yb/models/tables/rddp/report"
 	"hongze/hongze_yb/models/tables/rddp/report_chapter"
 	"hongze/hongze_yb/models/tables/rddp/session"
+	"hongze/hongze_yb/models/tables/wx_user"
 	"hongze/hongze_yb/models/tables/wx_user_log"
 	"hongze/hongze_yb/models/tables/yb_activity"
 	"hongze/hongze_yb/models/tables/yb_pc_suncode"
@@ -631,3 +633,216 @@ QUERY_WX_USER:
 	response.OkData("查询成功", resp, c)
 	return
 }
+
+// @Title 登录
+// @Description 登录接口
+// @Param	request	body models.LoginReq true "type json string"
+// @Success 200 {object} models.LoginResp
+// @router /pc/pcLogin [post]
+func PcLogin(c *gin.Context) {
+	var req pcModels.PcLoginReq
+	if c.ShouldBind(&req) != nil {
+		response.Fail("参数异常", c)
+		return
+	}
+
+	userId := 0
+	var isAdd bool
+	if req.LoginType == 1 {
+		if req.Mobile == "" {
+			response.Fail("请输入手机号", c)
+			return
+		}
+		if req.SmsCode == "" {
+			response.Fail("请输入有效手机验证码", c)
+			return
+		}
+		item, err := msg_code.GetMsgCode(req.Mobile, req.SmsCode)
+		if err != nil {
+			if err == utils.ErrNoRow {
+				response.Fail("校验验证码失败,Err:" + err.Error(), c)
+				return
+			} else {
+				response.Fail("校验验证码失败,Err:" + err.Error(), c)
+				return
+			}
+		}
+		if item == nil {
+			response.Fail("手机验证码错误,请重新输入", c)
+			return
+		}
+
+		wxUser, err := wx_user.GetByMobile(req.Mobile)
+		if err != nil {
+			if err == utils.ErrNoRow {
+				isAdd = true
+			} else {
+				response.Fail("根据手机号获取用户信息失败,Err:" + err.Error(), c)
+				return
+			}
+		}
+		if wxUser == nil {
+			isAdd = true
+		} else {
+			userId = int(wxUser.UserID)
+		}
+		//BindMobile(openId, mobile string, userId, loginType int) (err error) {
+		//newUserId, err = models.BindMobile(openId, req.Mobile, userId, req.LoginType)
+	} else if req.LoginType == 2 {
+		if req.Email == "" {
+			response.Fail("邮箱不能为空,请输入邮箱", c)
+			return
+		}
+		if !utils.ValidateEmailFormatat(req.Email) {
+			response.Fail("邮箱格式错误,请重新输入", c)
+			return
+		}
+		if req.SmsCode == "" {
+			response.Fail("请输入有效验证码", c)
+			return
+		}
+		item, err := msg_code.GetMsgCode(req.Email, req.SmsCode)
+		if err != nil {
+			if err == utils.ErrNoRow {
+				response.Fail("校验验证码失败,Err:" + err.Error(), c)
+				return
+			} else {
+				response.Fail("校验验证码失败,Err:" + err.Error(), c)
+				return
+			}
+		}
+		if item == nil {
+			response.Fail("邮箱证码错误,请重新输入:", c)
+			return
+		}
+		wxUser, err := wx_user.GetByEmail(req.Email)
+		if err != nil {
+			if err == utils.ErrNoRow {
+				isAdd = true
+			} else {
+				response.Fail("根据邮箱获取用户信息失败,Err:" + err.Error(), c)
+				return
+			}
+		}
+		if wxUser == nil {
+			isAdd = true
+		} else {
+			userId = int(wxUser.UserID)
+		}
+	} else {
+		response.Fail("无效的登录方式,loginType:" + strconv.Itoa(req.LoginType), c)
+	}
+	if isAdd {
+		user := new(wx_user.WxUser)
+		user.CompanyID = 1
+		user.CreatedTime = time.Now()
+		user.FirstLogin = 1
+		user.Enabled = 1
+		user.Email = req.Email
+		user.Mobile = req.Mobile
+		if req.LoginType == 1 {
+			user.BindAccount = user.Mobile
+		} else {
+			user.BindAccount = user.Email
+		}
+		user.RegisterTime = time.Now()
+		user.LoginTime = time.Now()
+		user.RegisterPlatform = 2
+		if req.IsFreeLogin {
+			user.IsFreeLogin = 1
+		}else {
+			user.IsFreeLogin = 0
+		}
+
+		lastId, err := user.Add()
+		if err != nil {
+			response.Fail("登录失败,新增客户信息失败,Err:" + err.Error(), c)
+			return
+		}
+		fmt.Println("lastId:", lastId)
+		userId = int(lastId)
+
+		timeUnix := time.Now().Unix()
+		timeUnixStr := strconv.FormatInt(timeUnix, 10)
+		token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
+		//新增session
+		{
+			session := new(session.Session)
+			session.UserID = int64(userId)
+			session.CreatedTime = time.Now()
+			session.LastUpdatedTime = time.Now()
+			session.ExpireTime = time.Now().AddDate(0, 3, 0)
+			session.AccessToken = token
+			err = session.Create()
+			if err != nil {
+				response.Fail("登录失败,新增用户session信息失败:" + err.Error(), c)
+				return
+			}
+		}
+	} else {
+		user := new(wx_user.WxUser)
+		user.UserID = uint64(userId)
+		if req.IsFreeLogin {
+			user.IsFreeLogin = 1
+		}else {
+			user.IsFreeLogin = 0
+		}
+
+		err := user.Update([]string{"is_free_login"})
+		if err != nil {
+			response.Fail("登录失败,修改登录信息失败,Err:" + err.Error(), c)
+			return
+		}
+	}
+	if userId == 0 {
+		response.Fail("登录失败,id为 0", c)
+		return
+	}
+
+	err := wx_user.ModifyFirstLogin(uint64(userId))
+	if err != nil {
+		response.Fail("登录失败,判断权限失败:" + err.Error(), c)
+		return
+	}
+	var token string
+	tokenItem, err := session.GetTokenByUid(userId)
+	if err != nil && err != utils.ErrNoRow {
+		response.Fail("登录失败,获取token失败:" + err.Error(), c)
+		return
+	}
+
+	if tokenItem == nil || (err != nil && err == utils.ErrNoRow) {
+		timeUnix := time.Now().Unix()
+		timeUnixStr := strconv.FormatInt(timeUnix, 10)
+		token = utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
+		//新增session
+		{
+			session := new(session.Session)
+			session.UserID = int64(userId)
+			session.CreatedTime = time.Now()
+			session.LastUpdatedTime = time.Now()
+			session.ExpireTime = time.Now().AddDate(0, 3, 0)
+			session.AccessToken = token
+			err = session.Create()
+			if err != nil {
+				response.Fail("登录失败,新增用户session信息失败:" + err.Error(), c)
+				return
+			}
+		}
+	} else {
+		token = tokenItem.AccessToken
+	}
+
+	//新增登录日志
+	{
+		loginLog := new(wx_user_log.WxUserLog)
+		loginLog.UserID = userId
+		loginLog.Mobile = req.Mobile
+		loginLog.Email = req.Email
+		loginLog.Handle = "pc_login"
+		loginLog.CreateTime = time.Now()
+		go loginLog.Create()
+	}
+
+	response.OkData("登录成功", token, c)
+}

+ 8 - 0
models/response/pc/wechat.go

@@ -29,3 +29,11 @@ type LoginResp struct {
 	Token  string `json:"authorization"`
 	IsBind bool   `json:"is_bind"`
 }
+
+type PcLoginReq struct {
+	LoginType   int    `description:"登录方式:1:手机,2:邮箱"`
+	Mobile      string `description:"手机号"`
+	Email       string `description:"邮箱"`
+	SmsCode     string `description:"短信/邮箱验证码"`
+	IsFreeLogin bool   `description:"是否免登陆,true:免登陆,false:非免登陆"`
+}

+ 6 - 0
models/tables/rddp/session/query.go

@@ -22,3 +22,9 @@ func GetTokenByToken(token string) (item *Session, err error) {
 func getDb() *gorm.DB {
 	return global.MYSQL["rddp"]
 }
+
+// GetTokenByUid 根据用户id获取token
+func GetTokenByUid(userId int) (item *Session, err error) {
+	err = getDb().Where("user_id = ? ", userId).First(&item).Error
+	return
+}

+ 7 - 0
models/tables/wx_user/create.go

@@ -7,3 +7,10 @@ func (wxUser *WxUser) Create() (err error) {
 	err = global.DEFAULT_MYSQL.Create(wxUser).Error
 	return
 }
+
+//  Create 新增记录
+func (wxUser *WxUser) Add() (id uint64,err error) {
+	err = global.DEFAULT_MYSQL.Create(wxUser).Error
+	id = wxUser.UserID
+	return
+}