diff --git a/box-bps/src/main/java/com/ruoyi/bps/controller/ExpSubsPushApiController.java b/box-bps/src/main/java/com/ruoyi/bps/controller/ExpSubsPushApiController.java index 02e7cc63e..b3146b5c5 100644 --- a/box-bps/src/main/java/com/ruoyi/bps/controller/ExpSubsPushApiController.java +++ b/box-bps/src/main/java/com/ruoyi/bps/controller/ExpSubsPushApiController.java @@ -3,7 +3,9 @@ package com.ruoyi.bps.controller; import com.kuaidi100.sdk.response.SubscribeResp; import com.ruoyi.bps.domain.ExpSubscribe; import com.ruoyi.bps.service.IExpSubsPushApiService; +import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.enums.BusinessType; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; 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 javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; /** * 接受快递推送信息的API接口Controller @@ -42,5 +46,14 @@ public class ExpSubsPushApiController extends BaseController { 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); + } + + } diff --git a/box-bps/src/main/java/com/ruoyi/bps/service/IExpSubsPushApiService.java b/box-bps/src/main/java/com/ruoyi/bps/service/IExpSubsPushApiService.java index 5867116de..f47f834de 100644 --- a/box-bps/src/main/java/com/ruoyi/bps/service/IExpSubsPushApiService.java +++ b/box-bps/src/main/java/com/ruoyi/bps/service/IExpSubsPushApiService.java @@ -4,8 +4,15 @@ import com.ruoyi.bps.domain.ExpSubscribe; import com.kuaidi100.sdk.response.SubscribeResp; import javax.servlet.http.HttpServletRequest; +import java.io.IOException; public interface IExpSubsPushApiService { + + /** + * 向快递100推送订阅请求 + * @param expSubscribe + * @return + */ public SubscribeResp ExpressSubscribe(ExpSubscribe expSubscribe); /** @@ -19,4 +26,11 @@ public interface IExpSubsPushApiService { * */ public SubscribeResp ExpressSubscribeCallBackUrl(HttpServletRequest request); + + /** + * 获取Topgp推送的快递信息,向快递100推送订阅请求 + * @param request + * @return + */ + public String ExpressSubscribeWithTopgp(HttpServletRequest request) throws IOException; } diff --git a/box-bps/src/main/java/com/ruoyi/bps/service/impl/ExpSubsPushApiServiceImpl.java b/box-bps/src/main/java/com/ruoyi/bps/service/impl/ExpSubsPushApiServiceImpl.java index 50b3ede50..a3b86615d 100644 --- a/box-bps/src/main/java/com/ruoyi/bps/service/impl/ExpSubsPushApiServiceImpl.java +++ b/box-bps/src/main/java/com/ruoyi/bps/service/impl/ExpSubsPushApiServiceImpl.java @@ -1,5 +1,6 @@ package com.ruoyi.bps.service.impl; +import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.kuaidi100.sdk.api.Subscribe; 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.IExpSubscribeService; import com.ruoyi.common.utils.DateUtils; +import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Service public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService { @@ -50,6 +55,8 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService { */ @Override public SubscribeResp ExpressSubscribe(ExpSubscribe expSubscribe) { + String subscribeFrom= expSubscribe.getSalt().equals("topgp")?"topgp":"bpsemi"; + SubscribeParameters subscribeParameters = new SubscribeParameters(); SubscribeResp subscribeResp = new SubscribeResp(); subscribeParameters.setCallbackurl("http://report.bpsemi.cn:8081/it_war/anon/subscribeCallBackUrl"); @@ -68,7 +75,7 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService { IBaseClient subscribe = new Subscribe(); try{ HttpResult httpResult= subscribe.execute(subscribeReq); - System.out.println(httpResult); + //System.out.println(httpResult); subscribeResp= new Gson().fromJson(httpResult.getBody(),SubscribeResp.class); }catch (Exception e) { @@ -85,13 +92,13 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService { newExpSubscribe.setCompany(expSubscribe.getCompany()); newExpSubscribe.setNumber(expSubscribe.getNumber()); newExpSubscribe.setPhone(expSubscribe.getPhone()); - newExpSubscribe.setSalt("bpsemi"); + newExpSubscribe.setSalt(subscribeFrom); //偷懒,把请求来源记录到salt栏位,不再增加exp_subscribe字段了。。以后找时间改吧 newExpSubscribe.setSubscribeTime(DateUtils.dateTimeNow("yyyy-MM-dd HH:mm:ss")); newExpSubscribe.setResult((subscribeResp.isResult())?"true":"false"); newExpSubscribe.setReturnCode(subscribeResp.getReturnCode()); newExpSubscribe.setMessage(subscribeResp.getMessage()); - ExpSubscribe queryExpSubscribe = new ExpSubscribe(); + /*ExpSubscribe queryExpSubscribe = new ExpSubscribe(); queryExpSubscribe.setCompany(expSubscribe.getCompany()); queryExpSubscribe.setNumber(expSubscribe.getNumber()); queryExpSubscribe.setResult(expSubscribe.getResult()); @@ -107,7 +114,9 @@ public class ExpSubsPushApiServiceImpl implements IExpSubsPushApiService { }else { //如果数据库中没有快递单号+快递公司编码,则更插入新记录 expSubscribeService.insertExpSubscribe(newExpSubscribe); - } + }*/ + //20210802 无论系统里有没有记录,都会记录本次推送。 + expSubscribeService.insertExpSubscribe(newExpSubscribe); //返回订阅结果 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 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(); + } + + + } diff --git a/box-test/src/main/java/com/ruoyi/test/conrtroller/WechatApiController.java b/box-test/src/main/java/com/ruoyi/test/conrtroller/WechatApiController.java index 7439ebbbf..ef71ebbee 100644 --- a/box-test/src/main/java/com/ruoyi/test/conrtroller/WechatApiController.java +++ b/box-test/src/main/java/com/ruoyi/test/conrtroller/WechatApiController.java @@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; -import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; @@ -32,7 +31,8 @@ public class WechatApiController extends BaseController { } @GetMapping("anon/userInfo") - public Map 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")); StringBuilder responseStrBuilder = new StringBuilder(); @@ -40,7 +40,8 @@ public class WechatApiController extends BaseController { while ((inputStr = streamReader.readLine()) != null) { responseStrBuilder.append(inputStr); } - return JSON.parseObject(responseStrBuilder.toString(), Map.class); + //return JSON.parseObject(responseStrBuilder.toString(), Map.class); + return JSON.parse(responseStrBuilder.toString()); } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/ServletUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/ServletUtils.java index a5a190cdd..f09cbadf9 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/ServletUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/ServletUtils.java @@ -1,13 +1,15 @@ package com.ruoyi.common.utils; -import java.io.IOException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; +import com.ruoyi.common.core.text.Convert; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; 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; } + + /** + * 从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; + } }