Blame view

apps/home/controllers/installment.js 21.8 KB
王水玲 authored
1 2 3 4 5
/**
 * 分期付款
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/08/01
 */
王水玲 authored
6 7 8 9 10
'use strict';

const installmentModel = require('../models/installment');
const _ = require('lodash');
const helpers = global.yoho.helpers;
郭成尧 authored
11
const logger = global.yoho.logger;
王水玲 authored
12
徐炜 authored
13
// 服务器报错页面
陈峰 authored
14 15 16 17
const _serverCrash = (res, params, err, next) => {
    if (err && err.code === 401) {
        return next(err);
    }
郭成尧 authored
18
    logger.error(err);
王水玲 authored
19 20
    params.title = params.title || '有货分期';
    res.render('installment/server-crash', params);
徐炜 authored
21 22
};
王水玲 authored
23
const _banksInit = () => {
李靖 authored
24
    const banks = ['农业银行', '中国银行', '工商银行', '建设银行', '光大银行', '兴业银行', '邮储银行', '中信银行', '广发银行'];
王水玲 authored
25 26 27 28 29 30 31 32 33

    if (new Date() >= new Date('2016-10-18 00:00:00')) {
        // 2016年10月12日至17日 平安银行业务暂停(平安银行期间不支持服务)
        banks.push('平安银行');
    }

    return banks;
};
王水玲 authored
34
// 还款列表公共处理块
陈峰 authored
35
const _repaymentList = (req, res, next, opt, params) => {
王水玲 authored
36
    params = _.assign({
37
        uid: req.user.uid
王水玲 authored
38 39
    }, params);
郝肖肖 authored
40
    req.ctx(installmentModel).getQueryAmtList(params).then((result) => {
王水玲 authored
41
        res.render('installment/repayment-list', _.assign({
王水玲 authored
42 43
            module: 'home',
            page: 'repayment-list',
王水玲 authored
44
            isInstallmentPage: true,
王水玲 authored
45
            data: result
王水玲 authored
46
        }, opt));
陈峰 authored
47
    }).catch((err) => {
王水玲 authored
48 49
        _serverCrash(res, {
            url: req.originalUrl,
王水玲 authored
50
            title: opt.title
陈峰 authored
51
        }, err, next);
徐炜 authored
52
    });
王水玲 authored
53 54 55
};

// 开通分期首页
陈峰 authored
56
const index = (req, res, next) => {
57
    let uid = req.user.uid;
58
郭成尧 authored
59
    logger.info(`installment index info: client_type=${req.query.client_type},req.user.uid=${req.user.uid},req.query.uid=${req.query.uid},cookies=${JSON.stringify(req.cookies)}`); // eslint-disable-line
王水玲 authored
60
    Promise.all([
王水玲 authored
61
        req.ctx(installmentModel).getStauts(uid)
王水玲 authored
62 63
    ]).then((result) => {
        // openStatus:0 未申请 1审核中 2已开通 3 审核未通过
王水玲 authored
64 65
        let openStatus = result[0];
王水玲 authored
66
        if (openStatus === '0') {
郝肖肖 authored
67 68 69 70
            return Promise.all([
                req.ctx(installmentModel).getResources('openN'),
                req.ctx(installmentModel).getNotices()
            ]).then(data => {
王水玲 authored
71
王水玲 authored
72
                return {
王水玲 authored
73 74
                    content: data[0],
                    notice: data[1],
王水玲 authored
75
                    notOpen: true,
王水玲 authored
76
                    goHomePath: req.yoho.isApp ? 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.home","params":{"channel":"1"}}' : '//m.yohobuy.com/boys'
王水玲 authored
77 78
                };
            });
王水玲 authored
79
        } else if (openStatus === '2') {
王水玲 authored
80
            return Promise.all([
郝肖肖 authored
81 82 83 84
                req.ctx(installmentModel).getQueryCreditInfo(uid),
                req.ctx(installmentModel).getQueryAmtInfo(uid),
                req.ctx(installmentModel).getResources('openY'),
                req.ctx(installmentModel).getNotices()
王水玲 authored
85
            ]).then((data) => {
王水玲 authored
86 87
                let params = _.assign({
                    isOverdue: false,
王水玲 authored
88 89
                    content: data[2],
                    notice: data[3]
王水玲 authored
90
                }, data[0], data[1]);
王水玲 authored
91
王水玲 authored
92
                // status: 1 正常 2 逾期 3 不可用 4 未开通
王水玲 authored
93
                if (data[0].status === '2') {
王水玲 authored
94 95
                    params.replayStatus = '逾期';
                    params.isOverdue = true;
王水玲 authored
96
                } else if (data[0].status === '3') {
王水玲 authored
97 98 99 100 101
                    params.replayStatus = '不可用';
                }

                return params;
            });
王水玲 authored
102
        } else if (openStatus === '1' || openStatus === '3' || openStatus === '4' || openStatus === '5') {
王水玲 authored
103
            res.redirect('/home/installment/review?status=' + openStatus);
王水玲 authored
104 105

            return false;
王水玲 authored
106
        }
王水玲 authored
107
    }).then((result) => {
王水玲 authored
108 109 110
        if (!result) {
            return;
        }
111
        res.cookie('installmentUid', uid.toString());
陈峰 authored
112 113 114 115
        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);
郭成尧 authored
116
王水玲 authored
117 118 119
        res.render('installment/open-index', _.assign({
            module: 'home',
            page: 'installment',
王水玲 authored
120
            isInstallmentPage: true,
王水玲 authored
121
            title: '有货分期'
王水玲 authored
122
        }, result));
陈峰 authored
123
    }).catch((err) => {
王水玲 authored
124 125
        _serverCrash(res, {
            url: req.originalUrl
陈峰 authored
126
        }, err, next);
徐炜 authored
127
    });
王水玲 authored
128 129
};
130
// ajax 请求分期专享商品
陈峰 authored
131
const getInstallmentGoods = (req, res, next) => {
郭成尧 authored
132 133 134 135 136 137
    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');
lijing authored
138 139 140
    let params = {
        uid: req.user.uid
    };
141
郝肖肖 authored
142
    req.ctx(installmentModel).getSearchIntallment(params).then((result) => {
143 144 145 146 147 148 149 150
        if (result) {
            res.render('installment/goods-template', {
                layout: false,
                goods: result
            });
        } else {
            res.json();
        }
陈峰 authored
151
    }).catch((err) => {
王水玲 authored
152 153
        _serverCrash(res, {
            url: req.originalUrl
陈峰 authored
154
        }, err, next);
徐炜 authored
155
    });
156 157
};
王水玲 authored
158
// 开通结果显示
陈峰 authored
159
const review = (req, res, next) => {
王水玲 authored
160
    let openStatus = req.query.status || false;
161
    let uid = req.user.uid;
王水玲 authored
162 163 164
    let data = {
        module: 'home',
        page: 'installment',
王水玲 authored
165
        title: '有货分期'
王水玲 authored
166
    };
王水玲 authored
167 168 169
    let templete = 'installment/open-result';
    let jumpUrl = helpers.appUrlFormat('/product/new', 'go.new');
    let params = {};
王水玲 authored
170
王水玲 authored
171 172
    if (openStatus === '2') {
        Promise.all([
郝肖肖 authored
173
            req.ctx(installmentModel).getSearchIntallment({
王水玲 authored
174 175
                page: 1
            }),
郝肖肖 authored
176 177
            req.ctx(installmentModel).getQueryCreditInfo(uid),
            req.ctx(installmentModel).getResources('advertisement')
王水玲 authored
178
        ]).then((result) => {
179 180 181 182 183 184
            let advertisement = false;

            if (result && result[2].length > 1) {
                advertisement = result[2][0].data[0];
            }
王水玲 authored
185 186 187 188 189 190 191 192 193 194 195 196
            res.render(templete, _.assign({
                success: {
                    price: result[1].currCreditLimit,
                    installmentOnly: {
                        title: {
                            title: '分期专享',
                            more_url: helpers.appUrlFormat(req.originalUrl, 'go.instalmentlist', {
                                title: '分期专享'
                            })
                        },
                        goods: result[0]
                    },
197
                    advertisement: advertisement,
王水玲 authored
198 199 200 201 202
                    url: helpers.appUrlFormat(req.originalUrl, 'go.instalmentlist', {
                        title: '分期专享'
                    })
                }
            }, data));
陈峰 authored
203
        }).catch((err) => {
王水玲 authored
204 205
            _serverCrash(res, {
                url: req.originalUrl
陈峰 authored
206
            }, err, next);
王水玲 authored
207
        });
王水玲 authored
208 209 210 211 212 213 214 215 216 217

    } else {
        if (openStatus === '1') {
            params = {
                review: {
                    url: jumpUrl
                }
            };
        } else if (openStatus === '3') {
            params = {
王水玲 authored
218 219
                error: true,
                failReason: req.query.failReason || '姓名、身份证、银行卡不匹配'
王水玲 authored
220 221 222 223 224 225 226
            };
        } else if (openStatus === '4') {
            params = {
                cancel: {
                    url: jumpUrl
                }
            };
王水玲 authored
227
        } else if (openStatus === '5') {
王水玲 authored
228 229
            params = {
                error: true,
王水玲 authored
230
                failReason: '您的授信未通过,在有货更多的消费,增加信用度,会大大提升审核通过率奥。'
王水玲 authored
231
            };
王水玲 authored
232 233
        }
        res.render(templete, _.assign(params, data));
王水玲 authored
234
    }
王水玲 authored
235 236
};
王水玲 authored
237
// 逾期未还款列表
陈峰 authored
238 239
const overdueList = (req, res, next) => {
    _repaymentList(req, res, next, {
王水玲 authored
240 241 242
        title: '逾期未还金额',
        posId: 1
    }, {
王水玲 authored
243
        queryDays: -1
王水玲 authored
244 245 246
    });
};
247
// 7日待还款列表
陈峰 authored
248 249
const sevenDayList = (req, res, next) => {
    _repaymentList(req, res, next, {
王水玲 authored
250 251 252
        title: '近7日待还款',
        posId: 2
    }, {
王水玲 authored
253 254 255 256 257
        queryDays: 7
    });
};

// 本月待还款列表
陈峰 authored
258 259
const monthRepayList = (req, res, next) => {
    _repaymentList(req, res, next, {
王水玲 authored
260 261 262
        title: '本月待还金额',
        posId: 3
    }, {
王水玲 authored
263 264 265 266 267
        queryDays: 30
    });
};

// 待还总金额列表
陈峰 authored
268 269
const totalRepayList = (req, res, next) => {
    _repaymentList(req, res, next, {
王水玲 authored
270 271 272
        title: '待还总金额',
        posId: 4
    }, {
王水玲 authored
273 274 275 276 277 278 279 280 281
        queryDays: 0
    });
};

// 还款记录页面渲染
const repayRecordPage = (req, res) => {
    res.render('installment/repay-record', {
        module: 'home',
        page: 'repay-record',
王水玲 authored
282
        isInstallmentPage: true,
王水玲 authored
283 284
        title: '还款记录',
        posId: 5
王水玲 authored
285 286 287 288
    });
};

// ajax 请求还款记录
陈峰 authored
289
const getRepayRecord = (req, res, next) => {
王水玲 authored
290
    let params = _.assign({
291
        uid: req.user.uid,
王水玲 authored
292 293 294
        pageNo: req.query.page || 1
    });
郝肖肖 authored
295
    req.ctx(installmentModel).getQueryRePayList(params).then((result) => {
王水玲 authored
296 297 298 299 300 301
        if (result) {
            res.render('installment/record-template', {
                layout: false,
                recordData: result
            });
        } else {
毕凯 authored
302
            res.send('');
王水玲 authored
303
        }
陈峰 authored
304
    }).catch((err) => {
王水玲 authored
305 306 307
        _serverCrash(res, {
            url: req.originalUrl,
            title: '还款记录'
陈峰 authored
308
        }, err, next);
徐炜 authored
309
    });
王水玲 authored
310 311 312
};

// 账号管理
徐炜 authored
313
const account = (req, res) => {
王水玲 authored
314
    var url = req.yoho.isApp || req.cookies.appVersion ? '/home/installment/account' : '/home/installment/bank-card';
315
陈峰 authored
316
    if (req.cookies.appVersion) {
317
        let versions = req.cookies.appVersion.split('.');
318
陈峰 authored
319
        if (versions.length >= 2) {
320
            if (versions[0] < 5 || (parseInt(versions[0], 10) === 5 && versions[1] <= 1)) {
321
                url = '/home/installment/bank-card';
陈峰 authored
322
            } else {
323
                url = helpers.appUrlFormat(url, 'go.instalmentMyCard');
陈峰 authored
324 325 326
            }
        }
    }
徐炜 authored
327
    res.render('installment/account', {
王水玲 authored
328 329
        module: 'home',
        page: 'account',
徐炜 authored
330
        title: '账号管理',
王水玲 authored
331
        isInstallmentPage: true,
王水玲 authored
332
        posId: 7,
陈峰 authored
333
        url: url
徐炜 authored
334 335
    });
};
王水玲 authored
336
徐炜 authored
337 338
// 添加新银行卡
const bindCard = (req, res) => {
王水玲 authored
339 340
    const banks = _banksInit();
徐炜 authored
341 342 343
    res.render('installment/bind-card', {
        module: 'home',
        page: 'bind-card',
王水玲 authored
344
        title: req.query.title || '使用新卡还款',
徐炜 authored
345
        bindCard: {
王水玲 authored
346
            userName: req.query.name,
王水玲 authored
347
            banks: banks.join('、')
徐炜 authored
348 349 350 351 352 353
        },
        isInstallmentPage: true
    });
};

// 添加新银行卡请求
陈峰 authored
354
const postAccount = (req, res, next) => {
徐炜 authored
355
    var params = _.assign({
356
        uid: req.user.uid
徐炜 authored
357 358
    }, req.query);
郝肖肖 authored
359
    req.ctx(installmentModel).postAccount(params).then((result) => {
徐炜 authored
360
        res.json(result);
陈峰 authored
361
    }).catch((err) => {
王水玲 authored
362 363 364
        _serverCrash(res, {
            url: req.originalUrl,
            title: '使用新卡还款'
陈峰 authored
365
        }, err, next);
徐炜 authored
366
    });
王水玲 authored
367 368
};
Aiden Xu authored
369
const startingService = (req, res) => {
王水玲 authored
370
    const banks = _banksInit();
371
Aiden Xu authored
372
    res.render('installment/starting-service', {
Aiden Xu authored
373 374
        module: 'home',
        page: 'installment.starting-service',
Aiden Xu authored
375
        title: '开通有货分期',
Aiden Xu authored
376
        navTitle: '开通有货分期',
王水玲 authored
377
        isInstallmentPage: true,
378 379
        navBtn: false,
        banks: banks.join('、')
Aiden Xu authored
380
    });
Aiden Xu authored
381 382
};
383 384 385 386 387 388 389 390 391 392
/**
 * 获取真实IP
 *
 * @param req
 * @returns {*|string}
 */
function getRealIP(req) {
    var realIP = req.headers['x-real-ip'];
    var forwardedFor = req.headers['x-forwarded-for'] || '';
毕凯 authored
393
    return req.yoho.clientIp || forwardedFor.split(',')[0] || realIP || req.connection.remoteAddress;
394 395
}
陈峰 authored
396
const activateService = (req, res, next) => {
397
郝肖肖 authored
398
    req.ctx(installmentModel).activateService({
399
        uid: req.user.uid,
Aiden Xu authored
400 401
        userName: req.body.userName,
        identityCardNo: req.body.identityCardNo,
王水玲 authored
402
        cardNo: req.body.cardNo,
Aiden Xu authored
403
        mobile: req.body.mobile,
Aiden Xu authored
404
        snsCheckCode: req.body.snsCheckCode,
Aiden Xu authored
405
        bankCode: req.body.bankCode,
Aiden Xu authored
406 407
        bankName: req.body.bankName,
        udid: req.cookies.udid || 0,
408 409
        from_client_type: req.cookies.clientType,
        ip: getRealIP(req)
Aiden Xu authored
410 411
    }).then((result)=> {
        res.json(result);
陈峰 authored
412
    }).catch((err) => {
王水玲 authored
413 414 415
        _serverCrash(res, {
            url: req.originalUrl,
            title: '开通有货分期'
陈峰 authored
416
        }, err, next);
徐炜 authored
417
    });
Aiden Xu authored
418 419
};
陈峰 authored
420
const getBankInfo = (req, res, next) => {
郝肖肖 authored
421
    req.ctx(installmentModel).getBankInfo({
Aiden Xu authored
422
        cardNo: req.query.cardNo,
423
        uid: req.user.uid
Aiden Xu authored
424 425
    }).then((result)=> {
        res.json(result);
陈峰 authored
426
    }).catch((err) => {
王水玲 authored
427 428
        _serverCrash(res, {
            url: req.originalUrl
陈峰 authored
429
        }, err, next);
徐炜 authored
430
    });
Aiden Xu authored
431 432
};
陈峰 authored
433
const verifyCode = (req, res, next) => {
郝肖肖 authored
434
    req.ctx(installmentModel).sendVerifyCode(req.user.uid || 1, req.query.mobile).then((result)=> {
Aiden Xu authored
435
        res.json(result);
陈峰 authored
436
    }).catch((err) => {
王水玲 authored
437 438
        _serverCrash(res, {
            url: req.originalUrl
陈峰 authored
439
        }, err, next);
徐炜 authored
440
    });
Aiden Xu authored
441 442
};
Aiden Xu authored
443
const orderIndex = (req, res) => {
Aiden Xu authored
444 445 446
    res.render('installment/order', {
        module: 'home',
        page: 'installment.order',
Aiden Xu authored
447
        title: '我的分期订单',
Aiden Xu authored
448
        navBtn: false,
王水玲 authored
449 450
        isInstallmentPage: true,
        posId: 6
Aiden Xu authored
451 452 453
    });
};
陈峰 authored
454
const orderList = (req, res, next) => {
Aiden Xu authored
455
    const params = {
456
        uid: req.user.uid,
Aiden Xu authored
457 458 459 460 461
        type: req.query.type || 1,
        page: req.query.page || 1,
        limit: req.query.limit || 10
    };
郝肖肖 authored
462
    req.ctx(installmentModel).getInstallmentOrders(params).then((result)=> {
Aiden Xu authored
463
        res.render('installment/order-list', {
Aiden Xu authored
464
            title: '我的分期订单',
Aiden Xu authored
465 466
            orders: ((()=> {
                // 处理数据,订单只包含第一条物品纪录
467
                var list = result.data ? result.data.order_list : [];
Aiden Xu authored
468
Aiden Xu authored
469 470
                if (list) {
                    list.forEach((item)=> {
471 472
                        item.order_goods = [
                            item.order_goods[0]
Aiden Xu authored
473 474 475
                        ];
                    });
                }
Aiden Xu authored
476 477 478

                return list;
            })()),
Aiden Xu authored
479 480
            layout: false,
            isInstallmentPage: true
Aiden Xu authored
481
        });
陈峰 authored
482
    }).catch((err) => {
王水玲 authored
483 484 485
        _serverCrash(res, {
            url: req.originalUrl,
            title: '我的分期订单'
陈峰 authored
486
        }, err, next);
徐炜 authored
487
    });
Aiden Xu authored
488 489
};
陈峰 authored
490
const orderDetail = (req, res, next) => {
Aiden Xu authored
491
    const params = {
492
        uid: req.user.uid,
Aiden Xu authored
493
        orderCode: req.params.id
Aiden Xu authored
494
    };
Aiden Xu authored
495
郝肖肖 authored
496
    req.ctx(installmentModel).getInstallmentOrderDetail(params).then((result)=> {
Aiden Xu authored
497 498 499
        res.render('installment/order-detail', {
            module: 'home',
            page: 'installment.order-detail',
Aiden Xu authored
500
            title: '分期详情',
Aiden Xu authored
501
            order: (()=> {
Aiden Xu authored
502 503
                let refundStatusCount = 0, completeStatusCount = 0, listCount = 0;
504 505 506
                if (result && result.data && result.data.order_goods) {
                    result.data.order_goods = [
                        result.data.order_goods[0]
Aiden Xu authored
507 508 509
                    ];
                }
510 511
                if (result && result.data && result.data.package_list) {
                    listCount = result.data.package_list.length;
Aiden Xu authored
512
513
                    result.data.package_list.forEach((item)=> {
Aiden Xu authored
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
                        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);
Aiden Xu authored
531
            })(),
Aiden Xu authored
532
            navBtn: false,
Aiden Xu authored
533 534
            currAmtCount: 0,
            currFeeCount: 0,
王水玲 authored
535 536 537
            isInstallmentPage: true,
            isCurrFee: true
        }, {
Aiden Xu authored
538 539
            helpers: {
                isPaymentComplete: function(status, options) {
Aiden Xu authored
540
                    if (status === 2 || status === 4 || status === 5) { // 已结清/已取消
Aiden Xu authored
541 542 543 544 545 546
                        return options.fn(this);
                    }

                    return options.inverse(this);
                },
                isPaymentIncomplete: function(status, options) {
Aiden Xu authored
547
                    if (status !== 2 && status !== 4 && status !== 5) { // 其他状态
Aiden Xu authored
548 549 550 551
                        return options.fn(this);
                    }

                    return options.inverse(this);
Aiden Xu authored
552 553 554 555 556 557 558
                },
                isRefundedAll: function(status, options) {
                    if (status === 5) { // 已退款
                        return options.fn(this);
                    }

                    return options.inverse(this);
Aiden Xu authored
559
                },
徐炜 authored
560
                isRepaymentAllCompleted: function(status, options) {
Aiden Xu authored
561
                    if (status === 2) { // 已结清
徐炜 authored
562 563 564 565
                        return options.fn(this);
                    }
                    return options.inverse(this);
                },
Aiden Xu authored
566 567
                greaterThanZero: function(value, options) {
                    if (value && parseFloat(value) > 0) {
Aiden Xu authored
568 569 570 571
                        return options.fn(this);
                    }

                    return options.inverse(this);
Aiden Xu authored
572 573
                }
            }
Aiden Xu authored
574
        });
陈峰 authored
575
    }).catch((err) => {
王水玲 authored
576 577 578
        _serverCrash(res, {
            url: req.originalUrl,
            title: '分期详情'
陈峰 authored
579
        }, err, next);
徐炜 authored
580
    });
Aiden Xu authored
581 582
};
王水玲 authored
583
// 还款详情
陈峰 authored
584
const repayDetail = (req, res, next) => {
王水玲 authored
585
    let params = {
586
        uid: req.user.uid,
王水玲 authored
587 588 589 590
        rePayNo: req.query.id || '',
        pageNo: 1
    };
郝肖肖 authored
591
    req.ctx(installmentModel).getQueryRePayList(params).then((result) => {
王水玲 authored
592 593 594 595 596
        res.render('installment/repay-detail', _.assign({
            title: '还款详情',
            isInstallmentPage: true,
            isOne: true
        }, result[0]));
陈峰 authored
597
    }).catch((err) => {
王水玲 authored
598 599 600
        _serverCrash(res, {
            url: req.originalUrl,
            title: '还款详情'
陈峰 authored
601
        }, err, next);
徐炜 authored
602
    });
王水玲 authored
603 604
};
郝肖肖 authored
605
// 帮助静态页面
606
const help = (req, res, next) => {
zhangxiaoru authored
607
    let id = 263;
608
郝肖肖 authored
609
    req.ctx(installmentModel).getHelpDetail(id).then((result) => {
610 611 612 613 614 615
        res.render('installment/help', {
            title: '分期支付帮助中心',
            helpdetail_list: result.helpdetail_list
        });
    }).catch(next);
郝肖肖 authored
616 617 618 619 620
};

// 协议静态页面
const agreement = (req, res) => {
    res.render('installment/agreement', {
郝肖肖 authored
621
        title: '用户服务协议'
郝肖肖 authored
622 623 624
    });
};
Aiden Xu authored
625
// 计算金额
陈峰 authored
626
const totalAmount = (req, res, next) => {
郝肖肖 authored
627
    req.ctx(installmentModel).totalAmount(req.query.prices).then((result) => {
Aiden Xu authored
628
        res.json(result);
陈峰 authored
629
    }).catch((err) => {
王水玲 authored
630 631
        _serverCrash(res, {
            url: req.originalUrl
陈峰 authored
632
        }, err, next);
徐炜 authored
633
    });
Aiden Xu authored
634 635
};
636
// 检查验证码
陈峰 authored
637
const checkVerifyCode = (req, res, next) => {
郝肖肖 authored
638
    req.ctx(installmentModel).checkVerifyCode(req.user.uid, req.query.mobile, req.query.code).then((result) => {
639
        res.json(result);
陈峰 authored
640
    }).catch((err) => {
王水玲 authored
641 642
        _serverCrash(res, {
            url: req.originalUrl
陈峰 authored
643
        }, err, next);
徐炜 authored
644 645 646 647 648 649 650 651 652
    });
};

// 服务器崩溃
const serverCrash = (req, res) => {
    res.render('installment/server-crash');
};

// 银行卡列表
陈峰 authored
653
const bankCard = (req, res, next) => {
654
    let uid = req.user.uid;
徐炜 authored
655
郝肖肖 authored
656
    req.ctx(installmentModel).getBankCards(uid).then((result) => {
李靖 authored
657 658 659 660 661 662 663 664
        let noBank = true;

        _.forEach(result, (val) => {
            if (val.validStatus === false) {
                noBank = false;
            }
        });
徐炜 authored
665
        res.render('installment/bank-card', {
王水玲 authored
666 667
            module: 'home',
            page: 'bank-card',
徐炜 authored
668 669
            title: '我的银行卡',
            isInstallmentPage: true,
王水玲 authored
670
            accountList: result,
李靖 authored
671
            noBank: noBank,
陈峰 authored
672
            userName: _.get(result, '[0].userName')
徐炜 authored
673
        });
陈峰 authored
674
    }).catch((err) => {
王水玲 authored
675 676 677
        _serverCrash(res, {
            url: req.originalUrl,
            title: '我的银行卡'
陈峰 authored
678
        }, err, next);
徐炜 authored
679
    });
680
};
Aiden Xu authored
681
王水玲 authored
682
// 银行卡详情
陈峰 authored
683
const cardDetail = (req, res, next) => {
684
    let uid = req.user.uid;
王水玲 authored
685 686
    let cardIdNo = req.query.cardIdNo;
郝肖肖 authored
687
    req.ctx(installmentModel).getCardDetail(uid, cardIdNo).then((result) => {
王水玲 authored
688 689 690 691 692 693 694 695
        res.render('installment/card-detail', {
            module: 'home',
            page: 'card-detail',
            title: '银行卡详情',
            isInstallmentPage: true,
            width750: true,
            cardDetail: result
        });
陈峰 authored
696
    }).catch((err) => {
王水玲 authored
697 698 699
        _serverCrash(res, {
            url: req.originalUrl,
            title: '银行卡详情'
陈峰 authored
700
        }, err, next);
王水玲 authored
701 702 703 704
    });
};

// 删除绑定
陈峰 authored
705
const delBankCard = (req, res, next) => {
王水玲 authored
706
    let params = {
707
        uid: req.user.uid,
王水玲 authored
708 709 710
        cardIdNo: req.query.cardIdNo
    };
郝肖肖 authored
711
    req.ctx(installmentModel).delBankCard(params).then((result) => {
王水玲 authored
712
        res.json(result);
陈峰 authored
713
    }).catch((err) => {
王水玲 authored
714 715 716
        _serverCrash(res, {
            url: req.originalUrl,
            title: '银行卡详情'
陈峰 authored
717
        }, err, next);
王水玲 authored
718 719 720 721
    });
};

// 切换银行卡绑定
陈峰 authored
722
const setMasterCard = (req, res, next) => {
王水玲 authored
723
    let params = {
724
        uid: req.user.uid,
王水玲 authored
725 726 727
        cardIdNo: req.query.cardIdNo
    };
郝肖肖 authored
728
    req.ctx(installmentModel).setMasterCard(params).then((result) => {
王水玲 authored
729
        res.json(result);
陈峰 authored
730
    }).catch((err) => {
王水玲 authored
731 732 733
        _serverCrash(res, {
            url: req.originalUrl,
            title: '银行卡详情'
陈峰 authored
734
        }, err, next);
王水玲 authored
735 736 737
    });
};
738
const sessionKeyAuth = (req, res, next) => {
陈峰 authored
739
    if (!req.user.uid && req.query.uid) {
740
        req.user.uid = req.query.uid || 0;
陈峰 authored
741
    }
742 743 744
    next();
};
王水玲 authored
745
module.exports = {
王水玲 authored
746
    index,
Aiden Xu authored
747
    review,
王水玲 authored
748
    overdueList,
王水玲 authored
749 750 751 752 753
    sevenDayList,
    monthRepayList,
    totalRepayList,
    repayRecordPage,
    getRepayRecord,
Aiden Xu authored
754
    startingService,
Aiden Xu authored
755 756
    activateService,
    verifyCode,
Aiden Xu authored
757
    getBankInfo,
Aiden Xu authored
758
    account,
Aiden Xu authored
759 760
    orderIndex,
    orderList,
王水玲 authored
761
    orderDetail,
郝肖肖 authored
762 763
    repayDetail,
    help,
Aiden Xu authored
764
    agreement,
765
    totalAmount,
766
    checkVerifyCode,
徐炜 authored
767 768 769 770
    getInstallmentGoods,
    bindCard,
    serverCrash,
    bankCard,
王水玲 authored
771
    postAccount,
王水玲 authored
772 773
    cardDetail,
    delBankCard,
774 775
    setMasterCard,
    sessionKeyAuth
王水玲 authored
776
};