Skip to content

Commit d84e821

Browse files
chore: 优化首屏获取用户接口和获取文章列表接口
1 parent ef3faea commit d84e821

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

service/app/controller/blog/index.js

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ class IndexController extends controller {
1414
qq_account as qqAccount, wechat_account as weChatAccount, github_url as githubUrl,
1515
logo_name as logoName, logo_sub as logoSub
1616
FROM admin_user WHERE id = ?`;
17-
const selectResult = await this.app.mysql.query(selectSql, [ id ]);
18-
const articleCountResult = await this.app.mysql.query('SELECT count(*) as count from article');
19-
const talkCountResult = await this.app.mysql.query('SELECT count(*) as count from talk');
20-
const novelCountResult = await this.app.mysql.query('SELECT count(*) as count from novel');
21-
if (selectResult.length > 0) {
22-
const dataObj = selectResult[0];
23-
dataObj.articleCount = articleCountResult[0].count;
24-
dataObj.talkCount = talkCountResult[0].count;
25-
dataObj.novelCount = novelCountResult[0].count;
26-
this.ctx.body = { success: true, data: dataObj };
27-
} else {
28-
this.ctx.body = { success: false, message: '获取个人信息失败' };
29-
}
17+
const userInfoPromise = this.app.mysql.query(selectSql, [ id ]);
18+
const articleCountPromise = this.app.mysql.query('SELECT count(*) as count from article');
19+
const talkCountPromise = this.app.mysql.query('SELECT count(*) as count from talk');
20+
const novelCountPromise = this.app.mysql.query('SELECT count(*) as count from novel');
21+
Promise.all([userInfoPromise, articleCountPromise, talkCountPromise, novelCountPromise])
22+
.then(([userInfoResult, articleCountResult, talkCountResult, novelCountResult]) => {
23+
if (userInfoResult.length > 0) {
24+
const dataObj = userInfoResult[0];
25+
dataObj.articleCount = articleCountResult[0].count;
26+
dataObj.talkCount = talkCountResult[0].count;
27+
dataObj.novelCount = novelCountResult[0].count;
28+
this.ctx.body = { success: true, data: dataObj };
29+
} else {
30+
this.ctx.body = { success: false, message: '获取个人信息失败' };
31+
}
32+
})
3033
}
3134

3235
async getAdverList() {
@@ -53,25 +56,28 @@ class IndexController extends controller {
5356
article_type.name as type
5457
FROM article LEFT JOIN article_type
5558
ON article.type_id = article_type.id WHERE article.is_publish = 1 ORDER BY article.publish_time DESC LIMIT ?,?`;
56-
const result = await this.app.mysql.query(sql, [ request.offset, request.limit ]);
57-
const countResult = await this.app.mysql.query('SELECT count(*) as total FROM article');
58-
if (result.length > 0) {
59-
for (const item of result) {
60-
item.listType = 'article';
59+
const articleListPromise = await this.app.mysql.query(sql, [ request.offset, request.limit ]);
60+
const countPromise = await this.app.mysql.query('SELECT count(*) as total FROM article');
61+
Promise.all([articleListPromise, countPromise])
62+
.then(([articleListResult, countResult]) => {
63+
if (articleListResult.length > 0) {
64+
for (const item of articleListResult) {
65+
item.listType = 'article';
66+
}
67+
this.ctx.body = {
68+
success: true,
69+
data: {
70+
total: countResult[0].total,
71+
list: articleListResult,
72+
},
73+
};
74+
} else {
75+
this.ctx.body = {
76+
success: false,
77+
message: '获取文章列表失败',
78+
};
6179
}
62-
this.ctx.body = {
63-
success: true,
64-
data: {
65-
total: countResult[0].total,
66-
list: result,
67-
},
68-
};
69-
} else {
70-
this.ctx.body = {
71-
success: false,
72-
message: '获取文章列表失败',
73-
};
74-
}
80+
})
7581
}
7682

7783
async getTalkList() {

0 commit comments

Comments
 (0)