|
|
@@ -0,0 +1,46 @@
|
|
|
+package com.fs.wx.service;
|
|
|
+
|
|
|
+import com.fs.system.domain.WxMiniProgramConfig;
|
|
|
+import com.fs.system.mapper.WxMiniProgramConfigMapper;
|
|
|
+import com.fs.wx.service.impl.InMemoryWeChatAuthServiceImpl;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
+import org.springframework.beans.factory.SmartInitializingSingleton;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.ApplicationContextAware;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.validation.constraints.NotNull;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class WeChatAuthFactory implements ApplicationContextAware, SmartInitializingSingleton {
|
|
|
+ private static ApplicationContext applicationContext;
|
|
|
+ private final static Map<String,WeChatAuthService> weChatAuthServices = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
|
|
|
+ WeChatAuthFactory.applicationContext = applicationContext;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void afterSingletonsInstantiated() {
|
|
|
+ WxMiniProgramConfigMapper mapper = applicationContext.getBean(WxMiniProgramConfigMapper.class);
|
|
|
+ List<WxMiniProgramConfig> configs = mapper.selectAll();
|
|
|
+ for (WxMiniProgramConfig config : configs) {
|
|
|
+ WeChatAuthService weChatAuthService = new InMemoryWeChatAuthServiceImpl(config.getAppid(),config.getAppSecret());
|
|
|
+ weChatAuthServices.put(config.getAppid(),weChatAuthService);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过appid获取WeChatAuthService
|
|
|
+ * @param appid 小程序ID
|
|
|
+ * @return WeChatAuthService
|
|
|
+ */
|
|
|
+ public static WeChatAuthService getWeChatAuthService(String appid){
|
|
|
+ return weChatAuthServices.get(appid);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|