-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
525 lines (490 loc) · 16 KB
/
Copy pathapp.js
File metadata and controls
525 lines (490 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
const express = require('express')
// 实例化express对象
const app = express()
const cors = require('cors')
// 解决请求跨域
app.use(cors())
const timeout = require('connect-timeout')
// 设置请求超时时间
app.use(timeout('5s'))
const bodyParser = require('body-parser')
// 解析 application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// 解析 application/json
app.use(bodyParser.json())
const fs = require('fs')
const utils = require('./utils')
const appIdMap = require('./alipay/appIdmap.js')
const request = require('request')
const homeService = require('./service/home')
const shelfService = require('./service/shelf')
const searchService = require('./service/search')
const userService = require('./service/user')
const bookService = require('./service/book')
const { resUrl, fileUrl2 } = require('./const')
const { connect } = require('./db')
const { OPEN_ID_NOT_NULL, QUERY_OK, QUERY_FAILED } = require('./utils/msg')
// 通用方法,用来处理失败时的响应值
function onFail(res, msg, code = -1, data = null) {
const result = {
error_code: code,
msg: msg,
}
data && (result.data = data)
res.json(result)
}
// 通用方法,用来处理成功时的响应值
function onSuccess(res, msg, data = {}, code = 0) {
res.json({
error_code: code,
msg: msg,
data,
})
}
app.get('/', function (req, res) {
res.send('欢迎学习mpvue实战多平台小程序')
})
/*
*********************
** 小程序首页相关API **
*********************
*/
// 获取首页数据
app.get('/book/home/v2', (req, res) => {
let openId = req.query.openId
if (!openId) {
onFail(res, OPEN_ID_NOT_NULL)
} else {
openId = decodeURIComponent(openId)
homeService.home(
openId,
(results) => onSuccess(res, QUERY_OK, results),
() => onFail(res, QUERY_FAILED),
)
}
})
// 获得微信openId
app.get('/openId/get', (req, res) => {
const appId = req.query.appId
const code = req.query.code
if (!appId || !code) {
onFail(res, '获取appId失败')
} else {
let secret = (appIdMap && appIdMap[appId]) || ''
if (!secret) {
secret = req.query.secret
}
if (secret) {
const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appId}&secret=${secret}&js_code=${code}&grant_type=authorization_code`
request.get(url, function (err, response, body) {
if (!err) {
const data = JSON.parse(body)
if (data.session_key && data.openid) {
onSuccess(res, '获取openId成功', data)
} else {
onFail(res, '获取openId失败')
}
} else {
onFail(res, '获取openId失败')
}
})
} else {
onFail(res, '获取密钥失败')
}
}
})
// 获取支付宝openId
const AlipaySdk = require('alipay-sdk').default
app.get('/openId/get/alipay', (req, res) => {
const appId = req.query.appId
const code = req.query.code
if (!appId || !code) {
onFail(res, '获取openId失败')
} else {
const alipaySdk = new AlipaySdk({
appId,
privateKey: fs.readFileSync(appIdMap[appId], 'ascii'),
})
alipaySdk
.exec('alipay.system.oauth.token', {
grantType: 'authorization_code',
code,
refreshToken: 'token',
})
.then((result) => {
console.log('alipay', result)
if (result) {
const { alipayUserId, userId, accessToken } = result
onSuccess(res, '获取openId成功', {
openid: `${userId}|${alipayUserId}`,
session_key: accessToken,
})
} else {
onFail(res, '获取openId失败')
}
})
.catch((err) => {
onFail(res, '获取openId失败', err)
})
}
})
// 用户注册
app.post('/user/register', (req, res) => {
const openId = req.body.openId
let platform = req.body.platform
if (!platform) {
req.body.platform = 'wx'
}
if (!openId) {
onFail(res, OPEN_ID_NOT_NULL)
} else {
userService.register(
req.body,
(results) => onSuccess(res, '用户注册成功'),
() => onFail(res, '用户注册失败'),
)
}
})
// 判断用户今天是否已经签到
app.get('/user/hasSignToday', (req, res) => {
const openId = req.query.openId
if (!openId) {
onFail(res, OPEN_ID_NOT_NULL)
} else {
userService.getSign(
openId,
(results) => {
if (results.length > 0) {
const now = new Date()
// 结果是按时间倒序的,所以获取第一个,就是最近的一次签到
const result = results[0]
const recentSignDate = new Date(result.create_dt)
let hasSignToday
// 和最近一次签到时间相隔不足24小时,说明是今天签到的
if (utils.isSameDay(now, recentSignDate)) {
hasSignToday = true
} else {
hasSignToday = false
}
// 判断连续签到天数
let continueSignDay = 0
let nextDay = now
// 如果今天签到了,连续签到先加1
if (hasSignToday) {
continueSignDay++
}
// 如果今天签到了,则从倒数第二开始计算,否则从第一次签到开始
const firstIndex = hasSignToday ? 1 : 0
for (let i = firstIndex; i < results.length; i++) {
// 获取最近的第二次签到
const r = results[i]
// 获取签到的日期
const signDate = new Date(r.create_dt)
// 获取今天前一天的日期
nextDay = utils.previousDate(nextDay)
if (utils.isSameDay(nextDay, signDate)) {
continueSignDay++
} else {
break
}
}
onSuccess(res, '获取签到数据成功', {
hasSignToday,
continueSignDay,
})
} else {
onSuccess(res, '尚未签到', {
hasSignToday: false,
continueSignDay: 0,
})
}
},
() => onFail(res, '获取签到状态失败'),
)
}
})
// 用户签到
app.get('/user/sign', (req, res) => {
const openId = req.query.openId
if (!openId) {
onFail(res, OPEN_ID_NOT_NULL)
} else {
userService.sign(
openId,
() => onSuccess(res, '用户签到成功'),
() => onFail(res, '用户签到失败'),
)
}
})
// 推荐阅读
app.get('/book/home/recommend/v2', (req, res) => {
homeService.recommend(
(results) => onSuccess(res, QUERY_OK, results),
() => onFail(res, QUERY_FAILED),
)
})
// 免费阅读
app.get('/book/home/freeRead/v2', (req, res) => {
homeService.freeRead(
(results) => onSuccess(res, QUERY_OK, results),
() => onFail(res, QUERY_FAILED),
)
})
// 热门阅读
app.get('/book/home/hotBook/v2', (req, res) => {
homeService.hotBook(
(results) => onSuccess(res, QUERY_OK, results),
() => onFail(res, QUERY_FAILED),
)
})
// 热搜
app.get('/book/hot-search', (req, res) => {
searchService.hotSearch(
(results) => onSuccess(res, QUERY_OK, results),
() => onFail(res, QUERY_FAILED),
)
})
// 图书详情
app.get('/book/detail', (req, res) => {
const conn = connect()
const fileName = req.query.fileName
const openId = req.query.openId
const sql = `select * from book where fileName='${fileName}'`
conn.query(sql, (err, results) => {
if (err) {
res.json({
error_code: 1,
msg: QUERY_FAILED,
})
} else {
if (results && results.length === 0) {
res.json({
error_code: 1,
msg: QUERY_FAILED,
})
} else {
const book = results[0]
book['cover'] = `${resUrl}/img/${book.cover}`
book['selected'] = false
book['private'] = false
book['cache'] = false
book['haveRead'] = 0
book['opf'] = `${fileUrl2}/${book.fileName}/${book.rootFile}`
if (openId && openId.length > 0) {
searchService.saveHotBook({
fileName: book.fileName,
title: book.title,
openId,
})
}
bookService.getBookDetailReader(
fileName,
openId,
(results) => {
book.readers = results[0]
book.readerNum = results[1][0].count
book.rank =
(results[2] &&
results[2].length > 0 &&
results[2][0].rank) ||
0
book.rankNum = results[3][0].count || 0
book.rankAvg = results[3][0].rank || 0
res.json({
error_code: 0,
msg: QUERY_OK,
data: book,
})
},
() => {
res.json({
error_code: 1,
msg: QUERY_FAILED,
data: book,
})
},
)
}
}
conn.end()
})
})
// 图书目录
app.get('/book/contents', (req, res) => {
const conn = connect()
const fileName = req.query.fileName || ''
const sql = `select * from contents where fileName='${fileName}' order by \`order\` asc`
conn.query(sql, (err, results) => {
if (err) {
res.json({
error_code: 1,
msg: '电子书信息获取失败',
})
} else {
if (results && results.length === 0) {
res.json({
error_code: 1,
msg: QUERY_FAILED,
})
} else {
res.json({
error_code: 0,
msg: QUERY_OK,
data: results,
})
}
}
conn.end()
})
})
// 加入书架
app.get('/book/shelf/save', (req, res) => {
let shelf = req.query.shelf
if (shelf) {
shelf = decodeURIComponent(shelf)
shelfService.saveToShelf(
JSON.parse(shelf),
() => onSuccess(res, '加入书架成功'),
() => onFail(res, '加入书架失败'),
)
} else {
onFail(res, '加入书架失败')
}
})
// 获取书架
app.get('/book/shelf/get', (req, res) => {
const openId = req.query.openId
const fileName = req.query.fileName
function wrapperBook(data) {
return data.map((book) => {
book['cover'] = `${resUrl}/img/${book.cover}`
return book
})
}
shelfService.getShelfByOpenId(
{ openId, fileName },
(data) => onSuccess(res, '书架获取成功', wrapperBook(data)),
() => onFail(res, '书架获取失败'),
)
})
// 移出书架
app.get('/book/shelf/remove', (req, res) => {
let shelf = req.query.shelf
if (shelf) {
shelf = decodeURIComponent(shelf)
shelfService.removeFromShelf(
JSON.parse(shelf),
() => onSuccess(res, '移出书架成功'),
() => onFail(res, '移出书架失败'),
)
} else {
onFail(res, '移出书架失败')
}
})
// 搜索图书
app.get('/book/search', (req, res) => {
function onQuerySuccess(results) {
onSuccess(res, QUERY_OK, results)
if (openId && openId.trim().length > 0) {
searchService.saveHotSearch({ keyword, openId })
}
}
let keyword = req.query.keyword
let page = req.query.page || 1
let pageSize = req.query.pageSize || 20
let openId = req.query.openId
if (keyword && keyword.length > 0) {
keyword = decodeURIComponent(keyword)
searchService.search({ keyword, page, pageSize }, onQuerySuccess, () =>
onFail(res, QUERY_FAILED),
)
} else {
onFail(res, QUERY_FAILED)
}
})
// 搜索图书列表
app.get('/book/search-list', (req, res) => {
const publisher = req.query.publisher
const author = req.query.author
const category = req.query.category
const categoryId = req.query.categoryId
let page = req.query.page || 1
let pageSize = req.query.pageSize || 20
if (publisher || author || category || categoryId) {
const params = {}
publisher && (params.publisher = decodeURIComponent(publisher))
author && (params.author = decodeURIComponent(author))
category && (params.category = decodeURIComponent(category))
categoryId && (params.categoryId = decodeURIComponent(categoryId))
params.page = page
params.pageSize = pageSize
searchService.searchBook(
params,
(results) => onSuccess(res, QUERY_OK, results),
() => onFail(res, QUERY_FAILED),
)
} else {
onFail(res, QUERY_FAILED)
}
})
// 分类图书列表
app.get('/book/category/list/v2', (req, res) => {
homeService.allCategory(
(results) => onSuccess(res, QUERY_OK, results),
() => onFail(res, QUERY_FAILED),
)
})
// 图书评分
app.get('/book/rank/save', (req, res) => {
const openId = req.query.openId
const fileName = req.query.fileName
const rank = req.query.rank
if (!openId) {
onFail(res, OPEN_ID_NOT_NULL)
} else {
bookService.saveBookRank(
fileName,
openId,
rank,
() => onSuccess(res, '保存评分成功'),
() => onFail(res, '保存评分失败'),
)
}
})
// 计算用户加入平台时间
app.get('/user/day', (req, res) => {
const openId = req.query.openId
if (!openId) {
onFail(res, OPEN_ID_NOT_NULL)
} else {
userService.day(
openId,
(results) => onSuccess(res, QUERY_OK, results),
() => onFail(res, QUERY_FAILED),
)
}
})
// 启动http服务
const server = app.listen(4000, function () {
const host = server.address().address
const port = server.address().port
console.log('listening at http://%s:%s', host, port)
})
// 启动https服务
const httpsPrivateKey = '/https/xiaoyangmm.top.key' // 替换为自己的密钥
const httpsCertificate = '/https/xiaoyangmm.top.pem' // 替换为自己的证书
const https = require('https')
const privateKey = fs.readFileSync(httpsPrivateKey, 'utf8')
const certificate = fs.readFileSync(httpsCertificate, 'utf8')
const credentials = { key: privateKey, cert: certificate }
const httpsServer = https.createServer(credentials, app)
httpsServer.listen(4001, function () {
console.log('HTTPS Server is running on: https://localhost:%s', 4001)
})
// 异常监听
process.on('uncaughtException', function (err) {
//打印出错误
console.log(err)
//打印出错误的调用栈方便调试
console.log(err.stack)
})