|
@@ -0,0 +1,93 @@
|
|
|
+package com.boman.wechat.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.boman.common.core.utils.StringUtils;
|
|
|
+import com.boman.domain.SysUser;
|
|
|
+import com.boman.domain.TemplateData;
|
|
|
+import com.boman.domain.dto.AjaxResult;
|
|
|
+import com.boman.system.api.RemoteUserService;
|
|
|
+import com.boman.wechat.config.WechatProperties;
|
|
|
+import com.boman.wechat.dto.WxMssVo;
|
|
|
+import com.boman.wechat.service.WxPushService;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class WxPushServiceImpl implements WxPushService {
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(WxPushServiceImpl.class);
|
|
|
+
|
|
|
+ //用来请求微信的get和post
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+ @Autowired
|
|
|
+ private WechatProperties properties;
|
|
|
+ @Autowired
|
|
|
+ private RemoteUserService remoteUserService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信小程序推送给用户
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String pushToUser(List<Long> userIds, Map<String, Object> params) {
|
|
|
+ String templateId = properties.getTemplateId();
|
|
|
+
|
|
|
+ //获取access_token
|
|
|
+ String accessToken = getAccessToken(properties.getAppId(), properties.getSecret());
|
|
|
+ if(StringUtils.isEmpty(accessToken)) {
|
|
|
+ return "获取access_token失败";
|
|
|
+ }
|
|
|
+
|
|
|
+ String url = properties.getMsgUrl() + "?access_token=" + accessToken;
|
|
|
+ AjaxResult result = remoteUserService.selectUserByIds(userIds);
|
|
|
+ List<SysUser> users = (List<SysUser>) result.get("data");
|
|
|
+ if(users == null || users.size() <= 0) {
|
|
|
+ return "推送失败,没有选择推送人员";
|
|
|
+ }
|
|
|
+ List<WxMssVo> vos = new ArrayList<>();
|
|
|
+ for(SysUser user : users) {
|
|
|
+ if(StringUtils.isEmpty(user.getOpenId())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //拼接推送的模版
|
|
|
+ WxMssVo wxMssVo = new WxMssVo();
|
|
|
+ wxMssVo.setTouser(user.getOpenId());//用户openid
|
|
|
+ wxMssVo.setTemplate_id(templateId);//模版id
|
|
|
+ Map<String, TemplateData> msgMap = new HashMap<>(5);
|
|
|
+ for(String key : params.keySet()) {
|
|
|
+ TemplateData templateData = new TemplateData();
|
|
|
+ templateData.setValue(params.get(key));
|
|
|
+ msgMap.put(key, templateData);
|
|
|
+ }
|
|
|
+ wxMssVo.setData(msgMap);
|
|
|
+ vos.add(wxMssVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(vos.size() == 0) {
|
|
|
+ return "没有可发送的用户,请确认所选用户是否订阅消息!";
|
|
|
+ }
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, vos, String.class);
|
|
|
+ logger.error("小程序推送结果={}", responseEntity.getBody());
|
|
|
+ return responseEntity.getBody();
|
|
|
+ }
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 获取access_token
|
|
|
+ * appid和appsecret到小程序后台获取,当然也可以让小程序开发人员给你传过来
|
|
|
+ * */
|
|
|
+ private String getAccessToken(String appid, String appsecret) {
|
|
|
+ //获取access_token
|
|
|
+ String url = properties.getAccessTokenUrl() + "&appid=" + properties.getAppId() + "&secret=" + properties.getSecret();
|
|
|
+ String json = restTemplate.getForObject(url, String.class);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(json);
|
|
|
+ return (String) jsonObject.get("access_token");
|
|
|
+ }
|
|
|
+}
|