Browse Source

no message

xingzai 8 months ago
parent
commit
298ea26bf1
6 changed files with 152 additions and 5 deletions
  1. 6 1
      controllers/base_auth.go
  2. 113 2
      controllers/user.go
  3. 2 2
      controllers/wechat.go
  4. 9 0
      models/session.go
  5. 9 0
      routers/commentsRouter.go
  6. 13 0
      services/user.go

+ 6 - 1
controllers/base_auth.go

@@ -52,7 +52,7 @@ func (this *BaseAuthController) Prepare() {
 			session, err := models.GetSessionByToken(authorization)
 			if err != nil {
 				if err.Error() == utils.ErrNoRow() {
-					this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + authorization}, false, false)
+					this.JSON(models.BaseResponse{Ret: 408, Msg: "您的登录状态已过期,请重新登录", ErrMsg: "Token 信息已变更:Token: " + authorization}, false, false)
 					this.StopRun()
 					return
 				}
@@ -70,6 +70,11 @@ func (this *BaseAuthController) Prepare() {
 				nilWxUser := new(models.WxUserItem)
 				this.User = nilWxUser
 			} else {
+				if session.SessionStatus == 1 {
+					this.JSON(models.BaseResponse{Ret: 408, Msg: "您的账号在另一设备登录,当前设备已被迫下线。若不是您本人操作,请确保未泄露短信验证码或及时修改您的登录密码", ErrMsg: "sesson is empty "}, false, false)
+					this.StopRun()
+					return
+				}
 				wxUser, err := models.GetWxUserItemByMobile(session.Mobile)
 				if err != nil && err.Error() != utils.ErrNoRow() {
 					this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取信息失败 " + strconv.Itoa(session.UserId)}, false, false)

+ 113 - 2
controllers/user.go

@@ -164,8 +164,8 @@ func (this *UserCommonController) Login() {
 		itemsSession.AccessToken = token
 		itemsSession.CreatedTime = time.Now()
 		itemsSession.LastUpdatedTime = time.Now()
-		itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0)
-		err = models.AddCygxMfyxWebSession(itemsSession)
+		itemsSession.ExpireTime = time.Now().AddDate(0, 0, 30)
+		err = services.HandleCygxMfyxWebSession(itemsSession)
 		if err != nil {
 			br.Msg = "获取用户信息失败"
 			br.ErrMsg = "添加Token失败,Err:" + err.Error()
@@ -2083,3 +2083,114 @@ func (this *UserController) UpdatePass() {
 	br.Success = true
 	br.Msg = "操作成功"
 }
+
+// @Title 重置密码接口(忘记密码)
+// @Description 设置密码接口
+// @Param	request	body models.SetUserPasswordReq true "type json string"
+// @Success 200 {object} models.LoginResp
+// @router /reset_pass [post]
+func (this *UserCommonController) ReSetPass() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	var req models.SetUserPasswordReq
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
+	if err != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + err.Error()
+		return
+	}
+	mobile := req.Mobile
+	mobile = strings.Trim(mobile, " ")
+	if mobile == "" {
+		br.Msg = "参数错误"
+		br.ErrMsg = "参数错误,手机号为空"
+		return
+	}
+	code := req.VCode
+	if code == "" {
+		br.Msg = "参数错误"
+		br.ErrMsg = "Code 为空"
+		return
+	}
+	password := req.Password
+	if password == "" {
+		br.Msg = "参数错误"
+		br.ErrMsg = "Password 为空"
+		return
+	}
+
+	item, err := models.GetMsgCode(req.Mobile, req.VCode)
+	if err != nil {
+		if err.Error() == utils.ErrNoRow() {
+			br.Msg = "验证码错误,请重新输入"
+			br.ErrMsg = "校验验证码失败,Err:" + err.Error()
+			return
+		} else {
+			br.Msg = "验证码错误,请重新输入"
+			br.ErrMsg = "校验验证码失败,Err:" + err.Error()
+			return
+		}
+	}
+	if item == nil {
+		br.Msg = "验证码错误,请重新输入"
+		return
+	}
+
+	user, err := models.GetWxUserItemByUserMobile(mobile)
+	if err != nil {
+		br.Msg = "获取信息失败"
+		br.ErrMsg = "获取信息失败,Err:" + err.Error()
+		return
+	}
+
+	specialAuthorCheck := services.GetYanxuanSpecialAuthorInfo(user) //用户是否没开通研选专栏以及,专栏信息是否完善
+	if !specialAuthorCheck.IsAuthor {
+		br.Msg = "该账号不是专栏作者,请使用验证码登录。"
+		return
+	}
+	isSetPassword := services.GetIsSetPassword(mobile)
+	if isSetPassword {
+		err = models.UpdateCygxUserPassword(mobile, password)
+	} else {
+		itemps := new(models.CygxUserPassword)
+		itemps.UserId = user.UserId
+		itemps.Mobile = mobile
+		itemps.RealName = user.RealName
+		itemps.Password = password
+		itemps.CreateTime = time.Now()
+		itemps.ModifyTime = time.Now()
+		err = models.AddCygxUserPassword(itemps)
+	}
+	var token string
+	timeUnix := time.Now().Unix()
+	timeUnixStr := strconv.FormatInt(timeUnix, 10)
+	token = utils.MD5(mobile) + utils.MD5(timeUnixStr)
+	itemsSession := new(models.CygxMfyxWebSession)
+	itemsSession.UserId = user.UserId
+	itemsSession.Mobile = mobile
+	itemsSession.AccessToken = token
+	itemsSession.CreatedTime = time.Now()
+	itemsSession.LastUpdatedTime = time.Now()
+	itemsSession.ExpireTime = time.Now().AddDate(0, 0, 30)
+	err = services.HandleCygxMfyxWebSession(itemsSession)
+	if err != nil {
+		br.Msg = "获取用户信息失败"
+		br.ErrMsg = "添加Token失败,Err:" + err.Error()
+		return
+	}
+
+	resp := new(models.LoginResp)
+	resp.UserId = user.UserId
+	resp.Headimgurl = user.Headimgurl
+	resp.Mobile = user.Mobile
+	resp.Email = user.Email
+	resp.CompanyName = user.CompanyName
+	resp.Authorization = token
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+	br.Data = resp
+}

+ 2 - 2
controllers/wechat.go

@@ -126,12 +126,12 @@ func (this *WechatCommonController) WechatLogin() {
 		itemsSession.AccessToken = token
 		itemsSession.CreatedTime = time.Now()
 		itemsSession.LastUpdatedTime = time.Now()
-		itemsSession.ExpireTime = time.Now().AddDate(0, 3, 0)
+		itemsSession.ExpireTime = time.Now().AddDate(0, 0, 30)
 		if user != nil {
 			itemsSession.UserId = user.UserId
 			itemsSession.Mobile = user.Mobile
 		}
-		err = models.AddCygxMfyxWebSession(itemsSession)
+		err = services.HandleCygxMfyxWebSession(itemsSession)
 		if err != nil {
 			br.Msg = "获取用户信息失败"
 			br.ErrMsg = "添加Token失败,Err:" + err.Error()

+ 9 - 0
models/session.go

@@ -35,6 +35,7 @@ type CygxMfyxWebSession struct {
 	LastUpdatedTime time.Time
 	OpenId          string `description:"用户openid,最大长度:32"`
 	UnionId         string `description:"用户unionid,最大长度:64"`
+	SessionStatus   int    `description:"token状态,0正常,1被顶号"`
 }
 
 // 添加用户session信息
@@ -81,3 +82,11 @@ func GetUserSessionByUserId(userId int) (item *CygxMfyxWebSession, err error) {
 	err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
 	return
 }
+
+// 把其他有效token改为被顶号状态
+func UpdateSessionStatusByMobile(mobile, token string) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE cygx_mfyx_web_session SET session_status= 1   WHERE  mobile=? AND expire_time> NOW()  AND access_token != ?  `
+	_, err = o.Raw(sql, mobile, token).Exec()
+	return
+}

+ 9 - 0
routers/commentsRouter.go

@@ -844,6 +844,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hongze_web_mfyx/controllers:UserCommonController"] = append(beego.GlobalControllerRouter["hongze/hongze_web_mfyx/controllers:UserCommonController"],
+        beego.ControllerComments{
+            Method: "ReSetPass",
+            Router: `/reset_pass`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hongze_web_mfyx/controllers:UserCommonController"] = append(beego.GlobalControllerRouter["hongze/hongze_web_mfyx/controllers:UserCommonController"],
         beego.ControllerComments{
             Method: "SetPass",

+ 13 - 0
services/user.go

@@ -708,3 +708,16 @@ func GetIsSetPassword(mobile string) (isSetPassword bool) {
 	}
 	return
 }
+
+// 处理用户登录之后的session
+func HandleCygxMfyxWebSession(item *models.CygxMfyxWebSession) (err error) {
+	err = models.AddCygxMfyxWebSession(item)
+	if err != nil {
+		return
+	}
+
+	if item.Mobile != "" {
+		err = models.UpdateSessionStatusByMobile(item.Mobile, item.AccessToken)
+	}
+	return
+}