All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zopen.wechat.mp.service.miniprogram.WechatProgramSubscribeMessageService Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.zopen.wechat.mp.service.miniprogram;

import com.zopen.ato.properties.WechatMpProperties;
import com.zopen.wechat.exception.WechatAssert;
import com.zopen.wechat.mp.dto.message.subscribe.SubscribeMessage;
import com.zopen.wechat.mp.dto.message.subscribe.SubscribeMessageData;
import com.zopen.wechat.mp.dto.message.subscribe.SubscribeMessageResp;
import com.zopen.wechat.mp.service.WechatHttpUrl;
import com.zopen.wechat.mp.task.WechatInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 订阅消息
 *
 * @author [email protected]
 * @since 2020/1/20
 */
@Component("atoWechatProgramSubscribeMessageService")
public class WechatProgramSubscribeMessageService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private WechatMpProperties wechatMpProperties;

    // 发送订阅消息(使用 application 中配置的 appId)
    public void postMessageByBoot(String openId, String templateId, String page, Map dataMap) {
        String appId = wechatMpProperties.getAppId();
        postMessage(openId, templateId, page, dataMap, appId);
    }

    // 发送订阅消息
    public void postMessage(String openId, String templateId, String page, Map dataMap, String appId) {
        String accessToken = WechatInfo.getAccessToken(appId);
        httpPostMessage(accessToken, openId, templateId, page, dataMap);
    }

    private void httpPostMessage(String accessToken, String openId, String templateId, String page, Map dataMap) {
        WechatAssert.notEmpty(dataMap, "dataMap 不能为空");
        Map dataMapObj = new HashMap<>(8);
        for (Map.Entry entry : dataMap.entrySet()) {
            dataMapObj.put(entry.getKey(), new SubscribeMessageData(entry.getValue()));
        }
        httpPostMessageBySrcMap(accessToken, openId, templateId, page, dataMapObj);
    }

    private void httpPostMessageBySrcMap(String accessToken, String openId, String templateId, String page, Map dataMap) {
        WechatAssert.notNullAndEmpty(accessToken, "accessToken 不能为空");
        WechatAssert.notNullAndEmpty(openId, "openId 不能为空");
        WechatAssert.notNullAndEmpty(templateId, "模板ID不能为空");
        WechatAssert.notEmpty(dataMap, "dataMap 不能为空");
        SubscribeMessage message = new SubscribeMessage(openId, templateId, page, dataMap);
        String postUrl = String.format(WechatHttpUrl.MINI_SEND_SUBSCRIBE_MESSAGE, accessToken);
        SubscribeMessageResp resp = restTemplate.postForObject(postUrl, message, SubscribeMessageResp.class);
        WechatAssert.notNull(resp, "发送订阅消息失败");
        resp.valid("发送订阅消息失败");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy