add rservice app notify

master
luoyang 5 years ago
parent ca9f244261
commit dd8bf3cb20

@ -9,7 +9,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>manage</artifactId>
<version>1.4.34</version>
<version>1.4.35</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jib-maven-plugin.version>1.8.0</jib-maven-plugin.version>

@ -16,5 +16,7 @@ public interface RetailRSvcService {
JSONObject setUpShopBySourceCode(String sourceCode, JSONObject params);
JSONObject appNotifyBySourceCode(String sourceCode, JSONObject params);
JSONObject getGeekSsoTokenInfo(String sourceCode, String clientMoniker);
}

@ -4,11 +4,16 @@ import au.com.royalpay.payment.core.exceptions.InvalidShortIdException;
import au.com.royalpay.payment.manage.appclient.beans.RSvcMchBean;
import au.com.royalpay.payment.manage.appclient.core.RetailRSvcService;
import au.com.royalpay.payment.manage.appclient.extend.JWTUtil;
import au.com.royalpay.payment.manage.mappers.log.AppMessageLogMapper;
import au.com.royalpay.payment.manage.mappers.system.ClientConfigMapper;
import au.com.royalpay.payment.manage.mappers.system.ClientDeviceTokenMapper;
import au.com.royalpay.payment.manage.mappers.system.ClientServicesApplyMapper;
import au.com.royalpay.payment.manage.merchants.core.ClientManager;
import au.com.royalpay.payment.manage.pushMessage.bean.AppManagerMessageBuilder;
import au.com.royalpay.payment.tools.codec.AESCrypt;
import au.com.royalpay.payment.tools.device.DeviceSupport;
import au.com.royalpay.payment.tools.device.message.AppMessage;
import au.com.royalpay.payment.tools.device.message.AppMsgSender;
import au.com.royalpay.payment.tools.encryptalgorithm.SignUtils;
import au.com.royalpay.payment.tools.exceptions.BadRequestException;
import au.com.royalpay.payment.tools.exceptions.NotFoundException;
@ -25,13 +30,15 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.Date;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Service
public class RetailRSvcServiceImpl implements RetailRSvcService {
private Logger logger = LoggerFactory.getLogger(getClass());
private Map<String, AppMsgSender> senderMap = new HashMap<>();
@Resource
private ClientManager clientManager;
@Resource
@ -42,6 +49,17 @@ public class RetailRSvcServiceImpl implements RetailRSvcService {
private ClientServicesApplyMapper clientServicesApplyMapper;
@Resource
private ClientConfigMapper clientConfigMapper;
@Resource
private ClientDeviceTokenMapper clientDeviceTokenMapper;
@Resource
private AppMessageLogMapper appMessageLogMapper;
private ThreadPoolExecutor sendingAppleMsgPool = new ThreadPoolExecutor(10, 30, 5, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
@Resource
public void setAppMsgSenders(AppMsgSender[] senders) {
Arrays.stream(senders).forEach(appMsgSender -> senderMap.put(appMsgSender.devType(), appMsgSender));
}
@Override
public JSONObject findMchInfoBySourceCode(JSONObject device, String sourceCode, boolean debug) {
@ -173,6 +191,42 @@ public class RetailRSvcServiceImpl implements RetailRSvcService {
return result;
}
@Override
public JSONObject appNotifyBySourceCode(String sourceCode, JSONObject params) {
JSONObject result = new JSONObject();
JSONObject svcInfo = commonIncrementalChannelMapper.findIncreamentalChannelBySourceCode(sourceCode);
try {
if (svcInfo == null || StringUtils.isEmpty(svcInfo.getString("channel_pub_key"))
|| StringUtils.isEmpty(svcInfo.getString("platform_pub_key")) || StringUtils.isEmpty("platform_pri_key")) {
throw new BadRequestException("this channel config is wrong");
}
Key key = AESCrypt.fromKeyString(Base64.decodeBase64(params.getString("nonce_str")));
String signa = params.getString("sign");
params.remove("sign");
params = JSONObject.parseObject(JSON.toJSONString(params), Feature.OrderedField);
boolean checkSign = SignUtils.validSign(params.toJSONString(), signa, svcInfo.getString("channel_pub_key"));
if (!checkSign) {
throw new BadRequestException("sign is wrong");
}
String clientMoniker = decData(params.getString("partnerCode"), key, svcInfo.getString("platform_pri_key"));
logger.debug("geek {} app notify info :{}", clientMoniker,params.toJSONString());
JSONObject client = clientManager.getClientInfoByMoniker(clientMoniker);
if (client == null) {
throw new InvalidShortIdException();
}
String title = params.getString("title");
String body = params.getString("body");
String url = decData(params.getString("url"), key, svcInfo.getString("platform_pri_key"));
sendRServiceNotifyMessage(client,title,body,url);
result.put("result_status", "SUCCESS");
} catch (Exception e) {
logger.error("geek notify app fail:{} - {}",sourceCode,e.getMessage());
result.put("result_status", "SYSTEMERROR");
result.put("result_msg", e.getMessage());
}
return result;
}
@Override
public JSONObject getGeekSsoTokenInfo(String sourceCode, String clientMoniker) {
JSONObject svcInfo = commonIncrementalChannelMapper.findIncreamentalChannelBySourceCode(sourceCode);
@ -198,6 +252,45 @@ public class RetailRSvcServiceImpl implements RetailRSvcService {
}};
}
private void sendRServiceNotifyMessage(JSONObject client, String title, String body, String url) {
logger.debug("sendAnnualMessage Begin");
JSONObject devToken = clientDeviceTokenMapper.getTestTokens();
Runnable task = () -> {
String token = devToken.getString("token");
JSONObject log = saveAppMessageLog(devToken.getString("dev_id"),
devToken.getIntValue("client_id"),
"annual_bill" + devToken.getString("client_type"),
token,
"跨境商城推送"
);
try {
JSONObject type = new JSONObject();
type.put("send_type", "annual_bill");
type.put("id", devToken.getString("dev_token_id"));
AppMsgSender sender = senderMap.get((devToken.getString("client_type")));
if (StringUtils.isBlank(token) || sender == null) {
return;
}
JSONObject managerMsg = new JSONObject();
managerMsg.put("title", title);
managerMsg.put("body", body);
managerMsg.put("type", type);
JSONObject messageData = new JSONObject();
messageData.put("url", url);
managerMsg.put("data", messageData);
managerMsg.put("msgType", "annual_bill");
AppMessage appMessage = new AppManagerMessageBuilder(managerMsg).buildMessage();
sender.sendMessage(appMessage, devToken);
log.put("status", 2);
appMessageLogMapper.update(log);
} catch (Exception e) {
logger.error("出错了:" + e.getMessage());
appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage());
}
};
sendingAppleMsgPool.execute(task);
}
private String encData(String data, Key key, String publicKey) {
String pubKeyEncData = SignUtils.encData(data, publicKey);
return org.apache.commons.codec.binary.Base64.encodeBase64String(AESCrypt.encrypt(pubKeyEncData.getBytes(StandardCharsets.UTF_8), key));
@ -207,4 +300,17 @@ public class RetailRSvcServiceImpl implements RetailRSvcService {
String aesData = new String(AESCrypt.decrypt(Base64.decodeBase64(data), key), StandardCharsets.UTF_8);
return SignUtils.decData(aesData, privateKey);
}
private JSONObject saveAppMessageLog(String devId, int clientId, String messageType, String devToken, String remark) {
JSONObject log = new JSONObject();
log.put("dev_id", devId);
log.put("client_id", clientId);
log.put("msg_type", messageType);
log.put("dev_token", devToken);
log.put("remark", remark);
log.put("send_time", new Date());
appMessageLogMapper.save(log);
return log;
}
}

@ -22,4 +22,9 @@ public class RsvcServiceController {
public JSONObject setUpShopBySourceCode(@PathVariable String source_code, @RequestBody JSONObject params) {
return retailRSvcService.setUpShopBySourceCode(source_code, params);
}
@PostMapping(value = "/{source_code}/appNotify")
public JSONObject appNotifyBySourceCode(@PathVariable String source_code, @RequestBody JSONObject params) {
return retailRSvcService.appNotifyBySourceCode(source_code, params);
}
}

Loading…
Cancel
Save