WeChatServiceImpl
package com.saas.erp.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.saas.erp.common.BizException;
import com.saas.erp.common.ErrorCodeEnum;
import com.saas.erp.domain.dal.auto.dao.EmployeeDAO;
import com.saas.erp.service.WeChatService;
import com.saas.erp.utils.TemplateData;
import com.saas.erp.web.vo.WeChatMessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Component
public class WeChatServiceImpl extends BaseServiceImpl implements WeChatService {
public static final String APP_ID = "xxx"; // 小程序ID
public static final String APP_SECRET = "xxx"; // 小程序密码
public static final String ACCESS_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";
public static final String OPEN_ID = "xxx"; // 发送对象的OpenId
public static final String TEMPLATE_ID = "xxx"; // 消息模板的ID
@Autowired
private EmployeeDAO employeeDAO;
@Override
@Transactional
public String getOpenId(String code) {
RestTemplate restTemplate = new RestTemplate();
Map<String, String> params = new HashMap<>();
params.put("appid", APP_ID);
params.put("secret", APP_SECRET);
params.put("js_code", code);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(
"https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code&appid={appid}&secret={secret}&js_code={js_code}", String.class, params);
JSONObject object = JSON.parseObject(responseEntity.getBody());
String openid = object.getString("openid");
return openid;
}
@Override
@Transactional
public String sendFeedbackMessage(String feedback, String employeeName, String time) {
RestTemplate restTemplate = new RestTemplate();
String url = ACCESS_URL + getAccessToken();
//拼接推送的模版
WeChatMessageVO weChatMessageVO = new WeChatMessageVO();
weChatMessageVO.setTouser(OPEN_ID);
weChatMessageVO.setTemplate_id(TEMPLATE_ID);
weChatMessageVO.setPage("pages/function/function");
Map<String, TemplateData> templateData = new HashMap<>(3);
templateData.put("thing2", new TemplateData(feedback));
templateData.put("name4", new TemplateData(employeeName));
templateData.put("date1", new TemplateData(time));
weChatMessageVO.setData(templateData);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, weChatMessageVO, String.class);
return responseEntity.getBody();
}
private String getAccessToken() {
RestTemplate restTemplate = new RestTemplate();
Map<String, String> params = new HashMap<>();
params.put("appid", APP_ID);
params.put("secret", APP_SECRET);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}", String.class, params);
String body = responseEntity.getBody();
JSONObject object = JSON.parseObject(body);
String Access_Token = object.getString("access_token");
// 需要考虑时效,如果还在时效内,没必要重新获取
String expires_in = object.getString("expires_in");
return Access_Token;
}
}
WeChatMessageVO
package com.saas.erp.web.vo;
import com.saas.erp.utils.TemplateData;
import java.util.Map;
public class WeChatMessageVO {
private String touser; // 用户openId
private String template_id; // 订阅消息模版id
private String page = "pages/function/function";// 默认跳到小程序首页
private Map<String, TemplateData> data; // 推送文字
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getTemplate_id() {
return template_id;
}
public void setTemplate_id(String template_id) {
this.template_id = template_id;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public Map<String, TemplateData> getData() {
return data;
}
public void setData(Map<String, TemplateData> data) {
this.data = data;
}
}
package com.saas.erp.utils;
public class TemplateData {
private String value;
public TemplateData(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}