ip查询失败后采用keycdn接口查询 (#8)

This commit is contained in:
kokororin 2017-05-15 17:36:27 +08:00
parent b23ee237ff
commit e4e18109c4
2 changed files with 31 additions and 20 deletions

View File

@ -32,9 +32,9 @@ class Access_Action implements Widget_Interface_Do
public function ip()
{
$this->response->setContentType('application/json');
$ip = $this->request->get('ip');
try {
$this->checkAuth();
$ip = $this->request->get('ip');
$response = Access_Ip::find($ip);
if (is_array($response)) {
$response = array(
@ -42,18 +42,27 @@ class Access_Action implements Widget_Interface_Do
'data' => implode(' ', $response),
);
} else {
throw new Exception('解析ip失败');
}
} catch (Exception $e) {
try {
$http = Typecho_Http_Client::get();
$result = $http->send('https://tools.keycdn.com/geo.json?host=' . $ip);
$result = Json::decode($result, true);
if ($result['status'] == 'success') {
$response = array(
'code' => 0,
'data' => $result['data']['geo']['country_name'] . ' ' . $result['data']['geo']['city'],
);
}
} catch (Exception $e) {
$response = array(
'code' => 100,
'message' => '解析ip失败',
'data' => '很抱歉ipip.net查询无结果同时你的服务器无法连接fallback接口(tools.keycdn.com)',
);
}
exit(Json::encode($response));
} catch (Exception $e) {
exit(Json::encode(array(
'code' => 100,
'message' => $e->getMessage(),
)));
}
exit(Json::encode($response));
}
public function deleteLogs()
@ -67,16 +76,18 @@ class Access_Action implements Widget_Interface_Do
throw new Exception('params invalid');
}
$this->access->deleteLogs($data);
exit(Json::encode(array(
$response = array(
'code' => 0,
)));
);
} catch (Exception $e) {
exit(Json::encode(array(
$response = array(
'code' => 100,
'message' => $e->getMessage(),
)));
'data' => $e->getMessage(),
);
}
exit(Json::encode($response));
}
protected function checkAuth()

View File

@ -274,7 +274,7 @@ $(document).ready(function() {
} else {
swal({
title: "IP查询失败",
text: '接口返回状态码错误',
text: data.data,
type: "warning",
confirmButtonText: "OK"
});
@ -326,12 +326,12 @@ $(document).ready(function() {
$('.typecho-list-table tbody tr[data-id="' + elem + '"]').fadeOut(500).remove();
});
} else {
swal({
title: "错误",
text: '发生错误了',
type: "warning",
confirmButtonText: "OK"
});
swal({
title: "错误",
text: '发生错误了',
type: "warning",
confirmButtonText: "OK"
});
}
}
});