需求说明
如果有用户注册会员或发布信息,管理员在后台能即时收到消息提醒,即不刷新页面,在后台后下方即时弹出用户动态信息
功能说明
用户在前台页面注册成功后,便生成缓存信息 在push_tips.js.php脚本文件中去获取缓存,如果缓存存在,则弹出缓存中的信息,同时删除缓存 在后台页面公共底部,通过js定时执行push_tips.js.php脚本文件
1、生成待推送消息缓存
function push_tips($code, $regid, $param = array(), $url = ''){
global $dc, $DT_TIME, $DT;
//判断后台是否开启推送功能
if(!$DT['pushtips_time']) return 1;
$code = trim($code);
$regid *= 1;
if(empty($code) || $regid<1) return 2;
$node_info = cache_read("node_$code.php");
if(empty($node_info)) return 3;
if(!$node_info['expire']) return 4; //判断该节点是否开启
if(!isset($node_info['group_'.$regid])) return 5;
$desc = format_tips($node_info['template'], $param);
$tips = array(
'desc' => $desc,
'addtime' => $DT_TIME,
'url' => $url,
);
$admins = get_admins_by_roles($node_info['group_'.$regid]);
$cache_key = 'node_'.$code;
$expire = $node_info['expire'] * 3600;
$fail_num = 0;
foreach ($admins as $v) {
$key = $cache_key.'_'.$v;
if ($temp_tips = $dc->get($key)) {
$tips['num'] = $temp_tips['num'] + 1;
} else {
$tips['num'] = 1;
}
if(!$dc->set($key, $tips, $expire)){
$fail_num++;
}
}
if($fail_num){
return "生成缓存失败{$fail_num}次";
}
return 'ok';
}
2、查询缓存php脚本
require '../common.inc.php';
//获取管理员ID
$secretkey = 'admin_'.strtolower(substr(DT_KEY, -6));
if($CFG['authadmin'] == 'cookie') {
$_destoon_admin = get_cookie($secretkey);
$_destoon_admin = $_destoon_admin ? intval($_destoon_admin) : 0;
} else {
$session = new dsession();
$_destoon_admin = isset($_SESSION[$secretkey]) ? intval($_SESSION[$secretkey]) : 0;
}
$userid = $_destoon_admin;
if($userid==0) return false;
$node_arr = cache_read('node_codes.php');
$html_str = $js_str = '';
foreach($node_arr as $v){
$key = "node_{$v}_{$userid}";
if($tips = $dc->get($key)){
$dc->rm($key); //清除缓存
$desc = $tips['desc'];
if($tips['num']>1){
$desc .= "<br>累计出现此类新消息{$tips['num']}条";
}
if(isset($tips['url']) && !empty($tips['url'])) {
$desc = '<a href="'.$tips['url'].'">'.$desc.'</a>';
}
$html_str .= '<div class="tip_box" id="'.$key.'"><div class="tip_tit"><h3>提示信息</h3><span class="close">×</span></div><div class="tip_con" id="tip_con">'.$desc.'</div></div>';
break; //每次只弹出一条消息
}
}
if($html_str){
//$html_str = str_replace(PHP_EOL, '', $html_str); //去掉换行符
$js_str = <<<JQUERY
$('#tip_p_box .tip_box').hide(500);
$('#tip_p_box').html('$html_str');
$('#tip_p_box .tip_box').show(1000);
JQUERY;
echo $js_str;
}
3、在前端页面写js定时执行php脚本代码
$("#tip_p_box").on('click','.close',function(){
$(this).parents('.tip_box').hide(500);
})
function pushTips(){
$('#aoma_tips').remove();
s = document.createElement("script");
s.type = "text/javascript";
s.id = "aoma_tips";
s.src = DTPath+"api/push_tips.js.php?refresh="+Math.random()+".js";
document.body.appendChild(s);
}
pushTips();
window.setInterval('pushTips()',<?php echo $DT['pushtips_time']; ?>*1000);