mainController.js 10.3 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();
			function getUrl(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if (r != null) return unescape(r[2]); return null;
			}
			var code = getUrl('code')
			var loginUrlCode = function (code) {
				if (code) {
					dingdaService.verifyThirdCode(code).success(function (res) {
						globalService.saveData('loginCode', code)
						if (res.meta.code == 200) {
							dingdaService.verifyPhoneCode(res.data.mobile, res.data.code).success(function (data) {
								if (data.meta.code == 200) {
									data.data.userName = res.data.mobile;
									globalService.saveData('user', data.data);
									layer.open({
										content: '登录成功',
										time: 1
									});
									setTimeout(function () {
										window.location.href = window.location.origin + window.location.pathname
										// location.reload();
									}, 300);
								} else if (data.meta.code == 401) {
									layer.open({
										content: data.meta.message,
										time: 1
									});
									//					alert(data.meta.message);
									//					layer.msg(data.data.error);
									return;
								}
							})
						}
						if (res.meta.code == 500) {
							$state.go('login', {}, { reload: true });
						}
					})
				} else {
					$scope.userinfo = {};
					$scope.userinfo.userName = "未登录";
					$scope.userinfo.loginState = false;
				}
			}
			$scope.userinfo = globalService.getData("user");
			if (!$scope.userinfo) {
				loginUrlCode()
			} else {
				var loginCode = globalService.getData("loginCode");
				if (code && (loginCode !== code)) {
					localStorage.removeItem('ls.user')
					loginUrlCode()
				} else {
					dingdaService.getUnfinishedOrder().success(function (data, status) {
						if (data.meta.code === 303) {
							layer.open({
								content: data.meta.message,
								time: 2
							})
							localStorage.removeItem('ls.user')
						}
						if (data.meta.code === 401) {
							layer.open({
								content: '登录token失效,请重新登录',
								time: 2
							})
							localStorage.removeItem('ls.user')
							return
						}
						if (data.meta.code === 200) {
							if (data.data.unfinishedOrder.id) {
								$rootScope.isRendBike = true;
								$scope.unfinishOrder = data.data.unfinishedOrder;
								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;


					if (!appClient) {
						layer.open({
							content: '调起摄像头失败',
							time: 2
						});

					} else {
						appClient.getERCode('', function (str, cb) {
							dingdaService.verifyScanCode(str).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
								});
							});
						})
						//    window.WebViewJavascriptBridge.registerHandler('postERCode', function(data, responseCallback) {
						//        layer.open({
						//            content: data,
						//            time: 10
						//        });
						//    })
						// 	window.WebViewJavascriptBridge.callHandler('getERCode', function(data, responseCallback) {
						//    })
						// }else if(isiOS){
						//    appClient.appObj.registerHandler('postERCode', function(data, responseCallback) {
						//        layer.open({
						//            content: JSON.stringify(data),
						//            time: 10
						//        });
						//    })
						//    appClient.getERCode('getERCode', function (res) {
						//        // layer.open({
						//        //     content: JSON.stringify(res) + 'cb',
						//        //     time: 10
						//        // });
						//    }, '')
					}

					// 					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) {
					if (document.getElementById("sliderMenu")) {
						document.getElementById("sliderMenu").style.display = "block";
					}
				}
			})

			document.getElementById("sliderMenu").addEventListener("touchmove", function () {
				return;
			}, false);
		}

		//		mui.init();
		initScan();
		init();


	}]);