Skip to content

Commit 1cef9df

Browse files
committed
首页导航接口/用户课程接口开发
1 parent e8e690a commit 1cef9df

File tree

13 files changed

+295
-55
lines changed

13 files changed

+295
-55
lines changed

server/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import articleRouter from './interface/article.js'
1919
import lessonRouter from './interface/lesson.js'
2020
import cartRouter from './interface/cart.js'
2121
import orderRouter from './interface/order.js'
22+
import userLessonRouter from './interface/userLesson.js'
2223
import mongoose from 'mongoose'
2324
import dbConfig from './config.js'
2425

@@ -76,6 +77,7 @@ app.use(articleRouter.routes(), articleRouter.allowedMethods())
7677
app.use(lessonRouter.routes(), lessonRouter.allowedMethods())
7778
app.use(cartRouter.routes(), cartRouter.allowedMethods())
7879
app.use(orderRouter.routes(), orderRouter.allowedMethods())
80+
app.use(userLessonRouter.routes(), userLessonRouter.allowedMethods())
7981

8082
// 启动服务
8183
app.listen(port, () => {

server/initData/navigation.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const navData = [
2+
{
3+
title: '前沿 / 区块链 / 人工智能',
4+
code: '4'
5+
},
6+
{
7+
title: '前端 / 小程序 / JS',
8+
code: '0'
9+
},
10+
{
11+
title: '后端 / JAVA / Python',
12+
code: '1'
13+
},
14+
{
15+
title: '移动 / Android / iOS',
16+
code: '2'
17+
},
18+
{
19+
title: '云计算 / 大数据 / 容器',
20+
code: '3,5'
21+
},
22+
{
23+
title: '运维 / 测试 / 数据库',
24+
code: '6,7'
25+
},
26+
{
27+
title: 'UI设计 / 3D动画 / 游戏',
28+
code: '8'
29+
}
30+
]
31+
32+
export default navData

server/interface/home.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import Router from 'koa-router'
22
import Slider from '../models/slider.js'
33
import Teacher from '../models/teacher.js'
44
import Student from '../models/student.js'
5+
import Navigation from '../models/navigation.js'
6+
import Label from '../models/label.js'
57
import { ERR_OK } from '../config.js'
68
const router = new Router({
79
prefix: '/home'
@@ -25,6 +27,62 @@ router.get('/slider', async (ctx) => {
2527
}
2628
})
2729

30+
// 首页导航接口
31+
router.get('/nav', async (ctx) => {
32+
try {
33+
const labelList = await Label.find()
34+
const navList = await Navigation.find().sort({
35+
sort: 1
36+
}).lean()
37+
if (labelList.length === 0 || navList.length === 0) {
38+
ctx.body = {
39+
code: -1,
40+
msg: '获取首页导航数据失败',
41+
data: []
42+
}
43+
return false
44+
}
45+
let LabelNormalizeList = []
46+
for (let index = 0; index < labelList.length; index++) {
47+
const label = labelList[index]
48+
const findIndex = LabelNormalizeList.findIndex(item => item.code === label.type.code)
49+
if (findIndex === -1) {
50+
LabelNormalizeList.push({
51+
title: label.type.text,
52+
code: label.type.code,
53+
list: [label.title]
54+
})
55+
} else {
56+
LabelNormalizeList[findIndex].list.push(label.title)
57+
}
58+
}
59+
for (let index = 0; index < navList.length; index++) {
60+
const nav = navList[index]
61+
const splitCodeArr = nav.code.split(',')
62+
splitCodeArr.forEach(code => {
63+
const findIndex = LabelNormalizeList.findIndex(item => +item.code === +code)
64+
if (findIndex !== -1) {
65+
if (!navList[index].hasOwnProperty('tags')) {
66+
navList[index]['tags'] = [LabelNormalizeList[findIndex]]
67+
} else {
68+
navList[index]['tags'].push(LabelNormalizeList[findIndex])
69+
}
70+
}
71+
})
72+
}
73+
ctx.body = {
74+
code: ERR_OK,
75+
msg: '获取首页导航数据成功',
76+
data: navList
77+
}
78+
} catch (e) {
79+
ctx.body = {
80+
code: -1,
81+
msg: e.message || '服务器异常'
82+
}
83+
}
84+
})
85+
2886
// 首页精英讲师接口
2987
router.get('/teacher', async (ctx) => {
3088
const result = await Teacher.find()

server/interface/order.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Order from '../models/order.js'
33
import Cart from '../models/cart.js'
44
import Bill from '../models/bill.js'
55
import Recharge from '../models/recharge.js'
6+
import UserLesson from '../models/userLesson.js'
67
import axios from 'axios'
78
import { getGuid, getOrderId } from '../../src/utils/utils.js'
89
import { ERR_OK, payWay, SIZE } from '../config.js';
@@ -109,6 +110,7 @@ router.post('/pay', async (ctx) => {
109110
}
110111
try {
111112
let insertBillData = []
113+
let userLessonData = []
112114
const searchResult = await Order.findOne({
113115
userid: userid,
114116
code: code
@@ -205,6 +207,17 @@ router.post('/pay', async (ctx) => {
205207
code: way
206208
}
207209
})
210+
userLessonData.push({
211+
id: getGuid(),
212+
userid: userid,
213+
lessonid: item.id,
214+
title: item.title,
215+
img: item.img,
216+
type: {
217+
text: '实战课程',
218+
code: 1
219+
}
220+
})
208221
})
209222
const orderResult = await Order.findOneAndUpdate({
210223
userid: userid,
@@ -219,8 +232,9 @@ router.post('/pay', async (ctx) => {
219232
code: way
220233
}
221234
})
222-
const result = await Bill.insertMany(insertBillData)
223-
if (orderResult && result) {
235+
const billResult = await Bill.insertMany(insertBillData)
236+
const userLessonResult = await UserLesson.insertMany(userLessonData)
237+
if (orderResult && billResult && userLessonResult) {
224238
ctx.body = {
225239
code: ERR_OK,
226240
msg: '订单支付成功'

server/interface/userLesson.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import Router from 'koa-router'
2+
import UserLesson from '../models/userLesson.js'
3+
import { SIZE, ERR_OK } from '../config.js'
4+
const router = new Router({
5+
prefix: '/user/course'
6+
})
7+
8+
// 课程列表
9+
router.get('/list', async (ctx) => {
10+
const userid = ctx.session.user_id
11+
const { page = 1, size = SIZE, type } = ctx.query
12+
try {
13+
let where = { userid }
14+
if (type) {
15+
where['type.code'] = type
16+
}
17+
const total = await UserLesson.find(where).countDocuments()
18+
const result = await UserLesson.find(where).skip((page - 1) * size).limit(+size)
19+
if (result) {
20+
ctx.body = {
21+
code: ERR_OK,
22+
msg: '获取用户课程数据成功',
23+
data: {
24+
list: result,
25+
total: total
26+
}
27+
}
28+
} else {
29+
ctx.body = {
30+
code: -1,
31+
msg: '获取用户课程数据失败',
32+
data: {
33+
list: [],
34+
total: 0
35+
}
36+
}
37+
}
38+
} catch (e) {
39+
ctx.body = {
40+
code: -1,
41+
msg: e.message || '服务器异常'
42+
}
43+
}
44+
})
45+
46+
export default router

server/models/navigation.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import mongoose from 'mongoose'
2+
import navData from '../initData/navigation.js'
3+
import { getGuid } from '../../src/utils/utils.js'
4+
const Schema = mongoose.Schema
5+
const NavigationSchema = new Schema({
6+
id: {
7+
type: String,
8+
required: true,
9+
unique: true,
10+
index: true
11+
},
12+
title: {
13+
type: String,
14+
required: true
15+
},
16+
code: {
17+
type: String,
18+
required: true
19+
},
20+
sort: Number
21+
})
22+
23+
const navigationModel = mongoose.model('navigation', NavigationSchema)
24+
// 判断是否有数据,无数据则初始化
25+
navigationModel.find((err, data) => {
26+
if (!data || data.length === 0) {
27+
navData.forEach((item, index) => {
28+
item.id = getGuid()
29+
item.sort = index
30+
navigationModel.create(item)
31+
})
32+
}
33+
})
34+
export default navigationModel

server/models/userLesson.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ const UserLessonSchema = new Schema({
1515
type: String,
1616
required: true
1717
},
18+
title: {
19+
type: String,
20+
required: true
21+
},
22+
img: {
23+
type: String,
24+
required: true
25+
},
1826
percent: {
1927
type: Number,
2028
default: 0
@@ -42,6 +50,14 @@ const UserLessonSchema = new Schema({
4250
questions: {
4351
type: Number,
4452
default: 0
53+
},
54+
type: {
55+
text: String,
56+
code: Number
57+
},
58+
lastChapter: {
59+
type: String,
60+
default: ''
4561
}
4662
})
4763

src/api/home.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import axios from 'utils/axios.js'
22

33
// 获取主页导航信息
44
export function getHomeNav () {
5-
return axios.get('/mock/home/nav.json')
5+
return axios.get('/mock/home/nav')
66
}
77

88
// 获取主页滚动轮播数据

src/api/user.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ export function updateUserInfo (data) {
3333
}
3434

3535
// 获取用户课程信息接口
36-
export function getUserCourse () {
37-
return axios.get('/mock/user/course.json')
36+
export function getUserCourse (params) {
37+
return axios.get('/mock/user/course/list', {
38+
params
39+
})
3840
}

src/pages/home/nav-content.vue

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
<template>
22
<div class="nav-content">
33
<div class="tags">
4-
<div v-for="(tags,index) in content.tags" :key="index" class="tags-item">
4+
<div
5+
v-for="(tags,index) in tags"
6+
:key="index"
7+
class="tags-item"
8+
>
59
<h2 class="title-box">
6-
<span class="title">{{ tags.subtitle }}</span>
10+
<span class="title">{{ tags.title }}</span>
711
<span class="line" />
812
</h2>
913
<ul class="list">
10-
<li v-for="(tag, index) in tags.list" :key="index" class="tag">
14+
<li
15+
v-for="(tag, index) in tags.list"
16+
:key="index"
17+
class="tag"
18+
>
1119
{{ tag }}
1220
</li>
1321
</ul>
1422
</div>
1523
</div>
1624

17-
<div class="nav-course">
25+
<!-- <div class="nav-course">
1826
<div v-for="(course,index) in content.course" :key="index" class="course-item">
1927
<div class="img-box">
2028
<img :src="course.img" width="64" height="42" alt="">
@@ -33,14 +41,14 @@
3341
</p>
3442
</div>
3543
</div>
36-
</div>
44+
</div> -->
3745
</div>
3846
</template>
3947
<script>
4048
export default {
4149
props: {
42-
content: {
43-
type: Object,
50+
tags: {
51+
type: Array,
4452
required: true
4553
}
4654
}

0 commit comments

Comments
 (0)