user.js
10.1 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
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
/**
* 用户管理controller
* @author: leo <qi.li@yoho.cn>
* @date: 23/06/2017
*/
const _ = require('lodash');
const moment = require('moment');
const UserModel = require('../models/user');
const camelcase = require('camelcase');
const {excel_export} = require('../../../utils/excel');
const DO_SUCCESS = '操作成功';
const GET_SUCCESS = '获取成功';
const timeFmt = (time) => {
if (_.isNumber(time)) {
time = moment.unix(time);
}
return moment(time).format('YYYY-MM-DD HH:mm:ss');
};
const userController = {
/**
* 用户列表页
* @param req
* @param res
*/
userListPage(req, res) {
res.render('user/list', {
bodyClass: 'nav-md',
module: 'admin',
page: 'user'
});
},
/**
* 用户信息收集列表
* @param req
* @param res
*/
usersListPage(req, res) {
res.render('user/users-list', {
bodyClass: 'nav-md',
module: 'admin',
page: 'users-list'
});
},
prizeUsersListPage(req, res) {
res.render('user/prize-users-list', {
bodyClass: 'nav-md',
module: 'admin',
page: 'prize-users-list'
});
},
/**
* 用户列表
* @param req
* @param res
* @param next
*/
userList(req, res, next) {
const pageNo = req.query.pageNo || 1;
const pageSize = req.query.pageSize || 20;
req.ctx(UserModel).userList({
pageNo,
pageSize
})
.then(list => {
_.each(list, item => {
item.createTime = timeFmt(item.createTime);
});
return list;
})
.then(list => {
req.ctx(UserModel).allUsersNum()
.then(totalCount => {
res.json({
code: 200,
data: list,
pageNo: +pageNo,
pageSize: +pageSize,
totalCount,
totalPage: Math.ceil(totalCount / pageSize),
message: GET_SUCCESS
});
});
})
.catch(next);
},
/**
* 用户信息收集列表
* @param req
* @param res
* @param next
*/
usersList(req, res, next) {
const pageNo = req.query.pageNo || 1;
const pageSize = req.query.pageSize || 20;
req.ctx(UserModel).usersList(pageNo, pageSize).then(list => {
let result = [];
_.each(list, item => {
let data = {};
item.user_birthday = timeFmt(item.user_birthday);
_.each(item, (val, key) => {
data[camelcase(key)] = val;
});
result.push(data);
});
return result;
}).then(list => {
req.ctx(UserModel).allInfoUsersNum().then(totalCount => {
res.json({
code: 200,
data: list,
pageNo: +pageNo,
pageSize: +pageSize,
totalCount,
totalPage: Math.ceil(totalCount / pageSize),
message: GET_SUCCESS
});
});
}).catch(next);
},
/**
* 中奖用户列表
* @param req
* @param res
* @param next
*/
prizeUsersList(req, res, next) {
const pageNo = req.query.pageNo || 1;
const pageSize = req.query.pageSize || 20;
const actId = req.query.actId || 25;
req.ctx(UserModel).prizeUsersList(pageNo, pageSize, actId).then(list => {
let result = [];
_.each(list, item => {
let data = {};
_.each(item, (val, key) => {
data[camelcase(key)] = val;
});
result.push(data);
});
return result;
}).then(list => {
req.ctx(UserModel).allPrizeUsersNum(actId).then(totalCount => {
res.json({
code: 200,
data: list,
pageNo: +pageNo,
pageSize: +pageSize,
totalCount,
totalPage: Math.ceil(totalCount / pageSize),
message: GET_SUCCESS
});
});
}).catch(next);
},
/**
* 用户删除
* @param req
* @param res
* @param next
*/
deleteUser(req, res, next) {
const userId = req.body.userId;
req.ctx(UserModel).deleteUser(userId)
.then(() => {
res.json({
code: 200,
message: DO_SUCCESS
});
})
.catch(next);
},
/**
* 用户信息收集用户删除
* @param req
* @param res
* @param next
*/
deleteInfoUsers(req, res, next) {
const userId = req.body.userId;
req.ctx(UserModel).deleteInfoUser(userId).then(() => {
res.json({
code: 200,
message: DO_SUCCESS
});
}).catch(next);
},
/**
* 抽奖用户删除
* @param req
* @param res
* @param next
*/
deletePrizeUsers(req, res, next) {
const userId = req.body.userId;
req.ctx(UserModel).deletePrizeUser(userId).then(() => {
res.json({
code: 200,
message: DO_SUCCESS
});
}).catch(next);
},
/**
* 用户列表导出
* @param req
* @param res
* @param next
*/
exportUserList(req, res, next) {
let conf = {
name: 'mysheet',
cols: ['用户ID', '昵称', '手机号', '创建时间'],
rows: []
};
req.ctx(UserModel).exportUserList()
.then(result => {
let temp = [];
_.each(result, item => {
temp = [];
temp.push(item.id);
temp.push(item.userName);
temp.push(item.userPhone);
temp.push(timeFmt(item.createTime));
conf.rows.push(temp);
});
return excel_export({
title: conf.cols,
data: conf.rows,
fileName: 'userList',
sheetName: conf.name,
res
});
})
.catch(next);
},
exportInfoUserList(req, res, next) {
let conf = {
name: 'myInfoSheet',
cols: [
'用户ID',
'昵称',
'生日',
'性别',
'手机号',
'email',
'省份',
'城市'],
rows: []
};
req.ctx(UserModel).exportInfoUserList().then(result => {
let temp = [];
_.each(result, item => {
temp = [];
temp.push(item.id);
temp.push(item.user_name);
temp.push(moment(item.user_birthday).format('YYYY-MM-DD'));
temp.push(['男', '女'][item.user_gender]);
temp.push(item.user_phone);
temp.push(item.user_email);
temp.push(item.user_province);
temp.push(item.user_city);
conf.rows.push(temp);
});
return excel_export({
title: conf.cols,
data: conf.rows,
fileName: 'userInfoList',
sheetName: conf.name,
res
});
}).catch(next);
},
exportPrizeUserList(req, res, next) {
let conf = {
name: 'myInfoSheet',
cols: [
'用户ID',
'昵称',
'手机号',
'地址',
'鞋码'],
rows: []
};
req.ctx(UserModel).exportPrizeUserList().then(result => {
let temp = [];
_.each(result, item => {
temp = [];
temp.push(item.id);
temp.push(item.user_name);
temp.push(item.user_phone);
temp.push(item.user_address);
temp.push(item.user_shoes_size);
conf.rows.push(temp);
});
return excel_export({
title: conf.cols,
data: conf.rows,
fileName: 'prizeUserList',
sheetName: conf.name,
res
});
}).catch(next);
},
userLoginLog(req, res) {
res.render('user/login-log', {
bodyClass: 'nav-md',
module: 'admin',
page: 'user-login-log'
});
},
exportUserLoginLog(req, res, next) {
const url = req.query.url;
const startTime = req.query.startTime;
const endTime = req.query.endTime;
let conf = {
name: 'mysheet',
cols: ['用户id', '登录时间', '平台', '地址', 'ip'],
rows: []
};
req.ctx(UserModel).exportUserLoginLog({url, startTime, endTime})
.then(result => {
let temp = [];
_.each(result, item => {
temp = [];
temp.push(item.user_id);
temp.push(moment(item.login_time).format('YYYY-MM-DD HH:mm:ss'));
temp.push(item.platform);
temp.push(item.referer);
temp.push(item.ip);
conf.rows.push(temp);
});
return excel_export({
title: conf.cols,
data: conf.rows,
fileName: 'user-login-log',
sheetName: conf.name,
res
});
})
.catch(next);
}
};
module.exports = userController;