|
@@ -1,5 +1,9 @@
|
|
|
package cn.com.ctop.common.module.utils;
|
|
|
|
|
|
+import cn.com.ctop.common.module.entity.WexinConfig;
|
|
|
+import cn.com.ctop.common.module.service.IWexinConfigService;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import lombok.val;
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
@@ -7,69 +11,79 @@ import me.chanjar.weixin.cp.api.WxCpService;
|
|
|
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
|
|
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
|
|
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
+@Service
|
|
|
@Slf4j
|
|
|
public class CorpWexinUtils {
|
|
|
- private static WxCpService wxCpService;
|
|
|
+ @Autowired
|
|
|
+ private IWexinConfigService wexinConfigService;
|
|
|
+ private static Map<String, WexinConfig> configMap = new HashMap<>();
|
|
|
|
|
|
- {
|
|
|
- initService();
|
|
|
- }
|
|
|
-
|
|
|
- public static void initService() {
|
|
|
- val configStorage = new WxCpDefaultConfigImpl();
|
|
|
- configStorage.setCorpId("ww24b8a47826f5875f");
|
|
|
- configStorage.setAgentId(1000002);
|
|
|
- configStorage.setCorpSecret("MJIlySADGPlgvADnkFQPUpfZD4yV-4WN6066OgNnb0s");
|
|
|
- configStorage.setToken("hcst2019");
|
|
|
- configStorage.setAesKey("hcst2019");
|
|
|
- wxCpService = new WxCpServiceImpl();
|
|
|
- wxCpService.setWxCpConfigStorage(configStorage);
|
|
|
+ private Map<String, WexinConfig> getConfigMap() {
|
|
|
+ QueryWrapper<WexinConfig> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.orderByDesc("create_time");
|
|
|
+ List<WexinConfig> list = wexinConfigService.list(queryWrapper);
|
|
|
+ for (WexinConfig config : list) {
|
|
|
+ WxCpService wxCpService = new WxCpServiceImpl();
|
|
|
+ val configStorage = new WxCpDefaultConfigImpl();
|
|
|
+ configStorage.setCorpId(config.getCorpId());
|
|
|
+ configStorage.setAgentId(config.getAgentId());
|
|
|
+ configStorage.setCorpSecret(config.getCorpSecret());
|
|
|
+ configStorage.setToken(config.getToken());
|
|
|
+ configStorage.setAesKey(config.getAesKey());
|
|
|
+ wxCpService.setWxCpConfigStorage(configStorage);
|
|
|
+ config.setWxCpService(wxCpService);
|
|
|
+ configMap.put(config.getCorpId(), config);
|
|
|
+ }
|
|
|
+ return configMap;
|
|
|
}
|
|
|
|
|
|
- public static void sendMessage(List<String> wxUserList, String content) {
|
|
|
- if (wxCpService == null) {
|
|
|
- initService();
|
|
|
+ public void sendMessage(List<JSONObject> wxUserList, String content) {
|
|
|
+ if (Check.isNullMap(configMap)) {
|
|
|
+ getConfigMap();
|
|
|
}
|
|
|
if (wxUserList != null && wxUserList.size() > 0) {
|
|
|
- for (String userId : wxUserList) {
|
|
|
+ for (JSONObject userInfo : wxUserList) {
|
|
|
WxCpMessage message = new WxCpMessage();
|
|
|
message.setMsgType("text");
|
|
|
message.setContent(content);
|
|
|
message.setAgentId(1000002);
|
|
|
- message.setToUser(userId);
|
|
|
+ message.setToUser(userInfo.getString("wexin_id"));
|
|
|
try {
|
|
|
+ WexinConfig config = configMap.get(userInfo.getString("corp_id"));
|
|
|
+ WxCpService wxCpService = config.getWxCpService();
|
|
|
wxCpService.messageSend(message);
|
|
|
} catch (WxErrorException e) {
|
|
|
- log.error("发送信息失败,userId:{}", userId);
|
|
|
+ log.error("发送信息失败,userId:{}", userInfo.getString("wexin_id"));
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- public synchronized static void sendMessageByWeChatId(String weChatId, String content) {
|
|
|
- if (wxCpService == null) {
|
|
|
- initService();
|
|
|
+ public void sendMessageByWeChatId(JSONObject weChatInfo, String content) {
|
|
|
+ if (Check.isNullMap(configMap)) {
|
|
|
+ this.getConfigMap();
|
|
|
}
|
|
|
- if (!Check.isNull(weChatId)) {
|
|
|
+ if (!Check.isNull(weChatInfo)) {
|
|
|
WxCpMessage message = new WxCpMessage();
|
|
|
message.setMsgType("text");
|
|
|
message.setContent(content);
|
|
|
message.setAgentId(1000002);
|
|
|
- message.setToUser(weChatId);
|
|
|
+ message.setToUser(weChatInfo.getString("wexin_id"));
|
|
|
try {
|
|
|
+ WexinConfig config = configMap.get(weChatInfo.getString("corp_id"));
|
|
|
+ WxCpService wxCpService = config.getWxCpService();
|
|
|
wxCpService.messageSend(message);
|
|
|
} catch (WxErrorException e) {
|
|
|
- log.error("发送信息失败,userId:{}", weChatId);
|
|
|
+ log.error("发送信息失败,userId:{}", weChatInfo.getString("wexin_id"));
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
}
|