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

com.mizhousoft.weixin.mp.service.impl.WxMpFreePublishServiceImpl Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package com.mizhousoft.weixin.mp.service.impl;

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

import org.apache.commons.lang3.StringUtils;

import com.mizhousoft.commons.json.JSONException;
import com.mizhousoft.commons.json.JSONUtils;
import com.mizhousoft.weixin.common.WXException;
import com.mizhousoft.weixin.mp.domain.freepublish.WxMpFreePublishInfo;
import com.mizhousoft.weixin.mp.domain.freepublish.WxMpFreePublishList;
import com.mizhousoft.weixin.mp.service.WxMpFreePublishService;
import com.mizhousoft.weixin.mp.service.WxMpService;

import kong.unirest.core.Unirest;
import kong.unirest.core.UnirestException;

/**
 * 微信 发布能力 接口.
 *
 */
public class WxMpFreePublishServiceImpl implements WxMpFreePublishService
{
	// WxMpService
	private WxMpService wxMpService;

	/**
	 * 构造函数
	 *
	 * @param wxMpService
	 */
	public WxMpFreePublishServiceImpl(WxMpService wxMpService)
	{
		super();
		this.wxMpService = wxMpService;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public WxMpFreePublishInfo getArticleFromId(String articleId) throws WXException
	{
		try
		{
			Map params = new HashMap<>(1);
			params.put("article_id", articleId);
			String body = JSONUtils.toJSONString(params);

			String url = API_URL_PREFIX + "/getarticle";

			String accessToken = wxMpService.getAccessToken();

			url = url + "?access_token=" + accessToken;

			String data = Unirest.post(url).body(body).asString().getBody();

			WxMpFreePublishInfo result = JSONUtils.parse(data, WxMpFreePublishInfo.class);

			if (!StringUtils.isBlank(result.getErrorCode()))
			{
				throw new WXException(result.getErrorCode(), result.getErrorMsg());
			}

			return result;
		}
		catch (UnirestException e)
		{
			throw new WXException(e.getMessage(), e);
		}
		catch (JSONException e)
		{
			throw new WXException(e.getErrorCode(), e.getCodeParams(), e.getMessage(), e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public WxMpFreePublishList getPublicationRecords(int offset, int count, boolean noContent) throws WXException
	{
		try
		{
			Map params = new HashMap<>(3);
			params.put("offset", offset);
			params.put("count", count);
			params.put("no_content", noContent ? 1 : 0);
			String body = JSONUtils.toJSONString(params);

			String url = API_URL_PREFIX + "/batchget";

			String accessToken = wxMpService.getAccessToken();

			url = url + "?access_token=" + accessToken;

			String data = Unirest.post(url).body(body).asString().getBody();

			WxMpFreePublishList result = JSONUtils.parse(data, WxMpFreePublishList.class);

			if (!StringUtils.isBlank(result.getErrorCode()))
			{
				throw new WXException(result.getErrorCode(), result.getErrorMsg());
			}

			return result;
		}
		catch (UnirestException e)
		{
			throw new WXException(e.getMessage(), e);
		}
		catch (JSONException e)
		{
			throw new WXException(e.getErrorCode(), e.getCodeParams(), e.getMessage(), e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public WxMpFreePublishList getPublicationRecords(int offset, int count) throws WXException
	{
		return getPublicationRecords(offset, count, false);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy