|
|
@@ -0,0 +1,144 @@
|
|
|
+package com.goafanti.common.utils;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.goafanti.common.constant.AFTConstants;
|
|
|
+import com.goafanti.common.error.BusinessException;
|
|
|
+import com.goafanti.core.shiro.token.TokenManager;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class WeChatUtils {
|
|
|
+
|
|
|
+
|
|
|
+ @Value(value = "${wx.appId}")
|
|
|
+ private String appId;
|
|
|
+ @Value(value = "${wx.appSecret}")
|
|
|
+ private String appSecret;
|
|
|
+ //待审核通知
|
|
|
+ private static final String sptz="j7WH3EwQnxGxwuV2HwmJhryxySPE8vOiV5cVOpp-42I";
|
|
|
+ //审核结通知
|
|
|
+ private static final String jgtz="pWia-KJPFwDM8ReDu_BOa9FJn31VFc81bp3yxfBmIRI";
|
|
|
+ private static String send_url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param openid
|
|
|
+ * @param type 状态 0驳回 1发起 2同意
|
|
|
+ * @param string
|
|
|
+ * @param date
|
|
|
+ */
|
|
|
+ public Integer addNotice(String openid ,Integer type, Date date, String aname,String remarks) {
|
|
|
+ Map<String, Object> map=new HashMap<String, Object>();
|
|
|
+ String accessToken=getAccessToken();
|
|
|
+ String url=send_url.replace("ACCESS_TOKEN", accessToken);
|
|
|
+ map.put("access_token", accessToken);
|
|
|
+ map.put("touser", openid);
|
|
|
+ String str=DateUtils.formatDate(date, AFTConstants.YMDHMS_CHINESE);
|
|
|
+ System.out.println(str);
|
|
|
+ if (type==1) {
|
|
|
+ map.put("template_id", sptz);
|
|
|
+ map.put("data",getData(type,date, aname,remarks));
|
|
|
+ }else {
|
|
|
+ map.put("template_id", jgtz);
|
|
|
+ map.put("data",getData(type,date, aname,remarks));
|
|
|
+ }
|
|
|
+ System.out.println("inupt="+JSONObject.toJSONString(map));
|
|
|
+ JSONObject res=HttpUtils.httpPost(url, map);
|
|
|
+ System.out.println("res="+JSONObject.toJSONString(res));
|
|
|
+ if (res.get("errcode")!=null) {
|
|
|
+ return res.getInteger("errcode");
|
|
|
+ }else {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ private Map<String, Object> getData(Integer type,Date date, String aname, String remarks) {
|
|
|
+ Map<String, Object> we=new HashMap<String, Object>();
|
|
|
+ String strDate=DateUtils.formatDate(date, AFTConstants.YMDHMS_CHINESE);
|
|
|
+ if (type==0) {
|
|
|
+ Map<String, Object> v1=new HashMap<String, Object>();
|
|
|
+ v1.put("value",strDate);
|
|
|
+ we.put("time5", v1);
|
|
|
+ Map<String, Object> v2=new HashMap<String, Object>();
|
|
|
+ v2.put("value", aname);
|
|
|
+ we.put("name3", v2);
|
|
|
+ Map<String, Object> v3=new HashMap<String, Object>();
|
|
|
+ v3.put("value", remarks);
|
|
|
+ we.put("thing10", v3);
|
|
|
+ }else {
|
|
|
+ String thing2="";
|
|
|
+ if (type==1){
|
|
|
+ thing2="通过";
|
|
|
+ }else if (type==2){
|
|
|
+ thing2="通过";
|
|
|
+ }
|
|
|
+ Map<String, Object> v1=new HashMap<String, Object>();
|
|
|
+ v1.put("value",strDate);
|
|
|
+ we.put("date3", v1);
|
|
|
+ Map<String, Object> v2=new HashMap<String, Object>();
|
|
|
+ v2.put("value", thing2);
|
|
|
+ we.put("thing2", v2);
|
|
|
+ Map<String, Object> v3=new HashMap<String, Object>();
|
|
|
+ v3.put("value", remarks);
|
|
|
+ we.put("thing6", v3);
|
|
|
+ }
|
|
|
+ return we;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public String getAccessToken() {
|
|
|
+ String access_token=(String) TokenManager.getVal2Session("access_token");
|
|
|
+ Long expires_in= (Long) TokenManager.getVal2Session("expires_in");
|
|
|
+ Date date=new Date();
|
|
|
+ if (access_token!=null&&expires_in!=null) {
|
|
|
+ LoggerUtils.debug(getClass(), "\n"
|
|
|
+ + "access_token="+access_token+"\n "
|
|
|
+ + "expires_in="+expires_in+"\n "
|
|
|
+ + "now="+DateUtils.formatDate(date, AFTConstants.YYYYMMDDHHMMSS));
|
|
|
+ if (new Date().getTime()<Long.valueOf(expires_in)) {
|
|
|
+ LoggerUtils.debug(getClass(), "session中获取accestoken");
|
|
|
+ return access_token;
|
|
|
+ }else {
|
|
|
+ TokenManager.deleteSession("access_token");
|
|
|
+ TokenManager.deleteSession("expires_in");
|
|
|
+ return setSessionAccessToken(date);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ return setSessionAccessToken(date);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String setSessionAccessToken(Date date) {
|
|
|
+ String url = String.format(
|
|
|
+ "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appId,
|
|
|
+ appSecret);
|
|
|
+ JSONObject js = HttpUtils.httpGet(url);
|
|
|
+ String errcode =js.getString("errcode");
|
|
|
+ if (errcode!=null) {
|
|
|
+ throw new BusinessException("获取AccessToken失败!");
|
|
|
+ }else {
|
|
|
+ LoggerUtils.debug(AsyncUtils.class,"获取AccessToken成功,accesstoken="+js.getString("access_token"));
|
|
|
+ }
|
|
|
+ String access_token = js.getString("access_token");
|
|
|
+ Long expires_in = js.getLong("expires_in");
|
|
|
+ expires_in+=date.getTime();
|
|
|
+ TokenManager.setVal2Session("access_token", access_token);
|
|
|
+ TokenManager.setVal2Session("expires_in", expires_in);
|
|
|
+ return access_token;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|