目的地-Destination 前程似锦、未来可期、寻得良人、共赴白头,祝你也祝我。
博主 目的地-Destination
沪ICP备16003146号-2沪公网安备 31010702004922号萌ICP备20238488号博主 昨天 17:25 在线自豪地使用 Typecho 建站搭配使用 🌻Sunny 主题当前在线 1 人
歌曲封面 未知作品

沪ICP备16003146号-2

沪公网安备 31010702004922号

萌ICP备20238488号

网站已运行 7 年 342 天 2 小时 58 分

Powered by Typecho & Sunny

2 online · 50 ms

Title

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

Chrison

·

烂笔头

·

Article
⚠️ 本文最后更新于2023年02月26日,已经过了604天没有更新,若内容或图片失效,请留言反馈

简述

我上一个用的主题是有博主标记的。然后网上也看到了很多typecho等级划分的代码。那我就打算把两者所结合,用户等级+用户身份融合在一起显示。
考虑+实现,花了个把小时,算是完成了吧!

效果

截图

说明

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

functions.php

纯手打啊,我是纯按自己想法来的,各位按自己的主题风格改写。基友邮箱请按格式自行添加。友链数据是从数据库likns表中获取的。如果你是别的友链方式,那就按别的方式来。举一反三,举二反六。实在不会,再留言吧!
♾️ php 代码:
/**    
 * 评论者认证等级 + 身份    
 *    
 * @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 = '🔗'.$linkSql[0]['description'].'&#10;✌️'.$userDesc;
        }
        
        $result['userDesc'] = $userDesc;
        $result['commentNum'] = $commentNum;
    } 
    return $result;
}

调用方法

评论中调用上面的方法<?php $commentApprove = commentApprove($comments, $comments->mail); ?>。返回以下四个数据。
♾️ text 代码:
$commentApprove['state'] //状态
$commentApprove['isAuthor'] //是否是博主
$commentApprove['userLevel'] //用户身份或等级名称
$commentApprove['userDesc'] //用户title描述
$commentApprove['bgColor'] //用户身份或等级背景色
$commentApprove['commentNum'] //评论数量
具体html代码,请根据自己主题调整样式。
♾️ php 代码:
<?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>
现在已有 371 次阅读,34 条评论,0 人点赞
Comment:共34条
发表
  1. 头像
    @

    好友

    Teacher Du
    一直想弄这个功能,但不知怎么整合到第三方的评论系统~
    · Chrome · 中国北京市中国移动北京分公司

    👍

    💖

    💯

    💦

    😄

    🪙

    👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
    1. 头像
      @
      第三方是什么?php的话,估计大差不多。可能需要研究吧
      · 火狐浏览器 · 中国江苏省苏州市电信

      👍

      💖

      💯

      💦

      😄

      🪙

      👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
  2. 头像
    @
    谁是你基友~噫~~~
    · Safari · 中国江苏省南京市电信

    👍

    💖

    💯

    💦

    😄

    🪙

    👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
    1. 头像
      @
      那你走远点
      · 火狐浏览器 · 中国江苏省苏州市电信

      👍

      💖

      💯

      💦

      😄

      🪙

      👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
  3. 头像
    @
    大湿兄
    更新频率有点高👍🏻
    · Safari · 中国江苏省南京市电信

    👍

    💖

    💯

    💦

    😄

    🪙

    👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
    1. 头像
      @
      到处逛,到处参😝
      · Safari · 中国江苏省苏州市电信

      👍

      💖

      💯

      💦

      😄

      🪙

      👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
  4. 头像
    @

    好友

    荒野孤灯
    第几步了?
    · Chrome · 中国湖北省武汉市联通

    👍

    💖

    💯

    💦

    😄

    🪙

    👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
    1. 头像
      @
      就这些了。没了。一共就两段代码
      · 火狐浏览器 · 中国江苏省苏州市电信

      👍

      💖

      💯

      💦

      😄

      🪙

      👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
      1. 头像
        @
        666 ,搞起来
        · Chrome · 中国湖北省武汉市联通

        👍

        💖

        💯

        💦

        😄

        🪙

        👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
        1. 头像
          @
          你网站手机版导航栏没了?是取消了还是出错了?
          · Safari · 中国江苏省苏州市电信

          👍

          💖

          💯

          💦

          😄

          🪙

          👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
          1. 头像
            @
            搜嘎
            · 火狐浏览器 · 中国江苏省苏州市电信

            👍

            💖

            💯

            💦

            😄

            🪙

            👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
          2. 头像
            @
            还在,注释了……
            · Chrome · 中国湖北省黄石市电信

            👍

            💖

            💯

            💦

            😄

            🪙

            👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
    2. 头像
      @
      啊?在这现学啊,代码不是发你了😂
      · 火狐浏览器 · 中国江苏省苏州市电信

      👍

      💖

      💯

      💦

      😄

      🪙

      👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
搜 索 消 息 足 迹
你还不曾留言过..
你还不曾留下足迹..
博主 不再显示
博主