xin
2025-06-04 9931d6f56816aecb09333cef2d12777c08793547
oying-system/src/main/java/com/oying/modules/security/service/WeiXinService.java
@@ -2,6 +2,10 @@
import com.alibaba.fastjson2.JSONObject;
import com.oying.modules.security.config.WeiXinProperties;
import com.oying.modules.system.domain.User;
import com.oying.modules.system.domain.UserSubscribe;
import com.oying.modules.system.service.UserService;
import com.oying.modules.system.service.UserSubscribeService;
import com.oying.utils.HttpRequest;
import com.oying.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
@@ -24,6 +28,10 @@
    private RedisUtils redisUtils;
    @Value("${wx.enabled}")
    private Boolean wxEnabled;
    @Resource
    private UserSubscribeService subscribeService;
    @Resource
    private UserService userService;
    /**
     * POST 获取稳定版接口调用凭据 获取小程序全局唯一后台接口调用凭据,token有效期为7200s,开发者需要进行妥善保存。
@@ -81,22 +89,30 @@
    /**
     * POST 该接口用于发送订阅消息。
     *
     * @param data 请求参数
     * @param openId 用户openId
     * @param data       请求参数
     * @param openid     用户openId
     * @param templateId 订阅模板id
     * @param page 小程序跳转链接
     * @return JSONObject
     * @param page       小程序跳转链接
     */
    public JSONObject sendMessage(Map<String, Object> data, String openId, String templateId, String page) {
    public void sendMessage(Map<String, Object> data, String openid, String templateId, String page) {
        JSONObject jsonObject = new JSONObject();
        if (wxEnabled) {
            String url = weiXinProperties.getSendMessage();
            url = url.replace("{accessToken}", getStableAccessToken());
            Map<String, Object> map = getSendMessageDto(data, openId, templateId, page);
            return HttpRequest.exchangeJsonObject(HttpMethod.POST, url, map);
            Map<String, Object> map = getSendMessageDto(data, openid, templateId, page);
            jsonObject = HttpRequest.exchangeJsonObject(HttpMethod.POST, url, map);
        } else {
            jsonObject.put("message", "测试环境");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", "测试环境");
        return jsonObject;
        User user = userService.findByOpenid(openid);
        UserSubscribe sub = new UserSubscribe();
        sub.setSubType(templateId);
        sub.setUserId(user.getId());
        sub.setOpenid(openid);
        sub.setUsername(user.getUsername());
        sub.setSendMessage(JSONObject.toJSONString(data));
        sub.setSubMessage(jsonObject.toJSONString());
        subscribeService.save(sub);
    }
    private Map<String, Object> getSendMessageDto(Map<String, Object> data, String openId, String templateId, String page) {