修复查询侧边栏样式问题,代码优化

This commit is contained in:
郑杰
2018-09-29 16:05:53 +08:00
parent 2efc1094a0
commit cff992b3d4
9 changed files with 10488 additions and 40 deletions
@@ -0,0 +1,36 @@
package me.aurora.app.rest.web;
import lombok.extern.slf4j.Slf4j;
import me.aurora.annotation.Log;
import me.aurora.domain.ResponseEntity;
import me.aurora.util.AuroraConstant;
import me.aurora.util.HttpUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@Slf4j
@RestController
@RequestMapping("weather")
public class WeatherController {
@Log("获取天气信息")
@GetMapping(value = "/index")
public ModelAndView weather() {
return new ModelAndView("/web/weather/weather");
}
@GetMapping(value = "/query")
public ResponseEntity queryWeather(@RequestParam String areaId) {
try {
String data = HttpUtils.sendPost(AuroraConstant.MEIZU_WEATHER_URL, "cityIds=" + areaId);
return ResponseEntity.ok(data);
} catch (Exception e) {
log.error("查询天气失败", e);
return ResponseEntity.error("查询天气失败,请联系网站管理员!");
}
}
}
@@ -5,7 +5,6 @@ import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
/**
* @author 郑杰
@@ -0,0 +1,5 @@
package me.aurora.service.query;
public class SysLogQueryService {
}
@@ -0,0 +1,14 @@
package me.aurora.util;
/**
* @author 郑杰
* @date 2018/09/29 12:52:06
* 常用接口
*/
public class AuroraConstant {
private AuroraConstant() {
}
public static final String MEIZU_WEATHER_URL = "http://aider.meizu.com/app/weather/listWeather";
}
+153
View File
@@ -0,0 +1,153 @@
package me.aurora.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.X509Certificate;
/**
* @author mybird
* @date 2018/09/29 12:45:00
*/
public class HttpUtils {
private static Logger log = LoggerFactory.getLogger(HttpUtils.class);
private static final String USER_AGENT = "user-agent";
private static final String USER_AGENT_VALUE = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)";
private static final String CONNECTION = "connection";
private static final String CONNECTION_VALUE = "Keep-Alive";
private static final String ACCEPT = "accept";
private static final String UTF8 = "utf-8";
private static final String ACCEPT_CHARSET = "Accept-Charset";
private static final String CONTENTTYPE = "contentType";
private static final String SSL = "ssl";
protected HttpUtils() {
}
/**
* 向指定 URL 发送GET方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) throws IOException {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
StringBuilder result = new StringBuilder();
connection.setRequestProperty(USER_AGENT, USER_AGENT_VALUE);
connection.setRequestProperty(CONNECTION, CONNECTION_VALUE);
connection.setRequestProperty(ACCEPT, "*/*");
connection.connect();
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
log.error("发送GET请求出现异常!", e);
}
return result.toString();
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) throws IOException {
StringBuilder result = new StringBuilder();
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
URLConnection conn = realUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty(CONTENTTYPE, UTF8);
conn.setRequestProperty(ACCEPT_CHARSET, UTF8);
conn.setRequestProperty(USER_AGENT, USER_AGENT_VALUE);
conn.setRequestProperty(CONNECTION, CONNECTION_VALUE);
conn.setRequestProperty(ACCEPT, "*/*");
try (PrintWriter out = new PrintWriter(conn.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF8))) {
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
out.flush();
out.print(param);
} catch (Exception e) {
log.error("发送 POST 请求出现异常!", e);
}
return result.toString();
}
public static String sendSSLPost(String url, String param) {
StringBuilder result = new StringBuilder();
String urlNameString = url + "?" + param;
try {
SSLContext sc = SSLContext.getInstance(SSL);
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
URL console = new URL(urlNameString);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setRequestProperty(ACCEPT, "*/*");
conn.setRequestProperty(CONNECTION, CONNECTION_VALUE);
conn.setRequestProperty(USER_AGENT, USER_AGENT_VALUE);
conn.setRequestProperty(ACCEPT_CHARSET, UTF8);
conn.setRequestProperty(CONTENTTYPE, UTF8);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader indata = new BufferedReader(new InputStreamReader(is));
String ret = "";
while (ret != null) {
ret = indata.readLine();
if (ret != null && !ret.trim().equals("")) {
result.append(ret);
}
}
conn.disconnect();
indata.close();
} catch (Exception e) {
log.error("发送SSL POST 请求出现异常!", e);
}
return result.toString();
}
private static class TrustAnyTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) {
//trust anything
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) {
//trust anything
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
}