From bf86235ce25d937e46563d701d9173a66814c1b8 Mon Sep 17 00:00:00 2001 From: gazebo Date: Tue, 24 Sep 2019 18:09:32 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E7=99=BB=E5=BD=95=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/auth2/auth2.go | 6 +++++- business/jxstore/cms/user2.go | 14 +++++++++++++- business/model/user.go | 10 +++++++++- controllers/auth2.go | 12 ++++++++---- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/business/auth2/auth2.go b/business/auth2/auth2.go index 4071af794..739782756 100644 --- a/business/auth2/auth2.go +++ b/business/auth2/auth2.go @@ -65,6 +65,7 @@ type IUserProvider interface { UpdateUserMobile(userID string, mobile string) (err error) UpdateUserEmail(userID string, email string) (err error) UpdateUserType(userID string, userTypeMask int8, updateType int) (err error) + UpdateLastLogin(userID string, lastLoginType, fromIP string) (err error) // CreateUser(userID2, mobile, email, name string) (user IUser, err error) } @@ -222,7 +223,7 @@ func SendVerifyCode(authToken, captchaID, captchaValue, authID string) (err erro // 公众号登录:authIDTypeD是UserIDEmpty,authSecret是code(这个函数是被微信的回调调用,不是直接被客户端调用) // 微信登录:authIDType是UserIDEmpty,authSecret是code(这个函数是被微信的回调调用,不是直接被客户端调用) // 小程序登录:authIDType是UserIDEmpty,authSecret是jsCode -func Login(authType, authID, authIDType, authSecret string) (authInfo *AuthInfo, err error) { +func Login(ctx *Context, authType, authID, authIDType, authSecret string) (authInfo *AuthInfo, err error) { authType = strings.ToLower(authType) authIDType = strings.ToLower(authIDType) if handler := authers[authType]; handler != nil { @@ -266,6 +267,9 @@ func Login(authType, authID, authIDType, authSecret string) (authInfo *AuthInfo, } } authInfo = createAuthInfo(user, authBindEx) + if user != nil && user.GetID() != "" { + userProvider.UpdateLastLogin(user.GetID(), authType, ctx.GetRealRemoteIP()) + } } } else { err = ErrIllegalAuthType diff --git a/business/jxstore/cms/user2.go b/business/jxstore/cms/user2.go index c1729ec22..55c9d1358 100644 --- a/business/jxstore/cms/user2.go +++ b/business/jxstore/cms/user2.go @@ -3,6 +3,7 @@ package cms import ( "errors" "fmt" + "time" "git.rosy.net.cn/baseapi/utils/errlist" @@ -87,6 +88,17 @@ func (*UserProvider) UpdateUserType(userID string, userTypeMask int8, updateType return err } +func (*UserProvider) UpdateLastLogin(userID string, lastLoginType, fromIP string) (err error) { + _, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{ + "LastLoginAt": utils.Time2Pointer(time.Now()), + "LastLoginType": lastLoginType, + "LastLoginIP": fromIP, + }, model.AdminName, map[string]interface{}{ + "UserID": userID, + }) + return err +} + // func (*UserProvider) CreateUser(userID2, mobile, email, name string) (user auth2.IUser, err error) { // realUser := &model.User{ // UserID2: userID2, @@ -102,7 +114,7 @@ func init() { } func RegisterUserWithMobile(ctx *jxcontext.Context, user *model.User, mobileVerifyCode string, inAuthInfo *auth2.AuthInfo) (outAuthInfo *auth2.AuthInfo, err error) { - mobileAuth, err2 := auth2.Login(auth2.AuthTypeMobile, user.GetMobile(), auth2.UserIDMobile, mobileVerifyCode) + mobileAuth, err2 := auth2.Login(ctx.Context, auth2.AuthTypeMobile, user.GetMobile(), auth2.UserIDMobile, mobileVerifyCode) if err = err2; err == nil { if !mobileAuth.IsUserEmpty() { return nil, jsonerr.New(mobileAuth, model.ErrCodeJsonUserAlreadyExist) diff --git a/business/model/user.go b/business/model/user.go index b71e6638a..fe65d11e1 100644 --- a/business/model/user.go +++ b/business/model/user.go @@ -1,6 +1,10 @@ package model -import "git.rosy.net.cn/baseapi/utils" +import ( + "time" + + "git.rosy.net.cn/baseapi/utils" +) const ( UserStatusNormal = 1 @@ -40,6 +44,10 @@ type User struct { IDCardNo string `orm:"size(18);column(id_card_no)" json:"idCardNo" compact:"idCardNo"` // 身份证号 Remark string `orm:"size(255)" json:"remark"` + + LastLoginAt *time.Time `orm:"null" json:"lastLoginAt"` + LastLoginIP string `orm:"size(64);column(last_login_ip)" json:"lastLoginIP"` + LastLoginType string `orm:"size(16)" json:"lastLoginType"` } func (*User) TableUnique() [][]string { diff --git a/controllers/auth2.go b/controllers/auth2.go index 0578bd6cc..38490265e 100644 --- a/controllers/auth2.go +++ b/controllers/auth2.go @@ -88,7 +88,8 @@ func (c *Auth2Controller) Login() { if params.AuthType == weixin.AuthTypeMini { params.AuthSecret = GetComposedCode(&c.Controller, params.AuthSecret) } - retVal, err = auth2.Login(params.AuthType, params.AuthID, params.AuthIDType, params.AuthSecret) + ctx := auth2.NewContext(c.Ctx.ResponseWriter, c.Ctx.Request) + retVal, err = auth2.Login(ctx, params.AuthType, params.AuthID, params.AuthIDType, params.AuthSecret) return retVal, "", err }) } @@ -127,7 +128,8 @@ func (c *Auth2Controller) WeixinOAuth2() { callResult *CallResult ) c.callWeixinOAuth2(func(params *tAuth2WeixinOAuth2Params) (retVal interface{}, errCode string, err error) { - authInfo, err := auth2.Login(weixin.AuthTypeWeixin, params.State, "", params.Code) + ctx := auth2.NewContext(c.Ctx.ResponseWriter, c.Ctx.Request) + authInfo, err := auth2.Login(ctx, weixin.AuthTypeWeixin, params.State, "", params.Code) if err == nil { callResult = &CallResult{ Code: model.ErrCodeSuccess, @@ -160,7 +162,8 @@ func (c *Auth2Controller) WeixinMPOAuth2() { callResult *CallResult ) c.callWeixinMPOAuth2(func(params *tAuth2WeixinMPOAuth2Params) (retVal interface{}, errCode string, err error) { - authInfo, err := auth2.Login(weixin.AuthTypeMP, params.State, "", params.Code) + ctx := auth2.NewContext(c.Ctx.ResponseWriter, c.Ctx.Request) + authInfo, err := auth2.Login(ctx, weixin.AuthTypeMP, params.State, "", params.Code) if err == nil { callResult = &CallResult{ Code: model.ErrCodeSuccess, @@ -197,7 +200,8 @@ func (c *Auth2Controller) DingDingOAuth2() { params.Block = params.State params.State = "" } - authInfo, err := auth2.Login(dingding.AuthTypeQRCode, params.State, "", params.Code) + ctx := auth2.NewContext(c.Ctx.ResponseWriter, c.Ctx.Request) + authInfo, err := auth2.Login(ctx, dingding.AuthTypeQRCode, params.State, "", params.Code) if err == nil { callResult = &CallResult{ Code: model.ErrCodeSuccess,