ajax+php实现观看记录

来源:互联网 发布:淘宝营销案例 编辑:程序博客网 时间:2024/05/17 07:33

mh_jilu.php

<?php
require('../class/connect.php');
require('../class/functions.php');

$action = isset($_GET['action'])?$_GET['action']:'';
$uid = intval(getcvar('mluserid'));
$cookie = isset($_GET['cookie'])?$_GET['cookie']:'';
$mhid = isset($_GET['mhid'])?$_GET['mhid']:'';
$partid = isset($_GET['partid'])?$_GET['partid']:'';
$id = isset($_GET['id'])?intval($_GET['id']):0;

switch ($action) {
    case 'record':
        record($uid,$cookie,$mhid,$partid);
        break;

    case 'list':
        getlist($uid,$cookie);
        break;

    case 'delete':
        remove($id);
        break;
    
    default:
        die('access error');
        break;
}

function record($uid,$cookie,$mhid,$partid){
    global $dbtbpre;
    $link=db_connect();

    $sql = "INSERT INTO `{$dbtbpre}ecms_manhua_jilu`(`id`,`uid`, `mhid`, `partid`, `time`, `cookie`) VALUES ('0','$uid', '$mhid', '$partid', '{$_SERVER['REQUEST_TIME']}', '$cookie') ";
    $rst = mysql_query($sql)?true:false;
    die(json_encode(array('ret'=>$rst?'0':'101')));
}

function getlist($uid,$cookie,$count=10,$page=1){
    global $dbtbpre;
    $link=db_connect();

    $cookie_where = $uid?'':"AND `cookie`='$cookie'";
    $sql = "SELECT * FROM `{$dbtbpre}ecms_manhua_jilu` WHERE `uid`='$uid' $cookie_where LIMIT ".($page-1)*$count.",$count";
    $rst = mysql_query($sql);
    $result = array();
    while ($rst && $row = mysql_fetch_assoc($rst)) {
        list($row['mhname'],$row['mhurl']) = mysql_fetch_array(mysql_query("SELECT `title`,`titleurl` FROM `{$dbtbpre}ecms_manhua` WHERE `id`='{$row['mhid']}' "));
        list($row['partname'],$row['parturl']) = mysql_fetch_array(mysql_query("SELECT `title`,`partURL` FROM `{$dbtbpre}ecms_manhua_part` WHERE `id`='{$row['partid']}' "));
        $result[] = $row;
    }
    $show = json_encode($result);
    die($show);
}

function remove($id){
    global $dbtbpre;
    $link=db_connect();

    $sql = "DELETE FROM `{$dbtbpre}ecms_manhua_jilu` WHERE `id`='$id' ";
    $rst = mysql_query($sql)?true:false;
    die(json_encode(array('ret'=>$rst?'0':'101')));
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

html代码,作参考

<div class="history" js-hook="history">
    <a class="history-key" href="javascript:;" js-hook="history-key">
        <em class="word">观看记录</em>
        <i class="icon-triangle"></i>
    </a>
    <div class="history-popup">
        <div class="none" js-hook="history-none">您最近没有阅读过漫画作品</div>
        <ul class="history-list" js-hook="history-list"></ul>
        <div class="popup-bottom">
            <div class="offline" js-hook="offline" style="display: block;">
                <a href="http://hao123manhua.92demo.com/e/member/login/" class="btn-login" data-hook="login">登录</a>
                <span class="offline-info">可以查看更多历史纪录</span>
            </div>
            <div class="online" js-hook="online">
                <a href="javascript:;" class="clean" js-hook="history-clean">清空</a>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="/assets/e88cd16a/jquery.js"></script>
<script type="text/javascript" src="/assets/e88cd16a/md5.js"></script>
<script type="text/javascript">
$(function(){
    function getCookie(c_name){
        if (document.cookie.length>0){
            c_start=document.cookie.indexOf(c_name + "=")
            if (c_start!=-1){
                c_start=c_start + c_name.length+1
                c_end=document.cookie.indexOf(";",c_start)
                if (c_end==-1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end))
            }
        }
        return ""
    }
    function setCookie(c_name,value,expiredays){
        var exdate=new Date()
        exdate.setDate(exdate.getDate()+expiredays)
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
    }
    var mh_jilu_tmpusr = getCookie('mh_jilu_tmpusr');
    mh_jilu_tmpusr = mh_jilu_tmpusr?mh_jilu_tmpusr:hex_md5(Date.parse(new Date())+Math.random()).substring(0,16);
    setCookie('mh_jilu_tmpusr',mh_jilu_tmpusr);
    $(".header .history .history-key").on('mouseenter',function(){

        $.ajax({
            url:'/e/action/mh_jilu.php',
            data:{
                action: 'list',
                cookie: mh_jilu_tmpusr
            },
            success: function(r){
                var o = eval("("+r+")");
                console.log(o);
                o?$(".header .history .history-popup .none").hide():$(".header .history .history-popup .none").show();
                o?$(".header .history .history-popup .history-list").show():$(".header .history .history-popup .history-list").hide();
                $(".header .history .history-popup .history-list").html();
                var html = '';
                for (var i = o.length - 1; i >= 0; i--) {
                    html+='<li class="item"><a href="/html/manhua/'+o[i].mhid+'.html" class="title" target="_blank" monkey="inter">'+o[i].mhname+'</a><a href="'+o[i].parturl+'" class="episode" target="_blank" monkey="exter">'+o[i].partname+'</a><a href="javascript:;" class="rm" js-hook="history-rm" data-id="'+o[i].id+'"></a></li>';
                }
                $(".header .history .history-popup .history-list").html(html);
                $(".header .history .history-popup .history-list li a.rm").on('click',function(){
                    var id = $(this).attr('data-id');
                    var self = this;
                    $.ajax({
                        url:'/e/action/mh_jilu.php',
                        data:{
                            action: 'delete',
                            cookie: mh_jilu_tmpusr,
                            id: id
                        },
                        success: function(r){
                            $(self).parent().remove();
                        }
                    });
                });
            }
        });
        $(".header .history").addClass("history-hover");
    });
    $(".header .history").on('mouseleave',function(){
        $(".header .history").removeClass("history-hover");
    });

    $("#bd .chapter-wrap a.item").on("click",function(){
        var mhid = [!--id--];
        var partid= $(this).attr("data-id");
        $.ajax({
            url:'/e/action/mh_jilu.php',
            data:{
                action: 'record',
                cookie: mh_jilu_tmpusr,
                mhid: mhid,
                partid:partid
            },
            success: function(r){
                $(self).parent().remove();
            }
        });
        location.href=$(this).attr("data-href");
    });
});
</script>