Skip to content

Commit 538b259

Browse files
committed
fix archives sorted issue
fix fix archives sorted issue, so the paper could sorted by the createAt Signed-off-by: farmer <cuferpan@gmail.com>
1 parent f11c377 commit 538b259

File tree

3 files changed

+70
-65
lines changed

3 files changed

+70
-65
lines changed

server/controllers/article.js

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99
reply: ReplyModel,
1010
user: UserModel,
1111
record: RecordModel,
12-
sequelize
12+
sequelize,
1313
} = require('../models')
1414

1515
const fs = require('fs')
@@ -25,7 +25,7 @@ class ArticleController {
2525
ArticleModel.create({
2626
id: -1,
2727
title: '关于页面',
28-
content: '关于页面存档,勿删'
28+
content: '关于页面存档,勿删',
2929
})
3030
}
3131
}
@@ -39,7 +39,7 @@ class ArticleController {
3939
categoryList: Joi.array(),
4040
tagList: Joi.array(),
4141
type: Joi.boolean(),
42-
top: Joi.boolean()
42+
top: Joi.boolean(),
4343
})
4444

4545
if (validator) {
@@ -65,7 +65,7 @@ class ArticleController {
6565
{ ...ctx.params, ...ctx.query },
6666
{
6767
id: Joi.number().required(),
68-
type: Joi.number() // type 用于区分是否增加浏览次数 1 新增浏览次数 0 不新增
68+
type: Joi.number(), // type 用于区分是否增加浏览次数 1 新增浏览次数 0 不新增
6969
}
7070
)
7171
if (validator) {
@@ -82,22 +82,22 @@ class ArticleController {
8282
{
8383
model: ReplyModel,
8484
attributes: ['id', 'content', 'createdAt'],
85-
include: [{ model: UserModel, as: 'user', attributes: { exclude: ['updatedAt', 'password'] } }]
85+
include: [{ model: UserModel, as: 'user', attributes: { exclude: ['updatedAt', 'password'] } }],
8686
},
87-
{ model: UserModel, as: 'user', attributes: { exclude: ['updatedAt', 'password'] } }
87+
{ model: UserModel, as: 'user', attributes: { exclude: ['updatedAt', 'password'] } },
8888
],
89-
row: true
90-
}
89+
row: true,
90+
},
9191
],
9292
order: [[CommentModel, 'createdAt', 'DESC'], [[CommentModel, ReplyModel, 'createdAt', 'ASC']]], // comment model order
93-
row: true
93+
row: true,
9494
})
9595

9696
const { type = 1 } = ctx.query
9797
// viewer count ++
9898
type === 1 && ArticleModel.update({ viewCount: ++data.viewCount }, { where: { id: ctx.params.id } })
9999
// 每个浏览记录都存一个stamp,这样后续能够看出文章的阅读趋势方便推荐
100-
type ===1 && RecordModel.create({articleId: ctx.params.id})
100+
type === 1 && RecordModel.create({ articleId: ctx.params.id })
101101
// JSON.parse(github)
102102
data.comments.forEach(comment => {
103103
comment.user.github = JSON.parse(comment.user.github)
@@ -119,7 +119,7 @@ class ArticleController {
119119
tag: Joi.string(),
120120
preview: Joi.number(),
121121
order: Joi.string(),
122-
type: Joi.boolean()
122+
type: Joi.boolean(),
123123
})
124124

125125
if (validator) {
@@ -135,36 +135,36 @@ class ArticleController {
135135
const data = await ArticleModel.findAndCountAll({
136136
where: {
137137
id: {
138-
$not: -1 // 过滤关于页面的副本
138+
$not: -1, // 过滤关于页面的副本
139139
},
140140
$and: {
141141
type: {
142-
$eq: JSON.parse(type)
143-
}
142+
$eq: JSON.parse(type),
143+
},
144144
},
145145
$or: {
146146
title: {
147-
$like: `%${keyword}%`
147+
$like: `%${keyword}%`,
148148
},
149149
content: {
150-
$like: `%${keyword}%`
151-
}
152-
}
150+
$like: `%${keyword}%`,
151+
},
152+
},
153153
},
154154
include: [
155155
{ model: TagModel, attributes: ['name'], where: tagFilter },
156156
{ model: CategoryModel, attributes: ['name'], where: categoryFilter },
157157
{
158158
model: CommentModel,
159159
attributes: ['id'],
160-
include: [{ model: ReplyModel, attributes: ['id'] }]
161-
}
160+
include: [{ model: ReplyModel, attributes: ['id'] }],
161+
},
162162
],
163163
offset: (page - 1) * pageSize,
164164
limit: parseInt(pageSize),
165165
order: articleOrder,
166166
row: true,
167-
distinct: true // count 计算
167+
distinct: true, // count 计算
168168
})
169169
if (preview === 1) {
170170
data.rows.forEach(d => {
@@ -177,31 +177,31 @@ class ArticleController {
177177
const data = await ArticleModel.findAndCountAll({
178178
where: {
179179
id: {
180-
$not: -1 // 过滤关于页面的副本
180+
$not: -1, // 过滤关于页面的副本
181181
},
182182
$or: {
183183
title: {
184-
$like: `%${keyword}%`
184+
$like: `%${keyword}%`,
185185
},
186186
content: {
187-
$like: `%${keyword}%`
188-
}
189-
}
187+
$like: `%${keyword}%`,
188+
},
189+
},
190190
},
191191
include: [
192192
{ model: TagModel, attributes: ['name'], where: tagFilter },
193193
{ model: CategoryModel, attributes: ['name'], where: categoryFilter },
194194
{
195195
model: CommentModel,
196196
attributes: ['id'],
197-
include: [{ model: ReplyModel, attributes: ['id'] }]
198-
}
197+
include: [{ model: ReplyModel, attributes: ['id'] }],
198+
},
199199
],
200200
offset: (page - 1) * pageSize,
201201
limit: parseInt(pageSize),
202202
order: articleOrder,
203203
row: true,
204-
distinct: true // count 计算
204+
distinct: true, // count 计算
205205
})
206206
if (preview === 1) {
207207
data.rows.forEach(d => {
@@ -212,15 +212,14 @@ class ArticleController {
212212
ctx.body = data
213213
}
214214
}
215-
216215
}
217216

218217
// 修改文章
219218
static async update(ctx) {
220219
const validator = ctx.validate(
221220
{
222221
articleId: ctx.params.id,
223-
...ctx.request.body
222+
...ctx.request.body,
224223
},
225224
{
226225
articleId: Joi.number().required(),
@@ -229,27 +228,27 @@ class ArticleController {
229228
categories: Joi.array(),
230229
tags: Joi.array(),
231230
type: Joi.boolean(),
232-
top: Joi.boolean()
231+
top: Joi.boolean(),
233232
}
234233
)
235234
if (validator) {
236235
const { title, content, categories = [], tags = [], type, top } = ctx.request.body
237236
const articleId = parseInt(ctx.params.id)
238237
const tagList = tags.map(tag => ({ name: tag, articleId }))
239238
const categoryList = categories.map(cate => ({ name: cate, articleId }))
240-
await ArticleModel.update({ title, content, type, top}, { where: { id: articleId } })
239+
await ArticleModel.update({ title, content, type, top }, { where: { id: articleId } })
241240
await TagModel.destroy({ where: { articleId } })
242241
await TagModel.bulkCreate(tagList)
243242
await CategoryModel.destroy({ where: { articleId } })
244243
await CategoryModel.bulkCreate(categoryList)
245-
ctx.body = { 'type': type }
244+
ctx.body = { type: type }
246245
}
247246
}
248247

249248
// 删除文章
250249
static async delete(ctx) {
251250
const validator = ctx.validate(ctx.params, {
252-
id: Joi.number().required()
251+
id: Joi.number().required(),
253252
})
254253
if (validator) {
255254
const articleId = ctx.params.id
@@ -269,7 +268,7 @@ class ArticleController {
269268
// 删除多个文章
270269
static async delList(ctx) {
271270
const validator = ctx.validate(ctx.params, {
272-
list: Joi.string().required()
271+
list: Joi.string().required(),
273272
})
274273

275274
if (validator) {
@@ -295,7 +294,7 @@ class ArticleController {
295294
*/
296295
static async checkExist(ctx) {
297296
const validator = ctx.validate(ctx.request.body, {
298-
fileNameList: Joi.array().required()
297+
fileNameList: Joi.array().required(),
299298
})
300299

301300
if (validator) {
@@ -343,7 +342,7 @@ class ArticleController {
343342
static async uploadConfirm(ctx) {
344343
const validator = ctx.validate(ctx.request.body, {
345344
authorId: Joi.number(),
346-
uploadList: Joi.array()
345+
uploadList: Joi.array(),
347346
})
348347
if (validator) {
349348
const { uploadList, authorId } = ctx.request.body
@@ -364,7 +363,7 @@ class ArticleController {
364363
categories: categories.map(d => ({ name: d })),
365364
tags: tags.map(d => ({ name: d })),
366365
content,
367-
authorId
366+
authorId,
368367
}
369368
if (date) data.createdAt = date
370369
if (item.articleId) data.articleId = item.articleId
@@ -400,7 +399,7 @@ class ArticleController {
400399
// 导出文章
401400
static async output(ctx) {
402401
const validator = ctx.validate(ctx.params, {
403-
id: Joi.number().required()
402+
id: Joi.number().required(),
404403
})
405404

406405
if (validator) {
@@ -409,8 +408,8 @@ class ArticleController {
409408
include: [
410409
// 查找 分类
411410
{ model: TagModel, attributes: ['name'] },
412-
{ model: CategoryModel, attributes: ['name'] }
413-
]
411+
{ model: CategoryModel, attributes: ['name'] },
412+
],
414413
})
415414

416415
const { filePath, fileName } = await generateFile(article)
@@ -421,20 +420,20 @@ class ArticleController {
421420

422421
static async outputList(ctx) {
423422
const validator = ctx.validate(ctx.params, {
424-
list: Joi.string().required()
423+
list: Joi.string().required(),
425424
})
426425
if (validator) {
427426
const articleList = ctx.params.list.split(',')
428427

429428
const list = await ArticleModel.findAll({
430429
where: {
431-
id: articleList
430+
id: articleList,
432431
},
433432
include: [
434433
// 查找 分类
435434
{ model: TagModel, attributes: ['name'] },
436-
{ model: CategoryModel, attributes: ['name'] }
437-
]
435+
{ model: CategoryModel, attributes: ['name'] },
436+
],
438437
})
439438

440439
// const filePath = await generateFile(list[0])
@@ -447,7 +446,7 @@ class ArticleController {
447446
zip.pipe(zipStream)
448447
list.forEach(item => {
449448
zip.append(fs.createReadStream(`${outputPath}/${item.title}.md`), {
450-
name: `${item.title}.md` // 压缩文件名
449+
name: `${item.title}.md`, // 压缩文件名
451450
})
452451
})
453452
await zip.finalize()
@@ -461,14 +460,14 @@ class ArticleController {
461460
const list = await ArticleModel.findAll({
462461
where: {
463462
id: {
464-
$not: -1 // 过滤关于页面的副本
465-
}
463+
$not: -1, // 过滤关于页面的副本
464+
},
466465
},
467466
include: [
468467
// 查找 分类
469468
{ model: TagModel, attributes: ['name'] },
470-
{ model: CategoryModel, attributes: ['name'] }
471-
]
469+
{ model: CategoryModel, attributes: ['name'] },
470+
],
472471
})
473472

474473
// const filePath = await generateFile(list[0])
@@ -481,7 +480,7 @@ class ArticleController {
481480
zip.pipe(zipStream)
482481
list.forEach(item => {
483482
zip.append(fs.createReadStream(`${outputPath}/${item.title}.md`), {
484-
name: `${item.title}.md` // 压缩文件名
483+
name: `${item.title}.md`, // 压缩文件名
485484
})
486485
})
487486
await zip.finalize()

src/utils/index.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ export const translateMarkdown2html = plainText => {
5858
if ($2.indexOf('<code>') >= 0 || $2.indexOf('</code>') >= 0) {
5959
return $2
6060
} else {
61-
return '<span class=\'marked_inline_tex\'>' + $2.replace(/\$/g, '') + '</span>'
61+
return "<span class='marked_inline_tex'>" + $2.replace(/\$/g, '') + '</span>'
6262
}
6363
})
6464
} else {
6565
// 如果是行间公式,则使用<div class='marked_tex'>包裹公式内容,消除$$定界符
6666
// 如果不是LaTex公式,则直接返回原文本
67-
text = (isTeXLine) ? '<div class=\'marked_tex\'>' + text.replace(/\$/g, '') + '</div>' : text
67+
text = isTeXLine ? "<div class='marked_tex'>" + text.replace(/\$/g, '') + '</div>' : text
6868
}
6969
// 使用渲染器原有的`paragraph()`方法渲染整段文本
7070
text = this.old_paragraph(text)
@@ -84,7 +84,7 @@ export const translateMarkdown2html = plainText => {
8484
highlight: function (code) {
8585
/*eslint no-undef: "off"*/
8686
return hljs.highlightAuto(code).value
87-
}
87+
},
8888
})
8989
}
9090

@@ -124,7 +124,8 @@ export const groupBy = (arr, f) => {
124124
groups[group] = groups[group] || []
125125
groups[group].push(item)
126126
})
127-
return Object.keys(groups).map(group => groups[group])
127+
var res = Object.keys(groups).sort().reverse()
128+
return Object.keys(groups).map((group, i) => groups[res[i]])
128129
}
129130

130131
/**
@@ -152,9 +153,7 @@ export function getToken() {
152153
* @param {Number} len - 长度
153154
*/
154155
export function RandomId(len) {
155-
return Math.random()
156-
.toString(36)
157-
.substr(3, len)
156+
return Math.random().toString(36).substr(3, len)
158157
}
159158

160159
/**
@@ -190,11 +189,12 @@ export function encryption(data) {
190189
strs = strs.join('&') // 数组变字符串
191190
const endData = strs + '&sign=' + CryptoJS.MD5(strs + 'ADfj3kcadc2349akvm1CPFFCD84f').toString() // MD5加密
192191
const key = CryptoJS.enc.Utf8.parse('0880076B18D7EE81') // 加密秘钥
193-
const iv = CryptoJS.enc.Utf8.parse('CB3EC842D7C69578')// 矢量
194-
const encryptResult = CryptoJS.AES.encrypt(endData, key, {// AES加密
192+
const iv = CryptoJS.enc.Utf8.parse('CB3EC842D7C69578') // 矢量
193+
const encryptResult = CryptoJS.AES.encrypt(endData, key, {
194+
// AES加密
195195
iv: iv,
196196
mode: CryptoJS.mode.CBC,
197-
padding: CryptoJS.pad.Pkcs7 // 后台用的是pad.Pkcs5,前台对应为Pkcs7
197+
padding: CryptoJS.pad.Pkcs7, // 后台用的是pad.Pkcs5,前台对应为Pkcs7
198198
})
199199
return encodeURIComponent(CryptoJS.enc.Base64.stringify(encryptResult.ciphertext)) // Base64加密encode;
200200
}

0 commit comments

Comments
 (0)