|
@@ -1,5 +1,6 @@
|
|
|
package com.boman.wechat.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.boman.common.core.utils.StringUtils;
|
|
|
import com.boman.domain.SysUser;
|
|
@@ -37,29 +38,35 @@ public class WxPushServiceImpl implements WxPushService {
|
|
|
* 微信小程序推送给用户
|
|
|
*/
|
|
|
@Override
|
|
|
- public String pushToUser(List<Long> userIds, Map<String, Object> params) {
|
|
|
+ public Map<String, Object> pushToUser(List<Long> userIds, Map<String, Object> params) {
|
|
|
String templateId = properties.getTemplateId();
|
|
|
+ Map<String, Object> pushResult = new HashMap<>();
|
|
|
|
|
|
//获取access_token
|
|
|
String accessToken = getAccessToken(properties.getAppId(), properties.getSecret());
|
|
|
if(StringUtils.isEmpty(accessToken)) {
|
|
|
- return "获取access_token失败";
|
|
|
+ pushResult.put("msg", "获取access_token失败");
|
|
|
+ return pushResult;
|
|
|
}
|
|
|
|
|
|
String url = properties.getMsgUrl() + "?access_token=" + accessToken;
|
|
|
AjaxResult result = remoteUserService.selectUserByIds(userIds);
|
|
|
- List<SysUser> users = (List<SysUser>) result.get("data");
|
|
|
+ List<Map<String, Object>> users = (List<Map<String, Object>>) result.get("data");
|
|
|
if(users == null || users.size() <= 0) {
|
|
|
- return "推送失败,没有选择推送人员";
|
|
|
+ pushResult.put("msg", "推送失败,没有选择推送人员");
|
|
|
+ return pushResult;
|
|
|
}
|
|
|
- List<WxMsgDto> vos = new ArrayList<>();
|
|
|
- for(SysUser user : users) {
|
|
|
- if(StringUtils.isEmpty(user.getOpenId())) {
|
|
|
+
|
|
|
+ WxMsgDto wxMssVo = new WxMsgDto();
|
|
|
+ Map<String, String> responseResult = new HashMap<>();
|
|
|
+ for(Map user : users) {
|
|
|
+ String openId = (String) user.get("openId");
|
|
|
+ if(StringUtils.isEmpty(openId)) {
|
|
|
continue;
|
|
|
}
|
|
|
//拼接推送的模版
|
|
|
- WxMsgDto wxMssVo = new WxMsgDto();
|
|
|
- wxMssVo.setTouser(user.getOpenId());//用户openid
|
|
|
+// WxMsgDto wxMssVo = new WxMsgDto();
|
|
|
+ wxMssVo.setTouser(openId);//用户openid
|
|
|
wxMssVo.setTemplate_id(templateId);//模版id
|
|
|
Map<String, TemplateData> msgMap = new HashMap<>(5);
|
|
|
for(String key : params.keySet()) {
|
|
@@ -68,15 +75,18 @@ public class WxPushServiceImpl implements WxPushService {
|
|
|
msgMap.put(key, templateData);
|
|
|
}
|
|
|
wxMssVo.setData(msgMap);
|
|
|
- vos.add(wxMssVo);
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, wxMssVo, String.class);
|
|
|
+ JSONObject resultJsonObject = JSON.parseObject(responseEntity.getBody());
|
|
|
+ if((Integer) resultJsonObject.get("errcode") != 0) {
|
|
|
+ responseResult.put(user.get("id").toString(), "用户 " + user.get("nickName") + " 推送失败," + resultJsonObject.get("errmsg").toString());
|
|
|
+ }
|
|
|
+ logger.info("result {}", responseEntity.getBody());
|
|
|
}
|
|
|
-
|
|
|
- if(vos.size() == 0) {
|
|
|
- return "没有可发送的用户,请确认所选用户是否订阅消息!";
|
|
|
+ if(responseResult.size() > 0) {
|
|
|
+ pushResult.put("msg", "有用户推送失败!");
|
|
|
+ pushResult.put("data", responseResult);
|
|
|
}
|
|
|
- ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, vos, String.class);
|
|
|
- logger.error("小程序推送结果={}", responseEntity.getBody());
|
|
|
- return responseEntity.getBody();
|
|
|
+ return pushResult;
|
|
|
}
|
|
|
|
|
|
/*
|