Skip to content

Commit 2e04b08

Browse files
committed
feat:view user posts
1 parent 2bdc23b commit 2e04b08

File tree

8 files changed

+334
-7
lines changed

8 files changed

+334
-7
lines changed

api/entrance.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func AppRun() {
5151
user.POST("/user/register", guest.RegisterByEmail)
5252
user.POST("/user/login", guest.Login)
5353
user.GET("/user/logout", guest.Logout)
54+
user.GET("/user/info/:id", guest.GetUserInfo)
5455
user.GET("/user/current", guest.GetCurrentUser)
5556
user.POST("/user/profile", guest.UpdateUserProfile)
5657
user.POST("/user/set/username", guest.SetUsername)

api/guest/articles.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,27 @@ func PostArticle(c *gin.Context) {
4545
// GetArticleList is the api that returns a list of articles
4646
// if you want to add parameter like limit, please use url like /article?limit=10
4747
func GetArticleList(c *gin.Context) {
48+
logs.Logger.Info(c.Request.URL.Path)
4849
user := service.UserService.GetCurrentUser(c)
4950
limit := c.DefaultQuery("limit", "10")
5051
sortby := c.DefaultQuery("sortby", "create_time")
5152
order := c.DefaultQuery("order", "desc")
5253
cursor := c.DefaultQuery("cursor", "2559090472000")
53-
// userID
54+
uID := c.DefaultQuery("user_id", "0")
5455

5556
var err error
5657

5758
limitNum, err1 := strconv.Atoi(limit)
5859
cursorTime, err2 := strconv.ParseInt(cursor, 10, 64)
59-
if err1 != nil || err2 != nil || limitNum <= 0 || order != "desc" && order != "asc" {
60+
authorID, err3 := strconv.ParseInt(uID, 10, 64)
61+
if err1 != nil || err2 != nil || err3 != nil || limitNum <= 0 || order != "desc" && order != "asc" {
6062
err = errors.New("参数有误")
6163
}
6264
if err != nil {
6365
setAPIResponse(c, nil, err.Error(), false)
6466
}
6567

66-
resp, err := service.ArticleService.GetArticleList(user, limitNum, cursorTime, sortby, order)
68+
resp, err := service.ArticleService.GetArticleList(user, authorID, limitNum, cursorTime, sortby, order)
6769
if err != nil {
6870
setAPIResponse(c, nil, err.Error(), false)
6971
} else {

api/guest/users.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package guest
22

33
import (
44
"fmt"
5+
"strconv"
56

67
"github.com/gin-gonic/gin"
78
"github.com/nk-akun/NeighborBBS/model"
@@ -14,6 +15,22 @@ func GetCurrentUser(c *gin.Context) {
1415
setAPIResponse(c, user, "", true)
1516
}
1617

18+
// GetUserInfo ...
19+
func GetUserInfo(c *gin.Context) {
20+
id := c.Param("id")
21+
userID, err := strconv.ParseInt(id, 10, 64)
22+
if err != nil {
23+
setAPIResponse(c, nil, err.Error(), false)
24+
return
25+
}
26+
resp, err := service.UserService.GetUserInfo(userID)
27+
if err != nil {
28+
setAPIResponse(c, nil, err.Error(), false)
29+
} else {
30+
setAPIResponse(c, resp, "查询成功", true)
31+
}
32+
}
33+
1734
// RegisterByEmail ...
1835
func RegisterByEmail(c *gin.Context) {
1936
user, err := service.UserService.SignUp(c)

repository/article_repo.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ func (r *articleRepository) Create(db *gorm.DB, article *model.Article) error {
2222
return db.Create(article).Error
2323
}
2424

25-
func (r *articleRepository) GetArticleFields(db *gorm.DB, fields []string, cursorTime int64, limit int, sortby string, order string) []model.Article {
25+
func (r *articleRepository) GetArticleFields(db *gorm.DB, authorID int64, fields []string, cursorTime int64, limit int, sortby string, order string) []model.Article {
2626
var articles []model.Article
27-
db.Where("create_time < ?", cursorTime).Select(fields).Order(fmt.Sprintf("%s %s", sortby, order)).Limit(limit).Find(&articles)
27+
if authorID == 0 {
28+
db.Where("create_time < ?", cursorTime).Select(fields).Order(fmt.Sprintf("%s %s", sortby, order)).Limit(limit).Find(&articles)
29+
} else {
30+
db.Where("user_id = ? and create_time < ?", authorID, cursorTime).Select(fields).Order(fmt.Sprintf("%s %s", sortby, order)).Limit(limit).Find(&articles)
31+
}
2832
return articles
2933
}
3034

service/articles.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ func (s *articleService) BuildArticle(user *model.User, title string, content st
4545
return article, nil
4646
}
4747

48-
func (s *articleService) GetArticleList(currentUser *model.User, limit int, cursorTime int64, sortby string, order string) (*model.ArticleListResponse, error) {
48+
func (s *articleService) GetArticleList(currentUser *model.User, authorID int64, limit int, cursorTime int64, sortby string, order string) (*model.ArticleListResponse, error) {
4949
resp := &model.ArticleListResponse{}
5050
fields := []string{"id", "title", "create_time", "user_id", "view_count", "comment_count", "like_count", "content"}
51-
articles := repository.ArticleRepository.GetArticleFields(util.DB(), fields, cursorTime, limit, sortby, order)
51+
articles := repository.ArticleRepository.GetArticleFields(util.DB(), authorID, fields, cursorTime, limit, sortby, order)
5252

5353
briefList, minCursorTime := buildArticleList(currentUser, articles)
5454

service/users.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ func (s *userService) SetToken(userID int64) string {
157157
return token
158158
}
159159

160+
func (s *userService) GetUserInfo(userID int64) (*model.UserBriefInfo, error) {
161+
user, err := repository.UserRepository.GetUserByUserID(util.DB(), userID)
162+
if err != nil {
163+
return nil, errors.New("此用户不存在")
164+
}
165+
briefInfo := BuildUserBriefInfo(user)
166+
if briefInfo == nil {
167+
return nil, errors.New("此用户不存在")
168+
}
169+
return briefInfo, nil
170+
}
171+
160172
func (s *userService) UpdateUserProfile(c *gin.Context) error {
161173
user := s.GetCurrentUser(c)
162174
if user == nil {

site/components/UserProfile.vue

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<template>
2+
<div
3+
class="profile"
4+
:class="{ background: backgroundImage }"
5+
:style="{ backgroundImage: 'url(' + 'https://cdn.jsdelivr.net/gh/nk-akun/cdn/pictures/71aa072e23f9cfff80fa69f76b2cc441.jpg' + ')' }"
6+
>
7+
<div class="profile-info">
8+
<avatar
9+
v-if="backgroundImage"
10+
:user="user"
11+
:round="true"
12+
:has-border="true"
13+
size="100"
14+
:extra-style="{ position: 'absolute', top: '50px' }"
15+
/>
16+
<avatar v-else :user="user" :round="true" size="100" />
17+
<div class="meta">
18+
<h1>
19+
<a :href="'/user/' + user.id">{{ user.nickname }}</a>
20+
</h1>
21+
<div v-if="user.description" class="description">
22+
<p>{{ user.description }}</p>
23+
</div>
24+
<div v-if="user.homePage" class="homepage">
25+
<i class="iconfont icon-home"></i>
26+
<a :href="user.homePage" target="_blank" rel="external nofollow">
27+
{{
28+
user.homePage
29+
}}
30+
</a>
31+
</div>
32+
</div>
33+
</div>
34+
</div>
35+
</template>
36+
37+
<script>
38+
import Avatar from '~/components/Avatar'
39+
export default {
40+
components: { Avatar },
41+
props: {
42+
user: {
43+
type: Object,
44+
required: true,
45+
},
46+
},
47+
computed: {
48+
backgroundImage() {
49+
return 'https://cdn.jsdelivr.net/gh/nk-akun/cdn/pictures/dLbeBmTdKa4.jpg'
50+
},
51+
currentUser() {
52+
return this.$store.state.user.current
53+
},
54+
// 是否是主人态
55+
isOwner() {
56+
const current = this.$store.state.user.current
57+
return this.user && current && this.user.id === current.id
58+
},
59+
},
60+
methods: {
61+
// async uploadBackground(e) {
62+
// const files = e.target.files
63+
// if (files.length <= 0) {
64+
// return
65+
// }
66+
// try {
67+
// // 上传头像
68+
// const file = files[0]
69+
// const formData = new FormData()
70+
// formData.append('image', file, file.name)
71+
// const ret = await this.$axios.post('/api/upload', formData, {
72+
// headers: { 'Content-Type': 'multipart/form-data' },
73+
// })
74+
// // 设置头像
75+
// await this.$axios.post('/api/user/set/background/image', {
76+
// backgroundImage: ret.url,
77+
// })
78+
// // 重新加载数据
79+
// this.user = await this.$store.dispatch('user/getCurrentUser')
80+
// this.$message.success('背景设置成功')
81+
// } catch (e) {
82+
// this.$message.error(e.message || e)
83+
// console.error(e)
84+
// }
85+
// },
86+
},
87+
}
88+
</script>
89+
90+
<style lang="scss" scoped>
91+
.profile {
92+
display: flex;
93+
margin-bottom: 10px;
94+
position: relative;
95+
96+
.change-bg {
97+
position: absolute;
98+
top: 10px;
99+
right: 10px;
100+
opacity: 0.7;
101+
&:hover {
102+
opacity: 1;
103+
}
104+
}
105+
106+
.profile-info {
107+
display: flex;
108+
width: 100%;
109+
padding: 10px;
110+
background: #fff;
111+
112+
.avatar {
113+
max-width: 66px;
114+
max-height: 66px;
115+
min-width: 66px;
116+
min-height: 66px;
117+
}
118+
119+
.meta {
120+
margin-left: 18px;
121+
122+
i {
123+
margin-right: 6px;
124+
}
125+
126+
h1 {
127+
font-size: 28px;
128+
font-weight: 700;
129+
margin-bottom: 6px;
130+
a {
131+
color: #000;
132+
&:hover {
133+
color: #000;
134+
text-decoration: underline;
135+
}
136+
}
137+
}
138+
139+
.description {
140+
font-size: 14px;
141+
color: #555;
142+
margin-bottom: 6px;
143+
}
144+
145+
.homepage {
146+
font-size: 14px;
147+
a {
148+
color: #555;
149+
&:hover {
150+
color: #3273dc;
151+
text-decoration: underline;
152+
}
153+
}
154+
}
155+
}
156+
}
157+
158+
&.background {
159+
//background-image: url('http://file.mlog.club/bg1.jpg!768_auto');
160+
background-size: cover;
161+
background-position: 50%;
162+
163+
.profile-info {
164+
margin-top: 100px;
165+
background-color: unset;
166+
background-image: linear-gradient(
167+
90deg,
168+
#dce9f25c,
169+
rgba(255, 255, 255, 0.76),
170+
#dce9f25c
171+
);
172+
173+
.meta {
174+
margin-left: 138px;
175+
}
176+
}
177+
}
178+
}
179+
</style>

0 commit comments

Comments
 (0)