Skip to content

Commit 750bf47

Browse files
committed
feat: add refresh data, updateTime and liked number
1 parent 74d8c37 commit 750bf47

File tree

6 files changed

+85
-34
lines changed

6 files changed

+85
-34
lines changed

server/juejin/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/**
2+
* 每天上午八点从掘金获取一次数据
3+
* 不在页面访问时直接从掘金获取数据,是为了防止页面刷新次数过多时调用掘金接口过多从而导致 IP 被封
4+
*/
5+
16
const axios = require('axios')
27
const { getDate, getDateStr } = require('../utils')
38

@@ -13,7 +18,7 @@ const getJueJinArticleList = async () => {
1318
}
1419
const res = await axios.post('https://api.juejin.cn/content_api/v1/article/query_list', params)
1520
const { data, has_more } = res.data
16-
data !== null && (result = result.concat(data))
21+
data !== null && data !== undefined && (result = result.concat(data))
1722
has_more && await loop(res.data.cursor)
1823
}
1924
console.log('掘金查询开始')

server/utils/mongodb.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const updateArticleList = async () => {
9797
const tagList = getTagListByArticleList(articleList)
9898
await insertTags(tagList)
9999
} catch (error) {
100-
console.log('updateArticleList error: ', updateArticleList)
100+
console.log('updateArticleList error: ', error)
101101
throw error
102102
}
103103
}

src/pages/article/index.tsx

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useEffect, useState, useRef } from 'react';
22
import styles from '@/styles/article.module.scss';
33
import NavHeader from "@/components/navHeader";
44
import APP_CONF from "@/data/config";
5-
import {Dropdown, Space, Menu, Spin, BackTop} from "antd";
5+
import {Dropdown, Space, Menu, Spin, BackTop, Button} from "antd";
66
import { OpenOriginUrl, seo } from "@/data/doc";
77
import { CaretDownOutlined, VerticalAlignTopOutlined } from '@ant-design/icons';
88
import Head from "next/head";
@@ -18,6 +18,8 @@ const Article = (data) => {
1818
const [tag_type, setTagType] = useState('');
1919
const [articleList, setArticleList] = useState([]);
2020
const [spinning, setSpinning] = useState(false);
21+
const [isAdmin, setIsAdmin] = useState(false);
22+
const [updating, setUpdating] = useState(false);
2123
const [clearArticle, setClearArticle] = useState(false);
2224
const [hasMore, setHasMore] = useState(false);
2325
const firstUpdate = useRef(true);
@@ -34,6 +36,15 @@ const Article = (data) => {
3436

3537
useEffect(() => {
3638
setMobile(isMobile(window));
39+
setIsAdmin(window.localStorage.getItem("is-admin") === 'true');
40+
getTagList()
41+
}, [])
42+
43+
useEffect(() => {
44+
getArticleList();
45+
}, [tag_id, sort_type, page])
46+
47+
const getTagList=()=>{
3748
fetch(`${fetchUrl}/api/getTagList`)
3849
.then(res => res.json())
3950
.then(res => {
@@ -47,11 +58,7 @@ const Article = (data) => {
4758
setTagType(tagList?.[0].key);
4859
setTagList(tagList || []);
4960
})
50-
}, [])
51-
52-
useEffect(() => {
53-
getArticleList();
54-
}, [tag_id, sort_type, page])
61+
}
5562

5663
// 处理文章数据
5764
const handleArticleList = (obj) => {
@@ -64,6 +71,7 @@ const Article = (data) => {
6471
setClearArticle(false)
6572
}
6673

74+
// 获取文章列表
6775
const getArticleList = () => {
6876
if (firstUpdate.current) {
6977
handleArticleList(data)
@@ -87,6 +95,16 @@ const Article = (data) => {
8795
})
8896
}
8997

98+
// 更新数据库中的文章数据
99+
const updateArticleList = () => {
100+
setUpdating(true)
101+
fetch(`${fetchUrl}/api/updateArticleList`)
102+
.finally(() => {
103+
setUpdating(false)
104+
window.location.reload()
105+
})
106+
}
107+
90108
const handleSelectSortType = (sort_type) => {
91109
setPage('1')
92110
setSortType(sort_type)
@@ -142,6 +160,14 @@ const Article = (data) => {
142160
<div className={styles.leftBox}>
143161
<div className={styles.sortBox}>
144162
<div className={styles.title}>文章列表</div>
163+
<div className={styles.updateTime}>
164+
{isAdmin ? (
165+
<Button loading={updating} onClick={updateArticleList}>
166+
更新文章数据库
167+
</Button>
168+
) : null}
169+
<span>文章数据更新时间:{articleList[0]?.createTime}</span>
170+
</div>
145171
<div className={styles.sortElement}>
146172
<Dropdown
147173
overlay={
@@ -194,7 +220,17 @@ const Article = (data) => {
194220
<div className={styles.row}>
195221
<div>{article.create_date} { mobile ? '' : article.create_time}</div>
196222
<a className={styles.username} href={'https://juejin.cn/user/2137106333053912'} target='_blank' rel="nofollow noopener noreferrer">{article.user_name}</a>
197-
<div className={styles.viewCount}><img src={`${APP_CONF.IMAGE_DOMAIN}/UEDLanding/Article/eye.svg`} alt=""/>{article.view_count}</div>
223+
224+
<div className={styles.countBox}>
225+
<div>
226+
<img src={`${APP_CONF.IMAGE_DOMAIN}/UEDLanding/Article/eye.svg`} alt=""/>
227+
{article.view_count}
228+
</div>
229+
<div>
230+
<img src={`${APP_CONF.IMAGE_DOMAIN}/UEDLanding/Article/liked.svg`} alt=""/>
231+
{article.digg_count}
232+
</div>
233+
</div>
198234
</div>
199235
</div>
200236
)

src/styles/article.module.scss

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@
126126
font-size: 16px;
127127
font-weight: 600;
128128
}
129+
.updateTime {
130+
flex: 1;
131+
display: flex;
132+
align-items: center;
133+
justify-content: flex-end;
134+
> span {
135+
color: #8B8FA8;
136+
margin: 0 10px;
137+
}
138+
}
129139
.typeSpace {
130140
color: #3D446E;
131141
cursor: pointer;
@@ -165,20 +175,22 @@
165175
display: flex;
166176
color: #8B8FA8;
167177
white-space: nowrap;
168-
img {
169-
margin-right: 8px;
170-
margin-bottom: 2px;
171-
}
172178
.username {
173179
padding: 0 10px 0 20px;
174180
user-select: none;
175181
cursor: pointer;
176182
color: #64698B;
177183
text-decoration: none;
178184
}
179-
.viewCount {
185+
.countBox {
180186
flex: 1;
181-
text-align: right;
187+
display: flex;
188+
align-items: center;
189+
justify-content: flex-end;
190+
img {
191+
width: 16px;
192+
margin: 0 4px 2px 24px;
193+
}
182194
}
183195
}
184196
}

static/Article/eye.svg

Lines changed: 8 additions & 19 deletions
Loading

static/Article/liked.svg

Lines changed: 9 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)