彭雪彬
2025-07-15 a0801ab3f16bd3d967c220c5925ad02673ce1baa
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,开发者需要进行妥善保存。
@@ -61,7 +69,15 @@
        url = url.replace("{appid}", weiXinProperties.getAppId())
                .replace("{secret}", weiXinProperties.getAppSecret())
                .replace("{js_code}", js_code);
        return HttpRequest.exchangeJsonObject(HttpMethod.GET, url, null);
        return JSONObject.parseObject(HttpRequest.exchangeString(HttpMethod.GET, url, null));
    }
    public JSONObject code2SessionRider(String js_code) {
        String url = weiXinProperties.getCode2Session();
        url = url.replace("{appid}", weiXinProperties.getRiderAppId())
                .replace("{secret}", weiXinProperties.getRiderAppSecret())
                .replace("{js_code}", js_code);
        return JSONObject.parseObject(HttpRequest.exchangeString(HttpMethod.GET, url, null));
    }
    /**
@@ -81,19 +97,30 @@
    /**
     * POST 该接口用于发送订阅消息。
     *
     * @param data 请求参数
     * @return JSONObject
     * @param data       请求参数
     * @param openid     用户openId
     * @param templateId 订阅模板id
     * @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) {