xuhy
2026-02-27 778391521e1818408b8c3bdaa9a20ee231ed2880
阿里云消息推送远程
4个文件已添加
1个文件已修改
233 ■■■■■ 已修改文件
xp-robotaxi-api/src/main/java/com/xiaopeng/api/dto/pushMessage/AliPushMessageDTO.java 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
xp-robotaxi-api/src/main/java/com/xiaopeng/api/factory/pushMessage/XpAliPushMessageClientFallbackFactory.java 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
xp-robotaxi-api/src/main/java/com/xiaopeng/api/feignClient/pushMessage/XpAliPushMessageClient.java 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
xp-robotaxi-push-message-boot/src/main/java/com/xiaopeng/pushMessage/controller/AliPushController.java 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
xp-robotaxi-push-message-boot/src/main/java/com/xiaopeng/pushMessage/utils/AliPushUtil.java 102 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
xp-robotaxi-api/src/main/java/com/xiaopeng/api/dto/pushMessage/AliPushMessageDTO.java
New file
@@ -0,0 +1,25 @@
package com.xiaopeng.api.dto.pushMessage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@Data
@ApiModel(value = "ali推送消息AliPushMessageDTO")
public class AliPushMessageDTO implements Serializable {
    @ApiModelProperty(value = "设备类型")
    private Integer deviceType;
    @ApiModelProperty(value = "运维手机号")
    private String account;
    @ApiModelProperty(value = "标题")
    private String title;
    @ApiModelProperty(value = "消息内容")
    private String body;
}
xp-robotaxi-api/src/main/java/com/xiaopeng/api/factory/pushMessage/XpAliPushMessageClientFallbackFactory.java
New file
@@ -0,0 +1,32 @@
package com.xiaopeng.api.factory.pushMessage;
import com.xiaopeng.api.dto.pushMessage.AliPushMessageDTO;
import com.xiaopeng.api.dto.pushMessage.PushOrderRouteDTO;
import com.xiaopeng.api.dto.pushMessage.SendMessageDTO;
import com.xiaopeng.api.feignClient.pushMessage.XpAliPushMessageClient;
import com.xiaopeng.api.feignClient.pushMessage.XpPushMessageClient;
import com.xiaopeng.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
/**
 * 消息Feign客户端降级工厂
 *
 * @author mitao
 */
@Slf4j
@Component
public class XpAliPushMessageClientFallbackFactory implements FallbackFactory<XpAliPushMessageClient> {
    @Override
    public XpAliPushMessageClient create(Throwable cause) {
        log.error("阿里消息服务调用失败: {}", cause.getMessage());
        return new XpAliPushMessageClient() {
            @Override
            public R<String> pushMessage(AliPushMessageDTO dto) {
                return R.fail("运维APP消息推送失败,"+cause.getMessage());
            }
        };
    }
}
xp-robotaxi-api/src/main/java/com/xiaopeng/api/feignClient/pushMessage/XpAliPushMessageClient.java
New file
@@ -0,0 +1,33 @@
package com.xiaopeng.api.feignClient.pushMessage;
import com.xiaopeng.api.dto.pushMessage.AliPushMessageDTO;
import com.xiaopeng.api.dto.pushMessage.PushOrderRouteDTO;
import com.xiaopeng.api.dto.pushMessage.SendMessageDTO;
import com.xiaopeng.api.factory.pushMessage.XpAliPushMessageClientFallbackFactory;
import com.xiaopeng.api.factory.pushMessage.XpPushMessageClientFallbackFactory;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.dto.ServiceNameConstantsEnum;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
/**
 * 消息Feign客户端
 *
 * @author xiaochen
 */
@FeignClient(contextId = "XpAliPushMessageClient",
        value = ServiceNameConstantsEnum.XP_PUSH_MESSAGE_SERVICE,
        fallbackFactory = XpAliPushMessageClientFallbackFactory.class)
public interface XpAliPushMessageClient {
    /**
     * 订单状态推送
     *
     * @param dto 消息实体
     * @return 订单状态推送
     */
    @PostMapping("/inner/v1/aliPush/pushMessage")
    R<String> pushMessage(@RequestBody AliPushMessageDTO dto);
}
xp-robotaxi-push-message-boot/src/main/java/com/xiaopeng/pushMessage/controller/AliPushController.java
New file
@@ -0,0 +1,41 @@
package com.xiaopeng.pushMessage.controller;
import com.xiaopeng.api.dto.pushMessage.AliPushMessageDTO;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.pushMessage.utils.AliPushUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@Api(tags = "阿里消息接口")
@RestController
@RequestMapping("")
public class AliPushController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private AliPushUtil aliPushUtil;
    /**
     * 运维端消息推送测试
     * @return
     */
    @GetMapping("/base/v1/aliPush/test")
    public R<String> test() throws Exception {
        String s = aliPushUtil.pushMessage(1, "13540694412", "测试", "{\"content\":\"测试消息\",\"strategy\":1\"type\":1,\"id\":1}");
        return R.ok(s);
    }
    /**
     * 运维端消息推送
     * @return
     */
    @PostMapping("/inner/v1/aliPush/pushMessage")
    public R<String> pushMessage(@RequestBody AliPushMessageDTO dto) {
        String result = aliPushUtil.pushMessage(dto.getDeviceType(), dto.getAccount(), dto.getTitle(), dto.getBody());
        return R.ok(result);
    }
}
xp-robotaxi-push-message-boot/src/main/java/com/xiaopeng/pushMessage/utils/AliPushUtil.java
@@ -9,12 +9,14 @@
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.util.concurrent.CompletableFuture;
@Slf4j
@Component
public class AliPushUtil {
    @Value("${ali.push.accessKeyId}")
@@ -50,62 +52,68 @@
    /**
     *
     *  消息推送方法
     * @param account 运维手机号
     * @param title 标题
     * @param body 消息内容
     * @throws Exception
     */
    public String pushMessage(Integer deviceType,String account, String title, String body) throws Exception {
    public String pushMessage(Integer deviceType,String account, String title, String body){
        StaticCredentialProvider credentialProvider = StaticCredentialProvider.create(
                Credential.builder()
                        .accessKeyId(accessKeyId)
                        .accessKeySecret(accessKeySecret)
                        .build()
        );
        try {
            StaticCredentialProvider credentialProvider = StaticCredentialProvider.create(
                    Credential.builder()
                            .accessKeyId(accessKeyId)
                            .accessKeySecret(accessKeySecret)
                            .build()
            );
        // Configure the Client
        try (AsyncClient client = AsyncClient.builder()
                .region(regionId) // Region ID
                .credentialsProvider(credentialProvider)
                .overrideConfiguration(
                    ClientOverrideConfiguration.create()
                    .setEndpointOverride("cloudpush.aliyuncs.com")
                )
                .build()) {
            // Configure the Client
            try (AsyncClient client = AsyncClient.builder()
                    .region(regionId) // Region ID
                    .credentialsProvider(credentialProvider)
                    .overrideConfiguration(
                            ClientOverrideConfiguration.create()
                                    .setEndpointOverride("cloudpush.aliyuncs.com")
                    )
                    .build()) {
            // 判断账号类型
            Long appKeys = appKey;
            if(deviceType == 2){
                appKeys = iosAppKey;
                // 判断账号类型
                Long appKeys = appKey;
                if(deviceType == 2){
                    appKeys = iosAppKey;
                }
                PushRequest pushRequest = PushRequest.builder()
                        .appKey(appKeys)
                        .pushType(PUSH_TYPE)
                        .deviceType(DEVICE_TYPE_ALL)
                        .target(TARGET)
                        .targetValue(account)
                        .title(title)
                        .body(body)
                        .build();
                CompletableFuture<PushResponse> response = client.push(pushRequest);
                PushResponse resp = response.get();
                String json = new Gson().toJson(resp);
                log.info("运维APP-消息推送:{}",json);
                JSONObject jsonObject = JSONObject.parseObject(json);
                Integer statusCode = jsonObject.getInteger("statusCode");
                if(statusCode == 200){
                    log.info("运维APP-消息推送成功");
                    JSONObject resultBody = jsonObject.getJSONObject("body");
                    return resultBody.getString("messageId");
                }else{
                    log.info("运维APP-消息推送失败");
                    return null;
                }
            }
            PushRequest pushRequest = PushRequest.builder()
                    .appKey(appKeys)
                    .pushType(PUSH_TYPE)
                    .deviceType(DEVICE_TYPE_ALL)
                    .target(TARGET)
                    .targetValue(account)
                    .title(title)
                    .body(body)
                    .build();
            CompletableFuture<PushResponse> response = client.push(pushRequest);
            PushResponse resp = response.get();
            String json = new Gson().toJson(resp);
            log.info("运维APP-消息推送:{}",json);
            JSONObject jsonObject = JSONObject.parseObject(json);
            Integer statusCode = jsonObject.getInteger("statusCode");
            if(statusCode == 200){
                log.info("运维APP-消息推送成功");
                JSONObject resultBody = jsonObject.getJSONObject("body");
                return resultBody.getString("messageId");
            }else{
                log.info("运维APP-消息推送失败");
                return null;
            }
        } catch (Exception e) {
            log.info("运维APP-消息推送失败-异常");
            e.printStackTrace();
            return null;
        }
    }