lejianwen 1 год назад
Родитель
Сommit
e8b2425222

+ 1 - 1
cmd/apimain.go

@@ -169,7 +169,7 @@ func InitGlobal() {
169 169
 	global.Lock = lock.NewLocal()
170 170
 }
171 171
 func DatabaseAutoUpdate() {
172
-	version := 246
172
+	version := 247
173 173
 
174 174
 	db := global.DB
175 175
 

+ 7 - 16
http/controller/admin/loginLog.go

@@ -56,10 +56,6 @@ func (ct *LoginLog) List(c *gin.Context) {
56 56
 		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
57 57
 		return
58 58
 	}
59
-	u := service.AllService.UserService.CurUser(c)
60
-	if !service.AllService.UserService.IsAdmin(u) || query.IsMy == 1 {
61
-		query.UserId = int(u.Id)
62
-	}
63 59
 	res := service.AllService.LoginLogService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
64 60
 		if query.UserId > 0 {
65 61
 			tx.Where("user_id = ?", query.UserId)
@@ -93,21 +89,16 @@ func (ct *LoginLog) Delete(c *gin.Context) {
93 89
 		return
94 90
 	}
95 91
 	l := service.AllService.LoginLogService.InfoById(f.Id)
96
-	u := service.AllService.UserService.CurUser(c)
97
-	if !service.AllService.UserService.IsAdmin(u) && l.UserId != u.Id {
98
-		response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
92
+	if l.Id == 0 {
93
+		response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
99 94
 		return
100 95
 	}
101
-	if l.Id > 0 {
102
-		err := service.AllService.LoginLogService.Delete(l)
103
-		if err == nil {
104
-			response.Success(c, nil)
105
-			return
106
-		}
107
-		response.Fail(c, 101, err.Error())
96
+	err := service.AllService.LoginLogService.Delete(l)
97
+	if err == nil {
98
+		response.Success(c, nil)
108 99
 		return
109 100
 	}
110
-	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
101
+	response.Fail(c, 101, err.Error())
111 102
 }
112 103
 
113 104
 // BatchDelete 删除
@@ -119,7 +110,7 @@ func (ct *LoginLog) Delete(c *gin.Context) {
119 110
 // @Param body body admin.LoginLogIds true "登录日志"
120 111
 // @Success 200 {object} response.Response
121 112
 // @Failure 500 {object} response.Response
122
-// @Router /admin/login_log/delete [post]
113
+// @Router /admin/login_log/batchDelete [post]
123 114
 // @Security token
124 115
 func (ct *LoginLog) BatchDelete(c *gin.Context) {
125 116
 	f := &admin.LoginLogIds{}

+ 113 - 0
http/controller/admin/my/loginLog.go

@@ -0,0 +1,113 @@
1
+package my
2
+
3
+import (
4
+	"Gwen/global"
5
+	"Gwen/http/request/admin"
6
+	"Gwen/http/response"
7
+	"Gwen/model"
8
+	"Gwen/service"
9
+	"github.com/gin-gonic/gin"
10
+	"gorm.io/gorm"
11
+)
12
+
13
+type LoginLog struct {
14
+}
15
+
16
+// List 列表
17
+// @Tags 我的登录日志
18
+// @Summary 登录日志列表
19
+// @Description 登录日志列表
20
+// @Accept  json
21
+// @Produce  json
22
+// @Param page query int false "页码"
23
+// @Param page_size query int false "页大小"
24
+// @Param user_id query int false "用户ID"
25
+// @Success 200 {object} response.Response{data=model.LoginLogList}
26
+// @Failure 500 {object} response.Response
27
+// @Router /admin/my/login_log/list [get]
28
+// @Security token
29
+func (ct *LoginLog) List(c *gin.Context) {
30
+	query := &admin.LoginLogQuery{}
31
+	if err := c.ShouldBindQuery(query); err != nil {
32
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
33
+		return
34
+	}
35
+	u := service.AllService.UserService.CurUser(c)
36
+	res := service.AllService.LoginLogService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
37
+		tx.Where("user_id = ? and is_deleted = ?", u.Id, model.IsDeletedNo)
38
+		tx.Order("id desc")
39
+	})
40
+	response.Success(c, res)
41
+}
42
+
43
+// Delete 删除
44
+// @Tags 我的登录日志
45
+// @Summary 登录日志删除
46
+// @Description 登录日志删除
47
+// @Accept  json
48
+// @Produce  json
49
+// @Param body body model.LoginLog true "登录日志信息"
50
+// @Success 200 {object} response.Response
51
+// @Failure 500 {object} response.Response
52
+// @Router /admin/my/login_log/delete [post]
53
+// @Security token
54
+func (ct *LoginLog) Delete(c *gin.Context) {
55
+	f := &model.LoginLog{}
56
+	if err := c.ShouldBindJSON(f); err != nil {
57
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
58
+		return
59
+	}
60
+	id := f.Id
61
+	errList := global.Validator.ValidVar(c, id, "required,gt=0")
62
+	if len(errList) > 0 {
63
+		response.Fail(c, 101, errList[0])
64
+		return
65
+	}
66
+	l := service.AllService.LoginLogService.InfoById(f.Id)
67
+	if l.Id == 0 || l.IsDeleted == model.IsDeletedYes {
68
+		response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
69
+		return
70
+	}
71
+	u := service.AllService.UserService.CurUser(c)
72
+	if l.UserId != u.Id {
73
+		response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
74
+		return
75
+	}
76
+	err := service.AllService.LoginLogService.SoftDelete(l)
77
+	if err == nil {
78
+		response.Success(c, nil)
79
+		return
80
+	}
81
+	response.Fail(c, 101, err.Error())
82
+}
83
+
84
+// BatchDelete 删除
85
+// @Tags 我的登录日志
86
+// @Summary 登录日志批量删除
87
+// @Description 登录日志批量删除
88
+// @Accept  json
89
+// @Produce  json
90
+// @Param body body admin.LoginLogIds true "登录日志"
91
+// @Success 200 {object} response.Response
92
+// @Failure 500 {object} response.Response
93
+// @Router /admin/my/login_log/batchDelete [post]
94
+// @Security token
95
+func (ct *LoginLog) BatchDelete(c *gin.Context) {
96
+	f := &admin.LoginLogIds{}
97
+	if err := c.ShouldBindJSON(f); err != nil {
98
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
99
+		return
100
+	}
101
+	if len(f.Ids) == 0 {
102
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
103
+		return
104
+	}
105
+	u := service.AllService.UserService.CurUser(c)
106
+	err := service.AllService.LoginLogService.BatchSoftDelete(u.Id, f.Ids)
107
+	if err == nil {
108
+		response.Success(c, nil)
109
+		return
110
+	}
111
+	response.Fail(c, 101, err.Error())
112
+	return
113
+}

+ 1 - 1
http/response/admin/user.go

@@ -19,7 +19,7 @@ func (lp *LoginPayload) FromUser(user *model.User) {
19 19
 }
20 20
 
21 21
 var UserRouteNames = []string{
22
-	"MyTagList", "MyAddressBookList", "MyInfo", "MyAddressBookCollection", "MyPeer", "MyShareRecordList",
22
+	"MyTagList", "MyAddressBookList", "MyInfo", "MyAddressBookCollection", "MyPeer", "MyShareRecordList", "MyLoginLog",
23 23
 }
24 24
 var AdminRouteNames = []string{"*"}
25 25
 

+ 8 - 1
http/router/admin.go

@@ -160,8 +160,8 @@ func OauthBind(rg *gin.RouterGroup) {
160 160
 
161 161
 }
162 162
 func LoginLogBind(rg *gin.RouterGroup) {
163
-	aR := rg.Group("/login_log")
164 163
 	cont := &admin.LoginLog{}
164
+	aR := rg.Group("/login_log").Use(middleware.AdminPrivilege())
165 165
 	aR.GET("/list", cont.List)
166 166
 	aR.POST("/delete", cont.Delete)
167 167
 	aR.POST("/batchDelete", cont.BatchDelete)
@@ -274,6 +274,13 @@ func MyBind(rg *gin.RouterGroup) {
274 274
 		rg.GET("/my/peer/list", cont.List)
275 275
 
276 276
 	}
277
+
278
+	{
279
+		cont := &my.LoginLog{}
280
+		rg.GET("/my/login_log/list", cont.List)
281
+		rg.POST("/my/login_log/delete", cont.Delete)
282
+		rg.POST("/my/login_log/batchDelete", cont.BatchDelete)
283
+	}
277 284
 }
278 285
 
279 286
 func ShareRecordBind(rg *gin.RouterGroup) {

+ 7 - 1
model/loginLog.go

@@ -4,12 +4,13 @@ type LoginLog struct {
4 4
 	IdModel
5 5
 	UserId      uint   `json:"user_id" gorm:"default:0;not null;"`
6 6
 	Client      string `json:"client"` //webadmin,webclient,app,
7
-	DeviceId	string `json:"device_id"`
7
+	DeviceId    string `json:"device_id"`
8 8
 	Uuid        string `json:"uuid"`
9 9
 	Ip          string `json:"ip"`
10 10
 	Type        string `json:"type"`     //account,oauth
11 11
 	Platform    string `json:"platform"` //windows,linux,mac,android,ios
12 12
 	UserTokenId uint   `json:"user_token_id" gorm:"default:0;not null;"`
13
+	IsDeleted   uint   `json:"is_deleted" gorm:"default:0;not null;"`
13 14
 	TimeModel
14 15
 }
15 16
 
@@ -24,6 +25,11 @@ const (
24 25
 	LoginLogTypeOauth   = "oauth"
25 26
 )
26 27
 
28
+const (
29
+	IsDeletedNo  = 0
30
+	IsDeletedYes = 1
31
+)
32
+
27 33
 type LoginLogList struct {
28 34
 	LoginLogs []*LoginLog `json:"list"`
29 35
 	Pagination

+ 9 - 0
service/loginLog.go

@@ -47,3 +47,12 @@ func (us *LoginLogService) Update(u *model.LoginLog) error {
47 47
 func (us *LoginLogService) BatchDelete(ids []uint) error {
48 48
 	return global.DB.Where("id in (?)", ids).Delete(&model.LoginLog{}).Error
49 49
 }
50
+
51
+func (us *LoginLogService) SoftDelete(l *model.LoginLog) error {
52
+	l.IsDeleted = model.IsDeletedYes
53
+	return us.Update(l)
54
+}
55
+
56
+func (us *LoginLogService) BatchSoftDelete(uid uint, ids []uint) error {
57
+	return global.DB.Model(&model.LoginLog{}).Where("user_id = ? and id in (?)", uid, ids).Update("is_deleted", model.IsDeletedYes).Error
58
+}