增加 uni cloud push 函数推送公共接口
This commit is contained in:
parent
f06ca9839b
commit
04157e9575
@ -9,7 +9,7 @@
|
||||
package com.suisung.mall.common.service;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.suisung.mall.common.service.impl.UniCloudPushServiceImpl;
|
||||
import org.springframework.data.util.Pair;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -23,7 +23,6 @@ public interface UniCloudPushService {
|
||||
* @param content 推送内容
|
||||
* @param payload 附加数据(JSON对象)
|
||||
* @return 推送结果响应对象
|
||||
* @throws UniCloudPushServiceImpl.UniCloudPushException 推送过程中发生的异常
|
||||
*/
|
||||
UniCloudPushServiceImpl.PushResponse sendBatchPush(String cloudFunctionUrl, List<String> clientIds, String title, String content, JSONObject payload);
|
||||
Pair<Boolean, String> sendBatchPush(String cloudFunctionUrl, List<String> clientIds, String title, String content, JSONObject payload);
|
||||
}
|
||||
|
||||
@ -11,7 +11,9 @@ package com.suisung.mall.common.service.impl;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.suisung.mall.common.service.UniCloudPushService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.util.Pair;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
@ -24,16 +26,9 @@ import java.util.List;
|
||||
* 调用 UniCloud 云函数 推送服务
|
||||
* 提供向多个客户端 ID 发送推送消息的功能
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class UniCloudPushServiceImpl implements UniCloudPushService {
|
||||
// 请求常量定义
|
||||
private static final String ACTION_KEY = "action";
|
||||
private static final String CID_LIST_KEY = "cidList";
|
||||
private static final String MESSAGE_KEY = "message";
|
||||
private static final String TITLE_KEY = "title";
|
||||
private static final String CONTENT_KEY = "content";
|
||||
private static final String PAYLOAD_KEY = "payload";
|
||||
|
||||
@Lazy
|
||||
@Resource
|
||||
private RestTemplate restTemplate;
|
||||
@ -47,10 +42,9 @@ public class UniCloudPushServiceImpl implements UniCloudPushService {
|
||||
* @param content 推送内容
|
||||
* @param payload 附加数据(JSON对象)
|
||||
* @return 推送结果响应对象
|
||||
* @throws UniCloudPushException 推送过程中发生的异常
|
||||
*/
|
||||
@Override
|
||||
public PushResponse sendBatchPush(String cloudFunctionUrl, List<String> clientIds, String title, String content, JSONObject payload) {
|
||||
public Pair<Boolean, String> sendBatchPush(String cloudFunctionUrl, List<String> clientIds, String title, String content, JSONObject payload) {
|
||||
// 参数校验
|
||||
validatePushParams(clientIds, title, content);
|
||||
|
||||
@ -65,17 +59,19 @@ public class UniCloudPushServiceImpl implements UniCloudPushService {
|
||||
return processResponse(response);
|
||||
} catch (RestClientException e) {
|
||||
// 处理REST客户端异常
|
||||
throw new UniCloudPushException("推送请求发送失败", e);
|
||||
log.error("推送请求发送失败:{}", e.getMessage(), e);
|
||||
return Pair.of(false, "推送请求发送失败");
|
||||
} catch (Exception e) {
|
||||
// 处理其他异常
|
||||
throw new UniCloudPushException("推送处理过程发生异常", e);
|
||||
log.error("推送处理过程发生异常:{}", e.getMessage(), e);
|
||||
return Pair.of(false, "推送处理过程发生异常");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证推送参数有效性
|
||||
*/
|
||||
private void validatePushParams(List<String> clientIds, String title, String content) throws IllegalArgumentException {
|
||||
private void validatePushParams(List<String> clientIds, String title, String content) {
|
||||
if (clientIds == null || clientIds.isEmpty()) {
|
||||
throw new IllegalArgumentException("客户端ID列表不能为空");
|
||||
}
|
||||
@ -94,18 +90,23 @@ public class UniCloudPushServiceImpl implements UniCloudPushService {
|
||||
*/
|
||||
private JSONObject buildPushRequest(List<String> clientIds, String title, String content, JSONObject payload) {
|
||||
JSONObject requestBody = new JSONObject();
|
||||
requestBody.put(ACTION_KEY, "push");
|
||||
requestBody.put(CID_LIST_KEY, clientIds);
|
||||
requestBody.put("action", "push");
|
||||
requestBody.put("cidList", clientIds);
|
||||
|
||||
JSONObject message = new JSONObject();
|
||||
message.put(TITLE_KEY, title);
|
||||
message.put(CONTENT_KEY, content);
|
||||
message.put("title", title);
|
||||
message.put("content", content);
|
||||
|
||||
if (payload != null) {
|
||||
message.put(PAYLOAD_KEY, payload);
|
||||
message.put("payload", payload);
|
||||
}
|
||||
|
||||
requestBody.put(MESSAGE_KEY, message);
|
||||
requestBody.put("message", message);
|
||||
|
||||
|
||||
//消息有效期设置,单位毫秒,-1表示不设离线。默认是 2 小时,取值范围:-1 ~ 3 * 24 * 3600 * 1000(3天)之间
|
||||
requestBody.put("settings", new JSONObject().set("ttl", 3 * 24 * 3600 * 1000));
|
||||
|
||||
return requestBody;
|
||||
}
|
||||
|
||||
@ -132,76 +133,23 @@ public class UniCloudPushServiceImpl implements UniCloudPushService {
|
||||
/**
|
||||
* 处理HTTP响应
|
||||
*/
|
||||
private PushResponse processResponse(ResponseEntity<String> response) {
|
||||
private Pair<Boolean, String> processResponse(ResponseEntity<String> response) {
|
||||
if (response.getStatusCode() != HttpStatus.OK) {
|
||||
throw new UniCloudPushException(
|
||||
String.format("推送请求失败,状态码: %d,响应: %s",
|
||||
response.getStatusCodeValue(),
|
||||
response.getBody())
|
||||
);
|
||||
return Pair.of(false, String.format("推送请求失败,状态码: %d,响应: %s",
|
||||
response.getStatusCodeValue(),
|
||||
response.getBody()));
|
||||
}
|
||||
|
||||
String responseBody = response.getBody();
|
||||
if (responseBody == null || responseBody.trim().isEmpty()) {
|
||||
throw new UniCloudPushException("推送响应内容为空");
|
||||
return Pair.of(false, "推送响应内容为空");
|
||||
}
|
||||
|
||||
// 解析响应JSON
|
||||
JSONObject responseJson = JSONUtil.parseObj(responseBody);
|
||||
|
||||
// 提取响应结果
|
||||
return new PushResponse(
|
||||
responseJson.getBool("success"),
|
||||
return Pair.of(true, String.format("推送请求成功,状态码: %d,响应: %s",
|
||||
responseJson.getInt("code"),
|
||||
responseJson.getStr("message"),
|
||||
responseJson.getJSONObject("data")
|
||||
);
|
||||
}
|
||||
responseJson.getJSONObject("data").toString()));
|
||||
|
||||
/**
|
||||
* 推送响应结果类
|
||||
*/
|
||||
public static class PushResponse {
|
||||
private final boolean success;
|
||||
private final int code;
|
||||
private final String message;
|
||||
private final JSONObject data;
|
||||
|
||||
public PushResponse(boolean success, int code, String message, JSONObject data) {
|
||||
this.success = success;
|
||||
this.code = code;
|
||||
this.message = message;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
// Getter方法
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public JSONObject getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义推送异常类
|
||||
*/
|
||||
public static class UniCloudPushException extends RuntimeException {
|
||||
public UniCloudPushException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public UniCloudPushException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user