增加 uni cloud push 函数推送公共接口
This commit is contained in:
parent
f06ca9839b
commit
04157e9575
@ -9,7 +9,7 @@
|
|||||||
package com.suisung.mall.common.service;
|
package com.suisung.mall.common.service;
|
||||||
|
|
||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
import com.suisung.mall.common.service.impl.UniCloudPushServiceImpl;
|
import org.springframework.data.util.Pair;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -23,7 +23,6 @@ public interface UniCloudPushService {
|
|||||||
* @param content 推送内容
|
* @param content 推送内容
|
||||||
* @param payload 附加数据(JSON对象)
|
* @param payload 附加数据(JSON对象)
|
||||||
* @return 推送结果响应对象
|
* @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.JSONObject;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.suisung.mall.common.service.UniCloudPushService;
|
import com.suisung.mall.common.service.UniCloudPushService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.context.annotation.Lazy;
|
import org.springframework.context.annotation.Lazy;
|
||||||
|
import org.springframework.data.util.Pair;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
@ -24,16 +26,9 @@ import java.util.List;
|
|||||||
* 调用 UniCloud 云函数 推送服务
|
* 调用 UniCloud 云函数 推送服务
|
||||||
* 提供向多个客户端 ID 发送推送消息的功能
|
* 提供向多个客户端 ID 发送推送消息的功能
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class UniCloudPushServiceImpl implements UniCloudPushService {
|
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
|
@Lazy
|
||||||
@Resource
|
@Resource
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
@ -47,10 +42,9 @@ public class UniCloudPushServiceImpl implements UniCloudPushService {
|
|||||||
* @param content 推送内容
|
* @param content 推送内容
|
||||||
* @param payload 附加数据(JSON对象)
|
* @param payload 附加数据(JSON对象)
|
||||||
* @return 推送结果响应对象
|
* @return 推送结果响应对象
|
||||||
* @throws UniCloudPushException 推送过程中发生的异常
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@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);
|
validatePushParams(clientIds, title, content);
|
||||||
|
|
||||||
@ -65,17 +59,19 @@ public class UniCloudPushServiceImpl implements UniCloudPushService {
|
|||||||
return processResponse(response);
|
return processResponse(response);
|
||||||
} catch (RestClientException e) {
|
} catch (RestClientException e) {
|
||||||
// 处理REST客户端异常
|
// 处理REST客户端异常
|
||||||
throw new UniCloudPushException("推送请求发送失败", e);
|
log.error("推送请求发送失败:{}", e.getMessage(), e);
|
||||||
|
return Pair.of(false, "推送请求发送失败");
|
||||||
} catch (Exception e) {
|
} 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()) {
|
if (clientIds == null || clientIds.isEmpty()) {
|
||||||
throw new IllegalArgumentException("客户端ID列表不能为空");
|
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) {
|
private JSONObject buildPushRequest(List<String> clientIds, String title, String content, JSONObject payload) {
|
||||||
JSONObject requestBody = new JSONObject();
|
JSONObject requestBody = new JSONObject();
|
||||||
requestBody.put(ACTION_KEY, "push");
|
requestBody.put("action", "push");
|
||||||
requestBody.put(CID_LIST_KEY, clientIds);
|
requestBody.put("cidList", clientIds);
|
||||||
|
|
||||||
JSONObject message = new JSONObject();
|
JSONObject message = new JSONObject();
|
||||||
message.put(TITLE_KEY, title);
|
message.put("title", title);
|
||||||
message.put(CONTENT_KEY, content);
|
message.put("content", content);
|
||||||
|
|
||||||
if (payload != null) {
|
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;
|
return requestBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,76 +133,23 @@ public class UniCloudPushServiceImpl implements UniCloudPushService {
|
|||||||
/**
|
/**
|
||||||
* 处理HTTP响应
|
* 处理HTTP响应
|
||||||
*/
|
*/
|
||||||
private PushResponse processResponse(ResponseEntity<String> response) {
|
private Pair<Boolean, String> processResponse(ResponseEntity<String> response) {
|
||||||
if (response.getStatusCode() != HttpStatus.OK) {
|
if (response.getStatusCode() != HttpStatus.OK) {
|
||||||
throw new UniCloudPushException(
|
return Pair.of(false, String.format("推送请求失败,状态码: %d,响应: %s",
|
||||||
String.format("推送请求失败,状态码: %d,响应: %s",
|
response.getStatusCodeValue(),
|
||||||
response.getStatusCodeValue(),
|
response.getBody()));
|
||||||
response.getBody())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String responseBody = response.getBody();
|
String responseBody = response.getBody();
|
||||||
if (responseBody == null || responseBody.trim().isEmpty()) {
|
if (responseBody == null || responseBody.trim().isEmpty()) {
|
||||||
throw new UniCloudPushException("推送响应内容为空");
|
return Pair.of(false, "推送响应内容为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 解析响应JSON
|
// 解析响应JSON
|
||||||
JSONObject responseJson = JSONUtil.parseObj(responseBody);
|
JSONObject responseJson = JSONUtil.parseObj(responseBody);
|
||||||
|
return Pair.of(true, String.format("推送请求成功,状态码: %d,响应: %s",
|
||||||
// 提取响应结果
|
|
||||||
return new PushResponse(
|
|
||||||
responseJson.getBool("success"),
|
|
||||||
responseJson.getInt("code"),
|
responseJson.getInt("code"),
|
||||||
responseJson.getStr("message"),
|
responseJson.getJSONObject("data").toString()));
|
||||||
responseJson.getJSONObject("data")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 推送响应结果类
|
|
||||||
*/
|
|
||||||
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