文档章节

微信公众平台深度开发JAVA版第一季 19.微信猜数字活动1

blueheart
 blueheart
发布于 2017/05/15 10:46
字数 1301
阅读 5
收藏 0

数据库选用Mysql.不能简单地把Mysql定义为小型或者是中型数据库.

建一个活动表存储每个粉丝提交的数据.

取消关注.

取消关注之后打印出的信息是取消关注事件

​
<xml><ToUserName><![CDATA[gh_44e4d096da50]]></ToUserName><FromUserName><![CDATA[o2d2nw7IVnGMvYohsBg5QVt3iVQI]]></FromUserName><CreateTime>1495336790</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[unsubscribe]]></Event><EventKey><![CDATA[]]></EventKey></xml>
北风网
<ToUserName><![CDATA[
]]></ToUserName>
<FromUserName><![CDATA[
]]></FromUserName>
<CreateTime>
</CreateTime>
<MsgType><![CDATA[
]]></MsgType>
<Event><![CDATA[
]]></Event>



​
package net.wxinterface;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.DocumentException;

import wx.event.Subscribe_Unsubscribe;
import wx.in.In;
import wx.in.In_Image;
import wx.in.In_Text;
import wx.in.In_Util;
import wx.in.In_Video;
import wx.in.In_Voice;
import wx.out.Out_Image;
import wx.out.Out_News;
import wx.out.Out_Text;
import wx.out.Out_Video;
import wx.out.Out_Voice;
import wx.util.Util;

import com.imooc.po.TextMessage;
import com.imooc.util.MessageUtil;
public class WX_Interface extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public WX_Interface() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//΢�ż���ǩ��,signature����˿�������д��tocken����������е�timestamp����nonce����
		String signature = request.getParameter("signature");
		//ʱ���
		String timestamp = request.getParameter("timestamp");
		//����� 
		String nonce = request.getParameter("nonce");
		
		String echostr = request.getParameter("echostr");
		
		String tocken = "test";
		try{
			if(null != signature){
			String[] ArrTmp = {tocken,timestamp,nonce};	
			Arrays.sort(ArrTmp);
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<ArrTmp.length;i++){
				sb.append(ArrTmp[i]);
			}
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			byte[] bytes = md.digest(new String(sb).getBytes());
			StringBuffer buf = new StringBuffer();
			for(int i=0;i<bytes.length;i++){
				if(((int)bytes[i] & 0xff)<0x10){
					buf.append("0");
				}
				buf.append(Long.toString((int) bytes[i] & 0xff,16));
				
			}
			if(signature.equals(buf.toString())){
				response.getOutputStream().println(echostr);
			}
		}
	}catch(Exception e){
			e.printStackTrace();
		}
		
		System.out.println("test0");

		
		System.out.println("doGet");
		System.out.println("signature     "+signature);
		System.out.println("timstamp      "+timestamp);
		System.out.println("nonce         "+nonce);
		System.out.println("echostr         "+echostr);
		
		
		System.out.println("doGet");
	
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
try {
	Map<String,String> map = MessageUtil.xmlToMap(request);
	String fromUserName = map.get("FromUserName");
			String toUserName = map.get("ToUserName");
					String msgType = map.get("MsgType");
					String content = map.get("Content");//
					String message = null;
					
					//if("text".equals(msgType)){
					if(MessageUtil.MESSAGE_TEXT.equals(msgType)){
					if("1".equals(content)){
						message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.firstMenu());				
					}else if("3".equals(content)){
						//message = MessageUtil.initText(toUserName,fromUserName,content);				
					//�Ϳ��������ﰴ3����ͼƬ��Ϣ,���ͼƬ��Ϣ�Ĵ����Լ��ظ��Ѿ�д����
						message = MessageUtil.initImageMessage(toUserName,fromUserName);
					}else if("4".equals(content)){//��servlet����һ���ж�,������4��ʱ��ظ�һ�����ָ���  
						message = MessageUtil.initMusicMessage(toUserName,fromUserName);
					}else if("2".equals(content)){
						//message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.secondMenu());			
                        //�����ͼ����Ϣ�Ĵ����Լ��ظ��Ѿ�д���� 
						message= MessageUtil.initNewsMessage(toUserName,fromUserName);
					}else if("?".equals(content)||" ? ".equals(content)){
						message = MessageUtil.initText(toUserName,fromUserName,MessageUtil.menuText());
					}
						TextMessage text = new TextMessage();
					text.setFromUserName(toUserName);//˭������,��ͷ���˭
					text.setToUserName(fromUserName);
					text.setMsgType("text");
					text.setCreateTime(new Date().getTime());
					text.setContent("���͵���Ϣ��:"+content);
					 message = MessageUtil.textMessageToXml(text);
					
					System.out.println(message);
					}else if(MessageUtil.MESSAGE_EVENT.equals(msgType)){//�����Ϣ���͵��߼�
						String eventType = map.get("Event");
						if(MessageUtil.MESSAGE_SUBSCRIBE.equals(eventType)){//��Ϣ�����¼�������
							message = MessageUtil.initText(toUserName, fromUserName, content);//��ע΢�Ź��ں�֮��΢�ź�̨����
						}//�������Servlet�������һ���߼����ж�
						else if(MessageUtil.MESSAGE_CLICK.equals(eventType)){//�����������Click�˵���һ���¼�����
							//Click���͵���Ϣ����,���Event��Click���͵Ļ�,��ô����ظ�һ�����˵�
							message = MessageUtil.initText(toUserName, fromUserName, MessageUtil.menuText());
						}else if(MessageUtil.MESSAGE_VIEW.equals(eventType)){//�����View���Ͳ˵�,�ͻظ�һ�������õ�Url
							String url  = map.get("EventKey");//�����View���Ͳ˵��Ļ�,���EventKey�������Url��ֵ
							if("11".equals(url)){//���Keyֵ��11
							message = MessageUtil.initText(toUserName, fromUserName, url);
							}
						}else if(MessageUtil.MESSAGE_SCANCODE.equals(eventType)){//�ظ�һ�����ɨ���¼�
							String key  = map.get("EventKey");//�����Click���Ͳ˵��Ļ�,EventKey���������õ�Keyֵ
							message = MessageUtil.initText(toUserName, fromUserName, key);
						}
						
					}
					out .print(message);
} catch (DocumentException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}finally{
	out.close();
}

System.out.println("doPost");*/
		//response.setContentType("text/html");
		//PrintWriter out = response.getWriter();
		
/*		StringBuffer strb = new StringBuffer();
		ServletInputStream in = request.getInputStream();
          BufferedReader     breader=       new BufferedReader( new InputStreamReader(in,"UTF-8"));
		
          String str = null;
          while(null!=(str=breader.readLine())){
        	  strb.append(str);
          }
          //out.println(str);
          System.out.println(strb);*/
        //out.flush();
		//out.close();
/*response.setContentType("text/html");
PrintWriter out = response.getWriter();

StringBuffer strb = new StringBuffer();
//strb要存储一下request获取过来的东西
ServletInputStream in =  request.getInputStream();

BufferedReader breader =  new BufferedReader( new InputStreamReader(in,"UTF-8"));//接收是对的,已经进行转码了.
String str = null;
//+-*\/\是运算符,赋值运算符=也是运算符
	/*
while(null!=(str = breader.readLine())){
	 strb.append(str);
}
out.println(strb);
System.out.println(strb);
out.flush();
out.close();*/
		//response.setContentType("text/html;charset=utf-8");
		//response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		//String requestStr = In_Util.getStringFromRequest(request);
		String requestStr = Util.getStringFromRequest(request);
		In in = new In(requestStr);
		System.out.println(requestStr);
		System.out.println("北风网");
/*		In_Text in_Text = new In_Text(requestStr);
		System.out.println(in_Text);
		in_Text.setContent(in_Text.getContent()+ "  吃屎啦");*/
	 /*    In_Image in_Image = new In_Image(requestStr);
         System.out.println(in_Image);*/
		//Out_Text out_Text = new Out_Text(in_Text);
		//Out_Text out_Text = new Out_Text(in_Text,in_Text.getContent()+ "  吃屎啦");
/*		Out_Text out_Text = new Out_Text(in_Text);
		out_Text.setContent(in_Text.getContent());*/
		//System.out.println(out_Text.getStr("这是北风网。。。。。。。"));
		//response.getOutputStream().print(out_Text.getStr(in_Text.getContent()+ "  吃屎啦"));
		//response.getWriter().print(out_Text.getStr(in_Text.getContent()+ "  吃屎啦"));
		//response.getWriter().print(out_Text.getStr());
		//new BufferedWriter(new OutputStreamWriter(response.getOutputStream(),"UTF-8")).write(out_Text.getStr(in_Text.getContent()+ "  chishila"));
		//response.getOutputStream().print(out_Text.getStr("这是北风网。。。。。。。"));
		//System.out.println(out_Text.getStr(in_Text.getContent()+ "  吃屎啦"));
		//System.out.println(out_Text.getStr());
		
		/*In_Image in_Image = new In_Image(requestStr);
		System.out.println(in_Image.getMediaId());
		
		Out_Image out_Image = new Out_Image(in_Image);
		out_Image.setMediaId(in_Image.getMediaId());
	    response.getWriter().print(out_Image.getStr());*/
/*		String shanghai = "szNaZ9h5SbG4kMDlCexVMvNLuezqTB20QE9XOgpbK5plhmCOnaO6JG6-ISXsIdvn";
		String xxx= "T9Bi3d-qxYbZnE6f1CsYJ2EfNrWDdr9x3Kpas_WUo04tc9b4zwTq9KZs1vQ6taQB";
		In_Text in_Text = new In_Text(requestStr);
		Out_Image out_Image = new Out_Image(in_Text);
		if("1".equals(in_Text.getContent())){
			out_Image.setMediaId(shanghai);
			
		}else{
			out_Image.setMediaId(xxx);
		}
		response.getWriter().print(out_Image.getStr());*/
/*		In_Voice in_Voice = new In_Voice(requestStr);
		Out_Voice out_Voice = new Out_Voice(in_Voice);
		out_Voice.setMediaId(in_Voice.getMediaId());
		response.getWriter().print(out_Voice.getStr());*/
/*		In_Video in_Video = new In_Video(requestStr);

		Out_Video out_Video = new Out_Video(in_Video);
		out_Video.setMediaId(in_Video.getMediaId());
		out_Video.setTitle("title");
		out_Video.setDescription("description");
		System.out.println(out_Video.getStr());
		//response.getWriter().print(out_Video.getStr());
	    response.getOutputStream().print(out_Video.getStr());*/
		Subscribe_Unsubscribe subscribe_Unsubscribe = new Subscribe_Unsubscribe(requestStr);
		/*System.out.println(subscribe_Unsubscribe);
		Out_News out_News = new Out_News(subscribe_Unsubscribe);
		List<Out_News> out_NewsList = new ArrayList<Out_News>();
		Out_News out_News_0 = new Out_News();
		out_News_0.setTitle("北风网微信系统");
		out_News_0.setDescription("北风网");
		out_News_0.setPicUrl("http://www.ibeifeng.com/themes/default/images/logo.png");
		out_News_0.setUrl("http://www.ibeifeng.com/");
		out_NewsList.add(out_News_0);
		Out_News out_News_1 = new Out_News();
		out_News_1.setTitle("java北风网微信系统");
		out_News_1.setDescription("北风网xxxxxxxxxxxxx");
		out_News_1.setPicUrl("http://www.ibeifeng.com/data/indeximg/a2.jpg");
		out_News_1.setUrl("http://www.ibeifeng.com/tc_java2.html");
		out_NewsList.add(out_News_1);
		Out_News out_News_2 = new Out_News();
		out_News_2.setTitle(".net北风网微信系统");
		out_News_2.setDescription("北风网AAAAAAAAAAAA");
		out_News_2.setPicUrl("http://www.ibeifeng.com/data/indeximg/a3.jpg");
		out_News_2.setUrl("http://www.ibeifeng.com/netcourse.html");
		out_NewsList.add(out_News_2);
		System.out.println(out_News.getStr(out_NewsList));
		response.getWriter().print(out_News.getStr(out_NewsList));*/
		/**
		 * 关注微信号时的欢迎信息
		 * 欢迎你关注本微信号
		 */
		if("event".equals(in.getMsgType())){//如果接收的消息是事件类型的话
		if("subscribe".equals(subscribe_Unsubscribe.getEvent())){
			//Out_Text out_Text = new Out_Text(in);
			Out_Text out_Text = new Out_Text(subscribe_Unsubscribe);
			out_Text.setContent("欢迎你关注本微信号");
			out.print(out_Text.getStr());
			
		}
		//判断是text类型
		}else if("text".equals(in.getMsgType())){
			In_Text in_Text = new In_Text(requestStr);
			Out_Text out_Text = new Out_Text(in);
			out_Text.setContent("这是您选择的数字"+in_Text.getContent());
			out.print(out_Text.getStr());
		}else{
			Out_Text out_Text = new Out_Text(in);
			out_Text.setContent("请选择一个数字");
			out.print(out_Text.getStr());
			
		}
		
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

 

© 著作权归作者所有

blueheart
粉丝 0
博文 35
码字总数 29716
作品 0
佛山
程序员
私信 提问
分享java微信开发视频

课程讲师:翟东平 课程分类:Java适合人群:高级课时数量:24(41节)课时更新程度:完毕服务类型:C类(普通服务类课程)用到技术:微信公众平台,微信,java涉及项目:超市促销活动,微信限时...

双层牛堡
2015/04/07
0
0
Java微信开发_00_资源汇总贴

1.微信公众平台技术文档(https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432) 2.微信企业号开发接口文档(http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E......

rayner
2017/08/07
0
0
JeeWx 捷微 2.3 发布,JAVA微信管家平台

JeeWx,敏捷微信开发,简称“捷微”,捷微是一款免费开源的JAVA微信管家平台 一、简介 Jeewx是一个开源、高效、敏捷的微信开发平台,采用JAVA语言基于Jeecg快速开发框架实现,Jeewx开源版实现...

Jeecg
2016/03/24
10.6K
0
微信公众平台开发教程新手解惑40则

【编者按】由CSDN和《程序员》杂志联合主办的 2014年微信开发者大会 将于8月23日在北京举行,邀请了来自于一线的微信开发商技术负责人或资深工程师从企业应用开发高级篇、智能客服与LBS、微信...

DavidBao
2015/04/29
0
0
为什么没有java版的微信开发平台

现在多数web环境上的很多开源框架都是PHP语言的,比如原来的cms系统,网站模板,网上商城,还有目前的微信公众平台相关的很多开发平台。为什么对应很少会出现Java版本的这些系统呢?这让我很...

刘文杰_
2017/08/18
907
1

没有更多内容

加载失败,请刷新页面

加载更多

Spark内置图像数据源初探

概述 在Apache Spark 2.4中引入了一个新的内置数据源, 图像数据源.用户可以通过DataFrame API加载指定目录的中图像文件,生成一个DataFrame对象.通过该DataFrame对象,用户可以对图像数据进行简...

阿里云官方博客
22分钟前
4
0
掌握Composer

这一次,真正掌握composer composer是现代PHP的基石 现代高级编程语言,依赖管理工具是必不可少的。Java有Maven,Python有pip,Nodejs有npm, 而在composer出现之前,PHP只有被广为诟病的Pea...

城市之雾
28分钟前
3
0
Shell中的函数、数组、告警系统

20.16/20.17 shell中的函数 20.18 shell中的数组 20.19 告警系统需求分析 20.20 告警系统主脚本 20.21 告警系统配置文件 20.22 告警系统监控项目 20.23/20.24/20.25 告警系统邮件引擎 20.26 ...

tobej
29分钟前
1
0
Win7系统安装hadoop

环境准备 安装JDK1.8,配置JAVA_HOME 下载hadoop_3.1.2,配置HADOOP_HOME 配置HDFS 修改hadoop-env.cmd 增加 set HADOOP_PREFIX=%HADOOP_HOME%set HADOOP_CONF_DIR=%HADOOP_PREFIX%\etc\ha......

铲平王
32分钟前
2
0
IT兄弟连 Java语法教程 Java语言的其他特性

Java语言中除了非常重要的跨平台特性外,还有如下几个关键特性: ● 语法简单易学 Java语言的语法简单明了,容易掌握,而且是纯面向对象(OOP)的语言,Java语言的简单性主要体现在以下几个方...

老码农的一亩三分地
46分钟前
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部