很久沒(méi)寫(xiě)教程了,因?yàn)樽罱荚嚨脑驎簳r(shí)沒(méi)有寫(xiě)更多的教程,今天就來(lái)寫(xiě)一個(gè)有關(guān)易優(yōu)會(huì)員登錄后頭像的問(wèn)題,大家在做站的時(shí)候,會(huì)碰到,想登錄后只顯示頭像而不顯示賬號(hào)昵稱或者用戶名。對(duì)于目前的邏輯,系統(tǒng)暫時(shí)還不能實(shí)現(xiàn),只能應(yīng)用于{eyou:user type='info'}里面的 頭像加昵稱
才可以。 這樣會(huì)碰到瓶頸,有的模板需求只要 單獨(dú)顯示頭像就好 不要顯示昵稱
可是你怎么調(diào)用是無(wú)法實(shí)現(xiàn)的。
所以這種的確 有點(diǎn)頭疼。小秋今天就利用user登錄會(huì)員標(biāo)簽 來(lái)模擬復(fù)制一個(gè)邏輯出來(lái),來(lái)填充這個(gè)所謂的不足吧,
效果如下:
如果你想登錄顯示用戶名/昵稱 你就正常的官方標(biāo)簽即可。
如果你想登錄后只顯示頭像,那么就用以下標(biāo)簽:
{eyou:users type='open'}
{eyou:users type='cart'}
{$field.hidden}
{/eyou:users}
{eyou:users type='login'}
{$field.hidden}
{/eyou:users}
{eyou:users type='reg'}
{$field.hidden}
{/eyou:users}
{eyou:users type='logout'}
{$field.hidden}
{/eyou:users}
{/eyou:users}
代碼有點(diǎn)多,因?yàn)槭侨聫?fù)制一個(gè)標(biāo)簽的結(jié)果,但我相信可以結(jié)合代碼出來(lái),只是不想費(fèi)腦,就直接全新復(fù)制標(biāo)簽出來(lái)了,請(qǐng)多見(jiàn)諒~希望官方后續(xù)整合下.
以下是修改的文件部分:
打開(kāi)applicationapicontroller
Ajax.php 文件 打開(kāi) 大概在 (179行 )
找到以下
/**
* 檢驗(yàn)會(huì)員登錄
*/
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'] = "";
} else {
$users['html'] = $nickname;
}
$users['ey_is_login'] = 1;
$this->success('請(qǐng)求成功', null, $users);
}
}
$this->success('請(qǐng)先登錄', 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('請(qǐng)求成功', null, $users);
}
else if ('logout' == $type)
{
if (!empty($users_id)) {
$users['ey_is_login'] = 1;
} else {
$users['ey_is_login'] = 0;
}
$this->success('請(qǐng)求成功', 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('請(qǐng)求成功', null, $users);
}
}
$this->error('訪問(wèn)錯(cuò)誤');
}
下面新增以下代碼:
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 = "";
if (empty($nickname)) {
$nickname = $users['username'];
}
$head_pic = get_head_pic($users['head_pic']);
if ('on' == $img) {
$users['html'] = "";
} else {
$users['html'] = $nickname;
}
$users['ey_is_login'] = 1;
$this->success('請(qǐng)求成功', null, $users);
}
}
$this->success('請(qǐng)先登錄', 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('請(qǐng)求成功', null, $users);
}
else if ('logout' == $type)
{
if (!empty($users_id)) {
$users['ey_is_login'] = 1;
} else {
$users['ey_is_login'] = 0;
}
$this->success('請(qǐng)求成功', 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('請(qǐng)求成功', null, $users);
}
}
$this->error('訪問(wèn)錯(cuò)誤');
}
保存
打開(kāi):corelibrary hink emplate aglib目錄
Eyou.php文件 打開(kāi)
找到:(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 = '
$parseStr .= ' $tagUser = new hink emplate aglibeyouTagUser;';
$parseStr .= ' $__LIST__ = $tagUser->getUser("'.$type.'", "'.$img.'", "'.$currentstyle.'", '.$txt.', "'.$txtid.'");';
$parseStr .= '?>';
$parseStr .= 'isEmpty())): ?>';
$parseStr .= '';
$parseStr .= $content;
$parseStr .= '';
$parseStr .= ''; // 清除變量值,只限于在標(biāo)簽內(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 = '
$parseStr .= ' $tagUser = new hink emplate aglibeyouTagUsers;';
$parseStr .= ' $__LIST__ = $tagUser->getUser("'.$type.'", "'.$img.'", "'.$currentstyle.'", '.$txt.', "'.$txtid.'");';
$parseStr .= '?>';
$parseStr .= 'isEmpty())): ?>';
$parseStr .= '';
$parseStr .= $content;
$parseStr .= '';
$parseStr .= ''; // 清除變量值,只限于在標(biāo)簽內(nèi)部使用
if (!empty($parseStr)) {
return $parseStr;
}
return;
}
再打開(kāi)
corelibrary hink emplate aglibeyou 新建一個(gè)php文件
文件名為:
TagUsers.php
里面內(nèi)容為:以下全部復(fù)制進(jìn)去:
/**
* 易優(yōu)cms
* ============================================================================
* 版權(quán)所有 2016-2028 ??诳焱瓶萍加邢薰?,并保留所有權(quán)利。
* 網(wǎng)站地址: http://www.eyoucms.com
* ----------------------------------------------------------------------------
* 如果商業(yè)用途務(wù)必到官方購(gòu)買(mǎi)正版授權(quán), 以免引起不必要的法律糾紛.
* ============================================================================
* Author: 小虎哥 <[email protected]>
* Date: 2018-4-3
*/
namespace think emplate aglibeyou;
use thinkDb;
/**
* 會(huì)員中心
*/
class TagUsers extends Base
{
/**
* 會(huì)員ID
*/
public $users_id = 0;
//初始化
protected function _initialize()
{
parent::_initialize();
// 會(huì)員信息
$this->users_id = session('users_id');
$this->users_id = !empty($this->users_id) ? $this->users_id : 0;
}
/**
* 會(huì)員中心
* @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)閉商城中心,同時(shí)隱藏購(gòu)物車(chē)入口
$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標(biāo)簽ID
$result['id'] = md5("ey_{$type}_{$this->users_id}_{$t_uniqid}");
// A標(biāo)簽里的文案ID
$result['txtid'] = !empty($txtid) ? md5($txtid) : md5("ey_{$type}_txt_{$this->users_id}_{$t_uniqid}");
// 文字文案
$result['txt'] = $txt;
// 購(gòu)物車(chē)的數(shù)量ID
$result['cartid'] = md5("ey_{$type}_cartid_{$this->users_id}_{$t_uniqid}");
// IMG標(biāo)簽里的ID
// $result['imgid'] = md5("ey_{$type}_img_{$this->users_id}_{$t_uniqid}");
// 圖片文案
$result['img'] = $img;
// 鏈接
$result['url'] = $url;
// 標(biāo)簽類(lèi)型
$result['type'] = $type;
// 圖片樣式類(lèi)
$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()) {
// 微信端和小程序則使用這個(gè)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;
break;
case 'info':
$hidden = <<
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;
}
}
最好再打開(kāi)publicstaticcommonjs目錄
新建一個(gè)js文件 新建文件名為:tag_users.js
將以下內(nèi)容全部復(fù)制進(jìn)去 保存:
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…';
}
} else if (obj) {
before_html = obj.innerHTML;
if ('login' == result.type) {
obj.innerHTML = 'Loading…';
}
}
if (obj) {
obj.style.display="none";
} else {
obj = txtObj;
}
//步驟一:創(chuàng)建異步對(duì)象
var ajax = new XMLHttpRequest();
//步驟二:設(shè)置請(qǐng)求的url參數(shù),參數(shù)一是請(qǐng)求的類(lèi)型,參數(shù)二是請(qǐng)求的url,可以帶參數(shù),動(dòng)態(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 += "¤tstyle="+result.currentstyle;
}
ajax.open("post", url, true);
// 給頭部添加ajax信息
ajax.setRequestHeader("X-Requested-With","XMLHttpRequest");
// 如果需要像 HTML 表單那樣 POST 數(shù)據(jù),請(qǐng)使用 setRequestHeader() 來(lái)添加 HTTP 頭。然后在 send() 方法中規(guī)定您希望發(fā)送的數(shù)據(jù):
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
//步驟三:發(fā)送請(qǐng)求+數(shù)據(jù)
ajax.send('_ajax=1');
//步驟四:注冊(cè)事件 onreadystatechange 狀態(tài)改變就會(huì)調(diào)用
ajax.onreadystatechange = function () {
//步驟五 如果能夠進(jìn)到這個(gè)判斷 說(shuō)明 數(shù)據(jù) 完美的回來(lái)了,并且請(qǐng)求的頁(yè)面是存在的
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 {
// 恢復(fù)未登錄前的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)建異步對(duì)象
var ajax = new XMLHttpRequest();
//步驟二:設(shè)置請(qǐng)求的url參數(shù),參數(shù)一是請(qǐng)求的類(lèi)型,參數(shù)二是請(qǐng)求的url,可以帶參數(shù),動(dòng)態(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ù),請(qǐng)使用 setRequestHeader() 來(lái)添加 HTTP 頭。然后在 send() 方法中規(guī)定您希望發(fā)送的數(shù)據(jù):
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
//步驟三:發(fā)送請(qǐng)求+數(shù)據(jù)
ajax.send('_ajax=1');
//步驟四:注冊(cè)事件 onreadystatechange 狀態(tài)改變就會(huì)調(diào)用
ajax.onreadystatechange = function () {
//步驟五 如果能夠進(jìn)到這個(gè)判斷 說(shuō)明 數(shù)據(jù) 完美的回來(lái)了,并且請(qǐng)求的頁(yè)面是存在的
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個(gè)user登錄標(biāo)簽 因?yàn)橛械哪0逯恍枰卿浐箫@示單純頭像,一部分有的地方還是需要顯示昵稱所以因需所調(diào)用吧。
重復(fù)說(shuō)下 以上調(diào)用的 標(biāo)簽為:
{eyou:users type='open'}
{eyou:users type='cart'}
{$field.hidden}
{/eyou:users}
{eyou:users type='login'}
{$field.hidden}
{/eyou:users}
{eyou:users type='reg'}
{$field.hidden}
{/eyou:users}
{eyou:users type='logout'}
{$field.hidden}
{/eyou:users}
{/eyou:users}
不過(guò)一般登錄 只需
{eyou:users type='login'}
{$field.hidden}
{/eyou:users}
即可
版權(quán)聲明: 本站資源均來(lái)自互聯(lián)網(wǎng)或會(huì)員發(fā)布,如果侵犯了您的權(quán)益請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)刪除!謝謝!
轉(zhuǎn)載請(qǐng)注明: 【教程】會(huì)員登錄后只顯示頭像的方法