AdvertController.js 6.2 KB
"use strict";
var advertController = angular.module('advertController',[]);


advertController.controller('advertController', [
 	'$rootScope',
	'$scope',
	'AdvertService',
	function($rootScope, $scope, AdvertService) {
 		$scope.currentPage = 1;
 	    $scope.numPages = 5;
 	    $scope.totalRecord = 0;
 	    $scope.pageSize = 50;
 	    $scope.adverts = [];
 	    AdvertService.listAdverts($scope.currentPage, $scope.pageSize);
		$scope.$on('advert.list', function() {
			$scope.adverts = AdvertService.adverts.datas;
			$scope.numPages = AdvertService.adverts.totalPage;
			$scope.totalRecord = AdvertService.adverts.totalRecord;
		});
		
	}]
);

advertController.controller('addAdvertController', [
 	'$rootScope',
	'$scope',
	'ProductService',
	'AdvertService',
	'CampaginCategoryService',
	'CategoryService',
	'BaseService',
	function($rootScope, $scope, ProductService, AdvertService, CampaginCategoryService,CategoryService,BaseService) {
 		var aid =  $rootScope.$stateParams.advertId;
 		$scope.imageUrl = "";
 		$scope.editAdvertInfo={
 				imAdvertId:'',
 				name:'',
 				imageUrl:'',
 				linkUrl:'',
 				typeId:0,
 				imProductId:'',
 				isShow:1,
 				sortIndex:0
 		};
 		$scope.imProductId = "";
 		$scope.products=[];
 		$scope.campagins=[];
 		$scope.selectProductObj ="";
 		$scope.selectCampaginObj ={};
 		var isLoad = 0;
		$scope.currentPage = 1;
		$scope.numPages = 1;
		$scope.pageSize = 3;
		$scope.totalRecord = 0;
		$scope.pages = [];

		$scope.name=[];
		$scope.editProductInfo= {
			name: ''
		};
		function getProductId(){
			if(isLoad&&$scope.products.length){
				var productId = $scope.editAdvertInfo.imProductId;
				ProductService.getProductDetail(productId, function (data) {
					$scope.editProductInfo = data.data;
					$scope.name = $scope.editProductInfo.name;
					$scope.selectProductObj =$scope.name;
				});
			}
		}
		$scope.showProduct=function(){
			$scope.productQuery={
				keyword:'',
				pageNow:1,
				pageSize:6
			};
			$("#contents").hide();
			$("#productNames").show(); //显示弹出框
			CategoryService.listParent();
			$scope.$on('parentCategory.list', function() {
				$scope.parentCategory = CategoryService.parentCategory;
			});
			ProductService.pagerProductByAdvert($scope.productQuery);
			$scope.$on('product.list', function() {
				$scope.products = ProductService.products.datas;
				$scope.numPages = ProductService.products.totalPage;
				$scope.totalRecord = ProductService.products.totalRecord;
			});
			$scope.onSelectPage = function(page) {
				$scope.productQuery.pageNow=page;
				ProductService.pagerProductByAdvert($scope.productQuery);
			};
			$scope.isActive = function(page){
				return $scope.currentPage === page;
			};
			$scope.searchProduct=function() {
				$scope.productQuery.pageNow = 1;
				ProductService.pagerProductByAdvert($scope.productQuery);
			};
		};

		$scope.ForProductObj=function(product){
			$scope.selectProductObj =product.name;
			$scope.editAdvertInfo.imProductId=product.imProductId;
			//隐藏弹出框
			$("#contents").show();
			$("#productNames").hide();
		};
		CampaginCategoryService.listCampaignCategoryToAdvert();
		$scope.$on('Campaigncategory.list', function() {
			$scope.campagins = CampaginCategoryService.categorys;
			getCampaginCategoryType();
		});

		function getCampaginCategoryType(){
			if(isLoad&&$scope.campagins.length){
				var type = $scope.editAdvertInfo.imProductId;
				var list = $scope.campagins;
				for(var i=0;i<list.length;i++){
					if(list[i]['imCampaignCategoryId'] == type){
						$scope.selectCampaginObj = $scope.campagins[i];
						break;
					}
					if(type == '') {
						$scope.selectCampaginObj = null;
						break;
					}
				}
			}
		}
		var type=0;
		$scope.onChangeAdvertType=function(typeId) {
			$scope.editAdvertInfo.typeId=typeId;
			type=typeId;
		};
		$scope.onSubmitAdvert= function () {
			var re = /^[0-9]+[0-9]*]*$/;
			$scope.editAdvertInfo.imageUrl=$scope.imageUrl;
			if(type==0) {
				BaseService.updateAlert("小主,您还没有选择广告位类型!");
			}else {
				if(!re.test($scope.editAdvertInfo.sortIndex)) {
					BaseService.updateAlert("小主,广告位的位置只能输入0或正整数!");
				}else if(($scope.editAdvertInfo.typeId == 1 || type==1) ){
					if($scope.selectProductObj == ''){
						BaseService.updateAlert("小主,关联的现金商品不能为空!");
					}else {
						$scope.editAdvertInfo.imProductId = $scope.editAdvertInfo.imProductId;
						AdvertService.addAdvert($scope.editAdvertInfo, $scope.editAdvertInfo.imAdvertId);
					}
				}else if(($scope.editAdvertInfo.typeId == 2 || type==2)){
					if($scope.editAdvertInfo.linkUrl==''){
						BaseService.updateAlert("小主,广告位链接URL不能为空!");
					}else {
						$scope.editAdvertInfo.linkUrl=$scope.editAdvertInfo.linkUrl;
						AdvertService.addAdvert($scope.editAdvertInfo, $scope.editAdvertInfo.imAdvertId);
					}
				}else if(($scope.editAdvertInfo.typeId == 3 || type==3)){
					if( $scope.selectCampaginObj == null){
						BaseService.updateAlert("小主,关联相关的专场不能为空!");
					}else {
						$scope.editAdvertInfo.imProductId = $scope.selectCampaginObj.imCampaignCategoryId;
						AdvertService.addAdvert($scope.editAdvertInfo, $scope.editAdvertInfo.imAdvertId);
					}
				}
			}
		};


		$scope.deleteAdvert=function(imAdvertId) {
			if(confirm("您确定要删除这个广告位吗?")) {
				AdvertService.deleteAdvert(imAdvertId);
			}
		};
		if(aid) {
			AdvertService.getAdvertDetail(aid, function (data) {
				$scope.editAdvertInfo=data.data;
				$scope.imProductId = $scope.editAdvertInfo.imProductId;
				$scope.imageUrl = $scope.editAdvertInfo.imageUrl;
				isLoad = 1;
				getCampaginCategoryType();
				if($scope.editAdvertInfo.typeId==1){
					ProductService.getProductDetail($scope.imProductId, function (data) {
						$scope.editProductInfo = data.data;
						$scope.name = $scope.editProductInfo.name;
						$scope.selectProductObj =$scope.name;
					});
				}
			});
		}
        $scope.myKeyup = function(e){
            //IE 编码包含在window.event.keyCode中,Firefox或Safari 包含在event.which中
            var keycode = window.event?e.keyCode:e.which;
            if(keycode==13){
                $scope.onSubmitAdvert();
            }
        };


	}]
);