package com.xxxx.xxx.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import com.alibaba.fastjson.JSONObject;
public class HttpUtil {
/**
* 發起https請求並獲取結果
*
* @param requestUrl
* 請求地址
* @param requestMethod
* 請求方式(GET、POST)
* @param outputStr
* 提交的數據
* @return JSONObject(通過JSONObject.get(key)的方式獲取json對象的屬性值)
*/
public static JSONObject httpRequest(String requestUrl,
String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
HttpURLConnection httpUrlConn = null;
try {
// 創建SSLContext對象,並使用我們指定的信任管理器初始化
URL url = new URL(requestUrl);
httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestProperty("content-type", "text/html");
// 設置請求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
// 當有數據需要提交時
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意編碼格式,防止中文亂碼
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 將返回的輸入流轉換成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 釋放資源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.parseObject(buffer.toString());
// jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
org.jeecgframework.core.util.LogUtil
.info("Weixin server connection timed out.");
} catch (Exception e) {
//e.printStackTrace();
org.jeecgframework.core.util.LogUtil.info("https request error:{}"
+ e.getMessage());
}finally{
try {
httpUrlConn.disconnect();
}catch (Exception e) {
e.printStackTrace();
org.jeecgframework.core.util.LogUtil.info("http close error:{}"+ e.getMessage());
}
}
return jsonObject;
}
}
----------------------------------------------
/**
* 剩余預約號數接口
* @param businessAppoint
* @param request
* @param response
* @return
* @throws ParseException
*/
@ResponseBody
@RequestMapping(params = "leftNumber",produces = "application/json; charset=utf-8")
public String leftNumber(BusinessAppoint businessAppoint,HttpServletRequest request,HttpServletResponse response) throws ParseException{
//實例化AjaxJson對象
JSONObject json=new JSONObject();
try {
// String businessTypeId = request.getParameter("businessTypeId");
//測試用的開始
// String appointDate2 = request.getParameter("appointDate");
// String appointDate = appointDate2.replaceAll("-", "");
// businessAppoint.setAppointTime(appointDate2);
//測試用的結束
String appointDate = businessAppoint.getAppointTime().replaceAll("-", ""); //把-去掉
//用http://api.goseek.cn/ 提供的節假日API接口判斷date是否是節假日
JSONObject jsonObject = HttpUtil.httpRequest("http://api.goseek.cn/Tools/holiday?date=" + appointDate, "GET", null);
if(jsonObject != null){
Map<String, Object> map = new HashMap<String, Object>();
//返回數據:{"code":10000,"data":2} 工作日對應結果為 0, 休息日對應結果為 1, 節假日對應的結果為 2
int data = (int) jsonObject.get("data");
if(0 == data){ //工作日
//查詢設定的可預約號數
List<AppointNumber> list = appointNumberService.queryAll();
AppointNumber appointNumber = list.get(0);
// BusinessType appointNumber = businessTypeService.queryById(businessTypeId);
int amNumber = appointNumber.getAmNumber(); //上午可預約數
int pmNumber = appointNumber.getPmNumber(); //下午可預約數
//查詢上午已預約的號數
businessAppoint.setAmPm("上午");
List<BusinessAppoint> amList = businessAppointService.queryListByWhere(businessAppoint);
int amLeft = amNumber - amList.size();//上午午剩余號數
//查詢下午已預約的號數
BusinessAppoint businessAppoint2 = new BusinessAppoint();
businessAppoint2.setAppointTime(businessAppoint.getAppointTime());
businessAppoint2.setAmPm("下午");
List<BusinessAppoint> pmList = businessAppointService.queryListByWhere(businessAppoint2);
int pmLeft = pmNumber - pmList.size();//下午剩余號數
map.put("amLeft", amLeft);
map.put("pmLeft", pmLeft);
json.put("code", "200");
json.put("msg", "獲取成功");
json.put("data", map);
}else{ //非工作日
json.put("code", "200");
json.put("msg", "獲取成功");
map.put("amLeft", 0);
map.put("pmLeft", 0);
json.put("data", map);
}
}
} catch (Exception e) {
e.printStackTrace();
json.put("code", "20004");
json.put("msg", "獲取失敗");
}
return json.toJSONString();
}