productExamineDetailCtrl.js 10.4 KB
(function (angular) {
	function productExamineDetailCtrl($scope, productService, $stateParams, $state, $http, $sce) {
		$scope.productId = $stateParams.id;
		$scope.type = $stateParams.type;
		$scope.showEdit = false;
		$scope.productStatistics = {};
		$scope.tagCodes = [];
		$scope.tagsList = [];
		$scope.complainDetails = [];
		$scope.media_img = '';

		//分页参数
		$scope.firstPage = 1; //第一页
		$scope.lastPage = 1; //最后页
        $scope.pageSizeList = [10, 20, 50];//分页大小选项
        $scope.pageSize = $scope.pageSizeList[0];//分页大小
		$scope.total = 0; //分页总条数
		$scope.pages = [];
		$scope.pageNum = 5; //分页格子数量
		$scope.page = 1; //当前页数

		$scope.params = {
			productId: $scope.productId,
			pageNum: $scope.page,
			pageSize: $scope.pageSize,
			displayStatus: 2,
			tagCodes: $scope.tagCodes,
			type: 1,
			source:$scope.type
		}
		switch ($scope.type) {
			case '1':
				$scope.source = '产品管理';
				$scope.name = '查看与修改';
				break;
			case '2':
				$scope.source = '产品查看';
				$scope.name = '查看';
				break;
			case '3':
				$scope.source = '产品维护';
				$scope.name = '查看';
				break;
		}

		$scope.getProductById = function () {
			$scope.dLoginUserId = '';
			productService.getDLoginUserId(function (res) {
				$scope.dLoginUserId = res.data; 
				productService.getProductById({ 'id': $scope.productId,'dLoginUserId':$scope.dLoginUserId, 'type': 1 }, function (res) {
					$scope.productInfo = res.data;
					if ($scope.productInfo.status == 5) {
						// 已经上架不给修改
						$scope.showEdit = false;
					} else if ($scope.type == 2 || ($scope.type == 3 && ($scope.productInfo.status != 1 && $scope.productInfo.status != 3))) {
						// 产品查看不行 产品维护时候不是待供应商完善和审核不通过不给修改
						$scope.showEdit = false;
					} else {
						$scope.showEdit = true;
					}
					$scope.productInfo.description = $sce.trustAsHtml($scope.productInfo.description);
				});
			});
			
		};

		$scope.iFrameHeight = function () {
			var ifm = angular.element('#iframe')[0]
			ifm.addEventListener('load', function () {
				resizeCrossDomainIframe('iframe', '*')
				// var subWeb = document.frames ? document.frames["iframe"].document : ifm.contentDocument
				// console.log(subWeb)
				// if (ifm != null && subWeb != null) {
				// 	ifm.height = subWeb.body.scrollHeight
				// 	console.log(ifm.contentWindow.document.getElementsByTagName('body'), subWeb.body.scrollHeight)
				// }
			})

		}

		$scope.videoUrl = function (url) {
			return $sce.trustAsResourceUrl(url)
		}

		$scope.chooseTag = function (index) {
			$scope.tagsList[index].active = !$scope.tagsList[index].active
			if ($scope.tagsList[index].active) {
				$scope.tagCodes.push($scope.tagsList[index].tagCode)
			} else {
				$scope.tagCodes.forEach(function (e, i) {
					if ($scope.tagsList[index].tagCode == e) $scope.tagCodes.splice(i, 1)
				})
			}
			$scope.getProductComplainDetails()
		}

		function resizeCrossDomainIframe(id, other_domain) {
			var iframe = angular.element("#" + id)[0]
			window.addEventListener('message', function (event) {
				//if (event.origin !== other_domain) return; // only accept messages from the specified domain 
				if (isNaN(event.data)) return; // only accept something which can be parsed as a number 
				var height = parseInt(event.data) + 32
				iframe.style.height = height + "px"
			}, false)
		}

		/*$scope.toPreview = function (){
			console.log("点击图像")
		}
		*/
		//点击图片时放大显示图片
		// $scope.changePic = function ($event) {
		// 	var img = $event.srcElement || $event.target;
		// 	angular.element("#bigimage")[0].src = img.src;
		// 	console.log("点击" + angular.element("#bigimage")[0]);
		// 	angular.element("#js-imgview")[0].style.display = "block";
		// 	angular.element("#js-imgview-mask")[0].style.display = "block";
		// }
		// //点击图片时放小显示图片
		// $scope.closePic = function () {
		// 	angular.element("#js-imgview")[0].style.display = "none";
		// 	angular.element("#js-imgview-mask")[0].style.display = "none";

		// } src="{{loadingiphone}}"

		$scope.toEdit = function () {
			$state.go('electronicDirectory.productUpdate', { id: $scope.productId, type: $scope.type });
		};
		$scope.backClickEvent = function () {
			switch ($scope.type) {
				case '1':
					$state.go('electronicDirectory.productManager', { supplierCode: '' });
					break;
				case '2':
					$state.go('electronicDirectory.productView');
					break;
				case '3':
					$state.go('electronicDirectory.productMaintenance');
					break;
			}
		};

		$scope.loadingiphone = function () {
			productService.getLoadingSrc({ 'id': $scope.productId }, function (res) {
				$scope.loadingiphone = res.data;
				var iframe = angular.element("#iframe")[0];
				iframe.src = res.data;
			})
		}

		$scope.sizeChange = val => {
			$scope.params.pageSize = val
			$scope.sub(1)
		}

		$scope.$watch('page', (newValue,oldValue) =>{
			let rex = new RegExp(/^[0-9]*$/)
			if(!rex.test(newValue)) {
				$scope.page = oldValue
				$scope.params.pageNum = oldValue
			} else {
				if(newValue == '') return
				$scope.page = newValue
				$scope.params.pageNum = newValue
			}
		})

		$scope.blur = () => {
			$scope.getProductComplainDetails()
		}

		// 获取到投诉和建议的数据
		$scope.getProductComplainDetails = function () {
			productService.getProductComplainDetails($scope.params, function (res) {
				res.data.list.forEach(function (e) {
					let imagesList = []
					e.duration = {
						minute: '00',
						second: '00'
					}
					e.imgList.forEach(i => {
						imagesList.push({
							active: false,
							url: i
						})
					})
					e.imagesList = imagesList
				})
				$scope.total = res.data.total
				$scope.lastPage = res.data.pages
				$scope.complainDetails = res.data.list
				$scope.pages = setPage($scope.total, $scope.pageSize, $scope.pageNum, $scope.firstPage)
			})
		}

		$scope.$on('ngRepeatFinished', function () {
			setTimeout(function () {
				setTime()
			}, 1000)
		})

		function setTime() {
			$scope.complainDetails.forEach(function (e, index) {
                if (angular.element("#v-" + index)[0]) {
                    e.duration = {
                        minute: parseInt((angular.element("#v-" + index)[0].duration % 3600) / 60) < 10 ? '0' + parseInt((angular.element("#v-" + index)[0].duration % 3600) / 60) : parseInt((angular.element("#v-" + index)[0].duration % 3600) / 60),
                        second: Math.ceil(angular.element("#v-" + index)[0].duration % 60) < 10 ? '0' + Math.ceil(angular.element("#v-" + index)[0].duration % 60) : Math.ceil(angular.element("#v-" + index)[0].duration % 60)
                    }
                }
			})
			$scope.$apply()
		}

		// 获取到产品的数据
        $scope.getProductStatistics = function () {
            var obj = {
                productId:$scope.productId,
                source:$scope.type,
                type:$scope.params.type
            };
            productService.getProductStatistics(obj, function (res) {
                if (res.returnCode == 0) {
                    $scope.productInfo = res.data.productInfo;
                    $scope.supplier = res.data.supplier;
                    $scope.productStatistics = res.data.productStatistics;
                    $scope.industryStatistics = res.data.industryStatistics;
                    $scope.tagsList.forEach(function (i) {
                        res.data.tagCountList.forEach(function (e) {
                            if (e.tagCode == i.tagCode){
                                i.tagCount = e.totalCount;
                            }
                        })
                    })
                }
            });
        };

        function setPage(length, amount, num, first) {
            let pages = []
            let page = Math.ceil(length / amount)
            if (page <= num) {
                for (var i = 1; i <= page; i++) {
                    pages.push(i)
                }
            }
            if (page > num) {
                for (var i = first; i < first + num; i++) {
                    pages.push(i)
                }
            }
            return pages
        }

        $scope.sub = function (page) {
            $scope.firstPage = page >= $scope.pageNum ? page - Math.floor($scope.pageNum / 2) : 1
            if (($scope.lastPage - $scope.pageNum) >= 0) {
                if ($scope.firstPage > $scope.lastPage - $scope.pageNum) {
                    $scope.firstPage = $scope.lastPage - $scope.pageNum + 1
                }
            }
            $scope.page = page
            $scope.params.pageNum = page
            $scope.getProductComplainDetails()
        }

		// 获取到所有标签数据的数据
		$scope.getTagsAll = function () {
			productService.getTagsAll(function (res) {
				$scope.tagsList = res.data
			})
		}

		$scope.showMedia = (el, ind, index) =>{
            $scope.playBox = true;
			if(el == 'video') {
                $scope.showMedia_video = true;
                $scope.complainDetails[index].video_active = true;
                $scope.media_video = $scope.complainDetails[index].videoUrl;
			} else {
                $scope.media_img = $scope.complainDetails[index].imgList[ind];
                $scope.complainDetails[index].imagesList[ind].active = true;
                $scope.showMedia_img = true
			}
		}

        $scope.hidePlayBox = () => {
            $scope.playBox = false;
            $scope.showMedia_video = false;
            $scope.showMedia_img = false;
            $scope.media_img = '';
            $scope.media_video = '';
            $scope.complainDetails.forEach(function (e) {
            	e.video_active = false;
                e.imagesList.forEach(function (i) {
                    i.active = false;
                })
            });
        }

		$scope.initComplainSuggest = function () {
            $scope.getTagsAll()
            $scope.getProductStatistics();
            $scope.getProductComplainDetails();
        }

		$scope.toComplainSuggest = function(){
			$state.go('electronicDirectory.addComplainSuggest',{'productId':$scope.productId});//2表示产品查看页
		};

		$scope.init = function () {
			$scope.getProductById();
			$scope.loadingiphone();
			$scope.qrImage = 'setmining/productInfo/qrcode?id=' + $scope.productId;
            $scope.initComplainSuggest();
		};
		$scope.init();
	}

	angular.module('electronicDirectory', ['ngSanitize'])
		.controller('productExamineDetailCtrl', productExamineDetailCtrl);
})(angular);