Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1138430

Browse files
committed
记录用户最后登录时间
1 parent b139d05 commit 1138430

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

websites/code/studygolang/src/model/user.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import (
1515

1616
// 用户登录信息
1717
type UserLogin struct {
18-
Uid int `json:"uid"`
19-
Username string `json:"username"`
20-
Passwd string `json:"passwd"`
21-
Email string `json:"email"`
22-
passcode string // 加密随机串
18+
Uid int `json:"uid"`
19+
Username string `json:"username"`
20+
Passwd string `json:"passwd"`
21+
Email string `json:"email"`
22+
LoginTime string `json:"login_time"`
23+
passcode string // 加密随机串
2324

2425
// 数据库访问对象
2526
*Dao
@@ -57,9 +58,9 @@ func (this *UserLogin) Set(clause string) *UserLogin {
5758
}
5859

5960
func (this *UserLogin) prepareInsertData() {
60-
this.columns = []string{"uid", "username", "passwd", "email", "passcode"}
61+
this.columns = []string{"uid", "username", "passwd", "email", "login_time", "passcode"}
6162
this.GenMd5Passwd("")
62-
this.colValues = []interface{}{this.Uid, this.Username, this.Passwd, this.Email, this.passcode}
63+
this.colValues = []interface{}{this.Uid, this.Username, this.Passwd, this.Email, this.LoginTime, this.passcode}
6364
}
6465

6566
// 生成加密密码
@@ -76,11 +77,12 @@ func (this *UserLogin) GenMd5Passwd(origPwd string) string {
7677
// 由于在DAO中没法调用 具体 model 的方法,如果将该映射关系定义为 具体 model 字段,有些浪费
7778
func (this *UserLogin) colFieldMap() map[string]interface{} {
7879
return map[string]interface{}{
79-
"uid": &this.Uid,
80-
"username": &this.Username,
81-
"passwd": &this.Passwd,
82-
"email": &this.Email,
83-
"passcode": &this.passcode,
80+
"uid": &this.Uid,
81+
"username": &this.Username,
82+
"passwd": &this.Passwd,
83+
"email": &this.Email,
84+
"login_time": &this.LoginTime,
85+
"passcode": &this.passcode,
8486
}
8587
}
8688

websites/code/studygolang/src/service/user.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"model"
1313
"net/url"
1414
"strconv"
15+
"time"
1516
"util"
1617
)
1718

@@ -134,6 +135,9 @@ func FindCurrentUser(username string) (user map[string]interface{}, err error) {
134135
user["isadmin"] = true
135136
}
136137
}
138+
139+
RecordLoginTime(username)
140+
137141
return
138142
}
139143

@@ -271,9 +275,21 @@ func Login(username, passwd string) (*model.UserLogin, error) {
271275
// 登录,活跃度+1
272276
go IncUserWeight("uid="+strconv.Itoa(userLogin.Uid), 1)
273277

278+
RecordLoginTime(username)
279+
274280
return userLogin, nil
275281
}
276282

283+
// 记录用户最后登录时间
284+
func RecordLoginTime(username string) error {
285+
userLogin := model.NewUserLogin()
286+
err := userLogin.Set("login_time=" + time.Now().Format("2006-01-02 15:04:05")).Where("username=" + username).Update()
287+
if err != nil {
288+
logger.Errorf("记录用户 %s 登录时间错误:%s", username, err)
289+
}
290+
return err
291+
}
292+
277293
// 更新用户密码(用户名或email)
278294
func UpdatePasswd(username, passwd string) (string, error) {
279295
userLogin := model.NewUserLogin()

websites/databases/studygolang_db.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,14 @@ CREATE TABLE `user_login` (
118118
`username` varchar(20) NOT NULL COMMENT '用户名',
119119
`passcode` char(12) NOT NULL DEFAULT '' COMMENT '加密随机数',
120120
`passwd` char(32) NOT NULL DEFAULT '' COMMENT 'md5密码',
121+
`login_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后一次登录时间(主动登录或cookie登录)',
121122
PRIMARY KEY (`uid`),
122123
UNIQUE KEY (`username`),
123124
UNIQUE KEY (`email`)
124125
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
125126

127+
alter table `studygolang`.`user_login` add column `login_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后一次登录时间(主动登录或cookie登录)' after `passwd`
128+
126129
/*---------------------------------------------------------------------------*
127130
NAME: bind_user
128131
用途:第三方绑定表

0 commit comments

Comments
 (0)