2018年02月18日   码农之路   3,831 次浏览

一般的项目中只使用到一个公众号,这个按照开发文档就可以实现了。但是很多平台型的项目,需要同时支持多个公众号。我也参与过几个平台型的项目,但是总觉得这些项目中多公众号的实现方式用着不顺手,总结了一下各个项目中的优缺点,便有了下面的实现方式。代码简单,直接上代码。

package cn.yyjjssnn.wxmp;

import java.util.Hashtable;
import java.util.Map;

import org.springframework.util.Assert;

/**
 * 支持多企业多公众号的使用
 * 
 * @author LiuYang
 * @date 2018年2月10日 上午10:11:40
 * @version V1.0
 */
public class WxMpUtil {

	/**
	 * 缓存MultiWxMpService,方便下次使用。
	 * 这里为演示采用MAP,可以根据业务的需要使用其他单机缓存或分布式缓存。
	 */
	private static Map<String, MultiWxMpService> multiWxMpServiceCache = new Hashtable<String, MultiWxMpService>();

	private WxMpUtil() {
	}

	/**
	 * 根据ID获取对应的MultiWxMpService
	 * @param companyId 企业ID
	 * @return
	 */
	public static synchronized MultiWxMpService get(String companyId) {
		Assert.hasText(companyId, "企业ID不能为空");

		MultiWxMpService wxMpService = multiWxMpServiceCache.get(companyId);
		if (wxMpService == null) {
			wxMpService = new MultiWxMpService(companyId);
			multiWxMpServiceCache.put(companyId, wxMpService);
		}
		return wxMpService;
	}

}

 

package cn.yyjjssnn.wxmp;

import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

/**
 * 微信公众号支持多帐号版本的实现
 * 
 * @author LiuYang
 * @date 2018年2月10日 下午4:39:25
 * @version V1.0
 */
public class MultiWxMpService extends WxMpServiceImpl {

	/**
	 * 区分微信公众号多帐号的唯一ID,我这里用的企业ID
	 */
	private String companyId = null;
	private WxMpMessageRouter router = null;

	public MultiWxMpService(String companyId) {
		this(companyId, null);
	}

	public MultiWxMpService(String companyId, WxMpMessageRouter router) {
		this.companyId = companyId;

		setWxMpConfigStorage();
		if (router == null) {
			setWxMpMessageRouterRule();
		} else {
			this.router = router;
		}
	}

	public String getCompanyId() {
		return companyId;
	}

	public WxMpMessageRouter getMessageRouter() {
		return router;
	}

	private void setWxMpConfigStorage() {
		//TODO 根据companyId读取公众号参数配置 这里可以从配置文件、数据库、缓存任意一个源读取
		String appId = "";
		String secret = "";
		String token = "";
		String aesKey = "";

		// 设置公众号参数
		WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
		config.setAppId(appId); // 设置微信公众号的appid
		config.setSecret(secret); // 设置微信公众号的app corpSecret
		config.setToken(token); // 设置微信公众号的token
		config.setAesKey(aesKey); // 设置微信公众号的EncodingAESKey

		this.setWxMpConfigStorage(config);
	}

	private void setWxMpMessageRouterRule() {

		router = new WxMpMessageRouter(this);

		// 记录所有事件的日志
		router.rule().handler(new LogHandler()).next();
	}
}

 

package cn.yyjjssnn.wxmp;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

/**
 * 消息路由基类 这里主要重载了一下handle方法,将参数WxMpService转换成MultiWxMpService方便其他路由使用
 * 
 * @author LiuYang
 * @date 2018年2月10日 上午11:38:46
 * @version V1.0
 */
public abstract class BaseHandler implements WxMpMessageHandler {

	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
			WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
		MultiWxMpService mpService = (MultiWxMpService) wxMpService;
		return handle(wxMessage, context, mpService, sessionManager);
	}

	public abstract WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
			MultiWxMpService mpService, WxSessionManager sessionManager) throws WxErrorException;
}

 

package cn.yyjjssnn.wxmp;

import java.util.Map;

import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

/**
 * 记录所有事件的日志
 * 
 * @author LiuYang
 * @date 2018年2月10日 下午4:12:30
 * @version V1.0
 */
public class LogHandler extends BaseHandler {

	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
			MultiWxMpService mpService, WxSessionManager sessionManager) {
		logger.info("\n接收到 {} 请求消息,内容:{}", mpService.getCompanyId(), wxMessage);
		return null;
	}

}

 

赞 赏
申明:除非注明,本站文章均为原创,转载请以链接形式标明本文地址。 如有问题,请于一周内与本站联系,本站将在第一时间对相关内容进行处理。
本文地址: http://www.yyjjssnn.cn/articles/820.html

>>> Hello World <<<

这篇内容是否帮助到你了呢?

如果你有任何疑问或有建议留给其他朋友,都可以给我留言。

:wink: :twisted: :surprised: :smile: :smile9: :smile8: :smile7: :smile6: :smile5: :smile56: :smile55: :smile54: :smile53: :smile52: :smile51: :smile50: :smile4: :smile49: :smile48: :smile47: :smile46: :smile45: :smile44: :smile43: :smile42: :smile41: :smile40: :smile3: :smile39: :smile38: :smile37: :smile36: :smile35: :smile34: :smile33: :smile32: :smile31: :smile30: :smile2: :smile29: :smile28: :smile27: :smile26: :smile25: :smile24: :smile23: :smile22: :smile21: :smile20: :smile1: :smile19: :smile18: :smile17: :smile16: :smile15: :smile14: :smile13: :smile12: :smile11: :smile10: :smile0: :sad: :rolleyes1: :redface: :razz: :question: :neutral: :mrgreen: :mad: :lol: :idea: :exclaim: :evil: :eek: :cry: :cool: :confused: :biggrin: :arrow:

友情链接: 程序员刘杨 刘杨
Copyright 2003~2018 保留所有权利 | 网站地图
备案号:湘ICP备14001005号-2

湘公网安备 43011102001322号