WeChatUtils.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.goafanti.common.utils;
  2. import java.util.Date;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.stereotype.Component;
  8. import com.alibaba.fastjson.JSONObject;
  9. import com.goafanti.common.constant.AFTConstants;
  10. import com.goafanti.common.error.BusinessException;
  11. @Component
  12. public class WeChatUtils {
  13. @Value(value = "${wx.appId}")
  14. private String appId;
  15. @Value(value = "${wx.appSecret}")
  16. private String appSecret;
  17. @Value(value = "${wx.state}")
  18. private String wxState;
  19. @Autowired
  20. private RedisUtil redisUtil;
  21. //待审核通知
  22. private static final String sptz="j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I";
  23. //审核结通知
  24. private static final String jgtz="pWia-KJPFwDM8ReDu_BOa9FJn31VFc81bp3yxfBmIRI";
  25. private static String send_url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
  26. /**
  27. *
  28. * @param openid
  29. * @param type 状态 0驳回 1发起 2同意
  30. * @param string
  31. * @param date
  32. */
  33. public Integer addNotice(String openid ,Integer type,Integer id, Date date, String aname,String remarks) {
  34. Map<String, Object> map=new HashMap<String, Object>();
  35. String accessToken=getAccessToken();
  36. String url=send_url.replace("ACCESS_TOKEN", accessToken);
  37. map.put("access_token", accessToken);
  38. map.put("touser", openid);
  39. map.put("page", "pages/egressDetails/index?id="+id);
  40. map.put("miniprogram_state", wxState);
  41. if (type==1) {
  42. map.put("template_id", sptz);
  43. map.put("data",getData(type,date, aname,remarks));
  44. }else {
  45. map.put("template_id", jgtz);
  46. map.put("data",getData(type,date, aname,remarks));
  47. }
  48. System.out.println("inupt="+JSONObject.toJSONString(map));
  49. JSONObject res=HttpUtils.httpPost(url, map);
  50. System.out.println("res="+JSONObject.toJSONString(res));
  51. if (res.get("errcode")!=null) {
  52. return res.getInteger("errcode");
  53. }else {
  54. return 1;
  55. }
  56. }
  57. private Map<String, Object> getData(Integer type,Date date, String aname, String remarks) {
  58. Map<String, Object> we=new HashMap<String, Object>();
  59. String strDate=DateUtils.formatDate(date, AFTConstants.YMDHMS_CHINESE);
  60. if (type==1) {
  61. Map<String, Object> v1=new HashMap<String, Object>();
  62. v1.put("value",strDate);
  63. we.put("time5", v1);
  64. Map<String, Object> v2=new HashMap<String, Object>();
  65. v2.put("value", aname);
  66. we.put("name3", v2);
  67. Map<String, Object> v3=new HashMap<String, Object>();
  68. v3.put("value", remarks);
  69. we.put("thing10", v3);
  70. }else {
  71. String thing2="";
  72. if (type==0){
  73. thing2="驳回";
  74. }else if (type==2){
  75. thing2="通过";
  76. }
  77. Map<String, Object> v1=new HashMap<String, Object>();
  78. v1.put("value",strDate);
  79. we.put("date3", v1);
  80. Map<String, Object> v2=new HashMap<String, Object>();
  81. v2.put("value", thing2);
  82. we.put("thing2", v2);
  83. Map<String, Object> v3=new HashMap<String, Object>();
  84. v3.put("value", remarks);
  85. we.put("thing6", v3);
  86. }
  87. return we;
  88. }
  89. public String getAccessToken() {
  90. Date date = new Date();
  91. if (redisUtil.presence("access_token")||redisUtil.presence("expires_in")) {
  92. String access_token=redisUtil.getString("access_token");
  93. String expires_in=redisUtil.getString("expires_in");
  94. if (date.getTime() < Long.valueOf(expires_in)) {
  95. LoggerUtils.debug(getClass(), "redis中获取accestoken");
  96. return access_token;
  97. } else {
  98. LoggerUtils.debug(getClass(), "accestoken超时重新获取");
  99. redisUtil.deleteString("access_token");
  100. redisUtil.deleteString("expires_in");
  101. return setSessionAccessToken(date);
  102. }
  103. } else {
  104. return setSessionAccessToken(date);
  105. }
  106. }
  107. private String setSessionAccessToken(Date date) {
  108. String url = String.format(
  109. "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appId,
  110. appSecret);
  111. JSONObject js = HttpUtils.httpGet(url);
  112. String errcode =js.getString("errcode");
  113. if (errcode!=null) {
  114. throw new BusinessException("获取AccessToken失败!");
  115. }else {
  116. LoggerUtils.debug(AsyncUtils.class,"获取并存入AccessToken成功,accesstoken="+js.getString("access_token"));
  117. }
  118. String access_token = js.getString("access_token");
  119. Long expires_in = js.getLong("expires_in");
  120. //返回时间为秒需要计算换成毫秒
  121. expires_in=expires_in*1000;
  122. expires_in+=date.getTime();
  123. redisUtil.setString("access_token", access_token);
  124. redisUtil.setString("expires_in", expires_in.toString());
  125. return access_token;
  126. }
  127. }