evalute.js 8.4 KB
var pic = '';
var pic1 = '';
var pic2 = '';
var oid = GetQueryString('oid');
console.log(oid);
var imgurl = [];
//图片1
function previewImage(file) {
	var MAXWIDTH = 50;
	var MAXHEIGHT = 50;
	var div = document.getElementById('preview');
	if(file.files && file.files[0]) {
		div.innerHTML = '<img id=imghead onclick=$("#previewImg").click()>';
		var img = document.getElementById('imghead');
		img.onload = function() {
			var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
			img.width = rect.width;
			img.height = rect.height;
			//                 img.style.marginLeft = rect.left+'px';
			img.style.marginTop = rect.top + 'px';
		}
		var reader = new FileReader();
		reader.onload = function(evt) {
			img.src = evt.target.result;
		}
		reader.readAsDataURL(file.files[0]);
	} else //兼容IE
	{
		var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
		file.select();
		var src = document.selection.createRange().text;
		div.innerHTML = '<img id=imghead>';
		var img = document.getElementById('imghead');
		img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
		var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
		status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
		div.innerHTML = "<div id=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;" + sFilter + src + "\"'></div>";
	}
	var formData = new FormData();
	var fileM = document.querySelector("#previewImg");
	var fileObj = fileM.files[0];
	formData.append("files", fileObj);
	$.ajax({
		url: 'http://hula.wx.bronet.cn/index.php/MyComment/upload',
		type: 'POST',
		data: formData,
		async: false,
		cache: false,
		contentType: false,
		processData: false,
		success: function(res) {
			console.log(res);
			pic = res.data;
			console.log(pic);
			imgurl.push(pic);
			console.log(imgurl)
			var text = '';
			text += '<div class="evaluate_pic_item" style="float:left">' +
				'<img src="' + pic + '" class="delimg"/>' +
				'<div class="icon">' +
				'<img src="../img/10拷贝2@2x_59.png" />' +
				'</div>' +
				'</div>'
			$(".addimg").append(text);
			if(imgurl.length == 3) {
			$(".imgone").css("display", "none");
			}
            $("#imghead").attr("src","../img/3@2x.png");
		},
		error: function() {

		}
	});
}
//图片2
//function previewImage1(file) {
//  var MAXWIDTH = 60;
//  var MAXHEIGHT = 60;
//  var div = document.getElementById('preview1');
//  if(file.files && file.files[0]) {
//      div.innerHTML = '<img id=imghead1 onclick=$("#previewImg1").click()>';
//      var img = document.getElementById('imghead1');
//      img.onload = function() {
//          var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
//          img.width = rect.width;
//          img.height = rect.height;
//          //                 img.style.marginLeft = rect.left+'px';
//          img.style.marginTop = rect.top + 'px';
//      }
//      var reader = new FileReader();
//      reader.onload = function(evt) {
//          img.src = evt.target.result;
//      }
//      reader.readAsDataURL(file.files[0]);
//  } else //兼容IE
//  {
//      var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
//      file.select();
//      var src = document.selection.createRange().text;
//      div.innerHTML = '<img id=imghead>';
//      var img = document.getElementById('imghead');
//      img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
//      var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
//      status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
//      div.innerHTML = "<div id=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;" + sFilter + src + "\"'></div>";
//  }
//  var formData = new FormData();
//  var fileM=document.querySelector("#previewImg");
//  var fileObj=fileM.files[0];
//
//  formData.append("files",fileObj);
//  
//  $.ajax({
//      url: 'http://hula.wx.bronet.cn/index.php/MyComment/upload',
//      type: 'POST',
//      data: formData,
//      async: false,
//      cache: false,
//      contentType: false,
//      processData: false,
//      success: function(res) {
//       console.log(res);
//       pic1=res.data;
//       console.log(pic1);
//        $(".pic1").val(pic1);
//        imgurl.push(pic1);
//
//      },
//      error: function() {
//
//      }
//  });
//}
//
////图片3
//function previewImage2(file) {
//  var MAXWIDTH = 60;
//  var MAXHEIGHT = 60;
//  var div = document.getElementById('preview2');
//  if(file.files && file.files[0]) {
//      div.innerHTML = '<img id=imghead2 onclick=$("#previewImg2").click()>';
//      var img = document.getElementById('imghead2');
//      img.onload = function() {
//          var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
//          img.width = rect.width;
//          img.height = rect.height;
//          //                 img.style.marginLeft = rect.left+'px';
//          img.style.marginTop = rect.top + 'px';
//      }
//      var reader = new FileReader();
//      reader.onload = function(evt) {
//          img.src = evt.target.result;
//      }
//      reader.readAsDataURL(file.files[0]);
//  } else //兼容IE
//  {
//      var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
//      file.select();
//      var src = document.selection.createRange().text;
//      div.innerHTML = '<img id=imghead>';
//      var img = document.getElementById('imghead');
//      img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
//      var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
//      status = ('rect:' + rect.top + ',' + rect.left + ',' + rect.width + ',' + rect.height);
//      div.innerHTML = "<div id=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;" + sFilter + src + "\"'></div>";
//  }
//  var formData = new FormData();
//  var fileM=document.querySelector("#previewImg");
//  var fileObj=fileM.files[0];
//
//  formData.append("files",fileObj);
//  
//  $.ajax({
//      url: 'http://hula.wx.bronet.cn/index.php/MyComment/upload',
//      type: 'POST',
//      data: formData,
//      async: false,
//      cache: false,
//      contentType: false,
//      processData: false,
//      success: function(res) {
//       console.log(res);
//      pic2=res.data;
//      console.log(pic2);
//       $(".pic2").val(pic2);
//       imgurl.push(pic1);
//      },
//      error: function() {
//
//      }
//  });
//}

function clacImgZoomParam(maxWidth, maxHeight, width, height) {
	var param = {
		top: 0,
		left: 0,
		width: width,
		height: height
	};
	if(width > maxWidth || height > maxHeight) {
		rateWidth = width / maxWidth;
		rateHeight = height / maxHeight;

		if(rateWidth > rateHeight) {
			param.width = maxWidth;
			param.height = Math.round(height / rateWidth);
		} else {
			param.width = Math.round(width / rateHeight);
			param.height = maxHeight;
		}
	}
	param.left = Math.round((maxWidth - param.width) / 2);
	param.top = Math.round((maxHeight - param.height) / 2);
	return param;
}

//删除图片
$(".addimg").on("click",".icon",function(){
	var url=$(this).siblings(".delimg").attr("src");
	for(var i=0;i<imgurl.length;i++){
		if(url==imgurl[i]){
			imgurl.splice(i, 1);
			console.log(imgurl)
		}
	}
	$(this).parent().remove();
	if(imgurl.length==0){
	console.log($(".imgone"))
	$(".imgone").css("display","block");	
	$("#imghead").attr("src","../img/3@2x.png");
	$("#imghead").css("width","50px");
	$("#imghead").css("height","50px");
	}
})

//提交按钮
$(".footer").click(function() {
	var comment_content = $(".evalutecontent").val();
	if(comment_content == '') {
		alert("请填写评价内容");
		return false;
	}
	//if(imgurl == '') {
	//	alert("请选择图片");
	//	return false;
	//}

	console.log(imgurl.join(','));
//	var img = JSON.stringify(imgurl);
	var url = '/MyComment/comment'
	var params = {
		oid: oid,
		unique_id: localStorage.getItem('unique_id'),
		comment_content: comment_content,
		comment_photo: imgurl.join(',')
	}

	ajaxsend(url, params, function(res) {
		/*console.log(JSON.stringify(res))*/
		console.log(res);
		if(res.status == true) {
			alert("评论成功");
			window.location.reload();
		} else {
			alert(res.msg);
		}
	})
})