Browse Source

微信公众号图文消息回复

chenyongxing 8 years ago
parent
commit
ce9eae31c0

+ 22 - 22
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/wx/MWxGraphicMessage.java

@ -8,18 +8,18 @@ import java.util.Date;
public class MWxGraphicMessage {
    private Long id;
    private String code;//业务code
    private String wechatCode;//关联的微信code 关联表 Wx_Wechat
    private String name;//名称
    private String value;//图文消息url值
    private String keyword;//关键字
    private String title;//标题
    private String description;//描述
    private String url;//图文消息url值
    private String picUrl;//图片地址
    private String createUser;//创建人
    private String createUserName;//创建人名
    private Date createTime;//创建时间
    private String updateUser;//修改人
    private String updateUserName;//修改人名称
    private Date updateTime;//修改时间
    private String remark;
    private Integer status; //状态 -1 已删除 0可用
    private String remark;//备注
    private Integer status;  //状态 -1删除 0 冻结 1可用
    public Long getId() {
        return id;
@ -37,36 +37,36 @@ public class MWxGraphicMessage {
        this.code = code;
    }
    public String getWechatCode() {
        return wechatCode;
    public String getTitle() {
        return title;
    }
    public void setWechatCode(String wechatCode) {
        this.wechatCode = wechatCode;
    public void setTitle(String title) {
        this.title = title;
    }
    public String getName() {
        return name;
    public String getDescription() {
        return description;
    }
    public void setName(String name) {
        this.name = name;
    public void setDescription(String description) {
        this.description = description;
    }
    public String getValue() {
        return value;
    public String getUrl() {
        return url;
    }
    public void setValue(String value) {
        this.value = value;
    public void setUrl(String url) {
        this.url = url;
    }
    public String getKeyword() {
        return keyword;
    public String getPicUrl() {
        return picUrl;
    }
    public void setKeyword(String keyword) {
        this.keyword = keyword;
    public void setPicUrl(String picUrl) {
        this.picUrl = picUrl;
    }
    public String getCreateUser() {

+ 109 - 0
svr/svr-base/src/main/java/com/yihu/jw/util/MessageUtil.java

@ -0,0 +1,109 @@
package com.yihu.jw.util;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by Administrator on 2017/5/23 0023.
 */
public class MessageUtil {
    /**
     * 返回消息类型:文本
     */
    public static final String RESP_MESSAGE_TYPE_TEXT = "text";
    /**
     * 返回消息类型:音乐
     */
    public static final String RESP_MESSAGE_TYPE_MUSIC = "music";
    /**
     * 返回消息类型:图文
     */
    public static final String RESP_MESSAGE_TYPE_NEWS = "news";
    /**
     * 请求消息类型:文本
     */
    public static final String REQ_MESSAGE_TYPE_TEXT = "text";
    /**
     * 请求消息类型:图片
     */
    public static final String REQ_MESSAGE_TYPE_IMAGE = "image";
    /**
     * 请求消息类型:链接
     */
    public static final String REQ_MESSAGE_TYPE_LINK = "link";
    /**
     * 请求消息类型:地理位置
     */
    public static final String REQ_MESSAGE_TYPE_LOCATION = "location";
    /**
     * 请求消息类型:音频
     */
    public static final String REQ_MESSAGE_TYPE_VOICE = "voice";
    /**
     * 请求消息类型:推送
     */
    public static final String REQ_MESSAGE_TYPE_EVENT = "event";
    /**
     * 事件类型:subscribe(订阅)
     */
    public static final String EVENT_TYPE_SUBSCRIBE = "subscribe";
    /**
     * 事件类型:unsubscribe(取消订阅)
     */
    public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe";
    /**
     * 事件类型:CLICK(自定义菜单点击事件)
     */
    public static final String EVENT_TYPE_CLICK = "CLICK";
    /**
     * 解析微信发来的请求(XML)
     *
     * @param request
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
        // 将解析结果存储在HashMap中
        Map<String, String> map = new HashMap<String, String>();
        // 从request中取得输入流
        InputStream inputStream = request.getInputStream();
        // 读取输入流
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputStream);
        // 得到xml根元素
        Element root = document.getRootElement();
        // 得到根元素的所有子节点
        List<Element> elementList = root.elements();
        // 遍历所有子节点
        for (Element e : elementList)
            map.put(e.getName(), e.getText());
        // 释放资源
        inputStream.close();
        inputStream = null;
        return map;
    }
}

+ 9 - 1
svr/svr-base/src/main/java/com/yihu/jw/wx/controller/WxGraphicMessageController.java

@ -20,7 +20,7 @@ import java.util.ArrayList;
import java.util.List;
/**
 * Created by chenweida on 2017/5/11.
 *   2017/5/11.
 */
@RestController
@RequestMapping(WxContants.WxGraphicMessage.api_common)
@ -126,4 +126,12 @@ public class WxGraphicMessageController extends EnvelopRestController {
        return Envelop.getSuccessList(WxContants.WxGraphicMessage.message_success_find_functions,mWxGraphicMessages);
    }
    @GetMapping(value = "sendGraphicMessages")
    @ApiOperation(value = "发送图文消息")
    public Envelop sendGraphicMessages(
            @ApiParam(name = "codes", value = "根据code发送微信图文消息,多个code用,分割")
            @RequestParam(value = "codes", required = true) String codes,HttpServletRequest request) throws Exception {
        wxGraphicMessageService.sendGraphicMessages(codes,request);
        return null;
    }
}

+ 34 - 33
svr/svr-base/src/main/java/com/yihu/jw/wx/model/WxGraphicMessage.java

@ -13,25 +13,29 @@ import java.util.Date;
public class WxGraphicMessage extends IdEntity implements java.io.Serializable {
    private String code;//业务code
    private String wechatCode;//关联的微信code 关联表 Wx_Wechat
    private String name;//名称
    private String value;//图文消息url值
    private String keyword;//关键字
    private String title;//标题
    private String description;//描述
    private String url;//图文消息url值
    private String picUrl;//图片地址
    private String createUser;//创建人
    private String createUserName;//创建人名
    private Date createTime;//创建时间
    private String updateUser;//修改人
    private String updateUserName;//修改人名称
    private Date updateTime;//修改时间
    private String remark;
    private String remark;//备注
    private Integer status;  //状态 -1删除 0 冻结 1可用
    public WxGraphicMessage(String code, String wechatCode, String name, String value, String keyword, String createUser, String createUserName, Date createTime, String updateUser, String updateUserName, Date updateTime, String remark, Integer status) {
    public WxGraphicMessage() {
    }
    public WxGraphicMessage(String code, String title, String description, String url, String picUrl, String createUser, String createUserName, Date createTime, String updateUser, String updateUserName, Date updateTime, String remark, Integer status) {
        this.code = code;
        this.wechatCode = wechatCode;
        this.name = name;
        this.value = value;
        this.keyword = keyword;
        this.title = title;
        this.description = description;
        this.url = url;
        this.picUrl = picUrl;
        this.createUser = createUser;
        this.createUserName = createUserName;
        this.createTime = createTime;
@ -42,9 +46,6 @@ public class WxGraphicMessage extends IdEntity implements java.io.Serializable {
        this.status = status;
    }
    public WxGraphicMessage() {
    }
    @Column(name = "code", length = 64)
    public String getCode() {
        return code;
@ -54,40 +55,40 @@ public class WxGraphicMessage extends IdEntity implements java.io.Serializable {
        this.code = code;
    }
    @Column(name = "wechat_code", length = 200)
    public String getWechatCode() {
        return wechatCode;
    @Column(name = "title", length = 200)
    public String getTitle() {
        return title;
    }
    public void setWechatCode(String wechatCode) {
        this.wechatCode = wechatCode;
    public void setTitle(String title) {
        this.title = title;
    }
    @Column(name = "name", length = 200)
    public String getName() {
        return name;
    @Column(name = "description", length = 2000)
    public String getDescription() {
        return description;
    }
    public void setName(String name) {
        this.name = name;
    public void setDescription(String description) {
        this.description = description;
    }
    @Column(name = "value", length = 2000)
    public String getValue() {
        return value;
    @Column(name = "url", length = 2000)
    public String getUrl() {
        return url;
    }
    public void setValue(String value) {
        this.value = value;
    public void setUrl(String url) {
        this.url = url;
    }
    @Column(name = "keyword", length = 100)
    public String getKeyword() {
        return keyword;
    @Column(name = "pic_url", length = 2000)
    public String getPicUrl() {
        return picUrl;
    }
    public void setKeyword(String keyword) {
        this.keyword = keyword;
    public void setPicUrl(String picUrl) {
        this.picUrl = picUrl;
    }
    @Column(name = "create_user", length = 200)

+ 75 - 6
svr/svr-base/src/main/java/com/yihu/jw/wx/service/WxGraphicMessageService.java

@ -4,6 +4,7 @@ import com.yihu.jw.mysql.query.BaseJpaService;
import com.yihu.jw.restmodel.common.CommonContants;
import com.yihu.jw.restmodel.exception.ApiException;
import com.yihu.jw.restmodel.wx.WxContants;
import com.yihu.jw.util.MessageUtil;
import com.yihu.jw.wx.dao.WxGraphicMessageDao;
import com.yihu.jw.wx.model.WxGraphicMessage;
import org.springframework.beans.factory.annotation.Autowired;
@ -11,6 +12,8 @@ import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.persistence.Transient;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
/**
 * Created by Administrator on 2017/5/20 0020.
@ -26,9 +29,6 @@ public class WxGraphicMessageService extends BaseJpaService<WxGraphicMessage, Wx
        if (StringUtils.isEmpty(wxGraphicMessage.getCode())) {
            throw new ApiException(WxContants.WxGraphicMessage.message_fail_code_is_null, CommonContants.common_error_params_code);
        }
        if (StringUtils.isEmpty(wxGraphicMessage.getValue())) {
            throw new ApiException(WxContants.WxGraphicMessage.message_fail_value_is_null, CommonContants.common_error_params_code);
        }
        WxGraphicMessage wxGraphicMessageTem = wxGraphicMessageDao.findByCode(wxGraphicMessage.getCode());
        if (wxGraphicMessageTem != null) {
            throw new ApiException(WxContants.WxGraphicMessage.message_fail_code_exist, CommonContants.common_error_params_code);
@ -41,9 +41,6 @@ public class WxGraphicMessageService extends BaseJpaService<WxGraphicMessage, Wx
        if (StringUtils.isEmpty(wxGraphicMessage.getCode())) {
            throw new ApiException(WxContants.WxGraphicMessage.message_fail_code_is_null, CommonContants.common_error_params_code);
        }
        if (StringUtils.isEmpty(wxGraphicMessage.getValue())) {
            throw new ApiException(WxContants.WxGraphicMessage.message_fail_value_is_null, CommonContants.common_error_params_code);
        }
        return wxGraphicMessageDao.save(wxGraphicMessage);
    }
@ -61,4 +58,76 @@ public class WxGraphicMessageService extends BaseJpaService<WxGraphicMessage, Wx
        WxGraphicMessage.setStatus(-1);
        wxGraphicMessageDao.save(WxGraphicMessage);
    }
    /**
     * 回复图文消息
     *
     * @param toUser
     * @param fromUser
     * @param articles
     * @return
     * @throws Exception
     */
    public String replyNewsMessage(String toUser,String fromUser,List<Map<String,String>> articles) throws Exception {
        if(articles == null || articles.size() < 1){
            throw new Exception("图文信息不能为空!");
        }
        StringBuilder result = new StringBuilder();
        result.append("<xml>");
        result.append("<ToUserName>" + toUser + "</ToUserName>");
        result.append("<FromUserName>" + fromUser + "</FromUserName>");
        result.append("<CreateTime>" + new Date().getTime() + "</CreateTime>");
        result.append("<MsgType>news</MsgType>");
        result.append("<ArticleCount>" + articles.size() + "</ArticleCount>");
        result.append("<Articles>");
        for(Map<String,String>  article : articles){
            result.append("<item>");
            result.append("<Title>" +  article.get("Title") +"</Title>");
            result.append("<Description>" + article.get("Description") + "</Description>");
            result.append("<PicUrl>" + article.get("PicUrl") + "</PicUrl>");
            result.append("<Url>" + article.get("Url") + "</Url>");
            result.append("</item>");
        }
        result.append("</Articles>");
        result.append("</xml>");
        return result.toString();
    }
    public String sendGraphicMessages(String codes, HttpServletRequest request) {
        try {
            // xml请求解析
            Map<String, String> requestMap = MessageUtil.parseXml(request);
            // 发送方帐号(open_id)
            String fromUserName = requestMap.get("FromUserName");
            // 公众帐号
            String toUserName = requestMap.get("ToUserName");
            // 图文信息
            List<Map<String,String>> articles =  new ArrayList<>();
            if(codes!=null){
                String[] codeArray = codes.split(",");
                for(String code: codeArray){
                    WxGraphicMessage graphicMessage = findByCode(code);
                    Map<String,String> article = new HashMap<>();
                    article.put("Url",graphicMessage.getUrl());
                    article.put("Title", graphicMessage.getTitle());
                    article.put("Description",graphicMessage.getDescription());
                    article.put("PicUrl",graphicMessage.getPicUrl());
                    articles.add(article);
                }
            }
            // 构建回复消息XML
            String result = replyNewsMessage(fromUserName, toUserName, articles);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
}