完成小程序直播添加相关功能
This commit is contained in:
@ -0,0 +1,73 @@
|
||||
package co.yixiang.tools.config;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
||||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||
import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
|
||||
import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
|
||||
import co.yixiang.utils.RedisUtil;
|
||||
import co.yixiang.utils.RedisUtils;
|
||||
import co.yixiang.utils.ShopKeyUtils;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
@Configuration
|
||||
public class WxMaConfiguration {
|
||||
private static Map<String, WxMaService> maServices = Maps.newHashMap();
|
||||
private static Map<String, WxMaMessageRouter> routers = Maps.newHashMap();
|
||||
private static RedisUtils redisUtils;
|
||||
private static WxMaMessageHandler wxMaMessageHandler;
|
||||
public static WxMaMessageRouter getRouter(String appid) {
|
||||
return routers.get(appid);
|
||||
}
|
||||
@Autowired
|
||||
public WxMaConfiguration(RedisUtils redisUtils) {
|
||||
this.redisUtils = redisUtils;
|
||||
this.wxMaMessageHandler = wxMaMessageHandler;
|
||||
}
|
||||
|
||||
public static WxMaService getWxMaService() {
|
||||
WxMaService wxMaService = maServices.get(ShopKeyUtils.getYshopWeiXinMaSevice());
|
||||
//增加一个redis标识
|
||||
if(wxMaService == null || redisUtils.get(ShopKeyUtils.getYshopWeiXinMaSevice()) == null){
|
||||
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
|
||||
config.setMsgDataFormat("JSON");
|
||||
config.setAppid(RedisUtil.get(ShopKeyUtils.getWxAppAppId()));
|
||||
config.setSecret(RedisUtil.get(ShopKeyUtils.getWxAppSecret()));
|
||||
config.setToken(RedisUtil.get(ShopKeyUtils.getWechatMaToken()));
|
||||
config.setAesKey(RedisUtil.get(ShopKeyUtils.getWechatMaEncodingAESKey()));
|
||||
wxMaService = new WxMaServiceImpl();
|
||||
wxMaService.setWxMaConfig(config);
|
||||
maServices.put(ShopKeyUtils.getYshopWeiXinMaSevice(), wxMaService);
|
||||
routers.put(ShopKeyUtils.getYshopWeiXinMaSevice(), newRouter(wxMaService));
|
||||
//增加标识
|
||||
redisUtils.set(ShopKeyUtils.getYshopWeiXinMaSevice(),"yshop");
|
||||
|
||||
}
|
||||
return wxMaService;
|
||||
}
|
||||
/**
|
||||
* 移除WxMpService
|
||||
*/
|
||||
public static void removeWxMaService(){
|
||||
redisUtils.del(ShopKeyUtils.getYshopWeiXinMaSevice());
|
||||
maServices.remove(ShopKeyUtils.getYshopWeiXinMaSevice());
|
||||
routers.remove(ShopKeyUtils.getYshopWeiXinMaSevice());
|
||||
}
|
||||
private static WxMaMessageRouter newRouter(WxMaService service) {
|
||||
final WxMaMessageRouter router = new WxMaMessageRouter(service);
|
||||
router.rule().handler(wxMaMessageHandler).next();
|
||||
return router;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user