forked from bailicangdu/node-elm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
273 lines (269 loc) · 6.41 KB
/
Copy pathadmin.js
File metadata and controls
273 lines (269 loc) · 6.41 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
'use strict';
import AdminModel from '../../models/admin/admin'
import AddressComponent from '../../prototype/addressComponent'
import crypto from 'crypto'
import formidable from 'formidable'
import dtime from 'time-formater'
class Admin extends AddressComponent {
constructor(){
super()
this.login = this.login.bind(this)
this.register = this.register.bind(this)
this.encryption = this.encryption.bind(this)
this.updateAvatar = this.updateAvatar.bind(this)
}
async login(req, res, next){
const form = new formidable.IncomingForm();
form.parse(req, async (err, fields, files) => {
if (err) {
res.send({
status: 0,
type: 'FORM_DATA_ERROR',
message: '表单信息错误'
})
return
}
const {user_name, password, status = 1} = fields;
try{
if (!user_name) {
throw new Error('用户名参数错误')
}else if(!password){
throw new Error('密码参数错误')
}
}catch(err){
console.log(err.message, err);
res.send({
status: 0,
type: 'GET_ERROR_PARAM',
message: err.message,
})
return
}
const newpassword = this.encryption(password);
try{
const admin = await AdminModel.findOne({user_name})
if (!admin) {
const adminTip = status == 1 ? '管理员' : '超级管理员'
const admin_id = await this.getId('admin_id');
const cityInfo = await this.guessPosition(req);
const newAdmin = {
user_name,
password: newpassword,
id: admin_id,
create_time: dtime().format('YYYY-MM-DD HH:mm'),
admin: adminTip,
status,
city: cityInfo.city
}
await AdminModel.create(newAdmin)
req.session.admin_id = admin_id;
res.send({
status: 1,
success: '注册管理员成功',
})
}else if(newpassword.toString() != admin.password.toString()){
console.log('管理员登录密码错误');
res.send({
status: 0,
type: 'ERROR_PASSWORD',
message: '该用户已存在,密码输入错误',
})
}else{
req.session.admin_id = admin.id;
res.send({
status: 1,
success: '登录成功'
})
}
}catch(err){
console.log('登录管理员失败', err);
res.send({
status: 0,
type: 'LOGIN_ADMIN_FAILED',
message: '登录管理员失败',
})
}
})
}
async register(req, res, next){
const form = new formidable.IncomingForm();
form.parse(req, async (err, fields, files) => {
if (err) {
res.send({
status: 0,
type: 'FORM_DATA_ERROR',
message: '表单信息错误'
})
return
}
const {user_name, password, status = 1} = fields;
try{
if (!user_name) {
throw new Error('用户名错误')
}else if(!password){
throw new Error('密码错误')
}
}catch(err){
console.log(err.message, err);
res.send({
status: 0,
type: 'GET_ERROR_PARAM',
message: err.message,
})
return
}
try{
const admin = await AdminModel.findOne({user_name})
if (admin) {
console.log('该用户已经存在');
res.send({
status: 0,
type: 'USER_HAS_EXIST',
message: '该用户已经存在',
})
}else{
const adminTip = status == 1 ? '管理员' : '超级管理员'
const admin_id = await this.getId('admin_id');
const newpassword = this.encryption(password);
const newAdmin = {
user_name,
password: newpassword,
id: admin_id,
create_time: dtime().format('YYYY-MM-DD'),
admin: adminTip,
status,
}
await AdminModel.create(newAdmin)
req.session.admin_id = admin_id;
res.send({
status: 1,
message: '注册管理员成功',
})
}
}catch(err){
console.log('注册管理员失败', err);
res.send({
status: 0,
type: 'REGISTER_ADMIN_FAILED',
message: '注册管理员失败',
})
}
})
}
encryption(password){
const newpassword = this.Md5(this.Md5(password).substr(2, 7) + this.Md5(password));
return newpassword
}
Md5(password){
const md5 = crypto.createHash('md5');
return md5.update(password).digest('base64');
}
async singout(req, res, next){
try{
delete req.session.admin_id;
res.send({
status: 1,
success: '退出成功'
})
}catch(err){
console.log('退出失败', err)
res.send({
status: 0,
message: '退出失败'
})
}
}
async getAllAdmin(req, res, next){
const {limit = 20, offset = 0} = req.query;
try{
const allAdmin = await AdminModel.find({}, '-_id -password').sort({id: -1}).skip(Number(offset)).limit(Number(limit))
res.send({
status: 1,
data: allAdmin,
})
}catch(err){
console.log('获取超级管理列表失败', err);
res.send({
status: 0,
type: 'ERROR_GET_ADMIN_LIST',
message: '获取超级管理列表失败'
})
}
}
async getAdminCount(req, res, next){
try{
const count = await AdminModel.count()
res.send({
status: 1,
count,
})
}catch(err){
console.log('获取管理员数量失败', err);
res.send({
status: 0,
type: 'ERROR_GET_ADMIN_COUNT',
message: '获取管理员数量失败'
})
}
}
async getAdminInfo(req, res, next){
const admin_id = req.session.admin_id;
if (!admin_id || !Number(admin_id)) {
console.log('获取管理员信息的session失效');
res.send({
status: 0,
type: 'ERROR_SESSION',
message: '获取管理员信息失败'
})
return
}
try{
const info = await AdminModel.findOne({id: admin_id}, '-_id -__v -password');
if (!info) {
throw new Error('未找到当前管理员')
}else{
res.send({
status: 1,
data: info
})
}
}catch(err){
console.log('获取管理员信息失败');
res.send({
status: 0,
type: 'GET_ADMIN_INFO_FAILED',
message: '获取管理员信息失败'
})
}
}
async updateAvatar(req, res, next){
const admin_id = req.params.admin_id;
if (!admin_id || !Number(admin_id)) {
console.log('admin_id参数错误', admin_id)
res.send({
status: 0,
type: 'ERROR_ADMINID',
message: 'admin_id参数错误',
})
return
}
try{
const image_path = await this.getPath(req);
await AdminModel.findOneAndUpdate({id: admin_id}, {$set: {avatar: image_path}});
res.send({
status: 1,
image_path,
})
return
}catch(err){
console.log('上传图片失败', err);
res.send({
status: 0,
type: 'ERROR_UPLOAD_IMG',
message: '上传图片失败'
})
return
}
}
}
export default new Admin()