Skip to content

Commit 1b9eb5a

Browse files
committed
全局添加路由鉴权/课程接口完善
1 parent 28a8474 commit 1b9eb5a

File tree

26 files changed

+145
-246
lines changed

26 files changed

+145
-246
lines changed

server/config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
export const ERR_OK = 0
2+
23
export const SIZE = 10
4+
35
export const payWay = {
46
0: '支付宝',
57
1: '微信',
68
2: '余额'
79
}
10+
811
export default {
912
dbs: 'mongodb://127.0.0.1:27017/mooc'
1013
}

server/interface/address.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import Router from 'koa-router'
22
import Address from '../models/address.js'
33
import { ERR_OK } from '../config.js'
44
import { getGuid } from '../../src/utils/utils.js'
5+
import checkUser from '../middleware/auth.js'
56
const router = new Router({
67
prefix: '/address'
78
})
89

910
// 获取收获地址接口
10-
router.get('/list', async (ctx) => {
11+
router.get('/list', checkUser, async (ctx) => {
1112
const result = await Address.find({
1213
userid: ctx.session.user_id
1314
}).sort({
@@ -29,7 +30,7 @@ router.get('/list', async (ctx) => {
2930
})
3031

3132
// 新增收获地址接口
32-
router.post('/create', async (ctx) => {
33+
router.post('/create', checkUser, async (ctx) => {
3334
const params = ctx.request.body
3435
params.id = getGuid()
3536
params.userid = ctx.session.user_id
@@ -49,7 +50,7 @@ router.post('/create', async (ctx) => {
4950
})
5051

5152
// 编辑收获地址接口
52-
router.post('/update', async (ctx) => {
53+
router.post('/update', checkUser, async (ctx) => {
5354
const params = ctx.request.body
5455
const result = await Address.findOneAndUpdate({
5556
userid: ctx.session.user_id,
@@ -75,7 +76,7 @@ router.post('/update', async (ctx) => {
7576
})
7677

7778
// 删除收获地址接口
78-
router.get('/delete', async (ctx) => {
79+
router.get('/delete', checkUser, async (ctx) => {
7980
const { id } = ctx.query
8081
if (!id) {
8182
ctx.body = {
@@ -102,7 +103,7 @@ router.get('/delete', async (ctx) => {
102103
})
103104

104105
// 设为默认收获地址
105-
router.get('/default', async (ctx) => {
106+
router.get('/default', checkUser, async (ctx) => {
106107
const { id } = ctx.query
107108
const userid = ctx.session.user_id
108109
const setResult = await Address.find({

server/interface/bill.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import Router from 'koa-router'
22
import Bill from '../models/bill.js'
3+
import checkUser from '../middleware/auth.js'
34
import { ERR_OK, SIZE } from '../config.js'
45
const router = new Router({
56
prefix: '/bill'
67
})
78

89
// 消费记录列表
9-
router.get('/list', async (ctx) => {
10+
router.get('/list', checkUser, async (ctx) => {
1011
const userid = ctx.session.user_id
1112
const { page = 1, size = SIZE } = ctx.query
1213
try {

server/interface/cart.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import Router from 'koa-router'
22
import Cart from '../models/cart.js'
3+
import checkUser from '../middleware/auth.js'
34
import { getGuid } from '../../src/utils/utils.js'
45
import { ERR_OK } from '../config.js';
56
const router = new Router({
67
prefix: '/cart'
78
})
89

910
// 购物车列表接口
10-
router.get('/list', async (ctx) => {
11+
router.get('/list', checkUser, async (ctx) => {
1112
const userid = ctx.session.user_id
1213
try {
1314
const where = { userid: userid }
@@ -35,7 +36,7 @@ router.get('/list', async (ctx) => {
3536
})
3637

3738
// 添加购物车
38-
router.post('/create', async (ctx) => {
39+
router.post('/create', checkUser, async (ctx) => {
3940
const userid = ctx.session.user_id
4041
const { id, title, img, price, isDiscount, discountPrice } = ctx.request.body
4142
try {
@@ -81,7 +82,7 @@ router.post('/create', async (ctx) => {
8182
})
8283

8384
// 删除购物车
84-
router.post('/delete', async (ctx) => {
85+
router.post('/delete', checkUser, async (ctx) => {
8586
const userid = ctx.session.user_id
8687
const { id } = ctx.request.body
8788
if (!id) {
@@ -117,7 +118,7 @@ router.post('/delete', async (ctx) => {
117118
})
118119

119120
// 批量删除购物车
120-
router.post('/delete/ids', async (ctx) => {
121+
router.post('/delete/ids', checkUser, async (ctx) => {
121122
const userid = ctx.session.user_id
122123
const { ids } = ctx.request.body
123124
if (!ids || ids.length === 0) {

server/interface/coupon.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import Router from 'koa-router'
22
import Coupon from '../models/coupon.js'
3+
import checkUser from '../middleware/auth.js'
34
import { ERR_OK } from '../config.js'
45
const router = new Router({
56
prefix: '/coupon'
67
})
78

89
// 优惠券列表数据
9-
router.get('/list', async (ctx) => {
10+
router.get('/list', checkUser, async (ctx) => {
1011
const userid = ctx.session.user_id
1112
const { status } = ctx.query
1213
try {

server/interface/label.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Router from 'koa-router'
22
import Label from '../models/label.js'
33
import LableType from '../models/labelType.js'
44
import LabelFollow from '../models/labelFollow.js'
5+
import checkUser from '../middleware/auth.js'
56
import { ERR_OK } from '../config.js'
67
import { getGuid } from '../../src/utils/utils.js';
78
const router = new Router({
@@ -69,7 +70,7 @@ router.get('/list', async (ctx) => {
6970
})
7071

7172
// 用户关注标签路由
72-
router.post('/follow', async (ctx) => {
73+
router.post('/follow', checkUser, async (ctx) => {
7374
const userid = ctx.session.user_id
7475
const { list } = ctx.request.body
7576
// 验证是否有要关注的标签
@@ -121,7 +122,7 @@ router.post('/follow', async (ctx) => {
121122
})
122123

123124
// 用户关注标签列表路由
124-
router.get('/follow/list', async (ctx) => {
125+
router.get('/follow/list', checkUser, async (ctx) => {
125126
const userid = ctx.session.user_id
126127
try {
127128
const result = await LabelFollow.find({

server/interface/lesson.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import Lesson from '../models/lesson.js'
33
import UserLesson from '../models/userLesson.js'
44
import Catalog from '../models/catalog.js'
55
import { ERR_OK, SIZE } from '../config.js'
6+
import checkUser from '../middleware/auth.js'
67
const router = new Router({
78
prefix: '/lesson'
89
})
910

1011
// 课程列表路由
1112
router.get('/list', async (ctx) => {
13+
const userid = ctx.session.user_id
1214
const {
1315
page = 1,
1416
size = SIZE,
@@ -44,8 +46,18 @@ router.get('/list', async (ctx) => {
4446
sortWhere[sort] = -1
4547
}
4648
const total = await Lesson.find(where).countDocuments()
47-
const result = await Lesson.find(where).skip((page - 1) * size).limit(+size).sort(sortWhere)
49+
const result = await Lesson.find(where).skip((page - 1) * size).limit(+size).sort(sortWhere).lean()
4850
if (result) {
51+
// 处理是否购买,是否收藏
52+
if (+type === 1) {
53+
for (let index = 0; index < result.length; index++) {
54+
const isBuyed = await UserLesson.findOne({
55+
userid: userid,
56+
lessonid: result[index].id
57+
})
58+
result[index]['isBuy'] = !!isBuyed
59+
}
60+
}
4961
ctx.body = {
5062
code: ERR_OK,
5163
msg: '获取课程数据成功',
@@ -77,7 +89,7 @@ router.get('/list', async (ctx) => {
7789
})
7890

7991
// 课程详情路由
80-
router.get('/info', async (ctx) => {
92+
router.get('/info', checkUser, async (ctx) => {
8193
const { id } = ctx.query
8294
if (!id) {
8395
ctx.body = {
@@ -115,7 +127,7 @@ router.get('/info', async (ctx) => {
115127
})
116128

117129
// 用户课程列表路由
118-
router.get('/user', async (ctx) => {
130+
router.get('/user', checkUser, async (ctx) => {
119131
const userid = ctx.session.user_id
120132
const { page = 1, size = SIZE } = ctx.query
121133
try {

server/interface/log.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import Router from 'koa-router'
22
import Log from '../models/log.js'
3+
import checkUser from '../middleware/auth.js'
34
import { SIZE, ERR_OK } from '../config.js'
45
const router = new Router({
56
prefix: '/log'
67
})
78

89
// 分页获取登录日志接口
9-
router.get('/list', async (ctx) => {
10+
router.get('/list', checkUser, async (ctx) => {
1011
const { page = 1 } = ctx.query
1112
const userid = ctx.session.user_id
1213
const where = { userid }
@@ -33,7 +34,7 @@ router.get('/list', async (ctx) => {
3334
})
3435

3536
// 新增一条登录日志
36-
router.post('/create', async (ctx) => {
37+
router.post('/create', checkUser, async (ctx) => {
3738
const params = ctx.request.body
3839
const result = await Log.create(params)
3940
if (result) {

server/interface/notice.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import Router from 'koa-router'
22
import Notice from '../models/notice.js'
33
import UserNotice from '../models/userNotice.js'
4+
import checkUser from '../middleware/auth.js'
45
import { SIZE, ERR_OK } from '../config.js'
56
import { getGuid } from '../../src/utils/utils.js'
67
const router = new Router({
78
prefix: '/notice'
89
})
910

1011
// 消息分页列表接口
11-
router.get('/list', async (ctx) => {
12+
router.get('/list', checkUser, async (ctx) => {
1213
const { page = 1, code } = ctx.query
1314
const userid = ctx.session.user_id
1415
let where = {}
@@ -61,7 +62,7 @@ router.get('/list', async (ctx) => {
6162
})
6263

6364
// 单个消息已读
64-
router.post('/read', async (ctx) => {
65+
router.post('/read', checkUser, async (ctx) => {
6566
const { id } = ctx.request.body
6667
const userid = ctx.session.user_id
6768
// 判断是否传入id
@@ -126,7 +127,7 @@ router.post('/read', async (ctx) => {
126127
})
127128

128129
// 消息全部已读
129-
router.post('/read/all', async (ctx) => {
130+
router.post('/read/all', checkUser, async (ctx) => {
130131
const { ids } = ctx.request.body
131132
const userid = ctx.session.user_id
132133
try {
@@ -161,7 +162,7 @@ router.post('/read/all', async (ctx) => {
161162
})
162163

163164
// 单个删除消息
164-
router.post('/delete', async (ctx) => {
165+
router.post('/delete', checkUser, async (ctx) => {
165166
const { id } = ctx.request.body
166167
const userid = ctx.session.user_id
167168
if (!id) {
@@ -224,7 +225,7 @@ router.post('/delete', async (ctx) => {
224225
})
225226

226227
// 是否存在未读消息
227-
router.get('/read/not', async (ctx) => {
228+
router.get('/read/not', checkUser, async (ctx) => {
228229
try {
229230
const noticeCount = await Notice.find().countDocuments()
230231
const userCount = await UserNotice.find({

server/interface/order.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import Bill from '../models/bill.js'
55
import Recharge from '../models/recharge.js'
66
import UserLesson from '../models/userLesson.js'
77
import axios from 'axios'
8+
import checkUser from '../middleware/auth.js'
89
import { getGuid, getOrderId } from '../../src/utils/utils.js'
910
import { ERR_OK, payWay, SIZE } from '../config.js';
1011
const router = new Router({
1112
prefix: '/order'
1213
})
1314

1415
// 生成订单
15-
router.post('/create', async (ctx) => {
16+
router.post('/create', checkUser, async (ctx) => {
1617
const userid = ctx.session.user_id
1718
const { list } = ctx.request.body
1819

@@ -68,7 +69,7 @@ router.post('/create', async (ctx) => {
6869
})
6970

7071
// 订单详情
71-
router.get('/info', async (ctx) => {
72+
router.get('/info', checkUser, async (ctx) => {
7273
const userid = ctx.session.user_id
7374
const { code } = ctx.query
7475
try {
@@ -99,7 +100,7 @@ router.get('/info', async (ctx) => {
99100
})
100101

101102
// 订单支付
102-
router.post('/pay', async (ctx) => {
103+
router.post('/pay', checkUser, async (ctx) => {
103104
const userid = ctx.session.user_id
104105
const { code, way = 0 } = ctx.request.body
105106
if (!code) {
@@ -210,7 +211,7 @@ router.post('/pay', async (ctx) => {
210211
userLessonData.push({
211212
id: getGuid(),
212213
userid: userid,
213-
lessonid: item.id,
214+
lessonid: item.lessonid,
214215
title: item.title,
215216
img: item.img,
216217
type: {
@@ -254,7 +255,7 @@ router.post('/pay', async (ctx) => {
254255
})
255256

256257
// 订单列表
257-
router.get('/list', async (ctx) => {
258+
router.get('/list', checkUser, async (ctx) => {
258259
const userid = ctx.session.user_id
259260
const { page = 1, status = '', size = SIZE } = ctx.query
260261
// 主动处理过期数据
@@ -326,7 +327,7 @@ router.get('/list', async (ctx) => {
326327
})
327328

328329
// 取消订单
329-
router.get('/cancel', async (ctx) => {
330+
router.get('/cancel', checkUser, async (ctx) => {
330331
const userid = ctx.session.user_id
331332
const { id } = ctx.query
332333
if (!id) {
@@ -366,7 +367,7 @@ router.get('/cancel', async (ctx) => {
366367
})
367368

368369
// 删除订单
369-
router.get('/delete', async (ctx) => {
370+
router.get('/delete', checkUser, async (ctx) => {
370371
const userid = ctx.session.user_id
371372
const { id } = ctx.query
372373
if (!id) {

server/interface/recharge.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import Router from 'koa-router'
22
import Recharge from '../models/recharge.js'
3+
import checkUser from '../middleware/auth.js'
34
import { ERR_OK, SIZE } from '../config.js'
45
import { getGuid } from '../../src/utils/utils.js'
56
const router = new Router({
67
prefix: '/recharge'
78
})
89

910
// 用户充值记录路由
10-
router.get('/list', async (ctx) => {
11+
router.get('/list', checkUser, async (ctx) => {
1112
const userid = ctx.session.user_id
1213
const { page = 1, size = SIZE } = ctx.query
1314
const where = { userid }
@@ -43,7 +44,7 @@ router.get('/list', async (ctx) => {
4344
})
4445

4546
// 新增用户充值记录路由
46-
router.post('/create', async (ctx) => {
47+
router.post('/create', checkUser, async (ctx) => {
4748
const userid = ctx.session.user_id
4849
const { money, way } = ctx.request.body
4950
// 判断金额
@@ -99,7 +100,7 @@ router.post('/create', async (ctx) => {
99100
})
100101

101102
// 用户余额路由
102-
router.get('/charge', async (ctx) => {
103+
router.get('/charge', checkUser, async (ctx) => {
103104
const userid = ctx.session.user_id || ctx.query.userid
104105
try {
105106
const result = await Recharge.find({

0 commit comments

Comments
 (0)