jquery+php+ajax异步提交,实现留言评论点赞

代码没有很好整理,不过功能基本实现了

在这里插入图片描述

html部分:

		<div class="main" id="allMessage">
        <foreach name="resComms" item="vo">
            <div class="reply Reply_1 clear" id="comms" data-id="{$vo.id}" data-fromuid="{$vo.fromuid}">
                <div class="clear" style=" margin:0">
                    
                    <a href="__APP__/Video/User/index?uid={$vo.fromuid}" target="_blank" id="fromNmae"><img src="/{$vo['fromPortrait']}" target="_blank" width="30px" height="30px" style="display: inline;border-radius:50%;">{$vo['fromInfo']}</a>
                    <span style=" color:red"> 回复 </span><a href="__APP__/Video/User/index?uid={$vo.touid}" target="_blank"> <img src="/{$vo['toPortrait']}" target="_blank" width="30px" height="30px" style="display: inline;border-radius:50%;">{$vo['toInfo']}</a>
                    :{$vo.content}
                </div>
                <div class="clear" style=" margin-top:10px;">
                    <span class="count" onclick="newReply(this)" style="line-height:16px; font-size:12px;">回复</span>
                    <span class="ri clear" style=" margin:0 10px;" onclick="dianzan(this)" value="{$vo.id}">
                        <i class="le" style="width:16px; height:16px; display:block;float: left;"><img src="__PUBLIC__/videos/images/dz1.png" width="100%" height="100%"></i>
                        <b id="commNums" class="lr" value="{$voo.likes}" style="line-height:16px; font-size:12px;float: left;">{$vo.likes}</b>
                    </span>
                    <p class="le" style=" margin:0; font-size:12px; float:left"><?php echo(date("Y-m-d",$vo['cdate'])); ?></p>
                </div>
            </div>
            <!--
            <div id="comms" style="background:#cccccc;border-radius: 5px;" data-id="{$vo.id}" data-fromuid="{$vo.fromuid}">-->
                <foreach name="vo['newchild']" item="voo">
                    <div class="reply Reply_2 clear" data-id="{$vo.id}" id="comms" data-fromuid="{$voo.fromuid}">
                		<div class="clear" style=" margin:0">
                        <a href="__APP__/Video/User/index?uid={$voo.fromuid}" target="_blank" id="fromNmae"><img src="/{$voo['fromInfo'][0]['portrait']}" target="_blank" width="30px" height="30px" style="display: inline;border-radius:50%;">{$voo['fromInfo'][0]['username']}</a>
                        <span style=" color:red"> 回复 </span><a href="__APP__/Video/User/index?uid={$voo.touid}" target="_blank"><img src="/{$voo['toInfo'][0]['portrait']}" target="_blank" width="30px" height="30px" style="display: inline;border-radius:50%;"> {$voo['toInfo'][0]['username']}</a>
                        :{$voo.content}
                    </div>
                	<div class="clear" style=" margin-top:10px;">
                        <span class="count" onclick="newReply(this)" style="line-height:16px; font-size:12px;">回复</span>
                        <span class=" ri clear" style=" margin:0 10px;" onclick="dianzan(this)" value="{$voo.id}">
                            <i class="le" style="width:16px; height:16px; display:block;float: left;"><img src="__PUBLIC__/videos/images/dz1.png" width="100%" height="100%"></i>
                            <b id="commNums" value="{$voo.likes}" style="line-height:16px; font-size:12px;float: left;">{$voo.likes}</b>
                        </span>
                        <p class="le " style=" margin:0; font-size:12px; float:left"><?php echo(date("Y-m-d",$voo['cdate'])); ?></p>
                    </div>
                    </div>
                </foreach>
            <!--</div>-->
        </foreach>
    </div>
</div>

js部分

fromUid = <?php $fUid = $userInfo[0]['id'] != "" ? $userInfo[0]['id'] : 0; echo $fUid ?>;//回复人id为空
    Vid = $("input[name='videoid']").val();
    function submit(){
        var content = $("#content").val();
        var toUid = $("input[name='videouid']").val();
        if(content == ""){
            alert("评论内容不能为空!"); return false;
        }
        if(fromUid == 0){alert("请登录后评论!"); return false;}
        
        $.ajax({
               type: "post",
                url: "{:U('Index/subComments')}",
               data: {"fromUid":fromUid, "toUid":toUid, "videoId":Vid, "content":content},
            success: function(data){
                    console.log(data);
                    
                    var newMessage = 
                        '<div class="reply Reply_1 clear" id="comms" data-id="'+data.comId+'" data-fromUid="'+data.fromUid+'">'+
                        '<span style="color:red;" id="fromNmae">{$userInfo[0]['username']}'+
                        ' 回复 {$res[0]['username']}</span>'+content+
                        '<span class="count" οnclick="newReply(this)">回复</span>'+
                        '</div>';
                    $('#allMessage').append(newMessage);
            }
        })
    
    }

    function newReply(obj){
        $("#replyTextarea").remove();
        var thisObj = $(obj).parents("#comms");
        var comId = thisObj.attr("data-id");
        var replayUid = thisObj.attr("data-fromuid");//被回复人id
        var fromName = thisObj.find("#fromNmae").html();
        var textarea = '<div id="replyTextarea" data-comId='+comId+' data-replayUid='+replayUid+'>'+
        '<textarea id="content" rows="4" cols="50" placeholder="回复'+fromName+'">'+
        '</textarea><span οnclick="cancel(this)">取消</span>'+
        '<span οnclick="reply(this)">回复</span>'+
        '</div>';
        thisObj.append(textarea);
        // console.log($(this).parents("#comms").attr("data-id"));
        // $(this).parents("#comms").attr("data-id");
    }
    function cancel(obj){
        $(obj).parents("#replyTextarea").remove();
    }
    //回复评论
    function reply(obj){
        var thisObj = $(obj).parents("#replyTextarea");
        var thisComms = $(obj).parents("#comms");
        var comId = thisObj.attr("data-comId");//一级评论id
        var replayUid = thisObj.attr("data-replayUid");
        var content = thisObj.children('#content').val();
        console.log(comId);
        console.log(replayUid);
        if(content == ""){alert("内容不能为空!");return false;}
        if(fromUid == 0){alert("请登录后评论!"); return false;}
        $.ajax({
               type: "post",
                url: "{:U('Index/subComments')}",
               data: {"fromUid":fromUid, "toUid":replayUid, "videoId":Vid, "content":content, "comId":comId},
            success: function(data){
                    console.log(data);
                    var newMessage =
                    '<div class="reply Reply_2 clear" id="comms" data-id="'+comId+'" data-fromUid="'+data.fromUid+'">'+
					'<span style="color:red;" id="fromNmae">{$userInfo[0]['username']} 回复 {$res[0]['username']}</span>'
                    +content+
                    '<span class="count" οnclick="newReply(this)">回复</span>'+
                    '</div>';
                    thisComms.after(newMessage);
                    $(obj).parents("#replyTextarea").remove();
            }
        })
    }

        function myFunction(ele) {
            var hour = parseInt((ele.duration)/3600);
            var minute = parseInt((ele.duration%3600)/60);
            var second = Math.ceil(ele.duration%60);
            // console.log(hour);
            if(hour == 0){
                hour = "";
            }else{
                hour = hour+"小时";
            }
            document.getElementById("times").innerHTML= +minute+"分"+second+"秒";
        }  
        //视频点赞
        $(document).on('click','#numsAction',function(){
        
        var id = $(this).attr("data-id");
        var fh=$(this);
        var newNums = Number($(this).parents(".like").find('#nums').html()) + 1;
        $.ajax({
             type: "post",
             url: "{:U('Index/thumbsUp')}",
             data: {'id':id,'ups':newNums,"type":"videoNums"},
             success: function(data){
                console.log(data);
                if(data.info == 1){
                   fh.parents(".like").find('#nums').html(newNums);
                   fh.parents(".like").find('.love').attr('src',"__PUBLIC__/videos/images/XX.png");
                }else{
                    alert(data.data); return false;
                }
            }
         });
         
        })
        //评论点赞
        function dianzan(obj){
            var newNums = Number($(obj).find("#commNums").html()) + 1;
            var coId = $(obj).attr("value");
            var data = {"coId":coId, "commNums":newNums, "type":"commNums"}
            $.post("__URL__/thumbsUp",data, function(data){
                console.log(data);
                if(data.info == 1){
                   $(obj).find("#commNums").html(newNums);
                   // fh.parents(".like").find('.love').attr('src',"__PUBLIC__/videos/images/XX.png");
                }else{
                    alert(data.data); return false;
                }
            })
        }
        //控制广告图片的显示和隐藏
        // $('.skip').hide();
        // var myVideo=document.getElementById("media");
        // myVideo.addEventListener('play',function(){
        //   $('.skip').hide();   
        // });
        // myVideo.addEventListener('pause',function(){
        //   $('.skip').show();
        // })
    
        var video = document.getElementById("player");  
        var res={:json_encode($res)};
        var type = res[0]['video'].substring(0,1)
        if(type != 'u'){
            dirs = res[0]['video'];
            video.src = dirs;
        }
        dirs = '/'+res[0]['video']
        //加密blob
        window.URL = window.URL || window.webkitURL; 
        var xhr = new XMLHttpRequest();
        xhr.open("GET", dirs, true);
        xhr.responseType = "blob"; 
        xhr.onload = function() { 
            if (this.status == 200) { 
                var blob = this.response; 
                video.onload = function(e) { 
                    window.URL.revokeObjectURL(video.src); 
                };  
                video.src = window.URL.createObjectURL(blob); 
            } 
        } 
            xhr.send();

<!--发布评论-->
$(document).on('click','.contentIN',function(){
	//alert(1)PL_fppl
	$(this).css({
  "height":"120px",
  "line-height":"30px",
  "width":"100%"
  });
	$('.PL').css('height','170px')
	$('.PL_fppl').css({
  "height":"50px",
  "width":"100%",
  "margin-top":"-2px"
  });
})

php部分(tp3.2)

	//视频页面
	public function video(){

		$id = $_GET['id'];
		if(!empty($id)){
			$res = M()->query('select * from js_video where id = '.$id.' and display = "1" and isdelete = "0"');
			if(!$res){
				echo "<script language=javascript>alert ('该资源已屏蔽或不存在!');</script>";
				echo '<script language=javascript>window.history.go(-1)</script>';
			}else{
				M('video')->where('id='.$id.'')->setInc('score',rand(1,3));
			}
		}else{
			echo "<script language=javascript>alert ('访问错误!');</script>";
			echo '<script language=javascript>window.history.go(-1)</script>';
		}
		
		if(!empty($res[0]['videouid'])){
			$resUser = M()->query('select id, username, portrait from js_video_user where id = '.$res[0]['videouid'].'');
		}
		$res[0]['username'] = $resUser[0]['username'];
		$res[0]['portrait'] = $resUser[0]['portrait'];
		
		$resComms = $this->getComments($id, $where = "child = 0"); 
		
		foreach($resComms as $key=>&$value){
			$fromuser = $this->getUserInfo($value['fromuid']);
			$touser = $this->getUserInfo($value['touid']);
			$value['fromInfo'] = $fromuser[0]['username'];
			$value['fromPortrait'] = $fromuser[0]['portrait'];
			$value['toPortrait'] = $touser[0]['portrait'];
			$value['toInfo'] = $touser[0]['username'];
			$childComms = $this->getComments($id, $where = "child != 0");
			foreach ($childComms as $k => &$v) {
				$fromuser = $this->getUserInfo($v['fromuid']);
				$touser = $this->getUserInfo($v['touid']);
				
				$v['fromInfo'] = $fromuser;
				$v['toInfo'] = $touser;
				if($value['id'] == $v["child"]){
					$value["newchild"][] = $v;
				}
			}
		}
		// dump($resComms);
		$this->userInfo = $this->userInfo();
		$this->res=$res;
		$this->assign('resComms', $resComms);
		$this->assign('videoInfo',$res);
		$this->display();
	}
	//获取videouser
	public function getUserInfo($uid){
		$res = M()->query('select username, portrait from js_video_user where id = '.$uid.'');
		return $res;
	}
	public function userInfo(){
		if(!empty($_SESSION['videoUid'])){
			$userInfo = M()->query('select id, username, portrait from js_video_user where id = "'.$_SESSION['videoUid'].'"');
		}
		if(!empty($userInfo)) return $userInfo;
	}
//评论or视频点赞
	public function thumbsUp(){
		
		$type = I('type');
		$vid = I('id');
		$ups = I('ups');
		$commNums = I('commNums');
		$ip = $_SERVER['REMOTE_ADDR'];

		if($type == "commNums"){
			$coId = I('coId');
			$Contrast = M()->query('select * from js_videoups where ip = "'.$ip.'" and commid = '.$coId.'');
			if(empty($Contrast)){
				$resUps = M()->execute('INSERT INTO js_videoups (commid,ip,time,type) VALUES ("'.$coId.'","'.$ip.'","'.time().'",2)');
				$resComms = M()->execute('UPDATE js_video_comment SET likes = '.$commNums.' WHERE id = '.$coId.'');
				$this->ajaxReturn(array('info'=>1,'data'=>"success"));
			}else{
				$this->ajaxReturn(array('info'=>0,'data'=>"你已经点过赞了!"));
			}
		}else{
			$Contrast = M()->query('select * from js_videoups where ip = "'.$ip.'" and vid = '.$vid.'');
			if(empty($Contrast)){
				$resVideoups = M()->execute('INSERT INTO js_videoups (vid,ip,time,type) VALUES ("'.$vid.'","'.$ip.'","'.time().'",1)');
				$resVideo = M()->execute('UPDATE js_video SET ups = '.$ups.' WHERE id = '.$vid.'');
				$this->ajaxReturn(array('info'=>1,'data'=>"success"));
			}else{
				$this->ajaxReturn(array('info'=>0,'data'=>"你已经点过赞了!"));
			}
		}
		
		
	}
	//提交评论
	public function subComments(){
		if(empty($_SESSION['videoUid'])){
			echo "<script>alert('请登录');</script>";
			echo '<script>window.location.href="/jinsuo/index.php/Video/Signin/index"</script>';
		}
		$res = D('Videocomment')->subComments(I());
		$this->ajaxReturn($res);
		
	}

	public function getComments($id, $where){
		$resComms = M()->query('select * from js_video_comment where videoid = '.$id.' and '.$where.' order by likes desc');
		return $resComms;
	}

mysql部分

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值