installment.js
21.4 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/**
* 分期付款
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/08/01
*/
'use strict';
const installmentModel = require('../models/installment');
const _ = require('lodash');
const helpers = global.yoho.helpers;
// 服务器报错页面
const _serverCrash = (res, params) => {
params.title = params.title || '有货分期';
res.render('installment/server-crash', params);
};
const _banksInit = () => {
const banks = ['农业银行', '中国银行', '工商银行', '建设银行', '光大银行', '兴业银行', '邮储银行', '民生银行', '中信银行', '广发银行'];
if (new Date() >= new Date('2016-10-18 00:00:00')) {
// 2016年10月12日至17日 平安银行业务暂停(平安银行期间不支持服务)
banks.push('平安银行');
}
return banks;
};
// 还款列表公共处理块
const _repaymentList = (req, res, opt, params) => {
params = _.assign({
uid: req.user.uid
}, params);
installmentModel.getQueryAmtList(params).then((result) => {
res.render('installment/repayment-list', _.assign({
module: 'home',
page: 'repayment-list',
isInstallmentPage: true,
data: result
}, opt));
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: opt.title
});
});
};
// 开通分期首页
const index = (req, res) => {
let uid = req.user.uid;
Promise.all([
installmentModel.getStauts(uid),
installmentModel.getSearchIntallment({
page: 1
})
]).then((result) => {
// openStatus:0 未申请 1审核中 2已开通 3 审核未通过
let openStatus = result[0];
let installmentOnly = {
title: {
title: '分期专享',
more_url: helpers.appUrlFormat(req.originalUrl, 'go.instalmentlist', {
title: '分期专享'
})
},
goods: result[1]
};
if (openStatus === '0') {
return Promise.all([installmentModel.getResources('openN'), installmentModel.getNotices()]).then(data => {
_.forEach(data[0], (item) => {
if (item.singleOne) {
item.data[0].url = 'javascript:void(0)'; //eslint-disable-line
}
});
return {
content: data[0],
notice: data[1],
notOpen: true,
installmentOnly: installmentOnly
};
});
} else if (openStatus === '2') {
return Promise.all([
installmentModel.getQueryCreditInfo(uid),
installmentModel.getQueryAmtInfo(uid),
installmentModel.getResources('openY'),
installmentModel.getNotices()
]).then((data) => {
let params = _.assign({
isOverdue: false,
installmentOnly: installmentOnly,
content: data[2],
notice: data[3]
}, data[0], data[1]);
if (installmentOnly.goods.length > 0) {
params.installmentOnly = _.assign({
showMore: true
}, installmentOnly);
}
// status: 1 正常 2 逾期 3 不可用 4 未开通
if (data[0].status === '2') {
params.replayStatus = '逾期';
params.isOverdue = true;
} else if (data[0].status === '3') {
params.replayStatus = '不可用';
}
return params;
});
} else if (openStatus === '1' || openStatus === '3' || openStatus === '4' || openStatus === '5') {
res.redirect('/home/installment/review?status=' + openStatus);
return false;
}
}).then((result) => {
if (!result) {
return;
}
res.cookie('installmentUid', uid.toString());
res.cookie('yhChannel', req.query.yh_channel);
res.cookie('udid', req.query.udid);
res.cookie('clientType', req.query.client_type);
res.cookie('appVersion', req.query.app_version);
res.render('installment/open-index', _.assign({
module: 'home',
page: 'installment',
isInstallmentPage: true,
title: '有货分期'
}, result));
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl
});
});
};
// ajax 请求分期专享商品
const getInstallmentGoods = (req, res) => {
let allowOrigin = _.get(req, 'headers.origin', null) ?
req.headers.origin : req.protocol + '://' + req.headers.host;
res.setHeader('Access-Control-Allow-Origin', allowOrigin);
res.setHeader('Access-Control-Allow-Credentials', 'true');
let params = req.query || {};
installmentModel.getSearchIntallment(params).then((result) => {
if (result) {
res.render('installment/goods-template', {
layout: false,
goods: result
});
} else {
res.json();
}
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl
});
});
};
// 开通结果显示
const review = (req, res) => {
let openStatus = req.query.status || false;
let uid = req.user.uid;
let data = {
module: 'home',
page: 'installment',
title: '有货分期'
};
let templete = 'installment/open-result';
let jumpUrl = helpers.appUrlFormat('/product/new', 'go.new');
let params = {};
if (openStatus === '2') {
Promise.all([
installmentModel.getSearchIntallment({
page: 1
}),
installmentModel.getQueryCreditInfo(uid),
installmentModel.getResources('advertisement')
]).then((result) => {
let advertisement = false;
if (result && result[2].length > 1) {
advertisement = result[2][0].data[0];
}
res.render(templete, _.assign({
success: {
price: result[1].currCreditLimit,
installmentOnly: {
title: {
title: '分期专享',
more_url: helpers.appUrlFormat(req.originalUrl, 'go.instalmentlist', {
title: '分期专享'
})
},
goods: result[0]
},
advertisement: advertisement,
url: helpers.appUrlFormat(req.originalUrl, 'go.instalmentlist', {
title: '分期专享'
})
}
}, data));
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl
});
});
} else {
if (openStatus === '1') {
params = {
review: {
url: jumpUrl
}
};
} else if (openStatus === '3') {
params = {
error: true,
failReason: req.query.failReason || '姓名、身份证、银行卡不匹配'
};
} else if (openStatus === '4') {
params = {
cancel: {
url: jumpUrl
}
};
} else if (openStatus === '5') {
params = {
error: true,
failReason: '您的授信未通过,在有货更多的消费,增加信用度,会大大提升审核通过率奥。'
};
}
res.render(templete, _.assign(params, data));
}
};
// 逾期未还款列表
const overdueList = (req, res) => {
_repaymentList(req, res, {
title: '逾期未还金额',
posId: 1
}, {
queryDays: -1
});
};
// 7日待还款列表
const sevenDayList = (req, res) => {
_repaymentList(req, res, {
title: '近7日待还款',
posId: 2
}, {
queryDays: 7
});
};
// 本月待还款列表
const monthRepayList = (req, res) => {
_repaymentList(req, res, {
title: '本月待还金额',
posId: 3
}, {
queryDays: 30
});
};
// 待还总金额列表
const totalRepayList = (req, res) => {
_repaymentList(req, res, {
title: '待还总金额',
posId: 4
}, {
queryDays: 0
});
};
// 还款记录页面渲染
const repayRecordPage = (req, res) => {
res.render('installment/repay-record', {
module: 'home',
page: 'repay-record',
isInstallmentPage: true,
title: '还款记录',
posId: 5
});
};
// ajax 请求还款记录
const getRepayRecord = (req, res) => {
let params = _.assign({
uid: req.user.uid,
pageNo: req.query.page || 1
});
installmentModel.getQueryRePayList(params).then((result) => {
if (result) {
res.render('installment/record-template', {
layout: false,
recordData: result
});
} else {
res.json();
}
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: '还款记录'
});
});
};
// 账号管理
const account = (req, res) => {
var url = req.yoho.isApp || req.cookies.appVersion ? '/home/installment/account' : '/home/installment/bank-card';
if (req.cookies.appVersion) {
let versions = req.cookies.appVersion.split('.');
if (versions.length >= 2) {
if (versions[0] < 5 || (parseInt(versions[0], 10) === 5 && versions[1] <= 1)) {
url = '/home/installment/bank-card';
} else {
url = helpers.appUrlFormat(url, 'go.instalmentMyCard');
}
}
}
res.render('installment/account', {
module: 'home',
page: 'account',
title: '账号管理',
isInstallmentPage: true,
posId: 7,
url: url
});
};
// 添加新银行卡
const bindCard = (req, res) => {
const banks = _banksInit();
res.render('installment/bind-card', {
module: 'home',
page: 'bind-card',
title: req.query.title || '使用新卡还款',
bindCard: {
userName: req.query.name,
banks: banks.join('、')
},
isInstallmentPage: true
});
};
// 添加新银行卡请求
const postAccount = (req, res) => {
var params = _.assign({
uid: req.user.uid
}, req.query);
installmentModel.postAccount(params).then((result) => {
res.json(result);
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: '使用新卡还款'
});
});
};
const startingService = (req, res) => {
const banks = _banksInit();
res.render('installment/starting-service', {
module: 'home',
page: 'installment.starting-service',
title: '开通有货分期',
navTitle: '开通有货分期',
isInstallmentPage: true,
navBtn: false,
banks: banks.join('、')
});
};
/**
* 获取真实IP
*
* @param req
* @returns {*|string}
*/
function getRealIP(req) {
var realIP = req.headers['x-real-ip'];
var forwardedFor = req.headers['x-forwarded-for'] || '';
return realIP || forwardedFor.split(',')[0] || req.connection.remoteAddress;
}
const activateService = (req, res) => {
installmentModel.activateService({
uid: req.user.uid,
userName: req.body.userName,
identityCardNo: req.body.identityCardNo,
cardNo: req.body.cardNo,
mobile: req.body.mobile,
snsCheckCode: req.body.snsCheckCode,
bankCode: req.body.bankCode,
bankName: req.body.bankName,
udid: req.cookies.udid || 0,
from_client_type: req.cookies.clientType,
ip: getRealIP(req)
}).then((result)=> {
res.json(result);
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: '开通有货分期'
});
});
};
const getBankInfo = (req, res) => {
installmentModel.getBankInfo({
cardNo: req.query.cardNo,
uid: req.user.uid
}).then((result)=> {
res.json(result);
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl
});
});
};
const verifyCode = (req, res) => {
installmentModel.sendVerifyCode(req.user.uid || 1, req.query.mobile).then((result)=> {
res.json(result);
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl
});
});
};
const orderIndex = (req, res) => {
res.render('installment/order', {
module: 'home',
page: 'installment.order',
title: '我的分期订单',
navBtn: false,
isInstallmentPage: true,
posId: 6
});
};
const orderList = (req, res) => {
const params = {
uid: req.user.uid,
type: req.query.type || 1,
page: req.query.page || 1,
limit: req.query.limit || 10
};
installmentModel.getInstallmentOrders(params).then((result)=> {
res.render('installment/order-list', {
title: '我的分期订单',
orders: ((()=> {
// 处理数据,订单只包含第一条物品纪录
var list = result.data ? result.data.order_list : [];
if (list) {
list.forEach((item)=> {
item.order_goods = [
item.order_goods[0]
];
});
}
return list;
})()),
layout: false,
isInstallmentPage: true
});
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: '我的分期订单'
});
});
};
const orderDetail = (req, res) => {
const params = {
uid: req.user.uid,
orderCode: req.params.id
};
installmentModel.getInstallmentOrderDetail(params).then((result)=> {
res.render('installment/order-detail', {
module: 'home',
page: 'installment.order-detail',
title: '分期详情',
order: (()=> {
let refundStatusCount = 0, completeStatusCount = 0, listCount = 0;
if (result && result.data && result.data.order_goods) {
result.data.order_goods = [
result.data.order_goods[0]
];
}
if (result && result.data && result.data.package_list) {
listCount = result.data.package_list.length;
result.data.package_list.forEach((item)=> {
if (item.status === 2) {
completeStatusCount++;
} else if (item.status === 4 || item.status === 5) {
refundStatusCount++;
}
});
}
return Object.assign({
status: (()=> {
if (refundStatusCount === listCount) {
return 5;
} else if (completeStatusCount === listCount) {
return 2;
}
})()
}, result.data);
})(),
navBtn: false,
currAmtCount: 0,
currFeeCount: 0,
isInstallmentPage: true,
isCurrFee: true
}, {
helpers: {
isPaymentComplete: function(status, options) {
if (status === 2 || status === 4 || status === 5) { // 已结清/已取消
return options.fn(this);
}
return options.inverse(this);
},
isPaymentIncomplete: function(status, options) {
if (status !== 2 && status !== 4 && status !== 5) { // 其他状态
return options.fn(this);
}
return options.inverse(this);
},
isRefundedAll: function(status, options) {
if (status === 5) { // 已退款
return options.fn(this);
}
return options.inverse(this);
},
isRepaymentAllCompleted: function(status, options) {
if (status === 2) { // 已结清
return options.fn(this);
}
return options.inverse(this);
},
greaterThanZero: function(value, options) {
if (value && parseFloat(value) > 0) {
return options.fn(this);
}
return options.inverse(this);
}
}
});
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: '分期详情'
});
});
};
// 还款详情
const repayDetail = (req, res) => {
let params = {
uid: req.user.uid,
rePayNo: req.query.id || '',
pageNo: 1
};
installmentModel.getQueryRePayList(params).then((result) => {
res.render('installment/repay-detail', _.assign({
title: '还款详情',
isInstallmentPage: true,
isOne: true
}, result[0]));
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: '还款详情'
});
});
};
// 帮助静态页面
const help = (req, res) => {
res.render('installment/help', {
title: '分期支付帮助中心'
});
};
// 协议静态页面
const agreement = (req, res) => {
res.render('installment/agreement', {
title: '用户服务协议'
});
};
// 计算金额
const totalAmount = (req, res) => {
installmentModel.totalAmount(req.query.prices).then((result) => {
res.json(result);
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl
});
});
};
// 检查验证码
const checkVerifyCode = (req, res) => {
installmentModel.checkVerifyCode(req.user.uid, req.query.mobile, req.query.code).then((result) => {
res.json(result);
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl
});
});
};
// 服务器崩溃
const serverCrash = (req, res) => {
res.render('installment/server-crash');
};
// 银行卡列表
const bankCard = (req, res) => {
let uid = req.user.uid;
installmentModel.getBankCards(uid).then((result) => {
res.render('installment/bank-card', {
module: 'home',
page: 'bank-card',
title: '我的银行卡',
isInstallmentPage: true,
accountList: result,
userName: result[0].userName
});
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: '我的银行卡'
});
});
};
// 银行卡详情
const cardDetail = (req, res) => {
let uid = req.user.uid;
let cardIdNo = req.query.cardIdNo;
installmentModel.getCardDetail(uid, cardIdNo).then((result) => {
res.render('installment/card-detail', {
module: 'home',
page: 'card-detail',
title: '银行卡详情',
isInstallmentPage: true,
width750: true,
cardDetail: result
});
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: '银行卡详情'
});
});
};
// 删除绑定
const delBankCard = (req, res) => {
let params = {
uid: req.user.uid,
cardIdNo: req.query.cardIdNo
};
installmentModel.delBankCard(params).then((result) => {
res.json(result);
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: '银行卡详情'
});
});
};
// 切换银行卡绑定
const setMasterCard = (req, res) => {
let params = {
uid: req.user.uid,
cardIdNo: req.query.cardIdNo
};
installmentModel.setMasterCard(params).then((result) => {
res.json(result);
}).catch(() => {
_serverCrash(res, {
url: req.originalUrl,
title: '银行卡详情'
});
});
};
const sessionKeyAuth = (req, res, next) => {
let uid = req.user.uid || req.query.uid || req.cookies.installmentUid;
let sessionKey = _.get(req, 'user.uid.sessionKey');
if (sessionKey && !req.session.SESSION_KEY) {
req.session.SESSION_KEY = uid.sessionKey;
}
req.user.uid = {
toString: () => {
return uid.toString();
},
sessionKey
};
next();
};
module.exports = {
index,
review,
overdueList,
sevenDayList,
monthRepayList,
totalRepayList,
repayRecordPage,
getRepayRecord,
startingService,
activateService,
verifyCode,
getBankInfo,
account,
orderIndex,
orderList,
orderDetail,
repayDetail,
help,
agreement,
totalAmount,
checkVerifyCode,
getInstallmentGoods,
bindCard,
serverCrash,
bankCard,
postAccount,
cardDetail,
delBankCard,
setMasterCard,
sessionKeyAuth
};