sMSRecharge.js 9.78 KB
(function(angular) {
	/*var timer = setInterval(function() {
	    if (localStorage.getItem('sMSRecharge') != null) {
	      localStorage.removeItem('sMSRecharge')
	      clearInterval(timer)
	    }
	}, 100);*/
	
    function sMSRecharge($scope, $state, systemService, SweetAlertX, $uibModal) {
    	if (localStorage.getItem('sMSRecharge') != null) {
  	      localStorage.removeItem('sMSRecharge')
  	    }
        // $scope.payType = [
        //     {
        //         text: '支付宝',
        //         active: true,
        //         src: 'images/al.png'
        //     },
        //     {
        //         text: '微信',
        //         active: false,
        //         src: 'images/wx.png'
        //     }
        // ];
        $scope.inputValue = 1;
        systemService.getMerchantSmsNum(function (response) {
            if (response.data != null){
                $scope.smsNum = response.data.smsNum;
            }
            else{
                $scope.smsNum = 0;
            }
        });

        $scope.sub = function () {
            if($scope.inputValue > 0){
                $scope.inputValue--;
            }
        };

        $scope.add = function () {
            $scope.inputValue++;
            $scope.payMoney = $scope.selectConfig.smsSum * $scope.inputValue * $scope.selectConfig.singleSellPrice;
        };

        $scope.blurInput = function (obj) {
            $scope.payMoney = $scope.selectConfig.smsSum * $scope.inputValue * $scope.selectConfig.singleSellPrice;
        };

        systemService.getSmsConfigInfo(function (response) {
            $scope.smsConfigInfoList = response.data;
            // console.log(response);
            if(response.data) { 
                $scope.smsConfigInfoList[0].active = true;
                $scope.selectConfig = response.data[0];
                $scope.payMoney = $scope.selectConfig.smsSum * $scope.inputValue * $scope.selectConfig.singleSellPrice;
            }
            //console.log($scope.selectConfig);
        });

        //新增套餐
        // $scope.getSmsConfig = function (item) {
        //     var obj = {
        //         packageName: item.packageName,
        //         packageUnitPrice:item.packageUnitPrice,
        //         singleSellPrice:item.singleSellPrice,
        //         smsSum:item.smsSum
        //     };
        //     systemService.addSmsRechargeInfo(obj,function (data) {
        //         if (data.returnCode == '0') {
        //             SweetAlertX.alert('', '提交成功', 'success');
        //
        //         } else {
        //             SweetAlertX.alert(data.message || '', '提交失败', 'error');
        //         }
        //     })
        // };

        $scope.getSmsConfig = function(item,index){
            //$scope.smsConfigInfoList.forEach(e => e.active = false)
            angular.forEach($scope.smsConfigInfoList, function(item){
                item.active = false;
            });
            $scope.smsConfigInfoList[index].active = true;
            $scope.payMoney = $scope.inputValue * item.singleSellPrice;
            $scope.selectConfig = $scope.smsConfigInfoList[index];
            $scope.payMoney = $scope.selectConfig.smsSum * $scope.inputValue * $scope.selectConfig.singleSellPrice;
        }

        //确定
        $scope.payRightNow = function () {
            var buyParam = {};
            buyParam.id = $scope.selectConfig.id;

            var money = $scope.selectConfig.smsSum * $scope.inputValue*$scope.selectConfig.singleSellPrice;
            if (money <= 0){
                swal({
                    type: 'error',
                    title: '支付',
                    text: '支付金额小于0',
                    showConfirmButton:false,
                    timer: 1500
                },function(){
                    window.sweetAlert.close();
                });
                return;
            }

            buyParam.payMoney = money;
            buyParam.buySmsCount = $scope.inputValue;
            systemService.buySmsNum(buyParam,function (response) {
                if(response.returnCode == 0){
                    if (response.data != null && response.data.qrCodeImage != null){
                        var obj = {};
                        obj.image = response.data.qrCodeImage;
                        if (response.data.qrCodeImage == null){
                            return;
                        }
                        console.log(response.data);
                        obj.smsNum = $scope.smsNum;
                        obj.orderNo = response.data.orderNo;
                        var modalInstance = $uibModal.open({
                            animation: true,
                            templateUrl: 'showPayModal',
                            controller: 'buyBtnCtrl',
                            size: 'sm',
                            resolve: {
                                dtInstance: $scope.dtInstance,
                                queryParams: obj
                            }
                        });
                        modalInstance.result
                            .then(function (_id) {
                            	systemService.getMerchantSmsNum(function (response) {
                                    if (response.data != null){
                                        $scope.smsNum = response.data.smsNum;
                                    }
                                    else{
                                        $scope.smsNum = 0;
                                    }
                                });
                            })
                        return;
                    }
                }
                swal({
                    type: 'error',
                    title: '支付',
                    text: '支付失败',
                    showConfirmButton:false,
                    timer: 1500
                },function(){
                    window.sweetAlert.close();
                });
            });

        };

    }

    function buyBtnCtrl($scope, $state, $uibModalInstance, systemService, dtInstance, queryParams){
        $scope.imgURL = queryParams.image;
        $scope.smsNum = queryParams.smsNum;
        $scope.orderNo = queryParams.orderNo;
        $scope.cancel = function() {
            clearInterval($scope.timeInterval);
            $uibModalInstance.close('cancel');
        };
        $scope.timeCount = 0;
        if ($scope.orderNo != null && $scope.orderNo.length > 0){
            $scope.timeInterval = setInterval(function () {
               // console.log("定时器");
                $scope.timeCount += 1;
                var params = {};
                params.orderNo = $scope.orderNo;
                systemService.getOrderDetail(params, function (result) {
                    if (result.data != null){
                        //console.log("查询订单状态");
                       // console.log(result.data);
                        if (result.data.payStatus == 1 || result.data.payStatus == 4){
                            clearInterval($scope.timeInterval);
                            $uibModalInstance.close('cancel');
                            swal({
                                type: 'success',
                                title: '支付成功',
                                text: '',
                                showConfirmButton:false,
                                timer: 1500
                            },function(){
                                window.sweetAlert.close();
                                $state.go('system.sms.recharge',{id:null});
                            });
                            return;
                        }
                    }
                    //console.log("查询结束------------");
                    if ($scope.timeCount > 60 * 5){
                        clearInterval($scope.timeInterval);
                        $uibModalInstance.close('cancel');
                        swal({
                            type: 'error',
                            title: '支付失败',
                            text: '',
                            showConfirmButton:false,
                            timer: 1500
                        },function(){
                            window.sweetAlert.close();
                        });
                    }
                });
            }, 1000);
        }

        $scope.$on('destroy',function(){
            clearInterval($scope.timeInterval);
        });

        $scope.confirm = function() {
            clearInterval($scope.timeInterval);

            var params = {};
            params.orderNo = $scope.orderNo;
            systemService.getOrderDetail(params, function (result) {
                if (result.data != null){
                    if (result.data.payStatus == 1 || result.data.payStatus == 4){
                        $uibModalInstance.close('cancel');
                        swal({
                            type: 'success',
                            title: '支付成功',
                            text: '',
                            showConfirmButton:false,
                            timer: 1500
                        },function(){
                            window.sweetAlert.close();
                            $state.go('system.sms.recharge',{id:null});
                        });
                        return;
                    }
                }
                $uibModalInstance.close('cancel');
                swal({
                    type: 'error',
                    title: '支付失败',
                    text: '',
                    showConfirmButton:false,
                    timer: 1500
                },function(){
                    window.sweetAlert.close();
                });
            });
        };
    }

    angular.module('system').controller('sMSRecharge', sMSRecharge)
        .controller('buyBtnCtrl', buyBtnCtrl);
})(angular);