- 添加用户最后登录信息

This commit is contained in:
gazebo
2019-09-24 18:09:32 +08:00
parent 412b11396a
commit bf86235ce2
4 changed files with 35 additions and 7 deletions

View File

@@ -65,6 +65,7 @@ type IUserProvider interface {
UpdateUserMobile(userID string, mobile string) (err error) UpdateUserMobile(userID string, mobile string) (err error)
UpdateUserEmail(userID string, email string) (err error) UpdateUserEmail(userID string, email string) (err error)
UpdateUserType(userID string, userTypeMask int8, updateType int) (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) // CreateUser(userID2, mobile, email, name string) (user IUser, err error)
} }
@@ -222,7 +223,7 @@ func SendVerifyCode(authToken, captchaID, captchaValue, authID string) (err erro
// 公众号登录authIDTypeD是UserIDEmptyauthSecret是code这个函数是被微信的回调调用不是直接被客户端调用 // 公众号登录authIDTypeD是UserIDEmptyauthSecret是code这个函数是被微信的回调调用不是直接被客户端调用
// 微信登录authIDType是UserIDEmptyauthSecret是code这个函数是被微信的回调调用不是直接被客户端调用 // 微信登录authIDType是UserIDEmptyauthSecret是code这个函数是被微信的回调调用不是直接被客户端调用
// 小程序登录authIDType是UserIDEmptyauthSecret是jsCode // 小程序登录authIDType是UserIDEmptyauthSecret是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) authType = strings.ToLower(authType)
authIDType = strings.ToLower(authIDType) authIDType = strings.ToLower(authIDType)
if handler := authers[authType]; handler != nil { if handler := authers[authType]; handler != nil {
@@ -266,6 +267,9 @@ func Login(authType, authID, authIDType, authSecret string) (authInfo *AuthInfo,
} }
} }
authInfo = createAuthInfo(user, authBindEx) authInfo = createAuthInfo(user, authBindEx)
if user != nil && user.GetID() != "" {
userProvider.UpdateLastLogin(user.GetID(), authType, ctx.GetRealRemoteIP())
}
} }
} else { } else {
err = ErrIllegalAuthType err = ErrIllegalAuthType

View File

@@ -3,6 +3,7 @@ package cms
import ( import (
"errors" "errors"
"fmt" "fmt"
"time"
"git.rosy.net.cn/baseapi/utils/errlist" "git.rosy.net.cn/baseapi/utils/errlist"
@@ -87,6 +88,17 @@ func (*UserProvider) UpdateUserType(userID string, userTypeMask int8, updateType
return err 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) { // func (*UserProvider) CreateUser(userID2, mobile, email, name string) (user auth2.IUser, err error) {
// realUser := &model.User{ // realUser := &model.User{
// UserID2: userID2, // 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) { 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 err = err2; err == nil {
if !mobileAuth.IsUserEmpty() { if !mobileAuth.IsUserEmpty() {
return nil, jsonerr.New(mobileAuth, model.ErrCodeJsonUserAlreadyExist) return nil, jsonerr.New(mobileAuth, model.ErrCodeJsonUserAlreadyExist)

View File

@@ -1,6 +1,10 @@
package model package model
import "git.rosy.net.cn/baseapi/utils" import (
"time"
"git.rosy.net.cn/baseapi/utils"
)
const ( const (
UserStatusNormal = 1 UserStatusNormal = 1
@@ -40,6 +44,10 @@ type User struct {
IDCardNo string `orm:"size(18);column(id_card_no)" json:"idCardNo" compact:"idCardNo"` // 身份证号 IDCardNo string `orm:"size(18);column(id_card_no)" json:"idCardNo" compact:"idCardNo"` // 身份证号
Remark string `orm:"size(255)" json:"remark"` 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 { func (*User) TableUnique() [][]string {

View File

@@ -88,7 +88,8 @@ func (c *Auth2Controller) Login() {
if params.AuthType == weixin.AuthTypeMini { if params.AuthType == weixin.AuthTypeMini {
params.AuthSecret = GetComposedCode(&c.Controller, params.AuthSecret) 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 return retVal, "", err
}) })
} }
@@ -127,7 +128,8 @@ func (c *Auth2Controller) WeixinOAuth2() {
callResult *CallResult callResult *CallResult
) )
c.callWeixinOAuth2(func(params *tAuth2WeixinOAuth2Params) (retVal interface{}, errCode string, err error) { 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 { if err == nil {
callResult = &CallResult{ callResult = &CallResult{
Code: model.ErrCodeSuccess, Code: model.ErrCodeSuccess,
@@ -160,7 +162,8 @@ func (c *Auth2Controller) WeixinMPOAuth2() {
callResult *CallResult callResult *CallResult
) )
c.callWeixinMPOAuth2(func(params *tAuth2WeixinMPOAuth2Params) (retVal interface{}, errCode string, err error) { 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 { if err == nil {
callResult = &CallResult{ callResult = &CallResult{
Code: model.ErrCodeSuccess, Code: model.ErrCodeSuccess,
@@ -197,7 +200,8 @@ func (c *Auth2Controller) DingDingOAuth2() {
params.Block = params.State params.Block = params.State
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 { if err == nil {
callResult = &CallResult{ callResult = &CallResult{
Code: model.ErrCodeSuccess, Code: model.ErrCodeSuccess,