我想为我的android项目添加手动登录和注册Web服务。以及如何在php的帮助下做到这一点?我已经尝试了登录的这些步骤
<?php
class My_Module_Controller extends Mage_Core_Controller_Front_Action {
public function indexAction() {
// if customer is not logged in
if(!Mage::getSingleton('customer/session')->isLoggedIn())
{
// get the email and load the customer by id
$login = $this->getRequest()->getPost('login');
$email = $login['hi@gmail.com'];
$customer = Mage::getModel('customer/customer')
->setWebsiteId(Mage::app()->getStore()
->getWebsiteId())->loadByEmail($email);
$quote = Mage::getSingleton('checkout/cart')->getQuote();
//If the customer exists, log them in by forwarding to loginPost
if($customer->getId())
{
// just make the customer log in
$mysession = Mage::getSingleton('customer/session');
$mysession->setBeforeAuthUrl(Mage::getUrl('checkout/cart'));
$mysession->setAfterAuthUrl(Mage::getUrl('checkout/cart'));
$this->_forward('login','account','customer');
}
else
{
//There is no customer with that email.
}
}
$this->_redirect('checkout/cart');
return;
}
}
?>
这个用于注册
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function loginAction()
{
$session = Mage::getSingleton('customer/session');
if ($session->isLoggedIn()) {
// is already login redirect to account page
return;
}
$result = array('success' => false);
if ($this->getRequest()->isPost())
{
$login_data = $this->getRequest()->getPost('login');
if (empty($login_data['username']) || empty($login_data['password'])) {
$result['error'] = Mage::helper('onepagecheckout')->__('Login and password are required.');
}
else
{
try
{
$session->login($login_data['username'], $login_data['password']);
$result['success'] = true;
$result['redirect'] = Mage::getUrl('*/*/index');
}
catch (Mage_Core_Exception $e)
{
switch ($e->getCode()) {
case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED:
$message = Mage::helper('onepagecheckout')->__('Email is not confirmed. Resend confirmation email.', Mage::helper('customer')->getEmailConfirmationUrl($login_data['username']));
break;
default:
$message = $e->getMessage();
}
$result['error'] = $message;
$session->setUsername($login_data['username']);
}
}
}
$this->_redirect('customer/account/');
//$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
但是我做不到。我怎样才能让它运行我通过谷歌搜索
用于登录
$websiteId = Mage::getModel('core/store')->load($store)->getWebsiteId();
$res = array();
$res["username"] = $username;
$res["password"] = base64_decode($password);
$login_status = 1;
try{
$login_customer_result = Mage::getModel('customer/customer')->setWebsiteId($websiteId)->authenticate($username, base64_decode($password));
$login_customer = Mage::getModel('customer/customer')->setWebsiteId($websiteId);
$login_customer->loadByEmail($username);
$res["firstname"] = $login_customer->firstname;
$res["lastname"] = $login_customer->lastname;
$res["id"] = $login_customer->getId();
//$res["password"]
//$res["password"]
}
catch( Exception $e ){
$login_status = 0;
}
$res["login_status"] = $login_status;
return $res;
Fir注册
$res = array();
$websiteId = Mage::getModel('core/store')->load($store)->getWebsiteId();
$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId;
$customer->setCurrentStore($store);
// echo 'Phase 2';
try {
// If new, save customer information
$customer->firstname = $firstname;
$customer->lastname = $lastname;
$customer->email = $email;
$customer->password_hash = md5(base64_decode($password));
$res["email"] =$email;
$res["firstname"] =$firstname;
$res["lastname"] =$lastname;
$res["password"] =$password;
$res["status"] = 0;
$res["id"] = 0;
$cust = Mage::getModel('customer/customer')
->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
->loadByEmail($email);
//check exists email address of users
if ($cust->getId()) {
$res["id"] = $cust->getId();
$res["status"] = 0;
}else {
//echo 'Phase 2.5';
if($customer->save()){
$customer->sendNewAccountEmail('confirmed');
$this->send_Password_Mail_to_NewUser($firstname, base64_decode($password), $email);
$res["id"] = $customer->getId();
$res["status"] = 1;
}
else{
//echo "Already Exist";
$exist_customer = Mage::getModel("customer/customer");
$exist_customer->setWebsiteId($websiteId);
$exist_customer->setCurrentStore($store);
$exist_customer->loadByEmail($email);
$res["id"] = $exist_customer->getId();
$res["status"] = 1;
//echo "An error occured while saving customer";
}
}
//echo 'Phase 3';
}
catch(Exception $e){
//echo "Already Exist Exception";
try {
$exist_customer = Mage::getModel("customer/customer");
$exist_customer->setWebsiteId($websiteId);
$exist_customer->setCurrentStore($store);
$exist_customer->loadByEmail($email);
$res["id"] = $exist_customer->getId();
$res["status"] = 1;
}
catch(Exception $ex) {
$res["id"] = -1;
$res["status"] = 0;
}
}
return $res;