首页 > 代码库 > Java微信公众平台开发模式+自定义按钮源码

Java微信公众平台开发模式+自定义按钮源码

首先,想用开放模式需要先成为开发者。成为开发者有两种写法。

  一是:通过jsp页面,用out.print("echostr")//SHA1加密的字符串;

  二是:通过Servlet。doGet返回exhostr,给微信平台。

  这里我只写第二种方式的请求(这里的请求是以get方式请求),代码如下:

  import java.io.IOException;

  import java.io.InputStream;

  import java.io.PrintWriter;

  import java.util.Arrays;

  import javax.servlet.ServletException;

  import javax.servlet.http.HttpServlet;

  import javax.servlet.http.HttpServletRequest;

  import javax.servlet.http.HttpServletResponse;

  import org.jdom.JDOMException;

  /**

  * 微信统一入口

  * @author slz

  * 2013-7-26 上午10:17:08

  */

  public class WeixinServlet extends HttpServlet {

  public WeixinServlet() {

  super();

  }

  public void destroy() {

  super.destroy();

  }

  /**

  * 微信公众平台  成为开发者验证入口

  */

  public void doGet(HttpServletRequest request, HttpServletResponse response)

  throws ServletException, IOException {

  doPost(request, response);

  }

  /**

  * 微信公众平台  所有接口入口

  */

  public void doPost(HttpServletRequest request, HttpServletResponse response)

  throws ServletException, IOException {

  response.setCharacterEncoding("UTF-8");

  request.setCharacterEncoding("UTF-8");

  PrintWriter out = response.getWriter();

  String TOKEN = "sun";//Token

  String signature = request.getParameter("signature");//SHA1加密字符串

  String timestamp = request.getParameter("timestamp");//时间

  String nonce = request.getParameter("nonce");//随机数

  String echoStr = request.getParameter("echostr");//随机字符串

  if(StrUtil.isNotNull(echoStr)){

  String[] a = {TOKEN,timestamp,nonce};

  Arrays.sort(a);//数组排序

  String str = "";

  for(int i=0;i<a.length;i++){

  str += a[i];

  }

  String echo = new SHA1().getDigestOfString(str.getBytes());//SHA1加密

  if(echo.equals(signature)){

  out.print(echoStr);

  }else{

  out.print("123");

  }

  }else{

  try {

  InputStream is = request.getInputStream();

  PushManage push = new PushManage();

  String getXml = push.PushManageXml(is);

  System.out.println("getXml:"+getXml);

  out.print(getXml);

  } catch (JDOMException e) {

  out.print("");

  }

  }

  out.flush();

  out.close();

  }

  }

  当我们完成第一步成为开发者后,我们就可以用微信提供给我们的接口。

  大家应该也看见上面有doPost方法(微信公共平台  所有接口入口)。当我们成为开发者后,微信用户向我们的公众账号发信息都是post请求。

  //这里写法多种,怎么写就要看你这么获取微信请求时发给我们的XML

  InputStream is = request.getInputStream();

  PushManage push = new PushManage();

  String getXml = push.PushManageXml(is);

  此方法代码如下:

  import java.io.IOException;

  import java.io.InputStream;

  import java.text.SimpleDateFormat;

  import java.util.Date;

  import java.util.List;

  import org.jdom.Document;

  import org.jdom.Element;

  import org.jdom.JDOMException;

  import org.jdom.input.SAXBuilder;

  import org.jdom.output.XMLOutputter;

  /**

  * 微信所有接口入口

  * @author slz

  * 2013-7-26 上午11:01:08

  */

  public class PushManage {

  public String PushManageXml(InputStream is) throws JDOMException{

  String returnStr = "";

  String toName = "";

  String FromName = "";

  String type = "";

  String content = "";

  String con = "";

  String event = "";//自定义按钮事件请求

  String eKey = "";//事件请求key值

  try {

  SAXBuilder sax = new SAXBuilder();

  Document doc = sax.build(is);

  //获得文件的根元素

  Element root = doc.getRootElement();

  //获得根元素的第一级子节点

  List list = root.getChildren();

  for(int j=0;j<list.size();j++){

  //获得结点

  Element first = (Element) list.get(j);

  if(first.getName().equals("ToUserName")){

  toName = first.getValue().trim();

  }else if(first.getName().equals("FromUserName")){

  FromName = first.getValue().trim();

  }else if(first.getName().equals("MsgType")){

  type = first.getValue().trim();

  }else if(first.getName().equals("Content")){

  con = first.getValue().trim();

  }else if(first.getName().equals("Event")){

  event = first.getValue().trim();

  }else if(first.getName().equals("EventKey")){

  eKey = first.getValue().trim();

  }

  }

  } catch (IOException e) {

  e.printStackTrace();

  }

  if(type.equals("event")){

  if(event.equals("subscribe")){//此为关注事件

  content = "您好,欢迎关注我!";

  }

  }else if(type.equals("text")){

  if(con.equals("help")||con.equals("帮助")||con.equals("?")||con.equals("?")){

  content = "帮助中心\n\n";

  }else if(con.equals("bind")||con.equals("绑定")){

  content = "点击 <A \‘\‘" target=_blank>绑定账户</A>";

  }else if(con.startsWith("ck")||con.startsWith("CK")){

  String order_id = "0";

  con.toLowerCase();

  if(con.startsWith("ck")){

  order_id = con.substring(con.indexOf("ck")+2);

  }else if(con.startsWith("CK")){

  order_id = con.substring(con.indexOf("CK")+2);

  }

  if(!"".equals(order_id)){

  StringBuffer sb = new StringBuffer();

  sb.append("订单详情...\n");

  content = sb.toString();

  }else{

  content = "未查询到相应信息,请重新输入!";

  }

  }

  }

  //以下为自定义按钮事件

  if(eKey.equals("music")){//音乐请求

  returnStr = getBackXMLTypeMusic(toName, FromName,

  }else if(eKey.equals("img")){//图片请求

  content

  returnStr = getBackXMLTypeImg(toName, FromName, content);

  }else{//无自定义按钮返回上面的文本信息

  returnStr = getBackXMLTypeText(toName, FromName, content);

  }

  return returnStr;

  }

  public String getBackXMLTypeText(String toName, String FromName, String content){

  String returnStr = "";

  SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");

  String times = format.format(new Date());

  Element rootXML = new Element("xml");

  rootXML.addContent(new Element("ToUserName").setText(FromName));

  rootXML.addContent(new Element("FromUserName").setText(toName));

  rootXML.addContent(new Element("CreateTime").setText(times));

  rootXML.addContent(new Element("MsgType").setText("text"));

  rootXML.addContent(new Element("Content").setText(content));

  Document doc = new Document(rootXML);

  XMLOutputter XMLOut = new XMLOutputter();

  returnStr = XMLOut.outputString(doc);

  return returnStr;

  }

  public String getBackXMLTypeImg(String toName, String FromName, String content){

  String returnStr = "";

  SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");

  String times = format.format(new Date());

  Element rootXML = new Element("xml");

  rootXML.addContent(new Element("ToUserName").setText(FromName));

  rootXML.addContent(new Element("FromUserName").setText(toName));

  rootXML.addContent(new Element("CreateTime").setText(times));

  rootXML.addContent(new Element("MsgType").setText("news"));

  rootXML.addContent(new Element("ArticleCount").setText("3"));

  Element fXML = new Element("Articles");

  Element mXML = null;

  String url = "";

  String ss = "";

  for(int i = 1 ;i<=3;i++){

  mXML = new Element("item");

  mXML.addContent(new Element("Title").setText("图片"+i));

  mXML.addContent(new Element("Description").setText("美女"+i));

  mXML.addContent(new Element("PicUrl").setText(ss));

  mXML.addContent(new Element("Url").setText(url));

  fXML.addContent(mXML);

  }

  rootXML.addContent(fXML);

  Document doc = new Document(rootXML);

  XMLOutputter XMLOut = new XMLOutputter();

  returnStr = XMLOut.outputString(doc);

  return returnStr;

  }

  public String getBackXMLTypeMusic(String toName, String FromName, String content){

  String returnStr = "";

  SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");

  String times = format.format(new Date());

  Element rootXML = new Element("xml");

  rootXML.addContent(new Element("ToUserName").setText(FromName));

  rootXML.addContent(new Element("FromUserName").setText(toName));

  rootXML.addContent(new Element("CreateTime").setText(times));

  rootXML.addContent(new Element("MsgType").setText("music"));

  Element mXML = new Element("Music");

  mXML.addContent(new Element("Title").setText("音乐"));

  mXML.addContent(new Element("Description").setText("音乐让人心情舒畅!"));

  mXML.addContent(new Element("MusicUrl").setText(content));

  mXML.addContent(new Element("HQMusicUrl").setText(content));

  rootXML.addContent(mXML);

  Document doc = new Document(rootXML);

  XMLOutputter XMLOut = new XMLOutputter();

  returnStr = XMLOut.outputString(doc);

  return returnStr;

  }

  }

  以上方法用到的jar包:jdom.jar