在这里我将分享ecshop商品限制只允许指定会员访问浏览权限代码
创建sql,产品用户管理表
[sql]
create table if not exists `ecs_plug_goods_users`(
`gu_id` int not null auto_increment comment ‘产品和用户关联id’,
`goods_id` int not null comment ‘产品id’,
`user_id` int not null comment ‘用户id’,
`admin_id` int not null comment ‘管理员id’,
unique key `gu_id` (`gu_id`)
) engine=myisam default charset=utf8 comment=’产品用户关联表’ auto_increment=1;
[/sql]
下面分享代码
一: 后台
1. 在根目录admin/templates/goods_info.html查找
[php]
<span
class="tab-back" id="article-tab">{$lang.tab_article}</span>
[/php]
后面添加
[php]<span
class="tab-back" id="user-tab">{$lang.tab_goods_users}</span>[/php]
2. 同一文件查找
[php]<div class="button-div">[/php]
在其上面添加
[php]
<!– 产品会员关联插件 –>
<!– 产品会员关联插件 –>
<table width="90%" id="user-table" style="display:none" align="center">
<!– 会员关联行 –>
<tr>
<td colspan="3">
<img src="images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH" />
{$lang.user_name} <input type="text" name="user_name" />
<input type="button" value="{$lang.button_search}" onclick="searchUser()" class="button" />
</td>
</tr>
<!– 显示内容 –>
<tr>
<th>{$lang.all_username}</th>
<th>{$lang.handler}</th>
<th>{$lang.link_username}</th>
</tr>
<tr>
<td width="45%">
<select name="source_select4" size="20" style="width:100%" multiple ondblclick="sz4.addItem(false, ‘add_goods_user’, goodsId, this.form.elements[‘price2’].value)">
</select>
</td>
<td align="center">
<p><input type="button" value=">>" onclick="sz4.addItem(true, ‘add_goods_user’, goodsId, this.form.elements[‘price2’].value)" class="button" /></p>
<p><input type="button" value=">" onclick="sz4.addItem(false, ‘add_goods_user’, goodsId, this.form.elements[‘price2’].value)" class="button" /></p>
<p><input type="button" value="<" onclick="sz4.dropItem(false, ‘drop_goods_user’, goodsId, elements[‘is_single’][0].checked)" class="button" /></p>
<p><input type="button" value="<<" onclick="sz4.dropItem(true, ‘drop_goods_user’, goodsId, elements[‘is_single’][0].checked)" class="button" /></p>
</td>
<td width="45%">
<select name="target_select4" size="20" style="width:100%" multiple ondblclick="sz4.dropItem(false, ‘drop_goods_user’, goodsId, elements[‘is_single’][0].checked)">
{foreach from=$goods_users_list item=goods_user}
<option value="{$goods_user.user_id}">{$goods_user.user_name}</option>
{/foreach}
</select>
</td>
</tr>
</table>
[/php]
3. 在同一文件查找
[php]var sz3 = new SelectZone(1, elements[‘source_select3’], elements[‘target_select3’]);[/php]
在下面添加
[php]var sz4 = new SelectZone(1, elements[‘source_select4’], elements[‘target_select4’]);[/php]
4. 查找
[php]function searchArticle()[/php]
在这函数下面添加函数
[php]/**
* 获取用户名称搜索
*/
function searchUser()
{
var filters = new Object;
filters.user_name = Utils.trim(elements[‘user_name’].value);
sz4.loadOptions(‘get_user_list’, filters);
}[/php]
5. 在根目录languages/zh_cn/admin/goods.php查找
[php]$_LANG[‘tab_article’] = ‘关联文章’;[/php]在下面添加一行
[php]$_LANG[‘tab_goods_users’] = ‘只允许以下会员访问’;[/php]
6. 在同一文件下查找
[php]$_LANG[‘js_languages’][‘cancel_color’] = ‘无样式’;[/php]
下面添加以下代码
[php]
/*—————————————————— */
//– 关联用户
/*—————————————————— */
$_LANG[‘user_name’] = ‘用户名称’;
$_LANG[‘all_username’] = ‘可选用户’;
$_LANG[‘link_username’] = ‘只允许选定的会员用户访问’;
$_LANG[‘single’] = ‘单向关联’;
$_LANG[‘double’] = ‘双向关联’;
$_LANG[‘top_cat’] = ‘顶级分类’;
[/php]
7. 在根目录admin/goods.php 大概202行找到
[php]
/* 关联文章 */
$goods_article_list = array();
$sql = "DELETE FROM " . $ecs->table(‘goods_article’) .
" WHERE goods_id = 0 AND admin_id = ‘$_SESSION[admin_id]’";
$db->query($sql);
[/php]
下面添加
[php]
/* 关联用户 */
$goods_users_list = array();
$sql = ‘delete from ‘. $ecs->table(‘plug_goods_users’) .’ where goods_id = "0" and admin_id = "’ . $_SESSION[‘admin_id’] . ‘"’;
$db->query($sql);
[/php]
8. 同一文件下大概345行找到
[php]
// 关联文章
$sql = "DELETE FROM " . $ecs->table(‘goods_article’) .
" WHERE goods_id = 0 AND admin_id = ‘$_SESSION[admin_id]’";
$db->query($sql);
$sql = "SELECT 0 AS goods_id, article_id, ‘$_SESSION[admin_id]’ AS admin_id " .
"FROM " . $ecs->table(‘goods_article’) .
" WHERE goods_id = ‘$_REQUEST[goods_id]’ ";
$res = $db->query($sql);
while ($row = $db->fetchRow($res))
{
$db->autoExecute($ecs->table(‘goods_article’), $row, ‘INSERT’);
}
[/php]
下面添加
[php]
//关联用户
$sql = ‘delete from ‘ . $ecs->table(‘plug_goods_users’) . ‘ where goods_id = "0" and admin_id = "’. $_SESSION[‘admin_id’] .’"’;
$db->query($sql);
$sql = ‘select 0 as goods_id, user_id, ‘. $_SESSION[‘admin_id’] .’ as admin_id from ‘. $ecs->table(‘plug_goods_users’) .’ where goods_id = "’. $_REQUEST[‘goods_id’] .’"’;
$res = $db->query($sql);
while($row = $db->fetchRow($res)){
$db->autoExecute($ecs->table(‘plug_goods_users’), $row, ‘insert’);
}
[/php]
9. 在同一文件下大概397行找到
[php]$goods_article_list = get_goods_articles($goods[‘goods_id’]); // 关联文章[/php]
下面添加
[php]$goods_users_list = get_goods_users($goods[‘goods_id’]); //关联用户[/php]
10. 在同一文件下大概459行找到
[php]$smarty->assign(‘goods_article_list’, $goods_article_list);[/php]
下面添加
[php]$smarty->assign(‘goods_users_list’, $goods_users_list);[/php]
11. 在根目录下admin/includes/lib_goods.php大概274行找到
[php]
/**
* 保存某商品的关联文章
* @param int $goods_id
* @return void
*/
function handle_goods_article($goods_id)
{
$sql = "UPDATE " . $GLOBALS[‘ecs’]->table(‘goods_article’) . " SET " .
" goods_id = ‘$goods_id’ " .
" WHERE goods_id = ‘0’" .
" AND admin_id = ‘$_SESSION[admin_id]’";
$GLOBALS[‘db’]->query($sql);
}
[/php]
下面添加
[php]
/**
* 保存某商品允许访问的会员
* @param int $goods_id
* @return void
*/
function handle_goods_users($goods_id){
$sql = ‘update ‘. $globals[‘ecs’]->table(‘plug_goods_users’) . ‘ set goods_id = "’. $goods_id .’" where goods_id = "0" and admin_id = "’. $_SESSION[‘admin_id’].’"’;
$GLOBALS[‘db’]->query($sql);
}
[/php]
12. 在同一文件下大概519行找到
[php]$sql = "DELETE FROM " . $GLOBALS[‘ecs’]->table(‘goods_article’) . " WHERE goods_id " . db_create_in($goods_id);
$GLOBALS[‘db’]->query($sql);[/php]
下面添加
[php]$sql = ‘DELETE FROM ‘. $GLOBALS[‘ecs’]->table(‘plug_goods_users’) .’ WHERE goods_id ‘. db_create_in($goods_id);
$GLOBALS[‘db’]->query($sql);[/php]
13. 在同一文件下大概792行找到
[php]
/**
* 获得商品的关联文章
*
* @access public
* @param integer $goods_id
* @return array
*/
function get_goods_articles($goods_id)
{
$sql = "SELECT g.article_id, a.title " .
"FROM " .$GLOBALS[‘ecs’]->table(‘goods_article’) . " AS g, " .
$GLOBALS[‘ecs’]->table(‘article’) . " AS a " .
"WHERE g.goods_id = ‘$goods_id’ " .
"AND g.article_id = a.article_id ";
if ($goods_id == 0)
{
$sql .= " AND g.admin_id = ‘$_SESSION[admin_id]’";
}
$row = $GLOBALS[‘db’]->getAll($sql);
return $row;
}
[/php]
下面添加
[php]
/**
* 获得商品的相关用户
*
* @access public
* @param integer $goods_id
* @return array
*/
function get_goods_users($goods_id){
$sql = "SELECT g.user_id, u.user_name " .
"FROM " .$GLOBALS[‘ecs’]->table(‘plug_goods_users’) . " AS g, " .
$GLOBALS[‘ecs’]->table(‘users’) . " AS u " .
"WHERE g.goods_id = ‘$goods_id’ " .
"AND g.user_id = u.user_id ";
if ($goods_id == 0)
{
$sql .= " AND g.admin_id = ‘$_SESSION[admin_id]’";
}
$row = $GLOBALS[‘db’]->getAll($sql);
return $row;
}
[/php]
14. 在根目录admin/goods.php中大概2150~2176之间查找
[php]
/*—————————————————— */
//– 删除关联文章
/*—————————————————— */
elseif ($_REQUEST[‘act’] == ‘drop_goods_article’)
{
include_once(ROOT_PATH . ‘includes/cls_json.php’);
$json = new JSON;
check_authz_json(‘goods_manage’);
$articles = $json->decode($_GET[‘drop_ids’]);
$arguments = $json->decode($_GET[‘JSON’]);
$goods_id = $arguments[0];
$sql = "DELETE FROM " .$ecs->table(‘goods_article’) . " WHERE " . db_create_in($articles, "article_id") . " AND goods_id = ‘$goods_id’";
$db->query($sql);
$arr = get_goods_articles($goods_id);
$opt = array();
foreach ($arr AS $val)
{
$opt[] = array(‘value’ => $val[‘article_id’],
‘text’ => $val[‘title’],
‘data’ => ”);
}
clear_cache_files();
make_json_result($opt);
}
[/php]
在下面添加以下代码
[php]
/*—————————————————— */
//– 搜索用户会员
/*—————————————————— */
elseif ($_REQUEST[‘act’] == ‘get_user_list’){
include_once(ROOT_PATH . ‘includes/cls_json.php’);
$json = new JSON;
$filters =(array) $json->decode(json_str_iconv($_GET[‘JSON’]));
$where = " WHERE 1 ";
if (!empty($filters[‘user_name’]))
{
$keyword = trim($filters[‘user_name’]);
$where .= " AND user_name LIKE ‘%" . mysql_like_quote($keyword) . "%’ ";
}
$sql = ‘SELECT user_id, user_name FROM ‘ .$ecs->table(‘users’). $where.
‘ORDER BY user_id DESC LIMIT 50’;
$res = $db->query($sql);
$arr = array();
while ($row = $db->fetchRow($res)){
$arr[] = array(‘value’ => $row[‘user_id’], ‘text’ => $row[‘user_name’], ‘data’=>”);
}
make_json_result($arr);
}
/*—————————————————— */
//– 添加关联用户信息
/*—————————————————— */
elseif ($_REQUEST[‘act’] == ‘add_goods_user’)
{
include_once(ROOT_PATH . ‘includes/cls_json.php’);
$json = new JSON;
check_authz_json(‘goods_manage’);
$users = $json->decode($_GET[‘add_ids’]);
$arguments = $json->decode($_GET[‘JSON’]);
$goods_id = $arguments[0];
foreach ($users AS $val)
{
$sql = "INSERT INTO " . $ecs->table(‘plug_goods_users’) . " (goods_id, user_id, admin_id) " .
"VALUES (‘$goods_id’, ‘$val’, ‘$_SESSION[admin_id]’)";
$db->query($sql);
}
$arr = get_goods_users($goods_id);
$opt = array();
foreach ($arr AS $val)
{
$opt[] = array(‘value’ => $val[‘user_id’],
‘text’ => $val[‘user_name’],
‘data’ => ”);
}
clear_cache_files();
make_json_result($opt);
}
/*—————————————————— */
//– 删除关联用户信息
/*—————————————————— */
elseif ($_REQUEST[‘act’] == ‘drop_goods_user’)
{
include_once(ROOT_PATH . ‘includes/cls_json.php’);
$json = new JSON;
check_authz_json(‘goods_manage’);
$users = $json->decode($_GET[‘drop_ids’]);
$arguments = $json->decode($_GET[‘JSON’]);
$goods_id = $arguments[0];
$sql = "DELETE FROM " .$ecs->table(‘plug_goods_users’) . " WHERE " . db_create_in($users, "user_id") . " AND goods_id = ‘$goods_id’";
$db->query($sql);
$arr = get_goods_users($goods_id);
$opt = array();
foreach ($arr AS $val)
{
$opt[] = array(‘value’ => $val[‘user_id’],
‘text’ => $val[‘user_name’],
‘data’ => ”);
}
clear_cache_files();
make_json_result($opt);
}
[/php]
16.在admin\users.php中大概438~453之间查找
[php]
if (isset($_POST[‘checkboxes’]))
{
$sql = "SELECT user_name FROM " . $ecs->table(‘users’) . " WHERE user_id " . db_create_in($_POST[‘checkboxes’]);
$col = $db->getCol($sql);
$usernames = implode(‘,’,addslashes_deep($col));
$count = count($col);
/* 通过插件来删除用户 */
$users = init_users();
$users->remove_user($col);
admin_log($usernames, ‘batch_remove’, ‘users’);
$lnk[] = array(‘text’ => $_LANG[‘go_back’], ‘href’=>’users.php?act=list’);
sys_msg(sprintf($_LANG[‘batch_remove_success’], $count), 0, $lnk);
}
[/php]
替换为
[php]
if (isset($_POST[‘checkboxes’]))
{
$sql = "SELECT user_name FROM " . $ecs->table(‘users’) . " WHERE user_id " . db_create_in($_POST[‘checkboxes’]);
$col = $db->getCol($sql);
$usernames = implode(‘,’,addslashes_deep($col));
$count = count($col);
//删除用户关联产品
$del_sql = "delete from ". $ecs->table(‘plug_goods_users’) ." where user_id " . db_create_in($_POST[‘checkboxes’]);
$db->query($del_sql);
/* 通过插件来删除用户 */
$users = init_users();
$users->remove_user($col);
admin_log($usernames, ‘batch_remove’, ‘users’);
$lnk[] = array(‘text’ => $_LANG[‘go_back’], ‘href’=>’users.php?act=list’);
sys_msg(sprintf($_LANG[‘batch_remove_success’], $count), 0, $lnk);
}
[/php]
在同一文件下大概541~562行找到
[php]
/*—————————————————— */
//– 删除会员帐号
/*—————————————————— */
elseif ($_REQUEST[‘act’] == ‘remove’)
{
/* 检查权限 */
admin_priv(‘users_drop’);
$sql = "SELECT user_name FROM " . $ecs->table(‘users’) . " WHERE user_id = ‘" . $_GET[‘id’] . "’";
$username = $db->getOne($sql);
/* 通过插件来删除用户 */
$users = init_users();
$users->remove_user($username); //已经删除用户所有数据
/* 记录管理员操作 */
admin_log(addslashes($username), ‘remove’, ‘users’);
/* 提示信息 */
$link[] = array(‘text’ => $_LANG[‘go_back’], ‘href’=>’users.php?act=list’);
sys_msg(sprintf($_LANG[‘remove_success’], $username), 0, $link);
}
[/php]
替换
[php]
/*—————————————————— */
//– 删除会员帐号
/*—————————————————— */
elseif ($_REQUEST[‘act’] == ‘remove’)
{
/* 检查权限 */
admin_priv(‘users_drop’);
$sql = "SELECT user_name FROM " . $ecs->table(‘users’) . " WHERE user_id = ‘" . $_GET[‘id’] . "’";
$username = $db->getOne($sql);
//删除用户关联产品
$del_sql = "delete from " . $ecs->table(‘plug_goods_users’) . " where user_id = ‘". $_GET[‘id’] ."’";
$db->query($del_sql);
/* 通过插件来删除用户 */
$users = init_users();
$users->remove_user($username); //已经删除用户所有数据
/* 记录管理员操作 */
admin_log(addslashes($username), ‘remove’, ‘users’);
/* 提示信息 */
$link[] = array(‘text’ => $_LANG[‘go_back’], ‘href’=>’users.php?act=list’);
sys_msg(sprintf($_LANG[‘remove_success’], $username), 0, $link);
}
[/php]
二: 前台
1. 在根目录下goods.php文件查找
[php]$smarty->assign(‘affiliate’, $affiliate);[/php]
在下面添加一行
[php]$user_id = $_SESSION[‘user_id’];[/php]
2. 在同一文件下查找
[php]$smarty->display(‘goods.dwt’, $cache_id);[/php]
在上面添加以下代码
[php]
/* 只允许会员访问的商品 */
$gu_sql = ‘select * from ‘. $ecs->table(‘plug_goods_users’) .’ where goods_id = "’. $_REQUEST[id] .’"’;
$goods_users_list = $GLOBALS[‘db’]->getAll($gu_sql);
if(count($goods_users_list) > 0){
foreach($goods_users_list as $k => $v){
if($v[‘user_id’] == $user_id){
$smarty->assign(‘is_user’, $user_id);
}else{
$smarty->assign(‘is_user’, 0);
}
}
}else{
$smarty->assign(‘is_user’, -1);
}
[/php]
3. 在themes/You Template/goods.dwt下查找
[php]<!– #BeginLibraryItem "/library/right_side.lbi" –><!– #EndLibraryItem –>[/php]
上面添加一行
[php]<!– {if $is_user eq 1 or $is_user eq -1} –>[/php]
4. 同一文件查找
[php]<!– #BeginLibraryItem "/library/help.lbi" –><!– #EndLibraryItem –> <!– #BeginLibraryItem "/library/page_footer.lbi" –><!– #EndLibraryItem –>[/php]
上面添加以下代码
[php]<!– {elseif $is_user eq 0} –>
<div class="wrap clearfix">
<div class=" product_baihuo">
<p style="text-align:center; margin:100px auto; font-weight:700;">对不起,你没有权限查看该商品!</p>
</div>
</div>
<!– {/if} –>
[/php]
三:手机端设置
1. 在根目录mobile/include/apps/default/controller/GoodsController.class.php文件下查找
[php]protected $goods_id;[/php] 下面添加一行
[php]protected $user_id;[/php]
2. 在同一文件查找
[php]$this->model->table(‘goods’)->data($data)->where(‘goods_id = ‘ . $this->goods_id)->update();[/php]
下面一行添加以下代码
[php]
/* 只允许会员访问的商品 */
$this->user_id = $_SESSION[‘user_id’];
$goods_users_list = $this->model->table(‘plug_goods_users’)->where(‘goods_id = ‘ . $this->goods_id)->select();
if(count($goods_users_list) > 0){
foreach($goods_users_list as $k => $v){
if($v[‘user_id’] == $this->user_id){
$this->assign(‘is_user’, 1);
}else{
$this->assign(‘is_user’, 0);
}
}
}else{
$this->assign(‘is_user’, -1);
}
[/php]
3. 在根目录mobile/themes/default/goods.dwt文件中查找
[php]
<!–商品图片相册–>
[/php]
上面添加一行
[php]
<!– {if $is_user eq 1 or $is_user eq -1} –>
[/php]
4. 在同一文件找到
[php]
<footer class="logo">
[/php]
上面添加以下代码
[php]
<!– {elseif $is_user eq 0} –>
<div class="proinfo_mess"><p style="text-align:center; margin:100px auto; font-weight:700;">对不起,你没有权限查看该商品!</p></div>
<!– {/if} –>
[/php]
下载代码链接: http://pan.baidu.com/s/1i5LtDzf 密码: ayi5
发表评论