记录学习与后端知识并分享学习代码过程(会飞的鱼Blog)

分享个自用头像函数 作用自己细品

会飞的鱼 0 612 2022年12月6日

由于做模板的时候很多地方需要调用头像,比如啊评论这块需要判断该评论是否是游客还有注册用户,此用户头是否已经上传头像等等等等等,所以就自己写了个函数方便调用,代码没什么难度小白一看就懂。

<?php
//评论头像获取函数  自用
function photo_img($uid,$mail=''){
    global $CACHE;
    $user_cache = $CACHE->readCache('user');
    $userinfo = $user_cache[$uid];
    $photo_img_url= TEMPLATE_URL.'/images/author.png'; //此处默认头像显示自己改
    switch($photo_img_url) {
        case ROLE == ROLE_VISITOR && $mail == null:
            echo $photo_img_url;
            break;
        case ROLE == ROLE_VISITOR && $mail != null:
            echo  '//q2.qlogo.cn/headimg_dl?dst_uin=' . $mail . '&spec=100';
            break;
        case $userinfo['avatar'] == null && $userinfo['mail'] != null:
            echo  '//q2.qlogo.cn/headimg_dl?dst_uin=' . $userinfo['mail'] . '&spec=100';
            break;
        case $userinfo['avatar'] != null && $userinfo['mail'] != null || $userinfo['avatar'] != null && $userinfo['mail'] == null:
            echo $userinfo['avatar'];
            break;
        default:
            echo $photo_img_url;
    }
}
?>

使用方法:

$uid为该用户的唯一id,$ckmail为邮箱可为空

<?php photo_img($uid,$ckmail);?>
本文由 @会飞的鱼 于 2022-12-6 发布在 会飞的鱼Blog,如无特别说明,本博文章均为原创,转载请保留出处。

网友评论

    暂无评论

会飞的鱼 V

一条会飞的鱼!

745 文章
7274 评论
1069 万 阅读
8年 博龄
最新文章
最新评论
嘻嘻嘻
1个月前 (2024-03-19)

ThinkPHP实现用户注册、登录模块

标签

会飞的鱼 在线咨询

在线时间:9:00-22:00
周六、周日:14:00-22:00