Browse Source

修正get请求方式

master
xyiege 3 years ago
parent
commit
8f5cd72dea
  1. 163
      src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java
  2. 2
      src/main/java/cn/chjyj/szwh/service/impl/LoginServiceImpl.java
  3. 2
      src/main/java/cn/chjyj/szwh/service/impl/UserServiceImpl.java
  4. 18
      src/main/java/cn/chjyj/szwh/utils/RequestUtils.java
  5. 6
      src/main/java/cn/chjyj/szwh/utils/SzwhApiUtils.java
  6. 2
      src/test/java/cn/chjyj/szwh/ApiTest.java

163
src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java

@ -84,95 +84,96 @@ public class GoodsServiceImpl implements GoodsService {
Map<String, Object> hmap = new HashMap(); Map<String, Object> hmap = new HashMap();
hmap.put("entrust_token", sign); hmap.put("entrust_token", sign);
// get 方式获取 // get 方式获取
JSONObject jsonObject = RequestUtils.GetData(apiurl, hmap); JSONObject jsonObject = RequestUtils.GetData(apiurl, hmap,true);
// json的data节点
JSONArray jsonArray = jsonObject.getJSONArray("data");
// 产品详情地址 // 产品详情地址
String gdurl = host + "/consign/exchange/v1/selectEntrustDetails"; String gdurl = host + "/consign/exchange/v1/selectEntrustDetails";
// 循环查询商品的详情 // 循环查询商品的详情
for(int i=0;i<jsonArray.size();i++){ if(jsonObject!=null && jsonObject.containsKey("data") ) {
// 获取到sourceData字段信息 // json的data节点
JSONObject sdjson = (JSONObject) jsonArray.get(i); JSONArray jsonArray = jsonObject.getJSONArray("data");
String nsdstr = sdjson.getString("sourceData"); for (int i = 0; i < jsonArray.size(); i++) {
if(nsdstr==null){ // 获取到sourceData字段信息
continue; JSONObject sdjson = (JSONObject) jsonArray.get(i);
}else { String nsdstr = sdjson.getString("sourceData");
JSONObject nsdjson = JSONObject.parseObject(nsdstr); if (nsdstr == null) {
// 从sourcedata节点中提取商品唯一编号
String goodsId = nsdjson.getString("identifier");
// 以identifier为主键查询详情
String gdetailUrl = gdurl + "/" + goodsId;
// 请求商品的详情
JSONObject gdJson = RequestUtils.GetData(gdetailUrl, hmap);
log.info(goodsId + "详情:" + gdJson);
//处理返回的结果
JSONArray gdArr = gdJson.getJSONArray("data");
// 如果goods表中存在记录,不进行操作
Goods tgoods = goodsMapper.getGoodsByIsli(goodsId);
if (tgoods != null) {
continue; continue;
}
// add goodsSource,循环插入 商品资源
for (int x = 0; x < gdArr.size(); x++) {
JSONObject gditem = (JSONObject) gdArr.get(x);
GoodsSource goodsSource = ApiGoodsUtils.jsonToGoodsSource(gditem, goodsId);
goodsSourceMapper.add(goodsSource);
}
// insertGoodsDetail
GoodsDetail goodsDetail = ApiGoodsUtils.jsonToGoodsDetail(nsdjson, sdjson);
// 返回商品详情
int gdid = goodsDetailMapper.add(goodsDetail);
//抽取target 字段中的json,转为jsonobject
JSONObject targetJson = nsdjson.getJSONObject("targetData");
//处理状态
int istatus = 0;
if (sdjson.getInteger("status") == 1 || sdjson.getInteger("status") == 4 ||
sdjson.getInteger("status") == 5) {
istatus = 1;
} else if (sdjson.getInteger("status") == 2) {
istatus = 2;
} else { } else {
istatus = 4; JSONObject nsdjson = JSONObject.parseObject(nsdstr);
// 从sourcedata节点中提取商品唯一编号
String goodsId = nsdjson.getString("identifier");
// 以identifier为主键查询详情
String gdetailUrl = gdurl + "/" + goodsId;
// 请求商品的详情
JSONObject gdJson = RequestUtils.GetData(gdetailUrl, hmap,true);
log.info(goodsId + "详情:" + gdJson);
//处理返回的结果
JSONArray gdArr = gdJson.getJSONArray("data");
// 如果goods表中存在记录,不进行操作
Goods tgoods = goodsMapper.getGoodsByIsli(goodsId);
if (tgoods != null) {
continue;
}
// add goodsSource,循环插入 商品资源
for (int x = 0; x < gdArr.size(); x++) {
JSONObject gditem = (JSONObject) gdArr.get(x);
GoodsSource goodsSource = ApiGoodsUtils.jsonToGoodsSource(gditem, goodsId);
goodsSourceMapper.add(goodsSource);
}
// insertGoodsDetail
GoodsDetail goodsDetail = ApiGoodsUtils.jsonToGoodsDetail(nsdjson, sdjson);
// 返回商品详情
int gdid = goodsDetailMapper.add(goodsDetail);
//抽取target 字段中的json,转为jsonobject
JSONObject targetJson = nsdjson.getJSONObject("targetData");
//处理状态
int istatus = 0;
if (sdjson.getInteger("status") == 1 || sdjson.getInteger("status") == 4 ||
sdjson.getInteger("status") == 5) {
istatus = 1;
} else if (sdjson.getInteger("status") == 2) {
istatus = 2;
} else {
istatus = 4;
}
//商品起止时间
String cstart = sdjson.getString("contractualPeriodStart");
String curTime = new SimpleDateFormat("HH:mm:ss").format(new Date());
String ncstart = cstart + " " + curTime;
//
String cend = nsdjson.getString("contractualPeriodEnd");
String ncend = cend + " 23:59:59";
Goods goods = new Goods();
//起止时间转为timestamp
goods.setContractualStartTime(Timestamp.valueOf(ncstart));
goods.setContractualtimeEndTime(Timestamp.valueOf(ncend));
goods.setGoodsDetailId(gdid);
goods.setContractStatus(nsdjson.getInteger("contractStatus"));
goods.setGoodsStatus(istatus);
goods.setOldStatus(0); // 默认为0
goods.setEntrustStatus(sdjson.getInteger("status"));
//
goods.setUserIslicode(targetJson.getString("identifier"));
goods.setUsername(targetJson.getString("titleName"));
goods.setIsLicode(sdjson.getString("isliCode"));
goods.setGoodsIslicode(nsdjson.getString("identifier"));
goods.setRecommendSort(1); // 默认排序1
// 添加,返回当前的编号
int ret_gid = goodsMapper.addGoods(goods);
//添加操作日志
OperationLog oplog = new OperationLog();
oplog.setLogid(goods.getId());
String nowStr = sdf.format(new Date());
String message = nowStr + " 发布标的成功";
oplog.setMessage(message);
oplog.setType("goods");
// 日志入库
operationLogMapper.addLog(oplog);
} }
//商品起止时间 // 计算器增加1
String cstart = sdjson.getString("contractualPeriodStart"); count++;
String curTime = new SimpleDateFormat("HH:mm:ss").format(new Date());
String ncstart = cstart + " " + curTime;
//
String cend = nsdjson.getString("contractualPeriodEnd");
String ncend = cend + " 23:59:59";
Goods goods = new Goods();
//起止时间转为timestamp
goods.setContractualStartTime(Timestamp.valueOf(ncstart));
goods.setContractualtimeEndTime(Timestamp.valueOf(ncend));
goods.setGoodsDetailId(gdid);
goods.setContractStatus(nsdjson.getInteger("contractStatus"));
goods.setGoodsStatus(istatus);
goods.setOldStatus(0); // 默认为0
goods.setEntrustStatus(sdjson.getInteger("status"));
//
goods.setUserIslicode(targetJson.getString("identifier"));
goods.setUsername(targetJson.getString("titleName"));
goods.setIsLicode(sdjson.getString("isliCode"));
goods.setGoodsIslicode(nsdjson.getString("identifier"));
goods.setRecommendSort(1); // 默认排序1
// 添加,返回当前的编号
int ret_gid = goodsMapper.addGoods(goods);
//添加操作日志
OperationLog oplog = new OperationLog();
oplog.setLogid(goods.getId());
String nowStr = sdf.format(new Date());
String message = nowStr + " 发布标的成功";
oplog.setMessage(message);
oplog.setType("goods");
// 日志入库
operationLogMapper.addLog(oplog);
} }
// 计算器增加1
count++;
} }
return count; return count;
} }

2
src/main/java/cn/chjyj/szwh/service/impl/LoginServiceImpl.java

@ -45,7 +45,7 @@ public class LoginServiceImpl implements LoginService {
hmap.put("Content-Type","application/json"); hmap.put("Content-Type","application/json");
//执行请求 //执行请求
JSONObject jsonObject = RequestUtils.GetData(path,hmap); JSONObject jsonObject = RequestUtils.GetData(path,hmap,true);
log.debug("request result:"+jsonObject.toString()); log.debug("request result:"+jsonObject.toString());
String code = jsonObject.getString("resultCode"); String code = jsonObject.getString("resultCode");

2
src/main/java/cn/chjyj/szwh/service/impl/UserServiceImpl.java

@ -68,7 +68,7 @@ public class UserServiceImpl implements UserService {
//hmap.put("Content-Type","application/json"); //hmap.put("Content-Type","application/json");
//执行请求 //执行请求
JSONObject json = RequestUtils.GetData(userUrl,hmap); JSONObject json = RequestUtils.GetData(userUrl,hmap,true);
log.info("request result:"+json); log.info("request result:"+json);
// json 内容转为map // json 内容转为map
Map rmap = JSONObject.parseObject(json.getString("data")); Map rmap = JSONObject.parseObject(json.getString("data"));

18
src/main/java/cn/chjyj/szwh/utils/RequestUtils.java

@ -31,9 +31,10 @@ public class RequestUtils {
* 能设置请求头的get方式 * 能设置请求头的get方式
* @param surl * @param surl
* @param map * @param map
* @param wflag 是否写入json文件
* @return * @return
*/ */
public static JSONObject GetData(String surl, Map<String,Object> map){ public static JSONObject GetData(String surl, Map<String,Object> map,boolean wflag){
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
try{ try{
URL url = new URL(surl); URL url = new URL(surl);
@ -51,13 +52,16 @@ public class RequestUtils {
InputStream inputStream = httpcon.getInputStream(); InputStream inputStream = httpcon.getInputStream();
//请求结果写入 //请求结果写入
String filename =String.valueOf(Calendar.getInstance().getTimeInMillis()); String filename =String.valueOf(Calendar.getInstance().getTimeInMillis());
SzFileUtils.createFile(inputStream,filename); if(wflag){
//字节转为utf-8 SzFileUtils.createFile(inputStream,filename);
String message=SzFileUtils.readJson(filename); //字节转为utf-8
if(message!=null){ String message=SzFileUtils.readJson(filename);
jsonObject = JSONObject.parseObject(message); if(message!=null){
jsonObject = JSONObject.parseObject(message);
logger.info("respone message :"+message);
}
logger.info("respone message is null");
} }
logger.info("respone message :"+message);
}catch (IOException ex){ }catch (IOException ex){
// 罗列出明细错误 // 罗列出明细错误
logger.error("请求错误:"+ex); logger.error("请求错误:"+ex);

6
src/main/java/cn/chjyj/szwh/utils/SzwhApiUtils.java

@ -37,7 +37,7 @@ public class SzwhApiUtils {
hmap.put("token",sign); hmap.put("token",sign);
hmap.put("Content-Type","application/json"); hmap.put("Content-Type","application/json");
//执行请求 //执行请求
JSONObject json = RequestUtils.GetData(userUrl,hmap); JSONObject json = RequestUtils.GetData(userUrl,hmap,false);
log.info("request result:"+json); log.info("request result:"+json);
return json; return json;
} }
@ -78,7 +78,7 @@ public class SzwhApiUtils {
System.out.println("token :"+sign); System.out.println("token :"+sign);
Map hmap = new HashMap(); Map hmap = new HashMap();
hmap.put("entrust_token",sign); hmap.put("entrust_token",sign);
JSONObject json = RequestUtils.GetData(url,hmap); JSONObject json = RequestUtils.GetData(url,hmap,false);
return json; return json;
} }
@ -94,7 +94,7 @@ public class SzwhApiUtils {
String sign=SignUtils.createSign("entrust"); String sign=SignUtils.createSign("entrust");
Map hmap = new HashMap(); Map hmap = new HashMap();
hmap.put("entrust_token",sign); hmap.put("entrust_token",sign);
JSONObject json = RequestUtils.GetData(url,hmap); JSONObject json = RequestUtils.GetData(url,hmap,true);
return json; return json;
} }
/** /**

2
src/test/java/cn/chjyj/szwh/ApiTest.java

@ -64,7 +64,7 @@ public class ApiTest {
// 请求头 // 请求头
Map<String, Object> hmap = new HashMap(); Map<String, Object> hmap = new HashMap();
hmap.put("entrust_token", sign); hmap.put("entrust_token", sign);
JSONObject jsonObject = RequestUtils.GetData(url, hmap); JSONObject jsonObject = RequestUtils.GetData(url, hmap,true);
// 抽取data // 抽取data
// String data = jsonObject.getString("data"); // String data = jsonObject.getString("data");
JSONArray jsonArray = jsonObject.getJSONArray("data"); JSONArray jsonArray = jsonObject.getJSONArray("data");

Loading…
Cancel
Save