1
+ const express = require ( 'express' )
2
+ const moment = require ( 'moment' )
3
+ const router = express . Router ( )
4
+ const mysql = require ( './../mysql/db' )
5
+ const MarkdownIt = require ( 'markdown-it' )
6
+ const markdownItTocAndAnchor = require ( 'markdown-it-toc-and-anchor' ) . default
7
+ const hljs = require ( 'highlight.js' )
8
+
9
+ router . post ( '/add' , addArticle )
10
+ router . post ( '/update' , updateArticle )
11
+ router . get ( '/detail' , getArticleDetail )
12
+ router . post ( '/delete' , deleteArticle )
13
+
14
+ async function addArticle ( req , res , next ) {
15
+ try {
16
+ let subTitle = req . body . content . slice ( 0 , 200 )
17
+ let titleArray = [ ]
18
+ let titleObject = { }
19
+ const md = new MarkdownIt ( {
20
+ html : true ,
21
+ linkify : true ,
22
+ typographer : true ,
23
+ highlight : function ( str , lang ) {
24
+ if ( lang && hljs . getLanguage ( lang ) ) {
25
+ try {
26
+ const preCode = hljs . highlight ( lang , str , true ) . value
27
+ const lines = preCode . split ( / \n / ) . slice ( 0 , - 1 )
28
+ let html = lines . map ( ( item , index ) => {
29
+ return '<li><span class="line-num" data-line="' + ( index + 1 ) + '"></span>' + item + '</li>'
30
+ } ) . join ( '' )
31
+ html = '<ol>' + html + '</ol>'
32
+ if ( lines . length ) {
33
+ html += '<b class="name">' + lang + '</b>'
34
+ }
35
+ return '<pre class="hljs"><code>' +
36
+ html +
37
+ '</code></pre>'
38
+ } catch ( __ ) { }
39
+ }
40
+
41
+ const preCode = md . utils . escapeHtml ( str )
42
+ const lines = preCode . split ( / \n / ) . slice ( 0 , - 1 )
43
+ let html = lines . map ( ( item , index ) => {
44
+ return '<li><span class="line-num" data-line="' + ( index + 1 ) + '"></span>' + item + '</li>'
45
+ } ) . join ( '' )
46
+ html = '<ol>' + html + '</ol>'
47
+ return '<pre class="hljs"><code>' +
48
+ html +
49
+ '</code></pre>'
50
+ }
51
+ } )
52
+ let articleContentHtml = md . use ( markdownItTocAndAnchor , {
53
+ tocCallback : function ( tocMarkdown , tocArray , tocHtml ) {
54
+ titleArray = tocArray
55
+ }
56
+ } ) . use ( require ( 'markdown-it-sub' ) ) . use ( require ( 'markdown-it-sup' ) ) . use ( require ( 'markdown-it-deflist' ) ) . use ( require ( 'markdown-it-abbr' ) ) . use ( require ( 'markdown-it-footnote' ) ) . use ( require ( 'markdown-it-ins' ) ) . use ( require ( 'markdown-it-mark' ) ) . render ( `@[toc]\r${ req . body . content } ` )
57
+ let insertData = await mysql . query ( 'INSERT INTO vue_blog (articleTitle, articleSubTitle, articleNature, articleKey, articleContentMarkdown, articleContentHtml, articleAuthorId, articleCreateTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' ,
58
+ [ req . body . title , subTitle , req . body . nature , req . body . keyWords . join ( ) , req . body . content , articleContentHtml , req . body . authorId , moment ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ] )
59
+ titleArray . map ( item => {
60
+ if ( titleObject . hasOwnProperty ( `h${ item . level } ` ) ) {
61
+ titleObject [ `h${ item . level } ` ] += `,${ item . content } `
62
+ } else {
63
+ titleObject [ `h${ item . level } ` ] = item . content
64
+ }
65
+ } )
66
+ await mysql . query ( 'INSERT INTO vue_blog_title (articleId, h0, h1, h2, h3, h4, h5, h6) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' ,
67
+ [
68
+ insertData . insertId ,
69
+ req . body . title ,
70
+ titleObject . h1 ? titleObject . h1 : '' ,
71
+ titleObject . h2 ? titleObject . h2 : '' ,
72
+ titleObject . h3 ? titleObject . h3 : '' ,
73
+ titleObject . h4 ? titleObject . h4 : '' ,
74
+ titleObject . h5 ? titleObject . h5 : '' ,
75
+ titleObject . h6 ? titleObject . h6 : ''
76
+ ]
77
+ )
78
+ return res . json ( {
79
+ isok : true ,
80
+ msg : ''
81
+ } ) ;
82
+ } catch ( error ) {
83
+ return res . json ( {
84
+ isok : false ,
85
+ msg : error
86
+ } ) ;
87
+ }
88
+ next ( ) ;
89
+ }
90
+
91
+ async function updateArticle ( req , res , next ) {
92
+ try {
93
+ let subTitle = req . body . content . slice ( 0 , 200 )
94
+ let titleArray = [ ]
95
+ let titleObject = { }
96
+ const md = new MarkdownIt ( {
97
+ html : true ,
98
+ linkify : true ,
99
+ typographer : true ,
100
+ highlight : function ( str , lang ) {
101
+ if ( lang && hljs . getLanguage ( lang ) ) {
102
+ try {
103
+ const preCode = hljs . highlight ( lang , str , true ) . value
104
+ const lines = preCode . split ( / \n / ) . slice ( 0 , - 1 )
105
+ let html = lines . map ( ( item , index ) => {
106
+ return '<li><span class="line-num" data-line="' + ( index + 1 ) + '"></span>' + item + '</li>'
107
+ } ) . join ( '' )
108
+ html = '<ol>' + html + '</ol>'
109
+ if ( lines . length ) {
110
+ html += '<b class="name">' + lang + '</b>'
111
+ }
112
+ return '<pre class="hljs"><code>' +
113
+ html +
114
+ '</code></pre>'
115
+ } catch ( __ ) { }
116
+ }
117
+
118
+ const preCode = md . utils . escapeHtml ( str )
119
+ const lines = preCode . split ( / \n / ) . slice ( 0 , - 1 )
120
+ let html = lines . map ( ( item , index ) => {
121
+ return '<li><span class="line-num" data-line="' + ( index + 1 ) + '"></span>' + item + '</li>'
122
+ } ) . join ( '' )
123
+ html = '<ol>' + html + '</ol>'
124
+ return '<pre class="hljs"><code>' +
125
+ html +
126
+ '</code></pre>'
127
+ }
128
+ } )
129
+ let articleContentHtml = md . use ( markdownItTocAndAnchor , {
130
+ tocCallback : function ( tocMarkdown , tocArray , tocHtml ) {
131
+ titleArray = tocArray
132
+ }
133
+ } ) . use ( require ( 'markdown-it-sub' ) ) . use ( require ( 'markdown-it-sup' ) ) . use ( require ( 'markdown-it-deflist' ) ) . use ( require ( 'markdown-it-abbr' ) ) . use ( require ( 'markdown-it-footnote' ) ) . use ( require ( 'markdown-it-ins' ) ) . use ( require ( 'markdown-it-mark' ) ) . render ( `@[toc]\r${ req . body . content } ` )
134
+
135
+ let updateData = await mysql . query ( 'UPDATE vue_blog SET articleTitle = ?, articleSubTitle = ?, articleNature = ?, articleKey = ?, articleContentMarkdown = ?, articleContentHtml = ?, articleUpdateTime = ? WHERE articleId = ?' ,
136
+ [ req . body . title , subTitle , req . body . nature , req . body . keyWords . join ( ) , req . body . content , articleContentHtml , moment ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) , req . body . articleId ] )
137
+
138
+ titleArray . map ( item => {
139
+ if ( titleObject . hasOwnProperty ( `h${ item . level } ` ) ) {
140
+ titleObject [ `h${ item . level } ` ] += `,${ item . content } `
141
+ } else {
142
+ titleObject [ `h${ item . level } ` ] = item . content
143
+ }
144
+ } )
145
+ let selectTitle = await mysql . query ( 'SELECT articleId FROM vue_blog_title WHERE articleId = ?' , [ req . body . articleId ] )
146
+ console . log ( selectTitle )
147
+ if ( selectTitle . length ) {
148
+ await mysql . query ( 'UPDATE vue_blog_title SET h0 = ?, h1 = ?, h2 = ?, h3 = ?, h4 = ?, h5 = ?, h6 = ? WHERE articleId = ?' ,
149
+ [
150
+ req . body . title ,
151
+ titleObject . h1 ? titleObject . h1 : '' ,
152
+ titleObject . h2 ? titleObject . h2 : '' ,
153
+ titleObject . h3 ? titleObject . h3 : '' ,
154
+ titleObject . h4 ? titleObject . h4 : '' ,
155
+ titleObject . h5 ? titleObject . h5 : '' ,
156
+ titleObject . h6 ? titleObject . h6 : '' ,
157
+ req . body . articleId
158
+ ]
159
+ )
160
+ } else {
161
+ await mysql . query ( 'INSERT INTO vue_blog_title (articleId, h0, h1, h2, h3, h4, h5, h6) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' ,
162
+ [
163
+ req . body . articleId ,
164
+ req . body . title ,
165
+ titleObject . h1 ? titleObject . h1 : '' ,
166
+ titleObject . h2 ? titleObject . h2 : '' ,
167
+ titleObject . h3 ? titleObject . h3 : '' ,
168
+ titleObject . h4 ? titleObject . h4 : '' ,
169
+ titleObject . h5 ? titleObject . h5 : '' ,
170
+ titleObject . h6 ? titleObject . h6 : ''
171
+ ]
172
+ )
173
+ }
174
+ return res . json ( {
175
+ isok : true ,
176
+ msg : ''
177
+ } ) ;
178
+ } catch ( error ) {
179
+ return res . json ( {
180
+ isok : false ,
181
+ msg : error
182
+ } ) ;
183
+ }
184
+ next ( ) ;
185
+ }
186
+
187
+ async function getArticleDetail ( req , res , next ) {
188
+ try {
189
+ const articleId = req . query . articleId * 1
190
+ let selectData = await mysql . query ( `SELECT * FROM vue_blog WHERE articleId = ?` ,
191
+ [ articleId ] )
192
+ let authorInfo = await mysql . query ( `SELECT authorId, authorName FROM vue_blog_author WHERE authorId = ?` ,
193
+ [ selectData [ 0 ] . articleAuthorId ] )
194
+ if ( req . query . changeView == 1 ) {
195
+ await mysql . query ( `UPDATE vue_blog SET articleView = ? WHERE articleId = ?` ,
196
+ [ selectData [ 0 ] . articleView + 1 , articleId ] )
197
+ }
198
+ let articleIdArray = await mysql . query ( `SELECT articleId FROM vue_blog` )
199
+ articleIdArray = articleIdArray . map ( item => item . articleId )
200
+ let nowIndex = articleIdArray . indexOf ( articleId )
201
+ let prevInfo , nextInfo , prevIndex , nextIndex
202
+ if ( nowIndex > 0 && nowIndex < articleIdArray . length - 1 ) {
203
+ prevIndex = nowIndex - 1
204
+ nextIndex = nowIndex + 1
205
+ } else if ( nowIndex === 0 ) {
206
+ prevIndex = articleIdArray . length - 1
207
+ nextIndex = nowIndex + 1
208
+ } else if ( nowIndex === articleIdArray . length - 1 ) {
209
+ prevIndex = nowIndex - 1
210
+ nextIndex = 0
211
+ }
212
+ prevInfo = await mysql . query ( `SELECT articleId, articleTitle FROM vue_blog WHERE articleId = ?` ,
213
+ [ articleIdArray [ prevIndex ] ] )
214
+ nextInfo = await mysql . query ( `SELECT articleId, articleTitle FROM vue_blog WHERE articleId = ?` ,
215
+ [ articleIdArray [ nextIndex ] ] )
216
+ return res . json ( {
217
+ isok : true ,
218
+ data : {
219
+ info : selectData . length ? selectData [ 0 ] : [ ] ,
220
+ prevInfo : prevInfo . length ? prevInfo [ 0 ] : [ ] ,
221
+ nextInfo : nextInfo . length ? nextInfo [ 0 ] : [ ] ,
222
+ authorInfo : authorInfo [ 0 ]
223
+ } ,
224
+ msg : ''
225
+ } ) ;
226
+ } catch ( error ) {
227
+ return res . json ( {
228
+ isok : false ,
229
+ msg : error
230
+ } ) ;
231
+ }
232
+ next ( ) ;
233
+ }
234
+
235
+ async function deleteArticle ( req , res , next ) {
236
+ try {
237
+ let selectArticleInfo = await mysql . query ( 'SELECT articleAuthorId FROM vue_blog WHERE articleId = ?' , [ req . body . articleId ] )
238
+ let selectUserInfo = await mysql . query ( 'SELECT authority FROM vue_blog_author WHERE authorId = ?' , [ req . body . userId ] )
239
+ if ( selectUserInfo [ 0 ] . authority == 0 || selectArticleInfo [ 0 ] . articleAuthorId == req . body . userId ) {
240
+ let deleteArticleInfo = await mysql . query ( 'DELETE FROM vue_blog WHERE articleId = ?' , [ req . body . articleId ] )
241
+ return res . json ( {
242
+ isok : true ,
243
+ msg : ''
244
+ } ) ;
245
+ } else {
246
+ return res . json ( {
247
+ isok : false ,
248
+ msg : '无权限操作'
249
+ } ) ;
250
+ }
251
+ } catch ( error ) {
252
+ return res . json ( {
253
+ isok : false ,
254
+ msg : error
255
+ } ) ;
256
+ }
257
+ next ( ) ;
258
+ }
259
+
260
+ module . exports = router ;
0 commit comments