Authored by xiaowei

PC重构 登录

<?php
use Action\AbstractAction;
use Action\WebAction;
use LibModels\Web\Passport\RegData;
use LibModels\Web\Passport\LoginData;
use Passport\PassportModel as PassportModel;
use Plugin\Helpers;
class LoginController extends AbstractAction
class LoginController extends WebAction
{
/**
... ... @@ -23,7 +22,7 @@ class LoginController extends AbstractAction
$this->setCookie('_TOKEN', '');
//登录后跳转页面
$refer = empty($_SERVER["HTTP_REFERER"])?'':$_SERVER["HTTP_REFERER"];
$refer = empty($_SERVER["HTTP_REFERER"]) ? '' : $_SERVER["HTTP_REFERER"];
if (!empty($refer)) {
$this->setCookie('refer', $refer);
}
... ... @@ -40,13 +39,21 @@ class LoginController extends AbstractAction
$simpleHeader = PassportModel::getSimpleHeader($isLogin, $username);
//获取登陆页左侧资源
$cover = PassportModel::getLeftBanner(PassportModel::SIGNIN_LEFT_BANNER_CODE);
//是否记住密码
$isRemember = $this->getCookie('isRemember', FALSE);
$account = '';
$password = '';
if ($isRemember) {
$account = $this->decrypt($this->getCookie('account', ''));
$password = $this->decrypt($this->getCookie('userInfo', ''));
}
//整合
$data = array(
'loginPage' => true,
'simpleHeader' => $simpleHeader,
'passport' => array(
'coverHref' => $cover['url'],
'coverImg' => !empty($cover['img'])?$cover['img']:'http://img12.static.yhbimg.com/yhb-img01/2015/12/01/07/020a0b6e7ff908d0c2bc4045b4fef42b9f.png?imageView/2/w/252/h/190',
'coverImg' => !empty($cover['img']) ? $cover['img'] : 'http://img12.static.yhbimg.com/yhb-img01/2015/12/01/07/020a0b6e7ff908d0c2bc4045b4fef42b9f.png?imageView/2/w/252/h/190',
'countryCode' => '+86',
'countryName' => '中国',
'countryList' => RegData::getAreasData(),
... ... @@ -58,6 +65,9 @@ class LoginController extends AbstractAction
'alipayLogin' => Helpers::url('/passport/autosign/alipay'),
'doubanLogin' => Helpers::url('/passport/autosign/douban'),
'renrenLogin' => Helpers::url('/passport/autosign/renren'),
'isRemember' => $isRemember,
'password' => $password,
'account' => $account
),
);
... ... @@ -84,15 +94,17 @@ class LoginController extends AbstractAction
/* 判断参数是否传递 */
$area = trim($this->post('areaCode', '86'));
$profile = trim($this->post('account'));
$account = trim($this->post('account'));
$password = trim($this->post('password'));
if (!is_numeric($area) || empty($profile) || empty($password)) {
$isRemember = trim($this->post('isRemember'));
$isRemember = false;
if (!is_numeric($area) || empty($account) || empty($password)) {
break;
}
/* 判断参数是否有效 */
$verifyEmail = Helpers::verifyEmail($profile);
$verifyMobile = ($area === '86') ? Helpers::verifyMobile($profile) : Helpers::verifyAreaMobile($profile, $area);
$verifyEmail = Helpers::verifyEmail($account);
$verifyMobile = ($area === '86') ? Helpers::verifyMobile($account) : Helpers::verifyAreaMobile($account, $area);
if (!$verifyEmail && !$verifyMobile) {
break;
}
... ... @@ -106,10 +118,21 @@ class LoginController extends AbstractAction
/* 调用登录接口进行登录 */
// 获取未登录时的唯一识别码
$shoppingKey = Helpers::getShoppingKeyByCookie();
$data = LoginData::signin($area, $profile, $password, $shoppingKey);
$data = LoginData::signin($area, $account, $password, $shoppingKey);
if (!isset($data['code']) || $data['code'] != 200 || !isset($data['data']['uid'])) {
break;
}
//登录成功记录账户信息
if ($isRemember) {
$this->setCookie('account', $this->encrypt($account), time() + 3600 * 24 * 7, '/');
$this->setCookie('userInfo', $this->encrypt($password), time() + 3600 * 24 * 7, '/');
$this->setCookie('isRemember', $isRemember, time() + 3600 * 24 * 7);
}
else {
$this->setCookie('account', '', -1, '/');
$this->setCookie('userInfo', '', -1, '/');
$this->setCookie('isRemember', '', -1, '/');
}
$refer = $this->getCookie('refer');
if (empty($refer)) {
... ... @@ -152,4 +175,60 @@ class LoginController extends AbstractAction
$this->go($refer);
}
/*
* 加密算法
*/
private function encrypt($data, $key='yohobuy')
{
$key = md5($key);
$x = 0;
$str='';
$char='';
$len = strlen($data);
$l = strlen($key);
for ($i = 0; $i < $len; $i++) {
if ($x == $l) {
$x = 0;
}
$char .= $key{$x};
$x++;
}
for ($i = 0; $i < $len; $i++) {
$str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
}
return base64_encode($str);
}
/*
* 解密算法
*/
private function decrypt($data, $key='yohobuy')
{
$key = md5($key);
$x = 0;
$str='';
$char='';
$data = base64_decode($data);
$len = strlen($data);
$l = strlen($key);
for ($i = 0; $i < $len; $i++) {
if ($x == $l) {
$x = 0;
}
$char .= substr($key, $x, 1);
$x++;
}
for ($i = 0; $i < $len; $i++) {
if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
}
else {
$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
}
}
return $str;
}
}
... ...