增加接受Topgp订阅快递请求处理方法

This commit is contained in:
bo.yang 2021-08-03 09:12:51 +08:00
parent faf8fa55a9
commit 36db7fa387
5 changed files with 115 additions and 12 deletions

View File

@ -3,7 +3,9 @@ package com.ruoyi.bps.controller;
import com.kuaidi100.sdk.response.SubscribeResp; import com.kuaidi100.sdk.response.SubscribeResp;
import com.ruoyi.bps.domain.ExpSubscribe; import com.ruoyi.bps.domain.ExpSubscribe;
import com.ruoyi.bps.service.IExpSubsPushApiService; import com.ruoyi.bps.service.IExpSubsPushApiService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -13,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/** /**
* 接受快递推送信息的API接口Controller * 接受快递推送信息的API接口Controller
@ -42,5 +46,14 @@ public class ExpSubsPushApiController extends BaseController {
return expSubsPushApiService.ExpressSubscribe(expSubscribe); return expSubsPushApiService.ExpressSubscribe(expSubscribe);
} }
//接受ERP订阅
@Log(title = "快递订阅", businessType = BusinessType.OTHER)
@CrossOrigin
@PostMapping("/topgpExpressSubscribe")
public String topgpSubscribe(HttpServletRequest request,HttpServletResponse response) throws IOException {
return expSubsPushApiService.ExpressSubscribeWithTopgp(request);
}
} }

View File

@ -4,8 +4,15 @@ import com.ruoyi.bps.domain.ExpSubscribe;
import com.kuaidi100.sdk.response.SubscribeResp; import com.kuaidi100.sdk.response.SubscribeResp;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public interface IExpSubsPushApiService { public interface IExpSubsPushApiService {
/**
* 向快递100推送订阅请求
* @param expSubscribe
* @return
*/
public SubscribeResp ExpressSubscribe(ExpSubscribe expSubscribe); public SubscribeResp ExpressSubscribe(ExpSubscribe expSubscribe);
/** /**
@ -19,4 +26,11 @@ public interface IExpSubsPushApiService {
* *
*/ */
public SubscribeResp ExpressSubscribeCallBackUrl(HttpServletRequest request); public SubscribeResp ExpressSubscribeCallBackUrl(HttpServletRequest request);
/**
* 获取Topgp推送的快递信息向快递100推送订阅请求
* @param request
* @return
*/
public String ExpressSubscribeWithTopgp(HttpServletRequest request) throws IOException;
} }

View File

@ -1,5 +1,6 @@
package com.ruoyi.bps.service.impl; package com.ruoyi.bps.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.kuaidi100.sdk.api.Subscribe; import com.kuaidi100.sdk.api.Subscribe;
import com.kuaidi100.sdk.contant.ApiInfoConstant; import com.kuaidi100.sdk.contant.ApiInfoConstant;
@ -20,12 +21,16 @@ import com.ruoyi.bps.service.IExpSubsPushApiService;
import com.ruoyi.bps.service.IExpSubsPushRespService; import com.ruoyi.bps.service.IExpSubsPushRespService;
import com.ruoyi.bps.service.IExpSubscribeService; import com.ruoyi.bps.service.IExpSubscribeService;
import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@Service @Service
public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService { public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
@ -50,6 +55,8 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
*/ */
@Override @Override
public SubscribeResp ExpressSubscribe(ExpSubscribe expSubscribe) { public SubscribeResp ExpressSubscribe(ExpSubscribe expSubscribe) {
String subscribeFrom= expSubscribe.getSalt().equals("topgp")?"topgp":"bpsemi";
SubscribeParameters subscribeParameters = new SubscribeParameters(); SubscribeParameters subscribeParameters = new SubscribeParameters();
SubscribeResp subscribeResp = new SubscribeResp(); SubscribeResp subscribeResp = new SubscribeResp();
subscribeParameters.setCallbackurl("http://report.bpsemi.cn:8081/it_war/anon/subscribeCallBackUrl"); subscribeParameters.setCallbackurl("http://report.bpsemi.cn:8081/it_war/anon/subscribeCallBackUrl");
@ -68,7 +75,7 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
IBaseClient subscribe = new Subscribe(); IBaseClient subscribe = new Subscribe();
try{ try{
HttpResult httpResult= subscribe.execute(subscribeReq); HttpResult httpResult= subscribe.execute(subscribeReq);
System.out.println(httpResult); //System.out.println(httpResult);
subscribeResp= new Gson().fromJson(httpResult.getBody(),SubscribeResp.class); subscribeResp= new Gson().fromJson(httpResult.getBody(),SubscribeResp.class);
}catch (Exception e) }catch (Exception e)
{ {
@ -85,13 +92,13 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
newExpSubscribe.setCompany(expSubscribe.getCompany()); newExpSubscribe.setCompany(expSubscribe.getCompany());
newExpSubscribe.setNumber(expSubscribe.getNumber()); newExpSubscribe.setNumber(expSubscribe.getNumber());
newExpSubscribe.setPhone(expSubscribe.getPhone()); newExpSubscribe.setPhone(expSubscribe.getPhone());
newExpSubscribe.setSalt("bpsemi"); newExpSubscribe.setSalt(subscribeFrom); //偷懒把请求来源记录到salt栏位不再增加exp_subscribe字段了以后找时间改吧
newExpSubscribe.setSubscribeTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss")); newExpSubscribe.setSubscribeTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss"));
newExpSubscribe.setResult((subscribeResp.isResult())?"true":"false"); newExpSubscribe.setResult((subscribeResp.isResult())?"true":"false");
newExpSubscribe.setReturnCode(subscribeResp.getReturnCode()); newExpSubscribe.setReturnCode(subscribeResp.getReturnCode());
newExpSubscribe.setMessage(subscribeResp.getMessage()); newExpSubscribe.setMessage(subscribeResp.getMessage());
ExpSubscribe queryExpSubscribe = new ExpSubscribe(); /*ExpSubscribe queryExpSubscribe = new ExpSubscribe();
queryExpSubscribe.setCompany(expSubscribe.getCompany()); queryExpSubscribe.setCompany(expSubscribe.getCompany());
queryExpSubscribe.setNumber(expSubscribe.getNumber()); queryExpSubscribe.setNumber(expSubscribe.getNumber());
queryExpSubscribe.setResult(expSubscribe.getResult()); queryExpSubscribe.setResult(expSubscribe.getResult());
@ -107,7 +114,9 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
}else { }else {
//如果数据库中没有快递单号+快递公司编码则更插入新记录 //如果数据库中没有快递单号+快递公司编码则更插入新记录
expSubscribeService.insertExpSubscribe(newExpSubscribe); expSubscribeService.insertExpSubscribe(newExpSubscribe);
} }*/
//20210802 无论系统里有没有记录都会记录本次推送
expSubscribeService.insertExpSubscribe(newExpSubscribe);
//返回订阅结果 //返回订阅结果
return subscribeResp; return subscribeResp;
@ -252,6 +261,52 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService {
} }
/**
* 获取Topgp推送的快递信息向快递100推送订阅请求
*
* @param request
* @return
*/
@Override
public String ExpressSubscribeWithTopgp(HttpServletRequest request) throws IOException {
//获取httpServletRequest传过来的Json字符串并进行解析
JSONObject contentJson= JSONObject.parseObject(ServletUtils.getRequestContent(request));
String deliveryNo= contentJson.getString("deliveryNo");
String expressNo = contentJson.getString("expressNo");
String company = contentJson.getString("company");
String phone = contentJson.getString("phone");
//如果出货单号或者快递单号为空则返回错误信息并写入Logo
if(StringUtils.isEmpty(deliveryNo) || StringUtils.isEmpty(expressNo)){
Map<String,Object> map=new HashMap<>();
map.put("result",false);
map.put("returnCode",700);
map.put("message","快递单号或出货单号为空!");
map.put("deliveryNo",deliveryNo);
map.put("expressNo",expressNo);
//写入Logo
//todo
//返回错误信息
return map.toString();
}
//向快递100推送订阅请求
ExpSubscribe expSubscribe=new ExpSubscribe();
expSubscribe.setNumber(expressNo);
expSubscribe.setCompany(company);
expSubscribe.setPhone(phone);
expSubscribe.setSalt("topgp"); //偷懒把请求来源记录到salt栏位不再增加exp_subscribe字段了以后找时间改吧
SubscribeResp subscribeResp= ExpressSubscribe(expSubscribe);
Object object = JSONObject.toJSON(subscribeResp);
Map map=JSONObject.parseObject(object.toString(), Map.class);
map.put("deliveryNo",deliveryNo);
map.put("expressNo",expressNo);
return map.toString();
}
} }

View File

@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -32,7 +31,8 @@ public class WechatApiController extends BaseController {
} }
@GetMapping("anon/userInfo") @GetMapping("anon/userInfo")
public Map<String, Object> getJSON(HttpServletRequest request, HttpServletResponse response) throws IOException { @ResponseBody
public Object getJSON(HttpServletRequest request, HttpServletResponse response) throws IOException {
BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8")); BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder(); StringBuilder responseStrBuilder = new StringBuilder();
@ -40,7 +40,8 @@ public class WechatApiController extends BaseController {
while ((inputStr = streamReader.readLine()) != null) { while ((inputStr = streamReader.readLine()) != null) {
responseStrBuilder.append(inputStr); responseStrBuilder.append(inputStr);
} }
return JSON.parseObject(responseStrBuilder.toString(), Map.class); //return JSON.parseObject(responseStrBuilder.toString(), Map.class);
return JSON.parse(responseStrBuilder.toString());
} }

View File

@ -1,13 +1,15 @@
package com.ruoyi.common.utils; package com.ruoyi.common.utils;
import java.io.IOException; import com.ruoyi.common.core.text.Convert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import com.ruoyi.common.core.text.Convert;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
/** /**
* 客户端工具类 * 客户端工具类
@ -161,4 +163,22 @@ public class ServletUtils
} }
return flag; return flag;
} }
/**
* 从HttpServletRequest中获取post的json对象数据content-type=text/plain
* @param request
* @return content
* @throws IOException
*/
public static String getRequestContent(HttpServletRequest request) throws IOException {
BufferedReader reader = request.getReader();
char[] buf = new char[request.getContentLength()];
int len = 0;
StringBuffer contentBuffer = new StringBuffer();
while ((len = reader.read(buf)) != -1) {
contentBuffer.append(buf, 0, len);
}
String content= contentBuffer.toString();
return content;
}
} }