-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
142 lines (132 loc) · 4.3 KB
/
server.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
const jsonServer = require('json-server');
const server = jsonServer.create();
const teacherJson = require('./teacher.json');
const studenJson = require('./student.json');
const permissionData = require('./permission');
const bjData = require('./bjsfa');
const captcha = require('svg-captcha');
let materialArr = require('./materialArr');
const router2 = jsonServer.router({...materialArr, ...studenJson});
const router3 = jsonServer.router(permissionData);
const router4 = jsonServer.router(bjData);
const router = jsonServer.router({...materialArr, ...teacherJson});
const userArr = require('./user');
const routerUser = jsonServer.router({user:userArr});
const multer = require('multer');
const path = require('path');
const middlewares = jsonServer.defaults();
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, path.join(__dirname, 'public/server/upload')); // 文件存储的路径
},
filename: function(req, file, cb) {
cb(null, Date.now() + '_' + file.originalname); // 调整文件的保存地址
}
});
const upload = multer({ storage: storage });
server.use(middlewares);
server.use(jsonServer.bodyParser);
// 所有的api的请求都要求登陆后才能获取到对应的数据
server.use('/api/teacher', (req, res, next) => {
if (req.get('Authorization')) {
next();
} else {
res.status(401).jsonp({
code: 8,
msg: '用户没有登录,不能访问'
});
}
});// 所有的api的请求都要求登陆后才能获取到对应的数据
server.use('/api/student', (req, res, next) => {
if (req.get('Authorization')) {
next();
} else {
res.status(401).jsonp({
code: 8,
msg: '用户没有登录,不能访问'
});
}
});
server.use('/api/user', (req, res, next) => {
if (req.get('Authorization')) {
next();
} else {
res.status(401).jsonp({
code: 8,
msg: '用户没有登录,不能访问'
});
}
});
// 用户登录成功
server.post('/api/userlogin', (req, res) => {
let comnum = req.body.comnum;
let usernum = req.body.usernum;
let password = req.body.password;
let loginUser = userArr.find((item) => item.comnum == comnum && item.usernum == usernum && item.password == password);
if(loginUser) {
res.jsonp({
user: loginUser,
code: 1,
msg: '授权成功',
token: 'jfksdjfldsjflk===>aicoder.com'
});
}
else {
res.jsonp({
code: 0,
msg: '用户名或者密码错误'
});
}
});
server.use('/api/teacher', router);
server.use('/api/student', router2);
// 权限数据
server.get('/api/code', (req,res)=>{
const cap = captcha.create();
// req.session.captcha = cap.text; // session 存储
res.type('svg'); // 响应的类型
res.send(cap.data);
});
// 文件上传
server.all('/api/upload', upload.single('imgF'), function(req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
var file = req.file;
console.log(file);
console.log('文件类型:%s', file.mimetype);
console.log('原始文件名:%s', file.originalname);
console.log('文件大小:%s', file.size);
console.log('文件保存路径:%s', file.path);
res.json({ img: `/server/upload/${file.filename}` });
});
server.use('/api/', routerUser);
// 权限相关接口
// 获取用户的所有权限
server.use('/per/getUserPer/:id', (req, res) => {
// 获得当前用户的特殊权限
let userPerArr = permissionData.user_permission.filter(item => item.userId == req.params.id);
let userPerIdArr = userPerArr.map(userPer => userPer.permissionId);
// 获取用户的角色
let userRoleArr = permissionData.user_role.filter(item => item.userId == req.params.id);
// 获得所有角色的权限
let rolePerIdArr = [];
permissionData.role_permission.forEach( rolePer => {
if(userRoleArr.find(userRole => userRole.roleId == rolePer.roleId)) {
rolePerIdArr.push(rolePer.permissionId);
}
});
let totalPerIdArr =[...new Set([...userPerIdArr, ...rolePerIdArr])];
let result = [];
// 把所有权限
permissionData.permission.forEach(per => {
if(totalPerIdArr.find(perId => per.id == perId && per.del == 0)) {
result.push(per);
}
});
res.json(result);
});
server.use('/per', router3);
server.use('/bj', router4);
server.listen(8848, () => {
console.log('JSON Server is running');
});