liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
xp-robotaxi-push-message-boot/src/main/java/com/xiaopeng/pushMessage/utils/SubscribeMessageUtil.java
@@ -6,7 +6,11 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaopeng.api.dto.account.UserResourcesDto;
import com.xiaopeng.api.feignClient.account.SSOClient;
import com.xiaopeng.api.vo.account.WechatAccessTokenResVo;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.core.utils.StringUtils;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.pushMessage.model.Category;
@@ -18,6 +22,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -28,14 +33,20 @@
@Component
public class SubscribeMessageUtil {
    @Value("${wx.appletsAppid}")
    @Value("${wx.appletsAppid:1234}")
    private String wxAppletsAppid;
    @Value("${wx.appletsAppSecret}")
    @Value("${wx.appletsAppSecret:123456}")
    private String wxAppletsAppSecret;
    @Value("${sso.appid}")
    private String appId;
    @Autowired
    private RedisService redisService;
    @Resource
    private SSOClient ssoClient;
@@ -262,6 +273,21 @@
    }
    String getToken(Boolean isRefresh){
        WechatAccessTokenResVo wechatAccessToken = ssoClient.getWechatAccessToken(appId, wxAppletsAppid, isRefresh);
        if(null == wechatAccessToken){
            log.info("获取微信access_token失败");
            throw new ServiceException("获取微信access_token失败");
        }
        if(0 != wechatAccessToken.getCode()){
            log.info("获取微信access_token失败:{}", wechatAccessToken.getMsg());
            throw new ServiceException("获取微信access_token失败");
        }
        String accessToken = wechatAccessToken.getData().getAccessToken();
        return accessToken;
    }
    /**
     * 发送消息
     * @param template_id           所需下发的订阅模板id
@@ -273,7 +299,7 @@
     * @return
     */
    public R send(String template_id, String page, String touser, String data, String miniprogram_state, String lang){
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getWxAppletsAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + getToken(false);
        Map<String, Object> params = new HashMap<>();
        params.put("template_id", template_id);
        params.put("page", page);
@@ -286,13 +312,13 @@
        HttpResponse execute = post.execute();
        if(execute.getStatus() != 200){
            log.info("发送消息失败:{}", execute.body());
            throw new RuntimeException("发送消息失败");
            return R.fail(execute.body());
        }
        JSONObject jsonObject = JSON.parseObject(execute.body());
        Integer errcode = jsonObject.getInteger("errcode");
        if(0 != errcode){
            log.info("发送消息失败:{}", jsonObject.getString("errmsg"));
            throw new RuntimeException(jsonObject.getString("errmsg"));
            return R.fail(jsonObject.getString("errmsg"));
        }
        return R.ok();
    }