WechatServiceApi.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package com.poteviohealth.ym.ipos.utils;
  2. import cn.binarywang.wx.miniapp.api.WxMaService;
  3. import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
  4. import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
  5. import lombok.extern.slf4j.Slf4j;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. /**
  9. * 微信 token接口
  10. *
  11. * @author cxw
  12. */
  13. @Slf4j
  14. public class WechatServiceApi {
  15. private static Map<Integer, WxMaService> map = new HashMap<>();
  16. private WechatServiceApi() {
  17. }
  18. /**
  19. * 获取实例
  20. *
  21. * @param operatorId operatorId
  22. * @return 实例
  23. */
  24. public static synchronized WxMaService getInstance(Integer operatorId) {
  25. return map.get(operatorId);
  26. }
  27. public static synchronized WxMaService initInstance(Integer operatorId, String appid, String secret) {
  28. WxMaService wxMaService = map.get(operatorId);
  29. if (wxMaService != null) {
  30. return wxMaService;
  31. }
  32. WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
  33. wxMaConfig.setAppid(appid);
  34. wxMaConfig.setSecret(secret);
  35. wxMaService = new WxMaServiceImpl();
  36. //设置配置文件
  37. wxMaService.setWxMaConfig(wxMaConfig);
  38. map.put(operatorId, wxMaService);
  39. return wxMaService;
  40. }
  41. }