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

Skip to content

Commit 9696d51

Browse files
committed
列表页赞&限制新用户发布信息
1 parent 5e76c72 commit 9696d51

File tree

12 files changed

+378
-160
lines changed

12 files changed

+378
-160
lines changed

http/controller/index.go

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package controller
99
import (
1010
"bytes"
1111
"html/template"
12-
"math/rand"
1312
"net/http"
1413
"net/url"
1514
"strings"
@@ -19,7 +18,7 @@ import (
1918
"github.com/studygolang/studygolang/logic"
2019
"github.com/studygolang/studygolang/model"
2120

22-
echo "github.com/labstack/echo/v4"
21+
"github.com/labstack/echo/v4"
2322
"github.com/polaris1119/config"
2423
"github.com/polaris1119/goutils"
2524
"github.com/polaris1119/logger"
@@ -68,58 +67,6 @@ func (IndexController) Index(ctx echo.Context) error {
6867
return render(ctx, "index.html", data)
6968
}
7069

71-
// Index 首页
72-
func (IndexController) OldIndex(ctx echo.Context) error {
73-
num := 10
74-
paginator := logic.NewPaginatorWithPerPage(1, num)
75-
topicsList := make([]map[string]interface{}, num)
76-
77-
// 置顶的topic
78-
topTopics := logic.DefaultTopic.FindAll(context.EchoContext(ctx), paginator, "ctime DESC", "top=1")
79-
if len(topTopics) < num {
80-
// 获取最新帖子
81-
paginator.SetPerPage(num - len(topTopics))
82-
newTopics := logic.DefaultTopic.FindAll(context.EchoContext(ctx), paginator, "ctime DESC", "top=0")
83-
84-
topicsList = append(topTopics, newTopics...)
85-
}
86-
87-
// 获得最新博文
88-
recentArticles := logic.DefaultArticle.FindBy(context.EchoContext(ctx), 10)
89-
// 获取当前用户喜欢对象信息
90-
var likeFlags map[int]int
91-
92-
if len(recentArticles) > 0 {
93-
curUser, ok := ctx.Get("user").(*model.Me)
94-
if ok {
95-
likeFlags, _ = logic.DefaultLike.FindUserLikeObjects(context.EchoContext(ctx), curUser.Uid, model.TypeArticle, recentArticles[0].Id, recentArticles[len(recentArticles)-1].Id)
96-
}
97-
}
98-
99-
// 资源
100-
resources := logic.DefaultResource.FindBy(context.EchoContext(ctx), 10)
101-
102-
books := logic.DefaultGoBook.FindBy(context.EchoContext(ctx), 24)
103-
if len(books) > 8 {
104-
bookNum := 8
105-
bookStart := rand.Intn(len(books) - bookNum)
106-
books = books[bookStart : bookStart+bookNum]
107-
}
108-
109-
// 学习资料
110-
materials := logic.DefaultLearningMaterial.FindAll(context.EchoContext(ctx))
111-
112-
return render(ctx, "index.html",
113-
map[string]interface{}{
114-
"topics": topicsList,
115-
"articles": recentArticles,
116-
"likeflags": likeFlags,
117-
"resources": resources,
118-
"books": books,
119-
"materials": materials,
120-
})
121-
}
122-
12370
// WrapUrl 包装链接
12471
func (IndexController) WrapUrl(ctx echo.Context) error {
12572
tUrl := ctx.QueryParam("u")

logic/index.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ func (self IndexLogic) FindData(ctx context.Context, tab string, paginator *Pagi
129129
data["feeds"] = self.findFeeds(ctx, paginator, tab)
130130
}
131131

132+
// 获取当前用户喜欢对象信息,有可能出现喜欢过,但是前端页面没正确显示
133+
me, ok := ctx.Value("user").(*model.Me)
134+
likeFlags := make(map[int]map[int]int)
135+
if ok {
136+
likeFlags, _ = DefaultLike.FindUserRecentLikes(ctx, me.Uid, 100)
137+
}
138+
data["likeflags"] = likeFlags
139+
132140
return data
133141
}
134142

logic/like.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package logic
99
import (
1010
"errors"
1111
"fmt"
12+
"time"
1213

1314
. "github.com/studygolang/studygolang/db"
1415

@@ -65,6 +66,32 @@ func (LikeLogic) FindUserLikeObjects(ctx context.Context, uid, objtype int, obji
6566
return likeFlags, nil
6667
}
6768

69+
// FindUserRecentLikes 获取用户最近喜欢的对象(不过滤对象)
70+
func (LikeLogic) FindUserRecentLikes(ctx context.Context, uid, limit int) (map[int]map[int]int, error) {
71+
objLog := GetLogger(ctx)
72+
73+
likes := make([]*model.Like, 0)
74+
// 过去 7 天内的
75+
err := MasterDB.Where("uid=? AND ctime>?", uid, time.Now().Add(-7*86400e9)).Limit(limit).Find(&likes)
76+
if err != nil {
77+
objLog.Errorln("LikeLogic FindUserRecentLikes error:", err)
78+
return nil, err
79+
}
80+
81+
likeFlags := make(map[int]map[int]int, len(likes))
82+
for _, like := range likes {
83+
if _, ok := likeFlags[like.Objid]; ok {
84+
likeFlags[like.Objid][like.Objtype] = like.Flag
85+
} else {
86+
likeFlags[like.Objid] = map[int]int{
87+
like.Objtype: like.Flag,
88+
}
89+
}
90+
}
91+
92+
return likeFlags, nil
93+
}
94+
6895
// LikeObject 喜欢或取消喜欢
6996
// objid 注册的喜欢对象
7097
// uid 喜欢的人

model/feed.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Feed struct {
2828
Lastreplytime OftenTime
2929
Tags string
3030
Cmtnum int
31+
Likenum int
3132
Top uint8
3233
Seq int
3334
State int

static/css/main.css

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,3 +456,47 @@ img.avatar { -moz-border-radius: 4px; border-radius: 4px; }
456456
#bottom .nav-content {
457457
margin: 0px auto 0px auto;
458458
}
459+
460+
.zan-operation {
461+
cursor: pointer;
462+
}
463+
.zan-operation:hover {
464+
color: #ce7358;
465+
}
466+
467+
.zan-operation .zan-wrap {
468+
background-color: rgba(1,126,102,0.08);
469+
color: #df957e;
470+
padding: 0;
471+
display: inline-block;
472+
height: 20px;
473+
width: 20px;
474+
line-height: 20px;
475+
text-align: center;
476+
margin-right: 5px;
477+
border-radius: 10px;
478+
margin-bottom: 1px;
479+
}
480+
.zan-operation:hover .zan-wrap, .zan-operation.active .zan-wrap {
481+
background-color: #ce7358;
482+
color: #FFF
483+
}
484+
.zan-operation .fa {
485+
font-size: 12px !important;
486+
vertical-align: baseline;
487+
}
488+
.zan-operation .fa:hover {
489+
color: #FFF !important;
490+
}
491+
.zan-operation .zan-num {
492+
color: #df957e;
493+
font-weight: bold;
494+
}
495+
.zan-operation .zan-num::before {
496+
content: 'x ';
497+
font-size: 12px;
498+
}
499+
.dot {
500+
color: #999;
501+
font-weight: normal;
502+
}

static/dist/css/sg_styles.css

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,50 @@ img.avatar { -moz-border-radius: 4px; border-radius: 4px; }
969969
#bottom .nav-content {
970970
margin: 0px auto 0px auto;
971971
}
972+
973+
.zan-operation {
974+
cursor: pointer;
975+
}
976+
.zan-operation:hover {
977+
color: #ce7358;
978+
}
979+
980+
.zan-operation .zan-wrap {
981+
background-color: rgba(1,126,102,0.08);
982+
color: #df957e;
983+
padding: 0;
984+
display: inline-block;
985+
height: 20px;
986+
width: 20px;
987+
line-height: 20px;
988+
text-align: center;
989+
margin-right: 5px;
990+
border-radius: 10px;
991+
margin-bottom: 1px;
992+
}
993+
.zan-operation:hover .zan-wrap, .zan-operation.active .zan-wrap {
994+
background-color: #ce7358;
995+
color: #FFF
996+
}
997+
.zan-operation .fa {
998+
font-size: 12px !important;
999+
vertical-align: baseline;
1000+
}
1001+
.zan-operation .fa:hover {
1002+
color: #FFF !important;
1003+
}
1004+
.zan-operation .zan-num {
1005+
color: #df957e;
1006+
font-weight: bold;
1007+
}
1008+
.zan-operation .zan-num::before {
1009+
content: 'x ';
1010+
font-size: 12px;
1011+
}
1012+
.dot {
1013+
color: #999;
1014+
font-weight: normal;
1015+
}
9721016

9731017
form .md-toolbar ul { margin-bottom:2px;}
9741018
form .md-toolbar ul a { -moz-border-radius: 8px;-webkit-border-radius: 8px;border-radius: 8px;padding: 0 5px;line-height: 18px;font-size: 12px;margin-right: 6px;text-shadow: 0;color: #444;border: 1px solid #fff;}

static/dist/css/sg_styles.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/dist/js/sg_base.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,49 @@ jQuery(document).ready(function($) {
368368
});
369369
}
370370

371+
// 用于列表页发送喜欢(取消喜欢)
372+
var postListLike = function(that, callback){
373+
if ($('#is_login_status').val() != 1) {
374+
openPop("#login-pop");
375+
return;
376+
}
377+
378+
var objid = $(that).data('objid'),
379+
objtype = $(that).data('objtype'),
380+
likeFlag = parseInt($(that).data('flag'), 10);
381+
382+
if (likeFlag) {
383+
likeFlag = 0;
384+
} else {
385+
likeFlag = 1;
386+
}
387+
388+
$.post('/like/'+objid, {objtype:objtype, flag:likeFlag}, function(data){
389+
if (data.ok) {
390+
391+
$(that).data('flag', likeFlag);
392+
393+
var likeNum = parseInt($(that).children('.zan-num').text(), 10);
394+
// 已喜欢
395+
if (likeFlag) {
396+
comTip("感谢赞!");
397+
$(that).children('.zan-word').text('已赞');
398+
likeNum++;
399+
} else {
400+
comTip("已取消赞!");
401+
$(that).children('.zan-word').text('赞');
402+
likeNum--;
403+
}
404+
405+
$(that).children('.zan-num').text(likeNum);
406+
407+
callback(likeNum, likeFlag);
408+
} else {
409+
alert(data.error);
410+
}
411+
});
412+
}
413+
371414
// 详情页喜欢(取消喜欢)
372415
$('.page #content-thank a').on('click', function(evt){
373416
evt.preventDefault();
@@ -392,6 +435,20 @@ jQuery(document).ready(function($) {
392435
});
393436
});
394437

438+
// 通用列表页点赞(取消赞)
439+
$('.zan-operation').on('click', function(evt) {
440+
evt.preventDefault();
441+
442+
var that = this;
443+
postListLike(that, function(likeNum, likeFlag){
444+
if (likeFlag) {
445+
$(that).addClass('active');
446+
} else {
447+
$(that).removeClass('active');
448+
}
449+
});
450+
});
451+
395452
// 收藏(取消收藏)
396453
var postFavorite = function(that, callback) {
397454

static/dist/js/sg_base.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/js/base/common.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,49 @@ jQuery(document).ready(function($) {
368368
});
369369
}
370370

371+
// 用于列表页发送喜欢(取消喜欢)
372+
var postListLike = function(that, callback){
373+
if ($('#is_login_status').val() != 1) {
374+
openPop("#login-pop");
375+
return;
376+
}
377+
378+
var objid = $(that).data('objid'),
379+
objtype = $(that).data('objtype'),
380+
likeFlag = parseInt($(that).data('flag'), 10);
381+
382+
if (likeFlag) {
383+
likeFlag = 0;
384+
} else {
385+
likeFlag = 1;
386+
}
387+
388+
$.post('/like/'+objid, {objtype:objtype, flag:likeFlag}, function(data){
389+
if (data.ok) {
390+
391+
$(that).data('flag', likeFlag);
392+
393+
var likeNum = parseInt($(that).children('.zan-num').text(), 10);
394+
// 已喜欢
395+
if (likeFlag) {
396+
comTip("感谢赞!");
397+
$(that).children('.zan-word').text('已赞');
398+
likeNum++;
399+
} else {
400+
comTip("已取消赞!");
401+
$(that).children('.zan-word').text('赞');
402+
likeNum--;
403+
}
404+
405+
$(that).children('.zan-num').text(likeNum);
406+
407+
callback(likeNum, likeFlag);
408+
} else {
409+
alert(data.error);
410+
}
411+
});
412+
}
413+
371414
// 详情页喜欢(取消喜欢)
372415
$('.page #content-thank a').on('click', function(evt){
373416
evt.preventDefault();
@@ -392,6 +435,20 @@ jQuery(document).ready(function($) {
392435
});
393436
});
394437

438+
// 通用列表页点赞(取消赞)
439+
$('.zan-operation').on('click', function(evt) {
440+
evt.preventDefault();
441+
442+
var that = this;
443+
postListLike(that, function(likeNum, likeFlag){
444+
if (likeFlag) {
445+
$(that).addClass('active');
446+
} else {
447+
$(that).removeClass('active');
448+
}
449+
});
450+
});
451+
395452
// 收藏(取消收藏)
396453
var postFavorite = function(that, callback) {
397454

template/common/layout.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<link rel="stylesheet" href="https://cdn.staticfile.org/bootswatch/3.2.0/css/cosmo/bootstrap.min.css">
1515
<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css">
1616
<link rel="stylesheet" href="{{.static_domain}}/static/dist/css/sg_libs.min.css?v=20180305"/>
17-
<link rel="stylesheet" href="{{.static_domain}}/static/dist/css/sg_styles.min.css?v=20190811"/>
17+
<link rel="stylesheet" href="{{.static_domain}}/static/dist/css/sg_styles.min.css?v=20190825"/>
1818

1919
{{template "css" .}}
2020

@@ -240,7 +240,7 @@
240240
</script>
241241

242242
<script src="{{.static_domain}}/static/dist/js/sg_libs.min.js"></script>
243-
<script src="{{.static_domain}}/static/dist/js/sg_base.min.js?v=0.01"></script>
243+
<script src="{{.static_domain}}/static/dist/js/sg_base.min.js?v=0.02"></script>
244244

245245
{{template "js" .}}
246246

0 commit comments

Comments
 (0)