Authored by xiaowei

防撞库修改

@@ -143,20 +143,15 @@ class PassportModel @@ -143,20 +143,15 @@ class PassportModel
143 * $expiry 缓存时间 143 * $expiry 缓存时间
144 */ 144 */
145 145
146 - public static function increment($key, $timeKey, $offset = 1, $initValue = 0, $expire = 1800) 146 + public static function increment($key, $offset = 1, $initValue = 0, $expire = 1800)
147 { 147 {
148 - //有效期之外清除key  
149 - if (!Cache::get($timeKey) && Cache::get($key)) {  
150 - Cache::delete($key);  
151 - }  
152 //初始化计时 148 //初始化计时
153 - if (!Cache::get($timeKey) && !Cache::get($key)) {  
154 - Cache::set($timeKey, TRUE, $expire);  
155 - Cache::set($key, $initValue); 149 + if (!Cache::get($key)) {
  150 + Cache::set($key, $initValue, $expire);
156 } 151 }
157 - //未过期则递增 152 + //递增,过期则重新计算
158 $cacheValue = intval(Cache::get($key)) + $offset; 153 $cacheValue = intval(Cache::get($key)) + $offset;
159 - Cache::set($key, $cacheValue); 154 + Cache::set($key, $cacheValue, $expire);
160 } 155 }
161 156
162 } 157 }
@@ -108,24 +108,14 @@ class LoginController extends WebAction @@ -108,24 +108,14 @@ class LoginController extends WebAction
108 */ 108 */
109 $ip = Helpers::getClientIp(); 109 $ip = Helpers::getClientIp();
110 $ipKey = md5('ip_signin_' . $ip); 110 $ipKey = md5('ip_signin_' . $ip);
111 - $ipTimeKey = md5('ip_signin_time' . $ip);  
112 $accountKey = md5('account_signin_' . $account); 111 $accountKey = md5('account_signin_' . $account);
113 - $accountTimeKey = md5('account_signin_time' . $account);  
114 - //cache初始化,非有效时间内清除次数,有效时间内叠加cache计数  
115 - if (!Cache::get($accountTimeKey) && Cache::get($accountKey)) {  
116 - Cache::delete($accountKey); 112 + if (!Cache::get($accountKey)) {
  113 + Cache::set($accountKey, 0, 1800);
117 } 114 }
118 - if (!Cache::get($accountTimeKey) && !Cache::get($accountKey)) {  
119 - Cache::set($accountTimeKey, true, 1800);  
120 - Cache::set($accountKey, 0);  
121 - }  
122 - if (!Cache::get($ipTimeKey) && Cache::get($ipKey)) {  
123 - Cache::delete($ipKey);  
124 - }  
125 - if (!Cache::get($ipTimeKey) && !Cache::get($ipKey)) {  
126 - Cache::set($ipTimeKey, true, 3600);  
127 - Cache::set($ipKey, 0); 115 + if (!Cache::get($ipKey)) {
  116 + Cache::set($ipKey, 0, 3600);
128 } 117 }
  118 + //调用接口前校验次数
129 $accountTimes = Cache::get($accountKey); 119 $accountTimes = Cache::get($accountKey);
130 $ipTimes = Cache::get($ipKey); 120 $ipTimes = Cache::get($ipKey);
131 if ($accountTimes >= 10) { 121 if ($accountTimes >= 10) {
@@ -138,8 +128,19 @@ class LoginController extends WebAction @@ -138,8 +128,19 @@ class LoginController extends WebAction
138 } 128 }
139 $data = LoginData::signin($area, $account, $password, $shoppingKey); 129 $data = LoginData::signin($area, $account, $password, $shoppingKey);
140 if (!isset($data['code']) || $data['code'] != 200 || !isset($data['data']['uid'])) { 130 if (!isset($data['code']) || $data['code'] != 200 || !isset($data['data']['uid'])) {
141 - Cache::set($accountKey, intval(Cache::get($accountKey)) + 1);  
142 - Cache::set($ipKey, intval(Cache::get($ipKey)) + 1); 131 + Cache::set($accountKey, intval(Cache::get($accountKey)) + 1, 1800);
  132 + Cache::set($ipKey, intval(Cache::get($ipKey)) + 1, 3600);
  133 + //再次校验
  134 + $accountTimes = Cache::get($accountKey);
  135 + $ipTimes = Cache::get($ipKey);
  136 + if ($accountTimes >= 1) {
  137 + $data = array('code' => 400, 'message' => '您的账号已被暂时锁定,请稍后再试', 'data' => '');
  138 + break;
  139 + }
  140 + if ($ipTimes >= 100) {
  141 + $data = array('code' => 400, 'message' => '您尝试的次数过多,账号已被暂时锁定,请稍后再试', 'data' => '');
  142 + break;
  143 + }
143 $data = array('code' => 400, 'message' => '您输入的密码及账户名不匹配,是否<a href="' . Helpers::url('/passport/back/index') . '" target="_blank">忘记密码?</a>', 'data' => ''); 144 $data = array('code' => 400, 'message' => '您输入的密码及账户名不匹配,是否<a href="' . Helpers::url('/passport/back/index') . '" target="_blank">忘记密码?</a>', 'data' => '');
144 break; 145 break;
145 } 146 }
@@ -48,8 +48,7 @@ class RegisterController extends WebAction @@ -48,8 +48,7 @@ class RegisterController extends WebAction
48 $ip = Helpers::getClientIp(); 48 $ip = Helpers::getClientIp();
49 $data = array('code' => 400, 'message' => '', 'data' => ''); 49 $data = array('code' => 400, 'message' => '', 'data' => '');
50 $ipKey = md5('ip_checkmobile_' . $ip); 50 $ipKey = md5('ip_checkmobile_' . $ip);
51 - $ipTimeKey = md5('ip_checkmobile_time_' . $ip);  
52 - PassportModel::increment($ipKey,$ipTimeKey,1, 0, 3600); 51 + PassportModel::increment($ipKey,1, 0, 3600);
53 $ipTimes = Cache::get($ipKey); 52 $ipTimes = Cache::get($ipKey);
54 do{ 53 do{
55 /* 判断是不是AJAX请求 */ 54 /* 判断是不是AJAX请求 */
@@ -131,8 +130,7 @@ class RegisterController extends WebAction @@ -131,8 +130,7 @@ class RegisterController extends WebAction
131 } 130 }
132 //发送代码 131 //发送代码
133 $sendCodeKey = md5('send_code_' . $area . '_' . $mobile); 132 $sendCodeKey = md5('send_code_' . $area . '_' . $mobile);
134 - $sendCodeTimeKey = md5('send_code_time_' . $area . '_' . $mobile);  
135 - PassportModel::increment($sendCodeKey,$sendCodeTimeKey, 1, 0, 3600); 133 + PassportModel::increment($sendCodeKey,1, 0, 3600);
136 $sendCodeTimes = Cache::get($sendCodeKey); 134 $sendCodeTimes = Cache::get($sendCodeKey);
137 if ($sendCodeTimes > 50) { 135 if ($sendCodeTimes > 50) {
138 $data['message'] = '发送验证码太多'; 136 $data['message'] = '发送验证码太多';