微信servlet开发
一、web.xml中注册servlet类
<!--微信信息拦截 -->
<servlet>
<servlet-name>wxServlet</servlet-name>
<servlet-class>com.outsource.weixin.client.servlet.WxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>wxServlet</servlet-name>
<url-pattern>/wxServlet.php</url-pattern>
</servlet-mapping>
二、处理业务的servlet类
package com.outsource.weixin.client.servlet;
/**
*微信信息控制层
*/
public class WxServlet extends HttpServlet {
private static final long serialVersionUID = -2776902810130266533L;
private static Logger log = Logger.getLogger(WxServlet.class.getName());
// 自定义 token
private String TOKEN = "weixin_kz";
public static String wsUrl="http://115.29.49.7";
private WxService wxService;
private StatUserService statUserService;
@Override
public void init() throws ServletException {
super.init();
ServletContext servletContext = this.getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
wxService = (WxService)ctx.getBean("wxService");
statUserService = (StatUserService)ctx.getBean("statUserService");
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String xmlReceive = null;
String xmlSend = null;
String msgContent=null;
response.setContentType("application/xml");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String openId=null;
try {
xmlReceive = WxUtil.inputStream2String(request.getInputStream());----获取xml形式的参数
try {
Map map = WxUtil.xml2Map(xmlReceive); ----把xml形式的参数转换成map键值对形式
openId=(String) map.get("FromUserName");
SysUser user=statUserService.getSysUser(openId);
//证明学生没有绑定系统账号
if(user==null || user.getSysUserId().equals("")){
xmlSend=wxService.sendText(map,"欢迎关注论文系统微信公众号,请立即绑定认证学生信息," +
"点击<a href='"+wsUrl+"/vsm/weixin/stuwx/jsp/stuUserBound.jsp?openId="+openId+"'>绑定认证.</a>");
}else{
if(map.get("Content")!=null &&!"".equals(map.get("Content")))
map.put("Content", map.get("Content").toString().trim().toLowerCase());
if (map.get("MsgType").equals("text")) {
msgContent=map.get("Content").toString();
}else if(map.get("MsgType").equals("event")){
//订阅
if(map.get("Event").equals("subscribe")){
xmlSend=wxService.sendText(map,"欢迎 "+user.getRealName()+"同学\n登录论文系统微信公众号");
}
//取消订阅
else if(map.get("Event").equals("unsubscribe")){
statUserService.Unbind(openId);
}
else if(map.get("Event").equals("LOCATION")){
xmlSend=wxService.getBaiduAddress(wxService.getBaiduGeocoding(map),map);
}else if(map.get("Event").equals("SCAN")){
System.out.println(map);
}else if(map.get("Event").equals("CLICK")){
//我的信息
if(map.get("EventKey").equals("me")){
xmlSend=wxService.sendText(map,"【学号】:"+user.getLoginName()+"\n" + "【姓名】:"+user.getRealName());
}//上传周记
else if(map.get("EventKey").equals("sczj")){
IsWeeklyEntity iwe=statUserService.getIsWeekly(openId);
//不能写周记
if(iwe.getStatus()==0){
xmlSend=wxService.sendText(map,"实习未开始不能上传周记!");
}else if(iwe.getStatus()==1){
List<Article> articleList=new ArrayList();
Article a=new Article();
a.setTitle("上传第"+iwe.getWeeklyCount()+"周记");
a.setDescription("请按时上传周记!");
a.setPicUrl(wsUrl+"/vsm/upload/image/zj.jpg");
a.setUrl(wsUrl+"/vsm/weixin/stuwx/jsp/uploadWeekly.jsp?openId="+openId+"&weeklyCount="+iwe.getWeeklyCount());
articleList.add(a);
xmlSend=wxService.sendArticle(map, articleList);
}else if(iwe.getStatus()==2){
xmlSend=wxService.sendText(map,"实习已结束!");
}
}
//周记评分查看
else if(map.get("EventKey").equals("zjpfck")){
List<Article> articleList=new ArrayList();
Article a=new Article();
a.setTitle("周记评分查看");
a.setDescription("周记评分查看");
a.setPicUrl(wsUrl+"/vsm/upload/image/zj.jpg");
a.setUrl(wsUrl+"/vsm/upwk.do?getPageWeeklyNotes&openId="+openId);
articleList.add(a);
xmlSend=wxService.sendArticle(map, articleList);
}//学生评教
else if(map.get("EventKey").equals("xspj")){
XspjEntity xj = statUserService.getGuide(openId);
if(xj.getStatus()==0){
xmlSend=wxService.sendText(map,"未分配指导老师,不能评教!");
}else if(xj.getStatus()==1){
List<Article> articleList=new ArrayList();
Article a=new Article();
a.setTitle("学生评教");
a.setDescription("学生评教");
a.setPicUrl(wsUrl+"/vsm/upload/image/pj.jpg");
a.setUrl(wsUrl+"/vsm/weixin/stuwx/jsp/teachingEvaluation.jsp?openId="+openId+"&guideId="+xj.getGuideId()+"&guideName="+xj.getGuideName()+"&studentId="+xj.getStudentId());
System.out.println(wsUrl+"/vsm/weixin/stuwx/jsp/teachingEvaluation.jsp?openId="+openId+"&guideId="+xj.getGuideId()+"&guideName="+xj.getGuideName()+"&studentId="+xj.getStudentId());
articleList.add(a);
xmlSend=wxService.sendArticle(map, articleList);
}else if(xj.getStatus()==2){
xmlSend=wxService.sendText(map,"你已对指导老师【"+xj.getGuideName()+"】评教过了,评分为"+xj.getScore()+"分");
}
}
//成绩查看
else if(map.get("EventKey").equals("sxcjck")){
UserPracticeEntity upe=statUserService.getUserPractice(openId);
if(upe!=null && !upe.getStudent().equals("")){
xmlSend=wxService.sendText(map,"【指导老师】:"+upe.getTeacherName()+
"\n" +"【周记成绩】:"+upe.getWeeklyScore()+
"\n" +"【实习成绩】:"+upe.getReportScore()+
"\n" +"【总成绩】:"+upe.getTotalGrade()+
"\n" +"【总评价】:"+upe.getGradeLabel());
}else{
xmlSend=wxService.sendText(map,"暂无");
}
}
//答辩安排查看
else if(map.get("EventKey").equals("dbap")){
ReplyPlanEntity replyPlan = statUserService.getReplyPlan(openId);
if(replyPlan!=null && !replyPlan.getGroupName().equals("")){
xmlSend=wxService.sendText(map,"【小组名称】:"+replyPlan.getGroupName()+
"\n" +"【答辩时间】:"+replyPlan.getReplyDateTimeChina()+
"\n" +"【答辩地址】:"+replyPlan.getReplyAddress()+
"\n" +"【组长】:"+replyPlan.getStudentLeaderName()+
"\n" +"【联系电话】:"+replyPlan.getStudentLeaderTel()+
"\n" +"【答辩人数】:"+replyPlan.getReplyCount());
}else{
xmlSend=wxService.sendText(map,"暂无");
}
}//论文成绩查看
else if(map.get("EventKey").equals("lwcjck")){
StatUserEntity statUserEntity = statUserService.getStatUser(openId);
if(statUserEntity!=null && !statUserEntity.getRealName().equals("")){
xmlSend=wxService.sendText(map,"【姓名】:"+statUserEntity.getRealName()+
"\n" +"【课题名称】:"+statUserEntity.getTopicName()+
"\n" +"【论文成绩】:"+statUserEntity.getThesisGrade()+
"\n" +"【答辩成绩】:"+statUserEntity.getReplyGrade()+
"\n" +"【总成绩】:"+statUserEntity.getTotalGrade());
}else{
xmlSend=wxService.sendText(map,"暂无");
}
}//中期检查成绩查看
else if(map.get("EventKey").equals("zqjccjck")){
ThesisCheckEntity thesisCheckEntity = statUserService.getCheck(openId);
if(thesisCheckEntity!=null && !thesisCheckEntity.getStudentName().equals("")){
xmlSend=wxService.sendText(map,"【姓名】:"+thesisCheckEntity.getStudentName()+
"\n" +"【课题名称】:"+thesisCheckEntity.getTopicName()+
"\n" +"【评价成绩】:"+thesisCheckEntity.getTotalEvaluate()+" 分"+
"\n" +"【评价建议】:"+thesisCheckEntity.getSuggest());
}else{
xmlSend=wxService.sendText(map,"暂无");
}
}
else{
xmlSend=wxService.sendText(map,"暂无");
}
}
}else if(map.get("MsgType").equals("location")){
}else if(map.get("MsgType").equals("image")){
// xmlSend=wxService.setMakefriends(map);
}else if(map.get("MsgType").equals("voice")){
}
}
System.out.println(xmlSend);
out.write(xmlSend);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
} finally {
if (out != null) {
out.flush();
out.close();
out = null;
}
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String signature = request.getParameter("signature"); ----微信加密签名
String timestamp = request.getParameter("timestamp"); ----时间戳
String nonce = request.getParameter("nonce"); ----随机数
String echostr = request.getParameter("echostr"); ----随机字符串
// 重写totring方法,得到三个参数的拼接字符串
List<String> list = new ArrayList<String>(3) {
private static final long serialVersionUID = 2621444383666420433L;
public String toString() {
return this.get(0) + this.get(1) + this.get(2);
}
};
list.add(TOKEN);
list.add(timestamp);
list.add(nonce);
Collections.sort(list);// 排序
String tmpStr = WxUtil.Encrypt(list.toString());// SHA-1加密
Writer out = response.getWriter();
if (signature.equals(tmpStr)) {
out.write(echostr);// 请求验证成功,返回随机码
} else {
out.write("");
}
out.flush();
out.close();
out = null;
}
}
相关推荐
微信小程序项目实例源码-本地电商平台小程序微信小程序项目实例源码-本地电商平台小程序微信小程序项目实例源码-本地电商平台小程序微信小程序项目实例源码-本地电商平台小程序微信小程序项目实例源码-本地电商平台...
微信小程序项目实例——家居团购微信小程序项目实例——家居团购微信小程序项目实例——家居团购微信小程序项目实例——家居团购微信小程序项目实例——家居团购微信小程序项目实例——家居团购微信小程序项目实例...
微信小程序项目实例——微信商城小程序源码纯前端项目,适合做毕业设计,下载即用,涉及CSS,HtML5、微信小程序等技术栈。无论学习还是二次开发必备
【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大作业】微信小程序源码 【微信小程序-毕设期末大...
微信小程序项目实例—微信小程序音乐播放器源码(毕业设计).zip 已获导师指导并通过的高分项目,代码完整下载即用,无需修改确保可以运行。也可作为期末大作业和课程设计。 微信小程序项目实例—微信小程序音乐...
微信小程序项目实例——备忘录小程序项目实例源码(95分以上课设大作业项目).zip 高分必过项目,需要小程序项目实战和课程设计大作业的可以放心下载,代码完整下载即用,无需修改确保可以运行。 微信小程序项目...
Python项目-实例-25 微信好友头像墙.zip
微信小程序开发项目实例,源码完整,亲测可正常运行。
微信小程序项目实例—物业交费缴费小程序项目源码+教程.zip 纯手打高分项目,可作为毕业设计、课程设计和期末大作业等等,下载即用确保可以运行。 微信小程序项目实例—物业交费缴费小程序项目源码+教程.zip 纯手...
微信小程序项目实例——五子棋微信小程序源码(95分以上课程设计).zip 高分必过项目,需要小程序项目实战和课程设计的可以放心下载,代码完整下载即用,无需修改确保可以运行。 微信小程序项目实例——五子棋微信...
微信小程序 实例汇总 完整项目源代码
Python项目-实例-31 wxpy获取微信好友头像.zip
微信小程序项目实例—酒店预约微信小程序源码(95分以上课设).zip 高分小程序课程设计,代码完整下载即用,无需修改确保可以运行,也可以作为小程序毕业设计和期末大作业。高分必备项目,小程序实战项目。 微信小...
微信小程序项目实例——今日美食 今日美食是为用户提供各种美食的制作方法,详细介绍了配料和制作流程
微信小程序项目实例-大转盘
实例-25 微信好友头像墙 实例-26 爬取网易云音乐评论 实例-27 生成词云图 实例-28 画楼花树 实例-29 回画 实例-30 wx 表情轰炸 实例-31 wxpy获取微信好友头像 实例-32 wxpy统计好友地理位置 游戏源码-01 pygame_...
微信小程序项目实例——烘焙电商小程序源码纯前端项目,适合做毕业设计,下载即用,涉及CSS,HtML5、微信小程序等技术栈。无论学习还是二次开发必备
python项目实例源码 实例-25 微信好友头像墙 完整源码+学习好素材
微信小程序项目实例——精简小程序商城源码纯前端项目,适合做毕业设计,下载即用,涉及CSS,HtML5、微信小程序等技术栈。无论学习还是二次开发必备