installmentActions.js 18.8 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
'use strict';

import ReactNative from 'react-native';
import InstallmentService from '../../services/InstallmentService';

const {

SET_PLATFORM,

INSTALLMENT_PRODUCT_REQUEST,
INSTALLMENT_PRODUCT_SUCCESS,
INSTALLMENT_PRODUCT_FAILURE,

GET_INSTALLMENT_STATUS_REQUEST,
GET_INSTALLMENT_STATUS_SUCCESS,
GET_INSTALLMENT_STATUS_FAILURE,

UPDATE_SHOW_BANK_ALERT_STATUS,
UPDATE_AGREE_PROTOCOL_STATUS,
UPDATE_SNS_CHECK_CODE_ENABLE_STATUS,
UPDATE_CELL_LIST,
UPDATE_NEXT_BTN_ENABLE_STATUS,
UPDATE_SNS_CHECK_CODE_TEXT,
UPDATE_TIP_MESSAGE,
NEXT_BTN_PROCESSING_REQUEST,
NEXT_BTN_PROCESSING_SUCCESS,
NEXT_BTN_PROCESSING_FAILURE,

SET_INSTALLMENT_STATUS_PAGE_ORIGINAL_INFO,
SET_INSTALLMENT_STATUS_PAGE_GOODS,

} = require('../../constants/actionTypes').default;
const codeContent = {
    openN: '0876085ff46bed27f1a1eb6ee8b68987',
    openY: 'c233ad4ca91fef45ed7d0b26d9953e61',
    advertisement: 'f3fe0793a7d59479542a14b15359c47d'
};

var Timer = function() {
    this.counter = 0;
    this.countdownTimer = null;
};

export function productListForInstallmentRequest(){
     return {
        type: INSTALLMENT_PRODUCT_REQUEST,
    }
}

export function productListForInstallmentSuccess(json){
     return {
        type: INSTALLMENT_PRODUCT_SUCCESS,
        payload:json,
    }
}

export function productListForInstallmentFailure(){
     return {
        type: INSTALLMENT_PRODUCT_FAILURE,
    }
}

export function featchInstallmentStatusRequest() {
    return {
        type: GET_INSTALLMENT_STATUS_REQUEST,
    }
}

export function featchInstallmentStatusSuccess(status) {
    return {
        type: GET_INSTALLMENT_STATUS_SUCCESS,
        payload: status
    }
}

export function featchInstallmentStatusFailure(error) {
    return {
        type: GET_INSTALLMENT_STATUS_FAILURE,
        payload: error
    }
}

export function updateShowBankAlertStatus(status) {
    return {
        type: UPDATE_SHOW_BANK_ALERT_STATUS,
        payload: status
    }
}

export function updateAgreeProtocolStatus(status) {
    return {
        type: UPDATE_AGREE_PROTOCOL_STATUS,
        payload: status
    }
}

export function updateSnsCheckCodeEnableStatus(status) {
    return {
        type: UPDATE_SNS_CHECK_CODE_ENABLE_STATUS,
        payload: status
    }
}

export function updateCellList(cellList) {
    return {
        type: UPDATE_CELL_LIST,
        payload: cellList
    }
}

export function updateNextBtnEnableStatus(status) {
    return {
        type: UPDATE_NEXT_BTN_ENABLE_STATUS,
        payload: status
    }
}

export function updateSnsCheckCodeText(text) {
    return {
        type: UPDATE_SNS_CHECK_CODE_TEXT,
        payload: text
    }
}

export function updateTipMessage(message) {
    return {
        type: UPDATE_TIP_MESSAGE,
        payload: message
    }
}

export function nextBtnProcessingRequest() {
    return {
        type: NEXT_BTN_PROCESSING_REQUEST,
    }
}
export function nextBtnProcessingFailure() {
    return {
        type: NEXT_BTN_PROCESSING_FAILURE
    }
}
export function nextBtnProcessingSuccess() {
    return {
        type: NEXT_BTN_PROCESSING_SUCCESS
    }
}

export function setInstallmentStatusPageOriginalInfo(params) {
    return {
        type: SET_INSTALLMENT_STATUS_PAGE_ORIGINAL_INFO,
        payload: params
    }
}

export function setInstallmentStatusPageGoods(goods) {
    return {
        type: SET_INSTALLMENT_STATUS_PAGE_GOODS,
        payload: goods
    }
}

export function productListForInstallment() {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {isFetching,open,installmentStatus} = installment;
        if(isFetching){
            return
        }
        dispatch(productListForInstallmentRequest());
        return new InstallmentService(app.host).fetchInstallmentProductList()
        .then(json => {
            let payload = json;//product_list
            if (payload.page > 1) {
                let oldList = open.fetchInstallmentProductList.list.toJS();
                let list = [...oldList, ...payload.list];
                payload.product_list = list;
            }
            dispatch(productListForInstallmentSuccess(payload));
        })
        .catch(error => {
            dispatch(productListForInstallmentFailure());
        });
    }
}

export function onPressOpenInstallment() {
    return (dispatch, getState) => {
        ReactNative.NativeModules.YH_InstallmentHelper.goToOpenInstallment();
    }
}

export function onPressMoreProducts() {
    return (dispatch, getState) => {
        //跳转原生筛选
        let getToInstallFilter = (uid) => {
            let url = `://m.yohobuy.com/home/installment/index?uid=${uid}?openby:yohobuy={"action":"go.instalmentlist","params":{"title":"分期专享"}}`;
            ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
        }
        ReactNative.NativeModules.YH_CommonHelper.uid()
		.then(uid => {
			getToInstallFilter(uid);
		})
		.catch(error => {
			ReactNative.NativeModules.YH_CommonHelper.login()
			.then(uid => {
				getToInstallFilter(uid);
			})
			.catch(error => {

			});
		});
    }
}

export function getInstallmentStatus() {
    return (dispatch, getState) => {

        let installmentStatus = (uid) => {
            let {app, installment} = getState();
            dispatch(featchInstallmentStatusRequest());
            return new InstallmentService(app.host).getStauts(uid)
            .then(json => {
                let status = 0;
                if (json.status) {
                    status = json.status;
                }
                dispatch(productListForInstallment());
            })
            .catch(error => {
                dispatch(featchInstallmentStatusFailure(error));
            });
        }

        ReactNative.NativeModules.YH_CommonHelper.uid()
		.then(uid => {
			installmentStatus(uid);
		})
		.catch(error => {
			ReactNative.NativeModules.YH_CommonHelper.login()
			.then(uid => {
				installmentStatus(uid);
			})
			.catch(error => {

			});
		});
    }
}

export function onPressProtocol() {
    return (dispatch, getState) => {
        //跳转原生静态页面
        let url = `://m.yohobuy.com/home/installment/agreement?openby:yohobuy={"action":"go.h5","params":{"url":"http://m.yohobuy.com/home/installment/agreement"}}`;
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(url);
    };
}

export function onPressOpenNext() {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {nextBtnEnable,cellList,nextProcessing} = installment.openPageInfo;
        let activateParams = {};
        let newCellList = cellList;
        // if (!nextBtnEnable) {
        //     return;
        // }
        ReactNative.NativeModules.YH_InstallmentHelper.gotoInstallmentStatusView('2', '您的授信未通过,在有货更多的消费,增加信用度,会大大提升审核通过率奥。');
        return;
        cellList.map((item, i) => {
            if (item.get('inputKey') != 'BankIcon') {
                activateParams[item.get('inputKey')]= item.get('text');
            }
        });
        let activateService = (uid) => {
            dispatch(nextBtnProcessingRequest());
            console.log('----0000');
            return new InstallmentService(app.host).checkVerifyCode(uid, activateParams.mobile, activateParams.snsCheckCode)
            .then(json => {
                if (json.result === '1') {

                    return new InstallmentService(app.host).activateService(activateParams)
                }else {
                    newCellList.map((item, i) => {
                        if (item.inputKey == 'snsCheckCode') {
                            item.text = '';
                        }
                        cellList[i] = item;
                    });
                    dispatch(updateTipMessage(json.message))
                    dispatch(updateCellList(cellList));
                    dispatch(nextBtnProcessingFailure());
                }
            })
            .then(json => {
                dispatch(nextBtnProcessingSuccess());
                console.log('2222222');
                console.log(json);
                let tipMessage = '';
                if (json.status == '3') {
                    json.failReason = json.failReason || '姓名、身份证、银行卡不匹配';
                }
                ReactNative.NativeModules.YH_InstallmentHelper.gotoInstallmentStatusView(json.status, json.failReason);
            })
            .catch(error => {
            });
        }

        ReactNative.NativeModules.YH_CommonHelper.uid()
		.then(uid => {
			activateService(uid);
		})
		.catch(error => {
			ReactNative.NativeModules.YH_CommonHelper.login()
			.then(uid => {
				activateService(uid);
			})
			.catch(error => {

			});
		});

    };
}

export function onPressCheckbox() {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {agreeProtocol} = installment.openPageInfo;
        agreeProtocol = !agreeProtocol;
        dispatch(updateAgreeProtocolStatus(agreeProtocol));
    };
}

export function onPressCardNoQuestion() {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {showBankAlert} = installment.openPageInfo;
        showBankAlert = !showBankAlert;
        dispatch(updateShowBankAlertStatus(showBankAlert));
    };
}

export function getBankInfo(cellInfo,cardNo) {
    return (dispatch, getState) => {

        let bankInfo = (uid) => {
            let {app, installment} = getState();
            let {cellList,snsCheckCodeEnable} = installment.openPageInfo;
            cellList = cellList.toJS();
            let newCellList = cellList;
            let nextBtnEnable = true;
            return new InstallmentService(app.host).fentchBankInfo(cardNo,uid)
            .then(json => {
                let bankIconInfo = {
                    inputKey: 'BankIcon',
                    bankCode: '',
                    bankName: '',
                    bankText: '',
                    show: true,
                };
                if (json.bankCode) {
                    bankIconInfo.bankCode = json.bankCode;
                    bankIconInfo.bankName = json.bankName;
                }
                if (json.businessSupport === '1') {
                    bankIconInfo.bankCode = '';
                    bankIconInfo.bankName = '';
                    bankIconInfo.bankText = '暂不支持此银行卡';
                }

                newCellList.map((item, i) => {
                    if (item.inputKey == 'BankIcon') {
                        item = bankIconInfo;
                    }
                    cellList[i] = item;
                });
                dispatch(updateCellList(cellList));
            })
            .catch(error => {
            });
        }

        ReactNative.NativeModules.YH_CommonHelper.uid()
		.then(uid => {
			bankInfo(uid);
		})
		.catch(error => {
			ReactNative.NativeModules.YH_CommonHelper.login()
			.then(uid => {
				bankInfo(uid);
			})
			.catch(error => {

			});
		});
    }
}

export function updateOpenPageCellInfo(cellInfo) {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {cellList,snsCheckCodeEnable} = installment.openPageInfo;
        cellList = cellList.toJS();
        let newCellList = cellList;
        let cardNoValid = false;
        let mobileValid = false;
        let nextBtnEnable = true;

        if (cellInfo.inputKey == "cardNo") {
            let value = cellInfo.text;
            console.log('value');
            console.log(value);
            let cardNo = value.replace(/\s/g, '');
            let newCardNo = value.replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ');
            console.log('newCardNo');
            console.log(newCardNo);
            cellInfo.text = newCardNo;
            if (cardNo && cardNo.length >= 16) {
                cardNoValid = true;
                dispatch(getBankInfo(cellInfo,value));
            }else {
                cardNoValid = false;
                cellInfo.showBankIcon = false;
            }

        }else if (cellInfo.inputKey == "mobile" && cellInfo.text.length ==11) {
            mobileValid = true;
        }


        newCellList.map((item, i) => {
            if (item.inputKey == 'cardNo' && item.text.length >=16 && cellInfo.inputKey != 'cardNo') {
                cardNoValid = true;
            } else if (item.inputKey == 'mobile' && item.text.length ==11 && cellInfo.inputKey != 'mobile') {
                mobileValid = true;
            }
            if (item.inputKey == cellInfo.inputKey) {
                item = cellInfo;
            }
            if (item.text == '') {
                nextBtnEnable = false;
            }
            cellList[i] = item;
        });
        if (cardNoValid && mobileValid) {
            snsCheckCodeEnable = true;
        }else {
            snsCheckCodeEnable = false;
        }
        console.log('cellInfo');
        console.log(cellInfo);
        dispatch(updateCellList(cellList));
        dispatch(updateSnsCheckCodeEnableStatus(snsCheckCodeEnable));
        dispatch(updateNextBtnEnableStatus(nextBtnEnable));
    };
}

/**
 *  倒计时
 *
 * @param start 启动回调
 * @param tick 进度回调
 * @param complete 完成回调
 */
Timer.prototype.startCountdown = function(start, tick, complete) {
    var self = this;

    if (this.counter > 0 || this.countdownTimer) {
        return;
    } else {
        this.counter = 59;
    }


    // 启动回调
    if (start) {
        start.call(this);
    }

    if (tick) {
        tick.call(this, this.counter);
    }

    this.complete = complete;


    // 开始计时器
    this.countdownTimer = setInterval(function() {
        self.counter--;

        if (self.counter <= 0) {
            if (complete) {
                clearInterval(self.countdownTimer);

                // 重置计时器
                self.counter = 0;
                self.countdownTimer = null;
                complete.call(self);
            }
        }

        // 完成回调
        if (tick && self.counter > 0) {
            tick.call(self, self.counter);
        }
    }, 1000);

    return this;
};

export function onPressCheckCode() {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {cellList,snsCheckCodeEnable,snsCheckCodeText} = installment.openPageInfo;
        if (snsCheckCodeEnable && snsCheckCodeText == '获取验证码') {
            return new InstallmentService(app.host).sendVerifyCode('17705176933')
            .then(json => {
                
                let status = 0;
                let snsText = '';
                new Timer().startCountdown(function() {
                }, function(counter) {
                    // 进度回调
                    snsText = counter + 's';
                    dispatch(updateSnsCheckCodeText(snsText));
                }, function() {
                    // 倒计时结束后再次显示 "获取验证码"
                    snsText = '获取验证码';
                    dispatch(updateSnsCheckCodeText(snsText));
                });
                dispatch(updateSnsCheckCodeText(snsText));
            })
            .catch(error => {
            });
        }

    };
}

export function setInstallmentStausPageParams(statusCode, failReason, uid) {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let status ='';
        let jumpUrl = '';
        let statusInfo = {};
        if (statusCode == '2') {//成功
            //请求用户信用额度
            return new InstallmentService(app.host).getQueryCreditInfo('5448919')
            .then(json => {
                console.log('---额度');
                console.log(json);
                status = 'success',
                statusInfo = {
                    message: '可用额度',
                    reason: json.currCreditLimit,
                    btnText: '去逛逛',
                }
                jumpUrl = `http://m.yohobuy.com/home/installment/index?uid=${uid}?openby:yohobuy={"action":"go.instalmentlist","params":{"title":"分期专享"}}`;
                let params = {
                    statusCode,
                    status,
                    jumpUrl,
                    statusInfo,
                };
                dispatch(setInstallmentStatusPageOriginalInfo(params));
                dispatch(getInstallmentProductAndAdvertisement(1));
            })
            .catch(error => {
            });
        }else {
            switch (statusCode) {
                case '1':{
                    status = 'review',
                    statusInfo = {
                        message: '审核中',
                        reason: '审核结果会在5分钟内短信通知您',
                        btnText: '先去逛逛',
                    }
                }
                    break;
                case '3':{
                    status = 'error',
                    statusInfo = {
                        message: '审核未通过暂时无法授信',
                        reason: failReason,
                        btnText: '重新申请',
                    }
                }
                    break;
                case '4':{
                    status = 'cancel',
                    statusInfo = {
                        message: '账户已注销',
                        reason: '您的有货分期账户经本人确认已注销,',
                        reasonTwo: '如有疑问,请联系客服。',
                        reasonThird: '提示:已注销用户不可再开通',
                        btnText: '先去逛逛',
                    }
                }
                    break;
                case '5':{
                    status = 'error',
                    statusInfo = {
                        message: '审核未通过暂时无法授信',
                        reason: '您的授信未通过,在有货更多的消费,增加信用度,会大大提升审核通过率奥。',
                        btnText: '重新申请',
                    }
                }
                    break;
                default:

            }
        }
        console.log('---1111');

        let params = {
            statusCode,
            status,
            jumpUrl,
            statusInfo,
        };
        dispatch(setInstallmentStatusPageOriginalInfo(params));
    };
}

export function getInstallmentProductAndAdvertisement(page) {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        return new InstallmentService(app.host).getSearchIntallment(page)
        .then(json => {
            dispatch(setInstallmentStatusPageGoods(json.product_list))
            console.log('goods');
            console.log(json);
        })
        .catch(error => {
        });
    };
}

// onPressStatusPageBtn
export function onPressStatusPageBtn(page) {
    return (dispatch, getState) => {
        let {app, installment} = getState();
        let {jumpUrl} = installment.installmentStausPageInfo;
        ReactNative.NativeModules.YH_CommonHelper.jumpWithUrl(jumpUrl);
    };
}