Authored by xiaowei

PC重构 登录

1 <?php 1 <?php
2 2
3 -use Action\AbstractAction; 3 +use Action\WebAction;
4 use LibModels\Web\Passport\RegData; 4 use LibModels\Web\Passport\RegData;
5 use LibModels\Web\Passport\LoginData; 5 use LibModels\Web\Passport\LoginData;
6 use Passport\PassportModel as PassportModel; 6 use Passport\PassportModel as PassportModel;
7 use Plugin\Helpers; 7 use Plugin\Helpers;
8 -  
9 -class LoginController extends AbstractAction 8 +class LoginController extends WebAction
10 { 9 {
11 10
12 /** 11 /**
@@ -23,7 +22,7 @@ class LoginController extends AbstractAction @@ -23,7 +22,7 @@ class LoginController extends AbstractAction
23 $this->setCookie('_TOKEN', ''); 22 $this->setCookie('_TOKEN', '');
24 23
25 //登录后跳转页面 24 //登录后跳转页面
26 - $refer = empty($_SERVER["HTTP_REFERER"])?'':$_SERVER["HTTP_REFERER"]; 25 + $refer = empty($_SERVER["HTTP_REFERER"]) ? '' : $_SERVER["HTTP_REFERER"];
27 if (!empty($refer)) { 26 if (!empty($refer)) {
28 $this->setCookie('refer', $refer); 27 $this->setCookie('refer', $refer);
29 } 28 }
@@ -40,13 +39,21 @@ class LoginController extends AbstractAction @@ -40,13 +39,21 @@ class LoginController extends AbstractAction
40 $simpleHeader = PassportModel::getSimpleHeader($isLogin, $username); 39 $simpleHeader = PassportModel::getSimpleHeader($isLogin, $username);
41 //获取登陆页左侧资源 40 //获取登陆页左侧资源
42 $cover = PassportModel::getLeftBanner(PassportModel::SIGNIN_LEFT_BANNER_CODE); 41 $cover = PassportModel::getLeftBanner(PassportModel::SIGNIN_LEFT_BANNER_CODE);
  42 + //是否记住密码
  43 + $isRemember = $this->getCookie('isRemember', FALSE);
  44 + $account = '';
  45 + $password = '';
  46 + if ($isRemember) {
  47 + $account = $this->decrypt($this->getCookie('account', ''));
  48 + $password = $this->decrypt($this->getCookie('userInfo', ''));
  49 + }
43 //整合 50 //整合
44 $data = array( 51 $data = array(
45 'loginPage' => true, 52 'loginPage' => true,
46 'simpleHeader' => $simpleHeader, 53 'simpleHeader' => $simpleHeader,
47 'passport' => array( 54 'passport' => array(
48 'coverHref' => $cover['url'], 55 'coverHref' => $cover['url'],
49 - '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', 56 + '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',
50 'countryCode' => '+86', 57 'countryCode' => '+86',
51 'countryName' => '中国', 58 'countryName' => '中国',
52 'countryList' => RegData::getAreasData(), 59 'countryList' => RegData::getAreasData(),
@@ -58,6 +65,9 @@ class LoginController extends AbstractAction @@ -58,6 +65,9 @@ class LoginController extends AbstractAction
58 'alipayLogin' => Helpers::url('/passport/autosign/alipay'), 65 'alipayLogin' => Helpers::url('/passport/autosign/alipay'),
59 'doubanLogin' => Helpers::url('/passport/autosign/douban'), 66 'doubanLogin' => Helpers::url('/passport/autosign/douban'),
60 'renrenLogin' => Helpers::url('/passport/autosign/renren'), 67 'renrenLogin' => Helpers::url('/passport/autosign/renren'),
  68 + 'isRemember' => $isRemember,
  69 + 'password' => $password,
  70 + 'account' => $account
61 ), 71 ),
62 ); 72 );
63 73
@@ -84,15 +94,17 @@ class LoginController extends AbstractAction @@ -84,15 +94,17 @@ class LoginController extends AbstractAction
84 94
85 /* 判断参数是否传递 */ 95 /* 判断参数是否传递 */
86 $area = trim($this->post('areaCode', '86')); 96 $area = trim($this->post('areaCode', '86'));
87 - $profile = trim($this->post('account')); 97 + $account = trim($this->post('account'));
88 $password = trim($this->post('password')); 98 $password = trim($this->post('password'));
89 - if (!is_numeric($area) || empty($profile) || empty($password)) { 99 + $isRemember = trim($this->post('isRemember'));
  100 + $isRemember = false;
  101 + if (!is_numeric($area) || empty($account) || empty($password)) {
90 break; 102 break;
91 } 103 }
92 104
93 /* 判断参数是否有效 */ 105 /* 判断参数是否有效 */
94 - $verifyEmail = Helpers::verifyEmail($profile);  
95 - $verifyMobile = ($area === '86') ? Helpers::verifyMobile($profile) : Helpers::verifyAreaMobile($profile, $area); 106 + $verifyEmail = Helpers::verifyEmail($account);
  107 + $verifyMobile = ($area === '86') ? Helpers::verifyMobile($account) : Helpers::verifyAreaMobile($account, $area);
96 if (!$verifyEmail && !$verifyMobile) { 108 if (!$verifyEmail && !$verifyMobile) {
97 break; 109 break;
98 } 110 }
@@ -106,10 +118,21 @@ class LoginController extends AbstractAction @@ -106,10 +118,21 @@ class LoginController extends AbstractAction
106 /* 调用登录接口进行登录 */ 118 /* 调用登录接口进行登录 */
107 // 获取未登录时的唯一识别码 119 // 获取未登录时的唯一识别码
108 $shoppingKey = Helpers::getShoppingKeyByCookie(); 120 $shoppingKey = Helpers::getShoppingKeyByCookie();
109 - $data = LoginData::signin($area, $profile, $password, $shoppingKey); 121 + $data = LoginData::signin($area, $account, $password, $shoppingKey);
110 if (!isset($data['code']) || $data['code'] != 200 || !isset($data['data']['uid'])) { 122 if (!isset($data['code']) || $data['code'] != 200 || !isset($data['data']['uid'])) {
111 break; 123 break;
112 } 124 }
  125 + //登录成功记录账户信息
  126 + if ($isRemember) {
  127 + $this->setCookie('account', $this->encrypt($account), time() + 3600 * 24 * 7, '/');
  128 + $this->setCookie('userInfo', $this->encrypt($password), time() + 3600 * 24 * 7, '/');
  129 + $this->setCookie('isRemember', $isRemember, time() + 3600 * 24 * 7);
  130 + }
  131 + else {
  132 + $this->setCookie('account', '', -1, '/');
  133 + $this->setCookie('userInfo', '', -1, '/');
  134 + $this->setCookie('isRemember', '', -1, '/');
  135 + }
113 136
114 $refer = $this->getCookie('refer'); 137 $refer = $this->getCookie('refer');
115 if (empty($refer)) { 138 if (empty($refer)) {
@@ -152,4 +175,60 @@ class LoginController extends AbstractAction @@ -152,4 +175,60 @@ class LoginController extends AbstractAction
152 $this->go($refer); 175 $this->go($refer);
153 } 176 }
154 177
  178 + /*
  179 + * 加密算法
  180 + */
  181 +
  182 + private function encrypt($data, $key='yohobuy')
  183 + {
  184 + $key = md5($key);
  185 + $x = 0;
  186 + $str='';
  187 + $char='';
  188 + $len = strlen($data);
  189 + $l = strlen($key);
  190 + for ($i = 0; $i < $len; $i++) {
  191 + if ($x == $l) {
  192 + $x = 0;
  193 + }
  194 + $char .= $key{$x};
  195 + $x++;
  196 + }
  197 + for ($i = 0; $i < $len; $i++) {
  198 + $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
  199 + }
  200 + return base64_encode($str);
  201 + }
  202 +
  203 + /*
  204 + * 解密算法
  205 + */
  206 +
  207 + private function decrypt($data, $key='yohobuy')
  208 + {
  209 + $key = md5($key);
  210 + $x = 0;
  211 + $str='';
  212 + $char='';
  213 + $data = base64_decode($data);
  214 + $len = strlen($data);
  215 + $l = strlen($key);
  216 + for ($i = 0; $i < $len; $i++) {
  217 + if ($x == $l) {
  218 + $x = 0;
  219 + }
  220 + $char .= substr($key, $x, 1);
  221 + $x++;
  222 + }
  223 + for ($i = 0; $i < $len; $i++) {
  224 + if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
  225 + $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
  226 + }
  227 + else {
  228 + $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
  229 + }
  230 + }
  231 + return $str;
  232 + }
  233 +
155 } 234 }