幫助

【教程】會員登錄后只顯示頭像的方法

2020-11-20 20:42 易優(yōu)技巧

       很久沒寫教程了,因為最近考試的原因暫時沒有寫更多的教程,今天就來寫一個有關(guān)易優(yōu)會員登錄后頭像的問題,大家在做站的時候,會碰到,想登錄后只顯示頭像而不顯示賬號昵稱或者用戶名。對于目前的邏輯,系統(tǒng)暫時還不能實現(xiàn),只能應(yīng)用于{eyou:user type='info'}里面的 頭像加昵稱 

 才可以。 這樣會碰到瓶頸,有的模板需求只要 單獨顯示頭像就好 不要顯示昵稱

 

 

可是你怎么調(diào)用是無法實現(xiàn)的。

 

 

所以這種的確 有點頭疼。小秋今天就利用user登錄會員標簽 來模擬復制一個邏輯出來,來填充這個所謂的不足吧,

效果如下:

 

 

如果你想登錄顯示用戶名/昵稱  你就正常的官方標簽即可。

如果你想登錄后只顯示頭像,那么就用以下標簽:

 

{eyou:users type='open'}

{eyou:users type='cart'}

        <a href="{$field.url}" id="{$field.id}" >購物車(<font color="red" id="{$field.cartid}">0</font>)</a>

         {$field.hidden}

        {/eyou:users}

{eyou:users type='login'}

<a href="{$field.url}" id="{$field.id}">登錄</a> 

{$field.hidden}

{/eyou:users}

{eyou:users type='reg'}

<a href="{$field.url}" id="{$field.id}">注冊</a> 

{$field.hidden}

{/eyou:users}

{eyou:users type='logout'}

<a href="{$field.url}" id="{$field.id}">退出</a> 

{$field.hidden}

{/eyou:users}

{/eyou:users}

 

代碼有點多,因為是全新復制一個標簽的結(jié)果,但我相信可以結(jié)合代碼出來,只是不想費腦,就直接全新復制標簽出來了,請多見諒~希望官方后續(xù)整合下.

 

 

以下是修改的文件部分:

打開applicationapicontroller

Ajax.php  文件 打開 大概在 (179行 )

 

找到以下

 

    /**

     * 檢驗會員登錄

     */

    public function check_user()

    {

        if (IS_AJAX) {

            $type = input('param.type/s', 'default');

            $img = input('param.img/s');

            $users_id = session('users_id');

            if ('login' == $type) {

                if (!empty($users_id)) {

                    $currentstyle = input('param.currentstyle/s');

                    $users = M('users')->field('username,nickname,head_pic')

                        ->where([

                            'users_id'  => $users_id,

                            'lang'      => $this->home_lang,  

                        ])->find();

                    if (!empty($users)) {

                        $nickname = $users['nickname'];

                        if (empty($nickname)) {

                            $nickname = $users['username'];

                        }

                        $head_pic = get_head_pic($users['head_pic']);

                        if ('on' == $img) {

                            $users['html'] = "<img class='{$currentstyle}' alt='{$nickname}' src='{$head_pic}' />";

                        } else {

                            $users['html'] = $nickname;

                        }

                        $users['ey_is_login'] = 1;

                        $this->success('請求成功', null, $users);

                    }

                }

                $this->success('請先登錄', null, ['ey_is_login'=>0]);

            }

            else if ('reg' == $type)

            {

                if (!empty($users_id)) {

                    $users['ey_is_login'] = 1;

                } else {

                    $users['ey_is_login'] = 0;

                }

                $this->success('請求成功', null, $users);

            }

            else if ('logout' == $type)

            {

                if (!empty($users_id)) {

                    $users['ey_is_login'] = 1;

                } else {

                    $users['ey_is_login'] = 0;

                }

                $this->success('請求成功', null, $users);

            }

            else if ('cart' == $type)

            {

                if (!empty($users_id)) {

                    $users['ey_is_login'] = 1;

                    $users['ey_cart_num_20191212'] = Db::name('shop_cart')->where(['users_id'=>$users_id])->sum('product_num');

                } else {

                    $users['ey_is_login'] = 0;

                    $users['ey_cart_num_20191212'] = 0;

                }

                $this->success('請求成功', null, $users);

            }

        }

        $this->error('訪問錯誤');

    }

 

下面新增以下代碼:

 

public function check_users()

    {

        if (IS_AJAX) {

            $type = input('param.type/s', 'default');

            $img = input('param.img/s');

            $users_id = session('users_id');

            if ('login' == $type) {

                if (!empty($users_id)) {

                    $currentstyle = input('param.currentstyle/s');

                    $users = M('users')->field('username,nickname,head_pic')

                        ->where([

                            'users_id'  => $users_id,

                            'lang'      => $this->home_lang,  

                        ])->find();

$head_pic = get_head_pic($users['head_pic']);

                    if (!empty($users)) {

                        $nickname = "<img src='{$head_pic}'>";

                        if (empty($nickname)) {

                            $nickname = $users['username'];

                        }

                        $head_pic = get_head_pic($users['head_pic']);

                        if ('on' == $img) {

                       $users['html'] = "<img class='{$currentstyle}' alt='{$nickname}' src='{$head_pic}' />";

                        } else {

                            $users['html'] = $nickname;

                        }

                        $users['ey_is_login'] = 1;

                        $this->success('請求成功', null, $users);

                    }

                }

                $this->success('請先登錄', null, ['ey_is_login'=>0]);

            }

            else if ('reg' == $type)

            {

                if (!empty($users_id)) {

                    $users['ey_is_login'] = 1;

                } else {

                    $users['ey_is_login'] = 0;

                }

                $this->success('請求成功', null, $users);

            }

            else if ('logout' == $type)

            {

                if (!empty($users_id)) {

                    $users['ey_is_login'] = 1;

                } else {

                    $users['ey_is_login'] = 0;

                }

                $this->success('請求成功', null, $users);

            }

            else if ('cart' == $type)

            {

                if (!empty($users_id)) {

                    $users['ey_is_login'] = 1;

                    $users['ey_cart_num_20191212'] = Db::name('shop_cart')->where(['users_id'=>$users_id])->sum('product_num');

                } else {

                    $users['ey_is_login'] = 0;

                    $users['ey_cart_num_20191212'] = 0;

                }

                $this->success('請求成功', null, $users);

            }

        }

        $this->error('訪問錯誤');

    }

保存

 

 

打開:corelibrary hink emplate aglib目錄

 

Eyou.php文件 打開

 

找到:(89行)

 

'user'       => ['attr' => 'type,id,key,mod,empty,currentstyle,img,txt,txtid'],

 

下面新增以下代碼:

 

'users'       => ['attr' => 'type,id,key,mod,empty,currentstyle,img,txt,txtid'],

 

再找到以下代碼(大概2626行)

   public function tagUser($tag, $content)

    {

        $type  =  !empty($tag['type']) ? $tag['type'] : 'default';

        $id     = isset($tag['id']) ? $tag['id'] : 'field';

        $key     = isset($tag['key']) ? $tag['key'] : 'i';

        $txt  =  !empty($tag['txt']) ? $tag['txt'] : '';

        $txt  = $this->varOrvalue($txt);

        $txtid  =  !empty($tag['txtid']) ? $tag['txtid'] : '';

        $img  =  !empty($tag['img']) ? $tag['img'] : 'off';

        $currentstyle   = !empty($tag['currentstyle']) ? $tag['currentstyle'] : '';

 

        $parseStr = '<?php ';

        $parseStr .= ' $tagUser = new hink emplate aglibeyouTagUser;';

        $parseStr .= ' $__LIST__ = $tagUser->getUser("'.$type.'", "'.$img.'", "'.$currentstyle.'", '.$txt.', "'.$txtid.'");';

        $parseStr .= '?>';

 

        $parseStr .= '<?php if(!empty($__LIST__) || (($__LIST__ instanceof hinkCollection || $__LIST__ instanceof hinkPaginator ) && $__LIST__->isEmpty())): ?>';

        $parseStr .= '<?php $'.$id.' = $__LIST__; ?>';

        $parseStr .= $content;

        $parseStr .= '<?php endif; ?>';

        $parseStr .= '<?php $'.$id.' = []; ?>'; // 清除變量值,只限于在標簽內(nèi)部使用

 

        if (!empty($parseStr)) {

            return $parseStr;

        }

        return;

    }

在下面新增以下代碼:

 

   public function tagUsers($tag, $content)

    {

        $type  =  !empty($tag['type']) ? $tag['type'] : 'default';

        $id     = isset($tag['id']) ? $tag['id'] : 'field';

        $key     = isset($tag['key']) ? $tag['key'] : 'i';

        $txt  =  !empty($tag['txt']) ? $tag['txt'] : '';

        $txt  = $this->varOrvalue($txt);

        $txtid  =  !empty($tag['txtid']) ? $tag['txtid'] : '';

        $img  =  !empty($tag['img']) ? $tag['img'] : 'off';

        $currentstyle   = !empty($tag['currentstyle']) ? $tag['currentstyle'] : '';

 

        $parseStr = '<?php ';

        $parseStr .= ' $tagUser = new hink emplate aglibeyouTagUsers;';

        $parseStr .= ' $__LIST__ = $tagUser->getUser("'.$type.'", "'.$img.'", "'.$currentstyle.'", '.$txt.', "'.$txtid.'");';

        $parseStr .= '?>';

 

        $parseStr .= '<?php if(!empty($__LIST__) || (($__LIST__ instanceof hinkCollection || $__LIST__ instanceof hinkPaginator ) && $__LIST__->isEmpty())): ?>';

        $parseStr .= '<?php $'.$id.' = $__LIST__; ?>';

        $parseStr .= $content;

        $parseStr .= '<?php endif; ?>';

        $parseStr .= '<?php $'.$id.' = []; ?>'; // 清除變量值,只限于在標簽內(nèi)部使用

 

        if (!empty($parseStr)) {

            return $parseStr;

        }

        return;

    }

 

再打開

corelibrary hink emplate aglibeyou 新建一個php文件 

文件名為:

TagUsers.php

 

里面內(nèi)容為:以下全部復制進去:

 

<?php

/**

 * 易優(yōu)cms

 * ============================================================================

 * 版權(quán)所有 2016-2028 ??诳焱瓶萍加邢薰?,并保留所有權(quán)利。

 * 網(wǎng)站地址: http://www.u-renovate.com

 * ----------------------------------------------------------------------------

 * 如果商業(yè)用途務(wù)必到官方購買正版授權(quán), 以免引起不必要的法律糾紛.

 * ============================================================================

 * Author: 小虎哥 <1105415366@qq.com>

 * Date: 2018-4-3

 */

 

namespace think emplate aglibeyou;

 

use thinkDb;

 

/**

 * 會員中心

 */

class TagUsers extends Base

{

    /**

     * 會員ID

     */

    public $users_id = 0;

 

    //初始化

    protected function _initialize()

    {

        parent::_initialize();

        // 會員信息

        $this->users_id = session('users_id');

        $this->users_id = !empty($this->users_id) ? $this->users_id : 0;

    }

 

    /**

     * 會員中心

     * @author wengxianhu by 2018-4-20

     */

    public function getUser($type = 'default', $img = '', $currentstyle = '', $txt = '', $txtid = '')

    {

        $result = false;

 

        if ($this->home_lang != $this->main_lang) {

            return false;

        }

 

        $web_users_switch = tpCache('web.web_users_switch');

        $users_open_register = getUsersConfigData('users.users_open_register');

 

        if ('open' == $type) {

            if (empty($web_users_switch) || 1 == $users_open_register) {

                return false;

            }

        }

 

        if (1 == intval($web_users_switch)) {

            if (empty($users_open_register)) {

                $url = '';

                $t_uniqid = '';

                switch ($type) {

                    case 'login':

                    case 'centre':

                    case 'reg':

                    case 'logout':

                    case 'cart':

                        if ('cart' == $type) {

                            $shop_open = getUsersConfigData('shop.shop_open');

                            if (empty($shop_open)) return false; // 關(guān)閉商城中心,同時隱藏購物車入口

                            $url = url('user/Shop/shop_cart_list');

                        } else if ('reg' == $type) {

                            $users_open_reg = getUsersConfigData('users.users_open_reg');

                            if (isset($users_open_reg) && 1 == $users_open_reg) return false;

                            $url = url('user/Users/'.$type);

                        } else {

                            $url = url('user/Users/'.$type);

                        } 

 

                        $t_uniqid = md5(getTime().uniqid(mt_rand(), TRUE));

                        // A標簽ID

                        $result['id'] = md5("ey_{$type}_{$this->users_id}_{$t_uniqid}");

                        // A標簽里的文案ID

                        $result['txtid'] = !empty($txtid) ? md5($txtid) : md5("ey_{$type}_txt_{$this->users_id}_{$t_uniqid}");

                        // 文字文案

                        $result['txt'] = $txt;

                        // 購物車的數(shù)量ID

                        $result['cartid'] = md5("ey_{$type}_cartid_{$this->users_id}_{$t_uniqid}");

                        // IMG標簽里的ID

                        // $result['imgid'] = md5("ey_{$type}_img_{$this->users_id}_{$t_uniqid}");

                        // 圖片文案

                        $result['img'] = $img;

                        // 鏈接

                        $result['url'] = $url;

                        // 標簽類型

                        $result['type'] = $type;

                        // 圖片樣式類

                        $result['currentstyle'] = $currentstyle;

                        break;

 

                    case 'info':

                        $t_uniqid = md5(getTime().uniqid(mt_rand(), TRUE));

                        $result = $this->getUserInfo();

                        foreach ($result as $key => $val) {

                            $html_key = md5($key.'-'.$t_uniqid);

                            $result[$key] = $html_key;

                        }

                        $result['t_uniqid'] = $t_uniqid;

                        $result['id'] = $t_uniqid;

                        break;

 

                    case 'open':

                        break;

 

                    default:

                        return false;

                        break;

                }

 

                if ('login' == $type) {

                    if (isMobile() && isWeixin()) {

                        // 微信端和小程序則使用這個url

                        $result['url'] = url('user/Users/users_select_login');

                    }

                }

 

                // 子目錄

                $result['root_dir'] = $this->root_dir;

 

                $result_json = json_encode($result);

                $version = getCmsVersion();

 

                $hidden = '';

                switch ($type) {

                    case 'login':

                    case 'reg':

                    case 'logout':

                    case 'cart':

                        $hidden = <<<EOF

<script type="text/javascript" src="{$this->root_dir}/public/static/common/js/tag_users.js?v={$version}"></script>

<script type="text/javascript">

    var tag_user_result_json = {$result_json};

    tag_user(tag_user_result_json);

</script>

EOF;

                        break;

 

                    case 'info':

                        $hidden = <<<EOF

<script type="text/javascript" src="{$this->root_dir}/public/static/common/js/tag_users.js?v={$version}"></script>

<script type="text/javascript">

    var tag_user_result_json = {$result_json};

    tag_user_info(tag_user_result_json);

</script>

EOF;

                        break;

                }

                $result['hidden'] = $hidden;

            }

        }

 

        return $result;

    }

 

    /**

     * 獲取用戶信息

     */

    private function getUsersInfo()

    {

        $users = [];

        $tableFields1 = Db::name('users')->getTableFields();

        $tableFields2 = Db::name('users_level')->getTableFields();

        $tableFields = array_merge($tableFields1, $tableFields2);

        foreach ($tableFields as $key => $val) {

            $users[$val] = '';

        }

        $users['url'] = '';

        unset($users['password']);

        unset($users['paypwd']);

 

        return $users;

    }

}

 

最好再打開publicstaticcommonjs目錄

新建一個js文件 新建文件名為:tag_users.js

將以下內(nèi)容全部復制進去 保存:

 

function tag_user(result)

{

    var obj = document.getElementById(result.id);

    var txtObj = document.getElementById(result.txtid);

    var before_display = document.getElementById(result.id).style.display;

    var before_html = '';

    var before_txt_html = '';

    if (txtObj) {

        before_txt_html = txtObj.innerHTML;

        if ('login' == result.type) {

            txtObj.innerHTML = 'Loading&hellip;';

        }

    } else if (obj) {

        before_html = obj.innerHTML;

        if ('login' == result.type) {

            obj.innerHTML = 'Loading&hellip;';

        }

    }

    if (obj) {

        obj.style.display="none";

    } else {

        obj = txtObj;

    }

    //步驟一:創(chuàng)建異步對象

    var ajax = new XMLHttpRequest();

    //步驟二:設(shè)置請求的url參數(shù),參數(shù)一是請求的類型,參數(shù)二是請求的url,可以帶參數(shù),動態(tài)的傳遞參數(shù)starName到服務(wù)端

    var url = result.root_dir+"/index.php?m=api&c=Ajax&a=check_users&type="+result.type+"&img="+result.img;

    if (result.currentstyle != '') {

        url += "&currentstyle="+result.currentstyle;

    }

    ajax.open("post", url, true);

    // 給頭部添加ajax信息

    ajax.setRequestHeader("X-Requested-With","XMLHttpRequest");

    // 如果需要像 HTML 表單那樣 POST 數(shù)據(jù),請使用 setRequestHeader() 來添加 HTTP 頭。然后在 send() 方法中規(guī)定您希望發(fā)送的數(shù)據(jù):

    ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    //步驟三:發(fā)送請求+數(shù)據(jù)

    ajax.send('_ajax=1');

    //步驟四:注冊事件 onreadystatechange 狀態(tài)改變就會調(diào)用

    ajax.onreadystatechange = function () {

        //步驟五 如果能夠進到這個判斷 說明 數(shù)據(jù) 完美的回來了,并且請求的頁面是存在的

        if (ajax.readyState==4 && ajax.status==200) {

            var json = ajax.responseText;  

            var res = JSON.parse(json);

            if (1 == res.code) {

                if (1 == res.data.ey_is_login) {

                    if (obj) {

                        if ('login' == result.type) {

                            if (result.txt.length > 0) {

                                res.data.html = result.txt;

                            }

                            if (txtObj) {

                                txtObj.innerHTML = res.data.html;

                            } else {

                                obj.innerHTML = res.data.html;

                            }

                            try {

                                obj.setAttribute("href", result.url);

                                if (!before_display) {

                                    obj.style.display=before_display;

                                }

                            }catch(err){}

                        } else if ('logout' == result.type) {

                            if (txtObj) {

                                txtObj.innerHTML = before_txt_html;

                            } else {

                                obj.innerHTML = before_html;

                            }

                            try {

                                if (!before_display) {

                                    obj.style.display=before_display;

                                }

                            }catch(err){}

                        } else if ('reg' == result.type) {

                            obj.style.display="none";

                        } else if ('cart' == result.type) {

                            try {

                                var cartidObj = document.getElementById(result.cartid);

                                if (cartidObj) {

                                    cartidObj.innerHTML = res.data.ey_cart_num_20191212;

                                }

                                if (!before_display) {

                                    obj.style.display=before_display;

                                }

                            }catch(err){}

                        }

                    }

                } else {

                    // 恢復未登錄前的html文案

                    if (obj) {

                        if (txtObj) {

                            txtObj.innerHTML = before_txt_html;

                        } else {

                            obj.innerHTML = before_html;

                        }

                        if ('logout' == result.type) {

                            obj.style.display="none";

                        } else if ('cart' == result.type) {

                            try {

                                var cartidObj = document.getElementById(result.cartid);

                                if (cartidObj) {

                                    cartidObj.innerHTML = res.data.ey_cart_num_20191212;

                                }

                                if (!before_display) {

                                    obj.style.display=before_display;

                                }

                            }catch(err){}

                        } else {

                            try {

                                if (!before_display) {

                                    obj.style.display=before_display;

                                }

                            }catch(err){}

                        }

                    }

                }

            } else {

                if (obj) {

                    obj.innerHTML = 'Error';

                    try {

                        if (!before_display) {

                            obj.style.display=before_display;

                        }

                    }catch(err){}

                }

            }

       }

    } 

}

 

function tag_user_info(result)

{

    var obj = document.getElementById(result.t_uniqid);

    var before_display = '';

    if (obj) {

        before_display = obj.style.display;

        obj.style.display="none";

    }

 

    //步驟一:創(chuàng)建異步對象

    var ajax = new XMLHttpRequest();

    //步驟二:設(shè)置請求的url參數(shù),參數(shù)一是請求的類型,參數(shù)二是請求的url,可以帶參數(shù),動態(tài)的傳遞參數(shù)starName到服務(wù)端

    ajax.open("post", result.root_dir+"/index.php?m=api&c=Ajax&a=get_tag_user_info&t_uniqid="+result.t_uniqid, true);

    // 給頭部添加ajax信息

    ajax.setRequestHeader("X-Requested-With","XMLHttpRequest");

    // 如果需要像 HTML 表單那樣 POST 數(shù)據(jù),請使用 setRequestHeader() 來添加 HTTP 頭。然后在 send() 方法中規(guī)定您希望發(fā)送的數(shù)據(jù):

    ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    //步驟三:發(fā)送請求+數(shù)據(jù)

    ajax.send('_ajax=1');

    //步驟四:注冊事件 onreadystatechange 狀態(tài)改變就會調(diào)用

    ajax.onreadystatechange = function () {

        //步驟五 如果能夠進到這個判斷 說明 數(shù)據(jù) 完美的回來了,并且請求的頁面是存在的

        if (ajax.readyState==4 && ajax.status==200) {

            var json = ajax.responseText;  

            var res = JSON.parse(json);

            if (1 == res.code) {

                if (1 == res.data.ey_is_login) {

                    var dtypes = res.data.dtypes;

                    var users = res.data.users;

                    for (var key in users) {

                        var subobj = document.getElementById(key);

                        if (subobj) {

                            if ('img' == dtypes[key]) {

                                subobj.setAttribute("src", users[key]);

                            } else if ('href' == dtypes[key]) {

                                subobj.setAttribute("href", users[key]);

                            } else {

                                subobj.innerHTML = users[key];

                            }

                        }

                    }

                    if (obj) {

                        try {

                            if (!before_display) {

                                obj.style.display=before_display;

                            }

                        }catch(err){}

                    }

                } else {

                    if (obj) {

                        obj.style.display="none";

                    }

                }

            }

       }

    }

}

 

// 讀取 cookie

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 "";

}

 

 

到這里教程就差不多了。

 

為何要做2個user登錄標簽 因為有的模板只需要登錄后顯示單純頭像,一部分有的地方還是需要顯示昵稱所以因需所調(diào)用吧。

 

重復說下  以上調(diào)用的 標簽為:

 

{eyou:users type='open'}

{eyou:users type='cart'}

        <a href="{$field.url}" id="{$field.id}" >購物車(<font color="red" id="{$field.cartid}">0</font>)</a>

         {$field.hidden}

        {/eyou:users}

{eyou:users type='login'}

<a href="{$field.url}" id="{$field.id}">登錄</a> 

{$field.hidden}

{/eyou:users}

{eyou:users type='reg'}

<a href="{$field.url}" id="{$field.id}">注冊</a> 

{$field.hidden}

{/eyou:users}

{eyou:users type='logout'}

<a href="{$field.url}" id="{$field.id}">退出</a> 

{$field.hidden}

{/eyou:users}

{/eyou:users}

 

不過一般登錄  只需

 

{eyou:users type='login'}

<a href="{$field.url}" id="{$field.id}">登錄</a> 

{$field.hidden}

{/eyou:users}

 

即可


標簽: 會員登錄
QQ在線咨詢
AI智能客服 ×