默认情况下,magento只支持邮箱登录,所以我们需要修改一下代码。思路是:可以通过输入的手机号和用户名来查找邮箱。
首先需要重写控制器,添加配置文件
<rewrite>
<模块名_控制器_account>
<from><![CDATA[#^/customer/account/#]]></from>
<to>/icustomer/account/</to>
</模块名_控制器_account>
现在,需要在你的模块下面的控制器文件夹新建立一个accountController.php这个控制器,然后重写里面哦loginPostAction这个方法:
public function loginPostAction()
{
if (!$this->_validateFormKey()) {
$this->_redirect('*/*/');
return;
}
if ($this->_getSession()->isLoggedIn()) {
$this->_redirect('*/*/');
return;
}
$session = $this->_getSession();
if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost('login');
$data = $login["username"];
//手机号和用户名登录过程
if (!(int)($data))
{
//如过输入的是字符就通过username把email查询出来
$read = Mage::getSingleton("core/resource")->getConnection('core_read');
$sql = "select * from `customer_entity` where username ='{$data}'";
$result = $read->fetchRow($sql);
$dataA = $result["email"];
$login['username'] = $dataA;
}else
{
//如果输入的是数字就通过手机号把email查询出来
$read = Mage::getSingleton("core/resource")->getConnection('core_read');
$sql = "select * from `customer_entity` where customer_mobile_number ='{$data}'";
$result = $read->fetchRow($sql);
$dataA = $result["email"];
$login['username'] = $dataA;
}
if (!empty($login['username']) && !empty($login['password'])) {
try {
$session->login($login['username'], $login['password']);
if ($session->getCustomer()->getIsJustConfirmed()) {
$this->_welcomeCustomer($session->getCustomer(), true);
}
} catch (Mage_Core_Exception $e) {
switch ($e->getCode()) {
case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
$value = $this->_getHelper('customer')->getEmailConfirmationUrl($login['username']);
$message = $this->_getHelper('customer')->__('该帐户尚未确认。<a href="%s">点击这里</a> 获取确认邮箱。', $value);
break;
case Mage_Customer_Model_Customer::EXCEPTION_INVALID_EMAIL_OR_PASSWORD:
$message = $e->getMessage();
break;
default:
$message = $e->getMessage();
}
$session->addError($message);
$session->setUsername($login['username']);
} catch (Exception $e) {
// Mage::logException($e); // PA DSS violation: this exception log can disclose customer password
}
} else {
$session->addError($this->__('用户名和密码错误'));
}
}
$this->_loginPostRedirect();
}
只需要对控制器加一个if判断语句就能很简单的实现使用手机号和用户名登录。
转载于:https://blog.51cto.com/11011023/1723400