1
jyy
2021-01-07 426614e2a35be683f4f035037204e59f72cc4a29
1
3 files added
217 ■■■■■ changed files
zq-erp/src/main/java/com/matrix/component/tools/HttpClientUtil.java 55 ●●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/GzhTemplateMessagePojo.java 142 ●●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo.java 20 ●●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/component/tools/HttpClientUtil.java
New file
@@ -0,0 +1,55 @@
package com.matrix.component.tools;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.tools.LogUtil;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.IOException;
/**
 * POST 请求工具
 */
public class HttpClientUtil {
    public static JSONObject sendPostWithJson(String url, String json) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        try {
            //第一步:创建HttpClient对象
            httpClient = HttpClients.createDefault();
            //第二步:创建httpPost对象
            HttpPost httpPost = new HttpPost(url);
            //第三步:给httpPost设置JSON格式的参数
            StringEntity requestEntity = new StringEntity(json, "utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-type", "application/json");
            httpPost.setEntity(requestEntity);
            //第四步:发送HttpPost请求,获取返回值
            String returnValue = httpClient.execute(httpPost, responseHandler); //调接口获取返回值时,必须用此方法
            return JSON.parseObject(returnValue);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                LogUtil.error("http请求发送失败", e);
                throw new GlobleException(e.getMessage());
            }
        }
        //第五步:处理返回值
        return null;
    }
}
zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/GzhTemplateMessagePojo.java
New file
@@ -0,0 +1,142 @@
package com.matrix.system.wechart.templateMsg;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.utils.HttpsUtils;
import org.apache.commons.collections.map.HashedMap;
import java.util.Map;
/**
 * 公众号模板消息对象
 */
public class GzhTemplateMessagePojo {
    private  String touser;
    private  String template_id;
    private  String url;
    private  Miniprogram miniprogram;
    public static class Miniprogram{
        private  String appid;
        private  String pagepath;
        public Miniprogram(String appid, String pagepath) {
            this.appid = appid;
            this.pagepath = pagepath;
        }
        public String getAppid() {
            return appid;
        }
        public void setAppid(String appid) {
            this.appid = appid;
        }
        public String getPagepath() {
            return pagepath;
        }
        public void setPagepath(String pagepath) {
            this.pagepath = pagepath;
        }
    }
    public Map<String ,Item> data=new HashedMap();
    public static class Item{
        private String value;
        private String color;
        public Item(String value, String color) {
            this.value = value;
            this.color = color;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
        public String getColor() {
            return color;
        }
        public void setColor(String color) {
            this.color = color;
        }
    }
    public static void main(String[] args) {
        GzhTemplateMessagePojo messagePojo=new GzhTemplateMessagePojo();
        messagePojo.setTouser("123");
        messagePojo.setTemplate_id("543");
        messagePojo.setUrl("231");
        messagePojo.setMiniprogram("65475234","/123/234123412");
        messagePojo.setFirst("1231","#453454");
        messagePojo.setKeyWord("1231","#453454");
        System.out.println(JSONObject.toJSON(messagePojo).toString());
    }
    public void setMiniprogram(String appid,String url){
        this.setMiniprogram(new Miniprogram(appid,url));
    }
    public void setFirst(String value, String color){
        this.data.put("first",new Item(value,color));
    }
    public void setKeyWord(String value, String color){
        this.data.put("keyword"+(this.data.size()),new Item(value,color));
    }
    public Miniprogram getMiniprogram() {
        return miniprogram;
    }
    public void setMiniprogram(Miniprogram miniprogram) {
        this.miniprogram = miniprogram;
    }
    public Map<String, Item> getData() {
        return data;
    }
    public void setData(Map<String, Item> data) {
        this.data = 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 getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
}
zq-erp/src/main/java/com/matrix/system/wechart/templateMsg/MsgDemo.java
New file
@@ -0,0 +1,20 @@
package com.matrix.system.wechart.templateMsg;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.io.IOException;
public class MsgDemo {
    public static void main(String[] args) {
    }
}