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

Skip to content

Commit f2ae624

Browse files
committed
后台支持 删除垃圾文章
1 parent 7b362a0 commit f2ae624

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed

http/controller/admin/user.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,16 @@ func (UserController) AddBlack(ctx echo.Context) error {
9090
return fail(ctx, 1, err.Error())
9191
}
9292

93-
// 获取用户上次登录 IP
93+
// 将用户 IP 加入黑名单
9494
logic.DefaultRisk.AddBlackIPByUID(uid)
9595

96+
truncate := goutils.MustBool(ctx.FormValue("truncate"))
97+
if truncate {
98+
err = logic.DefaultUser.DeleteUserContent(context.EchoContext(ctx), uid)
99+
if err != nil {
100+
return fail(ctx, 1, err.Error())
101+
}
102+
}
103+
96104
return success(ctx, nil)
97105
}

logic/user.go

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ package logic
99
import (
1010
"errors"
1111
"fmt"
12-
"github.com/studygolang/studygolang/model"
13-
"github.com/studygolang/studygolang/util"
1412
"math/rand"
1513
"net/url"
14+
"strconv"
1615
"strings"
1716
"time"
1817

18+
"github.com/studygolang/studygolang/model"
19+
"github.com/studygolang/studygolang/util"
20+
1921
"github.com/polaris1119/times"
2022

2123
"github.com/polaris1119/slices"
@@ -788,3 +790,62 @@ func (UserLogic) doCreateUser(ctx context.Context, session *xorm.Session, user *
788790

789791
return nil
790792
}
793+
794+
func (UserLogic) DeleteUserContent(ctx context.Context, uid int) error {
795+
user := &model.User{}
796+
_, err := MasterDB.ID(uid).Get(user)
797+
if err != nil || user.Username == "" {
798+
return err
799+
}
800+
801+
feedResult, feedErr := MasterDB.Exec("DELETE FROM `feed` WHERE uid=?", uid)
802+
topicResult, topicErr := MasterDB.Exec("DELETE t,tex FROM `topics` as t LEFT JOIN `topics_ex` as tex USING(tid) WHERE uid=?", uid)
803+
resourceResult, resourceErr := MasterDB.Exec("DELETE r,rex FROM `resource` as r LEFT JOIN `resource_ex` as rex USING(id) WHERE uid=?", uid)
804+
articleResult, articleErr := MasterDB.Exec("DELETE FROM `articles` WHERE author_txt=?", user.Username)
805+
806+
if feedErr == nil {
807+
affected, _ := feedResult.RowsAffected()
808+
if affected > 0 {
809+
feed := &model.Feed{}
810+
MasterDB.Desc("id").Get(feed)
811+
if feed.Id > 0 {
812+
MasterDB.Exec(`ALTER TABLE feed auto_increment=` + strconv.Itoa(feed.Id+1))
813+
}
814+
}
815+
}
816+
817+
if topicErr == nil {
818+
affected, _ := topicResult.RowsAffected()
819+
if affected > 0 {
820+
topic := &model.Topic{}
821+
MasterDB.Desc("tid").Get(topic)
822+
if topic.Tid > 0 {
823+
MasterDB.Exec(`ALTER TABLE topics auto_increment=` + strconv.Itoa(topic.Tid+1))
824+
}
825+
}
826+
}
827+
828+
if resourceErr == nil {
829+
affected, _ := resourceResult.RowsAffected()
830+
if affected > 0 {
831+
resource := &model.Resource{}
832+
MasterDB.Desc("id").Get(resource)
833+
if resource.Id > 0 {
834+
MasterDB.Exec(`ALTER TABLE resource auto_increment=` + strconv.Itoa(resource.Id+1))
835+
}
836+
}
837+
}
838+
839+
if articleErr == nil {
840+
affected, _ := articleResult.RowsAffected()
841+
if affected > 0 {
842+
article := &model.Article{}
843+
MasterDB.Desc("id").Get(article)
844+
if article.Id > 0 {
845+
MasterDB.Exec(`ALTER TABLE articles auto_increment=` + strconv.Itoa(article.Id+1))
846+
}
847+
}
848+
}
849+
850+
return nil
851+
}

template/admin/user/query.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ <h4>总数:{{ .total }}</h4><br/>
3030
ajax-hint="是否确认要加黑?"
3131
success-hint="加黑成功"
3232
callback="delCallback">加黑</a>
33+
<a data-type="ajax-submit" href="#"
34+
ajax-action="/admin/user/user/add_black?uid={{ .Uid }}&truncate=1"
35+
ajax-hint="是否确认要加黑并删除用户发布的信息?"
36+
success-hint="处理成功"
37+
callback="delCallback">加黑删除</a>
3338
</td>
3439
</tr>
3540
{{end}}

template/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<td width="10"></td>
4040

4141
<td width="auto" valign="middle">
42-
<span class="item_title"><a href="{{.Uri}}#reply{{.Cmtnum}}" class="noul">{{.Title}}</a></span>
42+
<span class="item_title"><a href="{{.Uri}}#reply{{.Cmtnum}}" class="noul" target="_blank">{{.Title}}</a></span>
4343
<div class="sep5"></div>
4444
<span class="small cc">
4545
{{if .Top}}

template/topics/common_list.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</dt>
99
<dd class="right-info">
1010
<div class="title">
11-
<a href="/topics/{{.tid}}" title="{{.title}}">{{substring .title 60 "..."}}</a>
11+
<a href="/topics/{{.tid}}" title="{{.title}}" target="_blank">{{substring .title 60 "..."}}</a>
1212
</div>
1313
<div class="meta">
1414
{{if .top}}
@@ -60,4 +60,4 @@
6060
<nav class="text-center"><ul class="pagination pagination-sm" style="margin: 1px 0;">{{.page}}</ul></nav>
6161
</div>
6262
</div>
63-
{{end}}
63+
{{end}}

0 commit comments

Comments
 (0)