我使用的是opencart Multi商店,根据我的代码,我们有多达10家商店,每个商店我们都有客户,在特定商店注册的客户只能登录该商店,如果他试图登录他没有注册的任何其他商店,一定会有一条错误消息,比如你没有在这个商店注册或其他什么。。。根据我的代码,如果客户登录到其他未注册的商店,他只重定向到他的商店,但没有登录,它不应该重定向,他应该得到错误消息。。。
下面是我的控制器:
<?php
class ControllerAccountLogin extends Controller {
private $error = array();
public function index() {
//echo 44; exit;
$this->load->model('account/customer');
// Login override for admin users
if (!empty($this->request->get['token'])) {
$this->event->trigger('pre.customer.login');
$this->customer->logout();
$this->cart->clear();
unset($this->session->data['wishlist']);
unset($this->session->data['payment_address']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
unset($this->session->data['shipping_address']);
unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['comment']);
unset($this->session->data['order_id']);
unset($this->session->data['coupon']);
unset($this->session->data['reward']);
unset($this->session->data['voucher']);
unset($this->session->data['vouchers']);
$customer_info = $this->model_account_customer->getCustomerByToken($this->request->get['token']);
if ($customer_info && $this->customer->login($customer_info['email'], '', true)) {
// Default Addresses
$this->load->model('account/address');
if ($this->config->get('config_tax_customer') == 'payment') {
$this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
if ($this->config->get('config_tax_customer') == 'shipping') {
$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
$this->event->trigger('post.customer.login');
$this->response->redirect($this->url->link('account/account', '', 'SSL'));
}
}
if ($this->customer->isLogged()) {
$this->response->redirect($this->url->link('account/account', '', 'SSL'));
}
$this->load->language('account/login');
$this->document->setTitle($this->language->get('heading_title'));
if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
//echo $this->config->get('config_store_id'); exit;
if ($this->config->get('config_store_id')) {
// echo 'sravz';
// echo $this->customer->getStoreId(); exit;
if ($this->customer->getStoreId() != 0) {
if ($this->customer->getStoreId() != $this->config->get('config_store_id')) {
$this->load->model('setting/store');
// $store_info = $this->model_setting_store->getStore($this->customer->getStoreId());
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "store where store_id=".$this->customer->getStoreId()."");
$store_info = $query->row;
$this->response->redirect($store_info['url']);
}
}
}
unset($this->session->data['guest']);
// Default Shipping Address
$this->load->model('account/address');
if ($this->config->get('config_tax_customer') == 'payment') {
$this->session->data['payment_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
if ($this->config->get('config_tax_customer') == 'shipping') {
$this->session->data['shipping_address'] = $this->model_account_address->getAddress($this->customer->getAddressId());
}
// Add to activity log
$this->load->model('account/activity');
$activity_data = array(
'customer_id' => $this->customer->getId(),
'name' => $this->customer->getFirstName() . ' ' . $this->customer->getLastName()
);
$this->model_account_activity->addActivity('login', $activity_data);
// Added strpos check to pass McAfee PCI compliance test (http://forum.opencart.com/viewtopic.php?f=10&t=12043&p=151494#p151295)
if (isset($this->request->post['redirect']) && (strpos($this->request->post['redirect'], $this->config->get('config_url')) !== false || strpos($this->request->post['redirect'], $this->config->get('config_ssl')) !== false)) {
$this->response->redirect(str_replace('&', '&', $this->request->post['redirect']));
} else {
$this->response->redirect($this->url->link('account/account', '', 'SSL'));
}
}
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_home'),
'href' => $this->url->link('common/home')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_account'),
'href' => $this->url->link('account/account', '', 'SSL')
);
$data['breadcrumbs'][] = array(
'text' => $this->language->get('text_login'),
'href' => $this->url->link('account/login', '', 'SSL')
);
$data['heading_title'] = $this->language->get('heading_title');
$data['text_new_customer'] = $this->language->get('text_new_customer');
$data['text_register'] = $this->language->get('text_register');
$data['text_register_account'] = $this->language->get('text_register_account');
$data['text_returning_customer'] = $this->language->get('text_returning_customer');
$data['text_i_am_returning_customer'] = $this->language->get('text_i_am_returning_customer');
$data['text_forgotten'] = $this->language->get('text_forgotten');
$data['entry_email'] = $this->language->get('entry_email');
$data['entry_password'] = $this->language->get('entry_password');
$data['button_continue'] = $this->language->get('button_continue');
$data['button_login'] = $this->language->get('button_login');
if (isset($this->error['warning'])) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = $this->url->link('account/login', '', 'SSL');
$data['register'] = $this->url->link('account/register', '', 'SSL');
$data['forgotten'] = $this->url->link('account/forgotten', '', 'SSL');
// Added strpos check to pass McAfee PCI compliance test (http://forum.opencart.com/viewtopic.php?f=10&t=12043&p=151494#p151295)
if (isset($this->request->post['redirect']) && (strpos($this->request->post['redirect'], $this->config->get('config_url')) !== false || strpos($this->request->post['redirect'], $this->config->get('config_ssl')) !== false)) {
$data['redirect'] = $this->request->post['redirect'];
} elseif (isset($this->session->data['redirect'])) {
$data['redirect'] = $this->session->data['redirect'];
unset($this->session->data['redirect']);
} else {
$data['redirect'] = '';
}
if (isset($this->session->data['success'])) {
$data['success'] = $this->session->data['success'];
unset($this->session->data['success']);
} else {
$data['success'] = '';
}
if (isset($this->request->post['email'])) {
$data['email'] = $this->request->post['email'];
} else {
$data['email'] = '';
}
if (isset($this->request->post['password'])) {
$data['password'] = $this->request->post['password'];
} else {
$data['password'] = '';
}
$data['column_left'] = $this->load->controller('common/column_left');
$data['column_right'] = $this->load->controller('common/column_right');
$data['content_top'] = $this->load->controller('common/content_top');
$data['content_bottom'] = $this->load->controller('common/content_bottom');
$data['footer'] = $this->load->controller('common/footer');
$data['header'] = $this->load->controller('common/header');
$data['account'] = $this->load->controller('module/account');
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/login.tpl')) {
$this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/account/login.tpl', $data));
} else {
$this->response->setOutput($this->load->view('default/template/account/login.tpl', $data));
}
}
protected function validate() {
//echo $this->request->post['email'];exit;
$this->event->trigger('pre.customer.login');
// Check how many login attempts have been made.
$login_info = $this->model_account_customer->getLoginAttempts($this->request->post['email']);
if ($login_info && ($login_info['total'] >= $this->config->get('config_login_attempts')) && strtotime('-1 hour') < strtotime($login_info['date_modified'])) {
$this->error['warning'] = $this->language->get('error_attempts');
}
// Check if customer has been approved.
$customer_info = $this->model_account_customer->getCustomerByEmail($this->request->post['email']);
//print_r($customer_info);exit;
if ($customer_info && !$customer_info['approved']) {
$this->error['warning'] = $this->language->get('error_approved');
}
//print_r( $this->error); exit;
if (!$this->error) {
if (!$this->customer->login($this->request->post['email'], $this->request->post['password'])) {
//echo 44; exit;
// $this->error['warning'] = $this->language->get('error_login');
// $this->model_account_customer->addLoginAttempt($this->request->post['email']);
} else {
$this->model_account_customer->deleteLoginAttempts($this->request->post['email']);
$this->event->trigger('post.customer.login');
}
}
return !$this->error;
}
}
以下是我的观点:
<?php echo $header; ?>
<div class="container">
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
<?php } ?>
</ul>
<?php if ($success) { ?>
<div class="alert alert-success"><i class="fa fa-check-circle"></i> <?php echo $success; ?></div>
<?php } ?>
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?></div>
<?php } ?>
<div class="row"><?php echo $column_left; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6 no-pad'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>"><?php echo $content_top; ?>
<div class="panel panel-primary">
<div class="top">
<div class="panel-heading title-heading search-head"><?php echo $heading_title; ?></div>
</div>
<div class="panel-body">
<div class="row">
<!-- <h2></h2>-->
<div class="col-sm-6">
<b style="margin-bottom: 2px; display: block;"><?php echo $text_new_customer; ?></b>
<div class="well left">
<!-- <p><strong><?php echo $text_register; ?></strong></p>-->
<p><?php echo $text_register_account; ?></p>
<!-- <a href="#" class="btn btn-primary"><?php echo "Go to Shopping"; ?></a> --></div>
</div>
<div class="col-sm-6">
<b style="margin-bottom: 2px; display: block;"><?php echo $text_returning_customer; ?></b>
<div class="well">
<!-- <h2><?php echo $text_returning_customer; ?></h2>-->
<p><?php echo $text_i_am_returning_customer; ?></p>
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="control-label" for="input-email"> <strong> <?php echo $entry_email; ?></strong></label>
<input type="text" name="email" value="<?php echo $email; ?>" placeholder="<?php echo $entry_email; ?>" id="input-email" class="form-control" />
</div>
<div class="form-group">
<label class="control-label" for="input-password"> <strong> <?php echo $entry_password; ?></strong></label></strong>
<input type="password" name="password" value="<?php echo $password; ?>" placeholder="<?php echo $entry_password; ?>" id="input-password" class="form-control" />
<a href="<?php echo $forgotten; ?>"><?php echo $text_forgotten; ?></a></div>
<input type="submit" value="<?php echo $button_login; ?>" class="btn btn-primary" />
<?php if ($redirect) { ?>
<input type="hidden" name="redirect" value="<?php echo $redirect; ?>" />
<?php } ?>
</form>
</div>
</div>
</div>
</div>
<?php echo $content_bottom; ?></div></div>
<div class="clearfix hidden-sm hidden-md hidden-lg"></div>
<?php echo $column_right; ?>
<div class="hidden-sm hidden-md hidden-lg"><div class="container"><?php echo $account; ?></div></div>
</div>
</div>
<?php echo $footer; ?>
要向客户返回错误消息,您必须检查该客户是否是分配给该商店的客户组的一部分。在catalog/controller/account/login.php
:中的validate()
方法开始时使用此项
$customer_info = $this->model_account_customer->getCustomerByEmail($this->request->post['email']);
$customerGroupId = isset($customer_info['customer_group_id']) ? $customer_info['customer_group_id'] : -1;
$allowedCustomerGroups = $this->config->get('config_customer_group_display');
if(!is_array($allowedCustomerGroups) || !in_array($customerGroupId, $allowedCustomerGroups)) {
$this->error['warning'] = $this->language->get('error_login');
return false;
}
请记住,如果您的客户登录到正确的商店,然后转到另一家商店,这将不会起到任何作用。
如果您想在客户更换商店时注销,请使用此代码并粘贴在catalog/controller/common/header.php
中第90行左右的$data
变量声明之后:
if ($this->customer->isLogged() && ($this->config->get('config_store_id') != $this->customer->getStoreId())) {
$this->url->link('account/logout', '', true);
$this->customer->logout();
}
然后
您必须在system/library/cart/customer.php
文件中添加此方法:
public function getStoreId() {
return $this->store_id;
}
希望这有帮助:)