WeChatServiceImpl

    1. package com.saas.erp.service.impl;
    2. import com.alibaba.fastjson.JSON;
    3. import com.alibaba.fastjson.JSONObject;
    4. import com.saas.erp.common.BizException;
    5. import com.saas.erp.common.ErrorCodeEnum;
    6. import com.saas.erp.domain.dal.auto.dao.EmployeeDAO;
    7. import com.saas.erp.service.WeChatService;
    8. import com.saas.erp.utils.TemplateData;
    9. import com.saas.erp.web.vo.WeChatMessageVO;
    10. import org.springframework.beans.factory.annotation.Autowired;
    11. import org.springframework.http.ResponseEntity;
    12. import org.springframework.stereotype.Component;
    13. import org.springframework.transaction.annotation.Transactional;
    14. import org.springframework.web.client.RestTemplate;
    15. import java.util.HashMap;
    16. import java.util.Map;
    17. @Component
    18. public class WeChatServiceImpl extends BaseServiceImpl implements WeChatService {
    19. public static final String APP_ID = "xxx"; // 小程序ID
    20. public static final String APP_SECRET = "xxx"; // 小程序密码
    21. public static final String ACCESS_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";
    22. public static final String OPEN_ID = "xxx"; // 发送对象的OpenId
    23. public static final String TEMPLATE_ID = "xxx"; // 消息模板的ID
    24. @Autowired
    25. private EmployeeDAO employeeDAO;
    26. @Override
    27. @Transactional
    28. public String getOpenId(String code) {
    29. RestTemplate restTemplate = new RestTemplate();
    30. Map<String, String> params = new HashMap<>();
    31. params.put("appid", APP_ID);
    32. params.put("secret", APP_SECRET);
    33. params.put("js_code", code);
    34. ResponseEntity<String> responseEntity = restTemplate.getForEntity(
    35. "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code&appid={appid}&secret={secret}&js_code={js_code}", String.class, params);
    36. JSONObject object = JSON.parseObject(responseEntity.getBody());
    37. String openid = object.getString("openid");
    38. return openid;
    39. }
    40. @Override
    41. @Transactional
    42. public String sendFeedbackMessage(String feedback, String employeeName, String time) {
    43. RestTemplate restTemplate = new RestTemplate();
    44. String url = ACCESS_URL + getAccessToken();
    45. //拼接推送的模版
    46. WeChatMessageVO weChatMessageVO = new WeChatMessageVO();
    47. weChatMessageVO.setTouser(OPEN_ID);
    48. weChatMessageVO.setTemplate_id(TEMPLATE_ID);
    49. weChatMessageVO.setPage("pages/function/function");
    50. Map<String, TemplateData> templateData = new HashMap<>(3);
    51. templateData.put("thing2", new TemplateData(feedback));
    52. templateData.put("name4", new TemplateData(employeeName));
    53. templateData.put("date1", new TemplateData(time));
    54. weChatMessageVO.setData(templateData);
    55. ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, weChatMessageVO, String.class);
    56. return responseEntity.getBody();
    57. }
    58. private String getAccessToken() {
    59. RestTemplate restTemplate = new RestTemplate();
    60. Map<String, String> params = new HashMap<>();
    61. params.put("appid", APP_ID);
    62. params.put("secret", APP_SECRET);
    63. ResponseEntity<String> responseEntity = restTemplate.getForEntity(
    64. "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={secret}", String.class, params);
    65. String body = responseEntity.getBody();
    66. JSONObject object = JSON.parseObject(body);
    67. String Access_Token = object.getString("access_token");
    68. // 需要考虑时效,如果还在时效内,没必要重新获取
    69. String expires_in = object.getString("expires_in");
    70. return Access_Token;
    71. }
    72. }

    WeChatMessageVO

    1. package com.saas.erp.web.vo;
    2. import com.saas.erp.utils.TemplateData;
    3. import java.util.Map;
    4. public class WeChatMessageVO {
    5. private String touser; // 用户openId
    6. private String template_id; // 订阅消息模版id
    7. private String page = "pages/function/function";// 默认跳到小程序首页
    8. private Map<String, TemplateData> data; // 推送文字
    9. public String getTouser() {
    10. return touser;
    11. }
    12. public void setTouser(String touser) {
    13. this.touser = touser;
    14. }
    15. public String getTemplate_id() {
    16. return template_id;
    17. }
    18. public void setTemplate_id(String template_id) {
    19. this.template_id = template_id;
    20. }
    21. public String getPage() {
    22. return page;
    23. }
    24. public void setPage(String page) {
    25. this.page = page;
    26. }
    27. public Map<String, TemplateData> getData() {
    28. return data;
    29. }
    30. public void setData(Map<String, TemplateData> data) {
    31. this.data = data;
    32. }
    33. }
    1. package com.saas.erp.utils;
    2. public class TemplateData {
    3. private String value;
    4. public TemplateData(String value) {
    5. this.value = value;
    6. }
    7. public String getValue() {
    8. return value;
    9. }
    10. public void setValue(String value) {
    11. this.value = value;
    12. }
    13. }