addComplainSuggestCtrl.js 7.72 KB
(function (angular) {
    function addComplainSuggestCtrl($scope,complainSuggestService,$stateParams,$state,$http,$sce,Upload,SweetAlertX) {
    	$scope.productId = $stateParams.productId;
    	$scope.saveSubmiting = false;
    	
    	$scope.imageArr = [];
    	$scope.videoUrl = null;

    	$scope.validLoss = function (value) {
			var obj = {
                result : ''
			}
            var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
            if (!reg.test(value)) {
                $scope.showValidLoss = true;
                obj.result = false;
            }else{
                $scope.showValidLoss = false;
                obj.result = true;
            }
            return obj;
        };
		var that = ''
		$scope.gradeFlag = true
        $(function () {
			$("ul.cleanfloat li").click(function () {
				$scope.gradeFlag = false
				that = $(this)
                $("ul.cleanfloat li").addClass('star_color');
                $(this).prevAll().addClass('star_color');
                $(this).nextAll().removeClass('star_color');
			})
			$("ul.cleanfloat li").mousemove(function () {
				$("ul.cleanfloat li").addClass('star_color');
                $(this).prevAll().addClass('star_color');
                $(this).nextAll().removeClass('star_color');
			})
			$("ul.cleanfloat li").mouseout(function () {
				$("ul.cleanfloat li").removeClass('star_color');
				that? that.addClass('star_color') &&
					that.prevAll().addClass('star_color') &&
					that.nextAll().removeClass('star_color') : null
			})
        });

		$scope.saveInfo = function () {
    		var infoForm = $scope.infoForm;
    		// console.log(infoForm)
            if(!$scope.hasSelectTag && $scope.complainSuggestType == 1){
                $scope.isInValidate = true;
                return false;
            }
            if($scope.complainSuggestType == 1 && $scope.imageArr.length == 0 && !$scope.videoUrl){
                $scope.isInValidate = true;
                return false;
            }
            if($scope.gradeFlag){
                $scope.isInValidate = true;
                return false;
            }
    		if(infoForm.$invalid){
                $scope.isInValidate = true;
                return false;
            }
    		$scope.submitInfo();
    	};
    	
    	$scope.submitInfo = function(){
    		if($scope.saveSubmiting){
    			return;
    		}
    		$scope.saveSubmiting = true;
    		var params = {};
    		params.productId = $scope.productId;
    		params.type = $scope.complainSuggestType;
    		params.userPhone = $scope.info.userPhone;
    		var tagList = [];
            if($scope.complainSuggestType == 1){
                params.orderCode = $scope.info.orderCode;
                for(var i = 0;i<$scope.tagOptions.length;i++){
                    if($scope.tagOptions[i].selected){
                        tagList.push({'tagCode':$scope.tagOptions[i].tagCode,'tagName':$scope.tagOptions[i].tagName});
                    }
                }
                params.tagList = tagList;
                params.grade = $("ul.cleanfloat li.star_color").length;
            }
    		params.description = $scope.info.description;
    		var imgList = [];
    		for(var i = 0;i<$scope.imageArr.length;i++){
    			imgList.push($scope.imageArr[i].imgSrc);
    		}
    		params.imgList = imgList;
    		params.videoUrl = $scope.videoUrl;
    		params.loss = $scope.info.loss;
    		complainSuggestService.add(params,function(res){
    			$scope.saveSubmiting = false;
    			if(res.returnCode == 0){
    				swal({type: 'success',title: '',text: res.message||'提交成功',showConfirmButton:false,timer: 1500,allowOutsideClick: false},function(){
                        window.sweetAlert.close();
                        $scope.backTo();
                    });
    			}else{
    				swal({type: 'error',title: '',text: res.message||'操作失败',timer: 1500,allowOutsideClick: false},function(){
                        window.sweetAlert.close();
                    });
    			}
            });
    	}
    	$scope.selectTag = function(index){
        	if($scope.tagOptions[index].selected){
        		$scope.tagOptions[index].selected = false;
        		$scope.hasSelectTag=false;
        		for(var i = 0;i<$scope.tagOptions.length;i++){
        			if($scope.tagOptions[i].selected){
                		$scope.hasSelectTag=true;
                		break;
        			}
        		}
        	}else{
        		$scope.tagOptions[index].selected = true;
        		$scope.hasSelectTag=true;
        	}
		};
		// 统一弹窗
		function popTips(type, text, timer) {
			timer = timer || 1000
			swal({
				type: type,
				title: '',
				html: true,
				text: text,
				showConfirmButton: false,
				timer: timer
			  });
		}
        /**
		 * 上传图片
		 * */
		$scope.uploadImage=function(file, errFiles){
			console.log(file,errFiles)
			if(file == null){
				if(errFiles != null && errFiles.length > 0){
					console.log(errFiles)
					// $scope.imgError = true;
					popTips('error', '只能上传800KB以内的PNG,JPG格式的图片')
				}
				return;
			}
			if(file.size > 800 * 1024 || !/^image\/(jpg|png|jpeg)+$/.test(file.type)){
				// $scope.imgError = true;
				popTips('error', '只能上传800KB以内的PNG,JPG格式的图片')
                return;
			}
			// $scope.imgError = false;
			$scope.showLoadingImg = true;
			Upload.upload({
                url: 'common/uploadImage',
                //上传的同时带的参数
                //data: { 'username': $scope.username },
                file: file
            }).success(function (data, status, headers, config) {
            	if(data.returnCode == 0){
            		$scope.imageArr.push({'imgSrc':data.data});//data.data
            	}
                $scope.showLoadingImg = false;
            }).error(function (data, status, headers, config) {
                $scope.showLoadingImg = false;
            });
		};
		/**
		 * 删除图片
		 */
		$scope.delImg = index => {
			$scope.imageArr.splice(index,1)
		}
		$scope.uploadVideo=function(file, errFiles){
			console.log(file)
			if(file == null){
				if(errFiles != null && errFiles.length > 0){
                    console.log(errFiles)
					// $scope.videoError = true;
					popTips('error', '只能上传20MB以内的mp4,AVI,wma,rmvb格式的视频')
				}
				return;
			}
            if(file.size > 20 * 1024 * 1024){
				// $scope.videoError = true;
				popTips('error', '只能上传20MB以内的mp4,AVI,wma,rmvb格式的视频')
                return;
            }
			$scope.showLoadingVideo = true;
			Upload.upload({
                url: 'common/uploadVideo',
                //上传的同时带的参数
                //data: { 'username': $scope.username },
                file: file
            }).success(function (data, status, headers, config) {
            	if(data.returnCode == 0){
            		$scope.videoUrl = data.data;
            	}
                $scope.showLoadingVideo = false;
            }).error(function (data, status, headers, config) {
                $scope.showLoadingVideo = false;
            });
		};
		/**
		 * 删除视频
		 */
		// $scope.videoDel = false
		$scope.delVideo = function() {
			$scope.videoUrl = null
		}
		// $('#video').on('mouseover', function () {
		// 	console.log('yiru')
		// 	$scope.videoDel = true
		// })
		$scope.trustUrl =function(url){
			return $sce.trustAsResourceUrl(url);
		}
		$scope.backTo=function(){
			$state.go('electronicDirectory.productExamineDetail',{id:$scope.productId,type:2});//2表示产品查看页
		}
    	$scope.init=function(){
    		complainSuggestService.getTagList(function(res){
    			$scope.tagOptions = res.data;
    		});
    	};
    	$scope.init();
    }
    
    angular.module('electronicDirectory',['ngFileUpload','ngSanitize'])
        .controller('addComplainSuggestCtrl', addComplainSuggestCtrl);
})(angular);