mainController.js 6.11 KB
angular.module('myApp')
	.controller('mainController',['$scope','$rootScope','dingdaService','$state','globalService',function($scope,$rootScope,dingdaService,$state,globalService){
		
		$rootScope.isRendBike = false;
		$rootScope.isLoadFinish = false;
		$scope.unfinishOrder = {};
		
		var init = function() {
//			globalService.init();
			$scope.userinfo = globalService.getData("user");
			if(!$scope.userinfo) {
				$scope.userinfo = {};
				$scope.userinfo.userName = "未登录";
				$scope.userinfo.loginState = false;
			} else {
				$scope.userinfo.loginState = true;
//				alert("getUnfinishedOrder")
				dingdaService.getUnfinishedOrder().success(function(data , status){
//					alert(JSON.stringify(data))
					if(data.data.unfinishedOrder.id) {
						$rootScope.isRendBike = true;
						$scope.unfinishOrder = data.data.unfinishedOrder;
					}
					if(data.data.unfinishedOrder.status == 200) {
						layer.open({
							content: '您有未支付的订单,是否前往支付?',
							btn: ['确认', '取消'],
							shadeClose: false,
							yes: function(index){
								layer.close(index);
								$state.go('tripDetail' , { orderId : $scope.unfinishOrder.id});
								return;
							},no: function(index){
								layer.close(index);
							}
						});
					}
				}).error(function(data , status) {
					console.log(JSON.stringify(data))
				})
			}
			$scope.moduleList = [];
			$scope.moduleList.push({
				name : "行程记录",
				icon : "images/trip.png",
				state : "trip"
			});
			$scope.moduleList.push({
				name : "钱包",
				icon : "images/wallet.png",
				state : "wallet"
			});
			$scope.moduleList.push({
				name : "保证金",
				icon : "images/bond.png",
				state : "bond"
			});
			$scope.moduleList.push({
				name : "联系我们",
				icon : "images/contact.png",
				state : "contact"
			});
			$scope.moduleList.push({
				name : "意见反馈",
				icon : "images/opinion.png",
				state : "opinion"
			});
		}
		
		$scope.doAction = function(index) {
			if($scope.checkLogin()) {
				var arg = {};
				if(index == 2 || index == 4) {
					arg = {longitude: $rootScope.currentPoint.longitude , latitude: $rootScope.currentPoint.latitude };
				}
				if(index == 3) {
					location.href = "http://wx.dy.dingdatech.com/contactUs.html";
				} else {
					$state.go($scope.moduleList[index].state, arg);
				}
			}
		}
		
		$scope.checkLogin = function() {
			if(!globalService.getData("user")) {
				$state.go('login', {}, {reload: true});
				return false;
			}
			return true;
		}
		
		$scope.logout = function() {
			$scope.userinfo = {};
			$scope.userinfo.userName = "未登录";
			$scope.userinfo.loginState = false;
            globalService.clearData("user");
            location.reload()
		}
		
		var initScan = function() {
			$rootScope.openScan = false;
			$rootScope.openDetail = false;
			$rootScope.openMenu = false;
			$rootScope.openRing = false;
			$rootScope.$watch("openScan",function(newVal, oldVal){
				if(newVal !== oldVal && newVal) {
					if(!$scope.checkLogin()) {
						return;
					}
					
					if($scope.unfinishOrder.status == 200) {
						$state.go('tripDetail' , { orderId : $scope.unfinishOrder.id});
						return;
					}
					$rootScope.openScan = false;
					wx.scanQRCode({
                        needResult: 1, // 默认为1,扫描结果由微信处理,1则直接返回扫描结果,
                        scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码,默认二者都有
                        success: function (res) {
                            var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
//                          alert(result);
							dingdaService.verifyScanCode(result).success(function(data , status){
//								alert(JSON.stringify(data))
								if(data.meta.code == 200) {
									if(data.data.orderDetail.id) {
										
							            layer.open({
							                content: "租车成功,订单号为:" + data.data.orderDetail.id,
							                time:2
							            });
//										alert("租车成功,订单号为:" + data.data.orderDetail.id);
										$rootScope.isRendBike = true;
										
										$state.go('rentDetail', {}, {reload: true});
										
									}
								} else if(data.meta.code == 202) {
						            layer.open({
						                content: data.meta.message,
						                time:2
						            });
									$state.go('bond', {longitude: $rootScope.currentPoint.longitude , latitude: $rootScope.currentPoint.latitude });
								} else {
									layer.open({
						                content: data.meta.message,
						                time:2
						            });
								}
							}).error(function(data , status){
					            layer.open({
					                content: "租车失败" + JSON.stringify(data),
					                time:2
					            });
							});
                        }
                    });
				}
	        })
			$rootScope.$watch("openDetail",function(newVal, oldVal){
				if(newVal === oldVal) return;
				
				if(newVal) {
					dingdaService.getUnfinishedOrder().success(function(data , status){
						if(data.meta.code == 200) {
							if(!data.data.unfinishedOrder.id) {
								layer.open({
					                content: '租车完成',
					                time:2
					            });
//								alert("租车完成")
			        			$state.go('main');
							}
						}
						
						if(data.data.unfinishedOrder.status == 200) {
							$state.go('tripDetail' , { orderId : data.data.unfinishedOrder.id});
						} else {
							$state.go('rentDetail', {}, {reload: true});
						}
					}).error(function(data , status) {
						console.log(JSON.stringify(data))
					})
					
				}
	        })
			$rootScope.$watch("openRing",function(newVal, oldVal){
				// TODO open new message
	        })
			$rootScope.$watch("isLoadFinish" , function(newVal , oldVal){
				if(newVal === oldVal) return;
				if(newVal) {
					document.getElementById("sliderMenu").style.display = "block";
				}
			})
			
			document.getElementById("sliderMenu").addEventListener("touchmove", function(){
				return;
			} , false);
		}
		
//		mui.init();
		initScan();
		init();
		
		
}]);