tripDetailController.js 5.12 KB
angular.module('myApp')
    .controller('tripDetailController', ['$scope', '$rootScope', '$state', 'dingdaService', '$stateParams', 'wechatPayment',
        function ($scope, $rootScope, $state, dingdaService, $stateParams, wechatPayment) {
            console.log('tripDetailController');
            /**
             * 2. 选择dom元素
             * */
            var tripDetail = $('.tripDetail');
            var winHeight = $(window).height();
            var tripHeight = tripDetail.height() + 40;
            var bottomPosition = $('.bottomPosition');
            if (winHeight >= tripHeight) {
                bottomPosition.css({'position': 'absolute', 'bottom': '0'})
            }
            /**
             * 3. 定义当前业务需要的数据
             * */
            $scope.tripDetail = {};
            $scope.needPay = false;
            $scope.backHome = function () {
                $state.go('main');
            }
            /**
             * 4. 支付逻辑
             * ispay 有无支付
             * isActive
             * choosePay 选择支付
             * */
            var ispay = false;
            $scope.isActive = true; // 这个是个关键 TODO 要更改的
            $scope.payType = 1;
            $scope.choosePay = function (index) {
                if ($($('.checkboxS')[index]).hasClass('checkboxTrue')) {
                    // 如果要选择的项已经选中,不处理
                    return
                } else {
                    $scope.isActive = !$scope.isActive;
                }

                $scope.payType = index;
            }
            $scope.goPay = function () {
                if (ispay) return;
                if ($scope.payType == 1) {  //微信支付

                    ispay = true;
                    wechatPayment.callRentPay($scope.tripDetail.id, $scope.tripDetail.total_fee)
                        .then(function (result) {
                            ispay = false;
                            //支付成功
                            init();
//                    $state.go('bondSuccess');
                        }, function (errMsg) {
                            ispay = false;
                            //支付失败
                            // layer.open({
                            //     content: '支付失败,请重试',
                            //     time:2
                            // });
                        })
                } else {
                    if ($scope.tripDetail.total_fee > $scope.balance) { //钱包支付判断余额是否足够支付
                        layer.open({
                            content: '您的余额不够是否去充值?',  //小米下面按钮对齐
                            btn: ['去充值', '取消'],
                            shadeClose: false,
                            yes: function (index) {
                                $state.go('prepay', {}, {reload: false});
                                layer.close(index);
                            }, no: function (index) {
                                layer.close(index);
                            }
                        });
                    } else {
//                    余额支付

                        ispay = true;
                        var data = {
                            amount: $scope.tripDetail.total_fee,
                            body: '余额支付',
                            type: 102
                        }
                        dingdaService.payWX($scope.tripDetail.id, data)
                            .success(function (data, status) {
                                ispay = false;
                                layer.open({
                                    content: '支付成功',
                                    time: 2
                                });
                                init();
                            })
                            .error(function (data) {
                                ispay = false;
                                layer.open({
                                    content: '支付失败',
                                    time: 2
                                });
                            })
                    }
                }
            }

            var orderid = $stateParams.orderId;
            /**
             * 1. init 获取初始化的数据 tripDetail 订单详情 needPay 是否需要发起支付
             * */
            var init = function () {
                dingdaService.getOrderFinishInfo(orderid).success(function (data, status) {
                    $scope.tripDetail = data.data.order;
//                alert(JSON.stringify(data));
                    if (data.data.order.status == 300 || $scope.tripDetail.total_fee == 0) {
                        $scope.needPay = false;
                    } else if (data.data.order.status == 200) {
                        $scope.needPay = true;
                    }
                })

                dingdaService.getWallet(orderid).success(function (data, status) {
                    $scope.balance = data.data.balance;
                })
            }
            init();

        }])