forked from wangtunan/vue-mooc
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathorder.js
404 lines (395 loc) · 8.84 KB
/
order.js
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
import Router from 'koa-router'
import Order from '../models/order.js'
import Cart from '../models/cart.js'
import Bill from '../models/bill.js'
import Recharge from '../models/recharge.js'
import UserLesson from '../models/userLesson.js'
import axios from 'axios'
import checkUser from '../middleware/auth.js'
import { getGuid, getOrderId } from '../../src/utils/utils.js'
import { ERR_OK, payWay, SIZE } from '../config.js';
const router = new Router({
prefix: '/order'
})
// 生成订单
router.post('/create', checkUser, async (ctx) => {
const userid = ctx.session.user_id
const { list } = ctx.request.body
if (!list || list.length === 0) {
ctx.body = {
code: -1,
msg: '缺少订单关键参数list'
}
return false
}
try {
let lessonIds = list.map(item => item.id)
const orderid = getOrderId()
const time = new Date().toISOString().replace('T', ' ').substring(0, 19)
const orderResult = await Order.create({
id: getGuid(),
userid: userid,
code: orderid,
time: time,
status: {
text: '待支付',
code: 0
},
way: {},
list: list
})
const cartResult = await Cart.deleteMany({
userid: userid,
id: {
$in: lessonIds
}
})
if (orderResult && cartResult) {
ctx.body = {
code: ERR_OK,
msg: '生成订单成功',
data: {
code: orderResult.code
}
}
} else {
ctx.body = {
code: -1,
msg: '生成订单失败'
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '服务器异常'
}
}
})
// 订单详情
router.get('/info', checkUser, async (ctx) => {
const userid = ctx.session.user_id
const { code } = ctx.query
try {
const result = await Order.findOne({
userid: userid,
code: code,
'status.code': 0
})
if (result) {
ctx.body = {
code: ERR_OK,
msg: '获取订单详情成功',
data: result
}
} else {
ctx.body = {
code: -1,
msg: '获取订单详情失败获取',
result
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: '服务器异常'
}
}
})
// 订单支付
router.post('/pay', checkUser, async (ctx) => {
const userid = ctx.session.user_id
const { code, way = 0 } = ctx.request.body
if (!code) {
ctx.body = {
code: -1,
msg: '缺少关键参数code'
}
}
try {
let insertBillData = []
let userLessonData = []
const searchResult = await Order.findOne({
userid: userid,
code: code
})
const list = searchResult.list
// 判断是否有订单信息
if (!searchResult) {
ctx.body = {
code: -1,
msg: '未找到当前订单信息'
}
return false
}
// 判断订单是否已过期
const time = new Date(searchResult.time).getTime()
const nowTime = new Date().getTime() - 8 * 60 * 60 * 1000
const expired = searchResult.expired
if ((time + expired) < nowTime) {
ctx.body = {
code: -1,
msg: '订单已过期,请重新选择并添加至购物车'
}
return false
}
// 判断订单是否已支付
if (+searchResult.status.code === 1) {
ctx.body = {
code: -1,
msg: '当前订单已支付,切勿重复支付'
}
return false
}
// 如果是余额支付,则扣除余额
if (+way === 2) {
try {
let cost = 0
list.forEach(item => {
if (item.isDiscount) {
cost = Number((cost + parseFloat(item.discountPrice)).toFixed(2))
} else {
cost = Number((cost + parseFloat(item.price)).toFixed(2))
}
})
// 判断余额是否充足
const { data } = await axios.get(`http://127.0.0.1:4300/recharge/charge?userid=${userid}`)
if (data.data < cost) {
ctx.body = {
code: -1,
msg: '余额不足'
}
return false
}
const result = await Recharge.create({
id: getGuid(),
userid: userid,
time: new Date().toISOString().replace('T', '').substring(0, 19),
money: cost,
action: {
text: '转出',
code: 1
},
way: {
text: '余额',
code: 2
},
remark: `订单支出,订单号:${code}`
})
if (!result) {
ctx.body = {
code: -1,
msg: '余额支付失败'
}
return false
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '余额支付失败'
}
return false
}
}
list.forEach(item => {
insertBillData.push({
id: getGuid(),
userid: userid,
orderno: code,
name: item.title,
time: searchResult.time,
cost: item.isDiscount ? item.discountPrice : item.price,
way: {
text: `${payWay[way]}支付`,
code: way
}
})
userLessonData.push({
id: getGuid(),
userid: userid,
lessonid: item.lessonid,
title: item.title,
img: item.img,
type: {
text: '实战课程',
code: 1
}
})
})
const orderResult = await Order.findOneAndUpdate({
userid: userid,
code: code
}, {
status: {
text: '已完成',
code: 1
},
way: {
text: `${payWay[way]}支付`,
code: way
}
})
const billResult = await Bill.insertMany(insertBillData)
const userLessonResult = await UserLesson.insertMany(userLessonData)
if (orderResult && billResult && userLessonResult) {
ctx.body = {
code: ERR_OK,
msg: '订单支付成功'
}
} else {
ctx.body = {
code: -1,
msg: '订单支付失败'
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '服务器异常'
}
}
})
// 订单列表
router.get('/list', checkUser, async (ctx) => {
const userid = ctx.session.user_id
const { page = 1, status = '', size = SIZE } = ctx.query
// 主动处理过期数据
try {
const time = new Date((new Date().getTime() - 30 * 60 * 1000)).toISOString().replace('T', ' ').substring(0, 19)
await Order.find({
userid: userid,
time: {
$lt: time
},
'status.code': {
$ne: 1
}
}).updateMany({
status: {
text: '已过期',
code: 3
}
})
} catch (e) {
console.log(e.message)
}
try {
let where = {
userid: userid
}
if (status || status === 0 || status === '0') {
if (+status === 2) {
where['status.code'] = {
$nin: [0, 1]
}
} else {
where['status.code'] = status
}
}
const total = await Order.find(where).countDocuments()
const result = await Order.find(where).sort({
time: -1
}).skip((page - 1) * size).limit(+size)
if (result) {
ctx.body = {
code: ERR_OK,
msg: '获取订单数据成功',
data: {
list: result,
total: total
}
}
} else {
ctx.body = {
code: -1,
msg: '获取订单数据成功',
data: {
list: [],
total: 0
}
}
}
} catch (e) {
ctx.body = {
code: -1,
msg: e.message || '服务器异常',
data: {
list: [],
total: 0
}
}
}
})
// 取消订单
router.get('/cancel', checkUser, async (ctx) => {
const userid = ctx.session.user_id
const { id } = ctx.query
if (!id) {
ctx.body = {
code: -1,
msg: '缺少关键参数id'
}
return false
}
try {
const result = await Order.findOneAndUpdate({
id: id,
userid: userid
}, {
status: {
text: '已关闭',
code: 2
}
})
if (result) {
ctx.body = {
code: ERR_OK,
msg: '订单取消成功'
}
} else {
ctx.body = {
code: -1,
msg: '订单取消失败'
}
}
} catch(e) {
ctx.body = {
code: -1,
msg: e.message || '服务器异常'
}
}
})
// 删除订单
router.get('/delete', checkUser, async (ctx) => {
const userid = ctx.session.user_id
const { id } = ctx.query
if (!id) {
ctx.body = {
code: -1,
msg: '缺少关键参数id'
}
return false
}
try {
const result = await Order.findOneAndDelete({
id: id,
userid: userid
})
if (result) {
ctx.body = {
code: ERR_OK,
msg: '订单删除成功'
}
} else {
ctx.body = {
code: -1,
msg: '订单删除失败'
}
}
} catch(e) {
ctx.body = {
code: -1,
msg: e.message || '服务器异常'
}
}
})
export default router