完成用户主页功能 · JavaCodeMood/BookStack@e9e82a6 · GitHub
Skip to content

Commit e9e82a6

Browse files
committed
完成用户主页功能
1 parent 7d5d2b0 commit e9e82a6

13 files changed

Lines changed: 319 additions & 30 deletions

File tree

commands/command.go

Lines changed: 2 additions & 0 deletions

controllers/BaseController.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,21 @@ func (this *BaseController) Search() {
432432
this.Data["SeoTitle"] = "搜索 - " + this.Sitename
433433
this.TplName = "widgets/search.html"
434434
}
435+
436+
//关注或取消关注
437+
func (this *BaseController) SetFollow() {
438+
var cancel bool
439+
if this.Member.MemberId == 0 {
440+
this.JsonResult(1, "请先登录")
441+
}
442+
uid, _ := this.GetInt(":uid")
443+
if uid == this.Member.MemberId {
444+
this.JsonResult(1, "自己不能关注自己")
445+
}
446+
cancel, _ = new(models.Fans).FollowOrCancle(uid, this.Member.MemberId)
447+
if cancel {
448+
this.JsonResult(0, "您已经成功取消了关注")
449+
} else {
450+
this.JsonResult(0, "您已经成功关注了Ta")
451+
}
452+
}

controllers/UserController.go

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,77 +9,101 @@ import (
99

1010
type UserController struct {
1111
BaseController
12+
UcenterMember models.Member
1213
}
1314

1415
func (this *UserController) Prepare() {
1516
this.BaseController.Prepare()
17+
username := this.GetString(":username")
18+
this.UcenterMember, _ = new(models.Member).GetByUsername(username)
19+
if this.UcenterMember.MemberId == 0 {
20+
this.Abort("404")
21+
return
22+
}
23+
24+
this.Data["IsSelf"] = this.UcenterMember.MemberId == this.Member.MemberId
25+
this.Data["User"] = this.UcenterMember
1626
this.Data["Tab"] = "share"
1727
}
1828

1929
//首页
2030
func (this *UserController) Index() {
21-
username := this.GetString(":username")
22-
member, _ := new(models.Member).GetByUsername(username)
31+
2332
page, _ := this.GetInt("page")
2433
pageSize := 10
2534
if page < 1 {
2635
page = 1
2736
}
28-
29-
if member.MemberId == 0 {
30-
this.Abort("404")
31-
return
32-
}
33-
books, totalCount, _ := models.NewBook().FindToPager(page, pageSize, member.MemberId, 0)
37+
books, totalCount, _ := models.NewBook().FindToPager(page, pageSize, this.UcenterMember.MemberId, 0)
3438
this.Data["Books"] = books
3539

3640
if totalCount > 0 {
37-
html := utils.NewPaginations(conf.RollPage, totalCount, pageSize, page, beego.URLFor("UserController.Index", ":username", member.Account), "")
41+
html := utils.NewPaginations(conf.RollPage, totalCount, pageSize, page, beego.URLFor("UserController.Index", ":username", this.UcenterMember.Account), "")
3842
this.Data["PageHtml"] = html
3943
} else {
4044
this.Data["PageHtml"] = ""
4145
}
4246
this.Data["Total"] = totalCount
43-
this.Data["User"] = member
47+
4448
this.TplName = "user/index.html"
4549
}
4650

4751
//收藏
4852
func (this *UserController) Collection() {
49-
username := this.GetString(":username")
50-
member, _ := new(models.Member).GetByUsername(username)
5153
page, _ := this.GetInt("page")
5254
pageSize := 10
5355
if page < 1 {
5456
page = 1
5557
}
5658

57-
if member.MemberId == 0 {
58-
this.Abort("404")
59-
return
60-
}
61-
62-
totalCount, books, _ := new(models.Star).List(member.MemberId, page, pageSize)
59+
totalCount, books, _ := new(models.Star).List(this.UcenterMember.MemberId, page, pageSize)
6360
this.Data["Books"] = books
6461

6562
if totalCount > 0 {
66-
html := utils.NewPaginations(conf.RollPage, int(totalCount), pageSize, page, beego.URLFor("UserController.Collection", ":username", member.Account), "")
63+
html := utils.NewPaginations(conf.RollPage, int(totalCount), pageSize, page, beego.URLFor("UserController.Collection", ":username", this.UcenterMember.Account), "")
6764
this.Data["PageHtml"] = html
6865
} else {
6966
this.Data["PageHtml"] = ""
7067
}
7168
this.Data["Total"] = totalCount
72-
this.Data["User"] = member
7369
this.Data["Tab"] = "collection"
7470
this.TplName = "user/collection.html"
7571
}
7672

77-
//粉丝和关注
73+
//关注
7874
func (this *UserController) Follow() {
79-
75+
page, _ := this.GetInt("page")
76+
pageSize := 18
77+
if page < 1 {
78+
page = 1
79+
}
80+
fans, totalCount, _ := new(models.Fans).GetFollowList(this.UcenterMember.MemberId, page, pageSize)
81+
if totalCount > 0 {
82+
html := utils.NewPaginations(conf.RollPage, int(totalCount), pageSize, page, beego.URLFor("UserController.Follow", ":username", this.UcenterMember.Account), "")
83+
this.Data["PageHtml"] = html
84+
} else {
85+
this.Data["PageHtml"] = ""
86+
}
87+
this.Data["Fans"] = fans
88+
this.Data["Tab"] = "follow"
89+
this.TplName = "user/fans.html"
8090
}
8191

8292
//粉丝和关注
8393
func (this *UserController) Fans() {
84-
94+
page, _ := this.GetInt("page")
95+
pageSize := 18
96+
if page < 1 {
97+
page = 1
98+
}
99+
fans, totalCount, _ := new(models.Fans).GetFansList(this.UcenterMember.MemberId, page, pageSize)
100+
if totalCount > 0 {
101+
html := utils.NewPaginations(conf.RollPage, int(totalCount), pageSize, page, beego.URLFor("UserController.Fans", ":username", this.UcenterMember.Account), "")
102+
this.Data["PageHtml"] = html
103+
} else {
104+
this.Data["PageHtml"] = ""
105+
}
106+
this.Data["Fans"] = fans
107+
this.Data["Tab"] = "fans"
108+
this.TplName = "user/fans.html"
85109
}

models/fans.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package models
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/astaxie/beego/orm"
7+
)
8+
9+
var tableFans = "md_fans"
10+
11+
type FansResult struct {
12+
Uid int
13+
Nickname string
14+
Avatar string
15+
Account string
16+
}
17+
18+
//粉丝表
19+
type Fans struct {
20+
Id int //自增主键
21+
Uid int `orm:"index"` //被关注的用户id
22+
FansId int `orm:"index"` //粉丝id
23+
}
24+
25+
// 多字段唯一键
26+
func (this *Fans) TableUnique() [][]string {
27+
return [][]string{
28+
[]string{"Uid", "FansId"},
29+
}
30+
}
31+
32+
//关注和取消关注
33+
func (this *Fans) FollowOrCancle(uid, fans_id int) (cancel bool, err error) {
34+
var fans Fans
35+
o := orm.NewOrm()
36+
qs := o.QueryTable(tableFans).Filter("uid", uid).Filter("fans_id", fans_id)
37+
qs.One(&fans)
38+
if fans.Id > 0 { //已关注,则取消关注
39+
_, err = qs.Delete()
40+
cancel = true
41+
} else { //未关注,则新增关注
42+
fans.Uid = uid
43+
fans.FansId = fans_id
44+
_, err = o.Insert(&fans)
45+
}
46+
return
47+
}
48+
49+
//查询是否已经关注了用户
50+
func (this *Fans) Relation(uid, fans_id interface{}) (ok bool) {
51+
var fans Fans
52+
orm.NewOrm().QueryTable(tableFans).Filter("uid", uid).Filter("fans_id", fans_id).One(&fans)
53+
return fans.Id != 0
54+
}
55+
56+
//查询用户的粉丝(用户id作为被关注对象)
57+
func (this *Fans) GetFansList(uid, page, pageSize int) (fans []FansResult, total int64, err error) {
58+
o := orm.NewOrm()
59+
total, _ = o.QueryTable(tableFans).Filter("uid", uid).Count()
60+
if total > 0 {
61+
sql := fmt.Sprintf(
62+
"select m.member_id uid,m.avatar,m.account,m.nickname from md_members m left join md_fans f on m.member_id=f.fans_id where f.uid=? order by f.id desc limit %v offset %v",
63+
pageSize, (page-1)*pageSize,
64+
)
65+
_, err = o.Raw(sql, uid).QueryRows(&fans)
66+
}
67+
return
68+
}
69+
70+
//查询用户的关注(用户id作为fans_id)
71+
func (this *Fans) GetFollowList(fans_id, page, pageSize int) (fans []FansResult, total int64, err error) {
72+
o := orm.NewOrm()
73+
total, _ = o.QueryTable(tableFans).Filter("fans_id", fans_id).Count()
74+
if total > 0 {
75+
sql := fmt.Sprintf(
76+
"select m.member_id uid,m.avatar,m.account,m.nickname from md_members m left join md_fans f on m.member_id=f.uid where f.fans_id=? order by f.id desc limit %v offset %v",
77+
pageSize, (page-1)*pageSize,
78+
)
79+
_, err = o.Raw(sql, fans_id).QueryRows(&fans)
80+
}
81+
return
82+
}

routers/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func init() {
107107
beego.Router("/user/:username/collection", &controllers.UserController{}, "get:Collection")
108108
beego.Router("/user/:username/follow", &controllers.UserController{}, "get:Follow")
109109
beego.Router("/user/:username/fans", &controllers.UserController{}, "get:Fans")
110-
110+
beego.Router("/follow/:uid", &controllers.BaseController{}, "get:SetFollow") //关注或取消关注
111111
//用户中心 【end】
112112

113113
beego.Router("/tag/:key", &controllers.LabelController{}, "get:Index")

static/css/main.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,4 +756,10 @@ a{color: #333}
756756
.ucenter-content li.clearfix .help-block{line-height: 180%;}
757757
.ucenter-content .book-info{font-size: 12px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}
758758
.ucenter-content .book-info span{margin-right: 10px;}
759-
.ucenter-content .book-description{max-height: 75px;overflow: hidden;}
759+
.ucenter-content .book-description{max-height: 75px;overflow: hidden;}
760+
.ucenter .fans-item{margin-top:15px;}
761+
.ucenter .fans-item a{display: inline-block;}
762+
.ucenter .fans-item a.fans-username{display: block;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;}
763+
.ucenter .fans-item .btn{margin-top: 5px;}
764+
.ucenter .fans-item .thumbnail{border-radius: 50%;margin-bottom: 5px;margin: 5px auto;}
765+
.ucenter .fans-item .thumbnail img{border-radius: 50%;}

static/js/main.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,36 @@ $(function () {
286286
$(".btn-upload-zip").removeClass("disabled");
287287
});
288288

289+
//添加关注
290+
// $(".btn-follow").click(function (e) {
291+
$(".ucenter").on("click",".btn-follow",function (e) {
292+
e.preventDefault();
293+
var _this=$(this),href=_this.attr("href");
294+
$.get(href,function (ret) {
295+
var obj=parseJson(ret);
296+
if(obj.errcode==0){
297+
var html='<a href="'+href+'" class="btn btn-default btn-sm btn-cancel"><i class="fa fa-heart text-danger"></i> 取消关注</a>';
298+
_this.hide();
299+
_this.before(html);
300+
_this.remove();
301+
}
302+
})
303+
})
304+
305+
//取消关注
306+
$(".ucenter").on("click",".btn-cancel",function (e) {
307+
e.preventDefault();
308+
var _this=$(this),href=_this.attr("href");
309+
$.get(href,function (ret) {
310+
var obj=parseJson(ret);
311+
if(obj.errcode==0){
312+
var html='<a href="'+href+'" class="btn btn-success btn-sm btn-follow"><i class="fa fa-heart-o"></i> 关注Ta</a>';
313+
_this.hide();
314+
_this.before(html);
315+
_this.remove();
316+
}
317+
})
318+
})
319+
289320

290321
});

views/manager/users.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@
4141
<tbody>
4242
<tr v-for="item in lists">
4343
<td>${item.member_id}</td>
44-
<td><img :src="item.avatar" onerror="this.src='/static/images/middle.gif'" class="img-circle" width="34" height="34"></td>
45-
<td>${item.nickname}(${item.account})</td>
44+
<td>
45+
<a :href="'/user/'+item.account" target="_blank">
46+
<img :src="item.avatar" onerror="this.src='/static/images/middle.gif'" class="img-circle" width="34" height="34">
47+
</a>
48+
</td>
49+
<td><a :href="'/user/'+item.account" target="_blank">${item.nickname}(${item.account})</a></td>
4650
<td>
4751
<template v-if="item.role == 0">
4852
超级管理员

views/user/base.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
<img class="thumbnail" src="{{showImg .User.Avatar "avatar"}}" alt="{{.User.Nickname}}">
77
<h3>{{.User.Nickname}}</h3>
88
<div class="help-block"><span class="text-muted">(@{{.User.Account}})</span></div>
9-
<!--<div><a href="#" class="btn btn-success btn-sm"><i class="fa fa-heart-o"></i> 关注Ta</a></div>-->
10-
<div><a target="_blank" href="{{urlfor "SettingController.Index"}}" class="btn btn-success btn-sm"><i class="fa fa-cogs"></i> 个人设置</a></div>
11-
<!--<div><a href="#" class="btn btn-default btn-sm"><i class="fa fa-heart text-danger"></i> 取消关注</a></div>-->
9+
{{if .IsSelf}}
10+
<div><a target="_blank" href="{{urlfor "SettingController.Index"}}" class="btn btn-success btn-sm"><i class="fa fa-cogs"></i> 个人设置</a></div>
11+
{{else}}
12+
{{if (IsFollow .User.MemberId .Member.MemberId)}}
13+
<div><a href="{{urlfor "BaseController.SetFollow" ":uid" .User.MemberId}}" class="btn btn-default btn-sm btn-cancel"><i class="fa fa-heart text-danger"></i> 取消关注</a></div>
14+
{{else}}
15+
<div><a href="{{urlfor "BaseController.SetFollow" ":uid" .User.MemberId}}" class="btn btn-success btn-sm btn-follow"><i class="fa fa-heart-o"></i> 关注Ta</a></div>
16+
{{end}}
17+
{{end}}
1218
<div class="help-block mgt-15px">{{.User.Description}}</div>
1319
</div>
1420
</div>

views/user/collection.html

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)