Typecho显示访客用户身份及用户等级

2023-05-20T11:28:40-20230520072841.png

说明
用户身份:博主、基友、博友。(基友是直接配置,博友是友链抓取)
除以上三种身份以外,由评论数量作为等级划分依据。
除博主和基友不显示评论数量以外,其他访客均显示评论量。名称指代为:目的地的前进步数。
鼠标经过后,会显示目的地前进步数。如果是友链博友的话,还会一并显示友链描述信息。
留言1条:您已经向目的地迈出了第一步。
留言2条+:您已经向目的地前进了XX步。
也算是正好和网站名称所契合吧!

functions.php
纯手打啊,我是纯按自己想法来的,各位按自己的主题风格改写。基友邮箱请按格式自行添加。友链数据是从数据库likns表中获取的。如果你是别的友链方式,那就按别的方式来。举一反三,举二反六。实在不会,再留言吧!

/**    
 * 评论者认证等级 + 身份    
 *    
 * @author Chrison    
 * @access public    
 * @param str $email 评论者邮址    
 * @return result     
 */     
function commentApprove($widget, $email = NULL)      
{   
    $result = array(
        "state" => -1,//状态
        "isAuthor" => 0,//是否是博主
        "userLevel" => '',//用户身份或等级名称
        "userDesc" => '',//用户title描述
        "bgColor" => '',//用户身份或等级背景色
        "commentNum" => 0//评论数量
    );
    if (empty($email)) return $result;      
    
    $result['state'] = 1;
    $master = array(      
        '基友邮箱1@qq.com',
        '基友邮箱1@qq.com'
    );      
    if ($widget->authorId == $widget->ownerId) {      
        $result['isAuthor'] = 1;
        $result['userLevel'] = '博主';
        $result['userDesc'] = '很帅的博主';
        $result['bgColor'] = '#FFD67A';
        $result['commentNum'] = 999;
    } else if (in_array($email, $master)) {      
        $result['userLevel'] = '基友';
        $result['userDesc'] = '很帅的基友';
        $result['bgColor'] = '#65C186';
        $result['commentNum'] = 888;
    } else {
        //数据库获取
        $db = Typecho_Db::get();
        //获取评论条数
        $commentNumSql = $db->fetchAll($db->select(array('COUNT(cid)'=>'commentNum'))
            ->from('table.comments')
            ->where('mail = ?', $email));
        $commentNum = $commentNumSql[0]['commentNum'];
        
        //获取友情链接
        $linkSql = $db->fetchAll($db->select()->from('table.links')
            ->where('user = ?',$email));
        
        //等级判定
        if($commentNum==1){
            $result['userLevel'] = '初识';
            $result['bgColor'] = '#999999';
            $userDesc = '你已经向目的地迈出了第一步!';
        } else {
            if ($commentNum<3 && $commentNum>1) {
                $result['userLevel'] = '初识';
                $result['bgColor'] = '#999999';
            }elseif ($commentNum<9 && $commentNum>=3) {
                $result['userLevel'] = '朋友';
                $result['bgColor'] = '#A0DAD0';
            }elseif ($commentNum<27 && $commentNum>=9) {
                $result['userLevel'] = '好友';
                $result['bgColor'] = '#A0DAD0';
            }elseif ($commentNum<81 && $commentNum>=27) {
                $result['userLevel'] = '挚友';
                $result['bgColor'] = '#A0DAD0';
            }elseif ($commentNum<100 && $commentNum>=81) {
                $result['userLevel'] = '兄弟';
                $result['bgColor'] = '#A0DAD0';
            }elseif ($commentNum>=100) {
                $result['userLevel'] = '老铁';
                $result['bgColor'] = '#A0DAD0';
            }
             $userDesc = '你已经向目的地前进了'.$commentNum.'步!'; 
        }
        if($linkSql){
            $result['userLevel'] = '博友';
            $result['bgColor'] = '#21b9bb';
            $userDesc = '1'.$linkSql[0]['description'].'&#10;2'.$userDesc;
        }
        //以上12自行修改
        $result['userDesc'] = $userDesc;
        $result['commentNum'] = $commentNum;
    } 
    return $result;
}

调用方法
评论中调用上面的方法<?php $commentApprove = commentApprove($comments, $comments->mail); ?>。返回以下四个数据。

$commentApprove['state'] //状态
$commentApprove['isAuthor'] //是否是博主
$commentApprove['userLevel'] //用户身份或等级名称
$commentApprove['userDesc'] //用户title描述
$commentApprove['bgColor'] //用户身份或等级背景色
$commentApprove['commentNum'] //评论数量

具体html代码,请根据自己主题调整样式。

<?php $commentApprove = commentApprove($comments, $comments->mail); ?>
<h4 class="author" title="<?php echo $commentApprove['userDesc'] ?>">
    <a href="<?php $comments->url(); ?>">
        <img alt="<?php $comments->author(false); ?>" src="<?php echo $avatar ?>" srcset="<?php echo $avatar2x ?> 2x" class="avatar avatar-50 photo" height="50" width="50"/>
        <?php $comments->author(false); ?>
        <span class="isauthor" title="Author">
            <span title="<?php echo $commentApprove['userDesc'] ?>" style="background:<?php echo $commentApprove['bgColor'] ?>;"><?php echo $commentApprove['userLevel'] ?></span>
            <?php if ($commentApprove['isAuthor'] == 1){ ?>
                <i title="<?php echo $commentApprove['userDesc'] ?>" class="iconfont">&#xe876;</i>
            <?php } ?>
        </span>
    </a>
</h4>

转载:https://blog.chrison.cn/work/170.html


扫描二维码,在手机上阅读!