易优CMS-使用技巧-【教程】会员登录后只显示头像的方法
很久没写教程了,因为最近考试的原因暂时没有写更多的教程,今天就来写一个有关易优会员登录后头像的问题,大家在做站的时候,会碰到,想登录后只显示头像而不显示账号昵称或者用户名。对于目前的逻辑,系统暂时还不能实现,只能应用于{eyou:user type='info'}里面的 头像加昵称
才可以。 这样会碰到瓶颈,有的模板需求只要 单独显示头像就好 不要显示昵称
可是你怎么调用是无法实现的。
所以这种的确 有点头疼。小秋今天就利用user登录会员标签 来模拟复制一个逻辑出来,来填充这个所谓的不足吧,
效果如下:
如果你想登录显示用户名/昵称 你就正常的官方标签即可。
如果你想登录后只显示头像,那么就用以下标签:
{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}
代码有点多,因为是全新复制一个标签的结果,但我相信可以结合代码出来,只是不想费脑,就直接全新复制标签出来了,请多见谅~希望官方后续整合下.
以下是修改的文件部分:
打开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'] = "";
} 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 = "";
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('请求成功', 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 __list__="" instanceof="" hinkcollection="" hinkpaginator="" __list__-=""-->isEmpty())): ?>';<!--?php-->
$parseStr .= '<!--?php-->';<!--?php-->
$parseStr .= $content;
$parseStr .= '<!--?php-->';<!--?php-->
$parseStr .= '<!--?php-->'; // 清除变量值,只限于在标签内部使用<!--?php-->
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 __list__="" instanceof="" hinkcollection="" hinkpaginator="" __list__-=""-->isEmpty())): ?>';<!--?php-->
$parseStr .= '<!--?php-->';<!--?php-->
$parseStr .= $content;
$parseStr .= '<!--?php-->';<!--?php-->
$parseStr .= '<!--?php-->'; // 清除变量值,只限于在标签内部使用<!--?php-->
if (!empty($parseStr)) {
return $parseStr;
}
return;
}
再打开
corelibrary hink emplate aglibeyou 新建一个php文件
文件名为:
TagUsers.php
里面内容为:以下全部复制进去:
<?php
/**
* 易优cms
* ============================================================================
* 版权所有 2016-2028 海口快推科技有限公司,并保留所有权利。
* 网站地址: http://www.eyoucms.com
* ----------------------------------------------------------------------------
* 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
* ============================================================================
* Author: 小虎哥<1105415366@qq.com><!--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; // 关闭商城中心,同时隐藏购物车入口
$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;
// 购物车的数量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
2. 本站积分货币获取途径以及用途的解读,想在本站混的好,请务必认真阅读!
3. 本站强烈打击盗版/破解等有损他人权益和违法作为,请各位会员支持正版!
4. 易优CMS > 易优CMS-使用技巧-【教程】会员登录后只显示头像的方法