这篇文章将为大家详细讲解有关如何实现微信小程序签到考勤功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
鉴于很多小伙伴给我私信,询问关于后端代码的事。很开心很帮助到这么多人。但之前由于某种原因没能将其与客户端代码一并发布,这里将代码发布到GitHub上,让大家方便下载学习。这里用的是Java Servlet ,运行在 Web 服务器或应用服务器上的程序,作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。数据库使用的是MySQL,持久层使用了JDBC,Java的原生API。没有使用框架,方便新手学习,也更能理解web的运行机制和原理。
GitHub地址:传送门
这里要说明一下关键的代码:
/** * Servlet implementation class Login */@WebServlet("/Login")public class Login extends HttpServlet { private static final long serialVersionUID = 1L; private static final String APPID="xxxxxxxxxx"; private static final String SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxx"; /** * Default constructor. */ public Login() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //ÉèÖÃÇëÇó±àÂë request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); /* ÉèÖÃÏìӦͷÔÊÐíajax¿çÓò·ÃÎÊ */ response.setHeader("Access-Control-Allow-Origin", "*"); /* ÐǺűíʾËùÓеÄÒìÓòÇëÇ󶼿ÉÒÔ½ÓÊÜ£¬ */ response.setHeader("Access-Control-Allow-Methods", "GET,POST"); String flag=request.getParameter("flag"); // System.out.println(flag); if("login".equals(flag)) { String code=request.getParameter("js_code"); String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+APPID+ "&secret="+SECRET+"&js_code="+ code +"&grant_type=authorization_code"; JSONObject sjson=CommonUtil.httpsRequest(url, "GET", null); /*String openid=""; String session_key=""; if (sjson != null) { try { openid = sjson.getString("openid"); session_key=sjson.getString("session_key"); } catch (Exception e) { System.out.println("ÒµÎñ²Ù×÷ʧ°Ü"); e.printStackTrace(); } } else { System.out.println("codeÎÞЧ"); } System.out.println(session_key+" "+openid);*/ /*Map<String, Object> result = new HashMap<String, Object>(); result.put("res", "test"); result.put("msg", "ºǫ́ÒÑÊÕµ½");*/ // String json = new Gson().toJson(sjson); // System.out.println(json); Writer out=response.getWriter(); out.write(sjson.toString()); out.flush(); } if("init".equals(flag)) { StudentDAO studentDAO=new StudentDAO(); String userid=request.getParameter("userid"); boolean res=true; try { res=studentDAO.findCheck(userid); } catch (Exception e) { e.printStackTrace(); } Map<String, Object> result = new HashMap<String, Object>(); result.put("res", res); result.put("msg", "ºǫ́ÒÑÊÕµ½"); String json = new Gson().toJson(result); //·µ»ØÖµ¸ø΢ÐÅС³ÌÐò Writer out = response.getWriter(); out.write(json); out.flush(); } if("student".equals(flag)) { StudentDAO studentDAO=new StudentDAO(); String userid=request.getParameter("userid"); String studentName=request.getParameter("sname"); String studentNum=request.getParameter("snum"); Student student=new Student(userid, studentName, studentNum,new Date()); try { int a=studentDAO.create(student); if(a!=0) { System.out.println("²åÈë³É¹¦"); } } catch (Exception e) { e.printStackTrace(); } } if("teacher".equals(flag)) { TeacherDAO teacherDAO=new TeacherDAO(); String userid=request.getParameter("userid"); String teacherName=request.getParameter("tname"); String teacherID=request.getParameter("tnum"); Teacher tea=new Teacher(userid, teacherID, teacherName,new Date()); try { int a=teacherDAO.create(tea); if(a!=0) { System.out.println("²åÈë³É¹¦"); } } catch (Exception e) { e.printStackTrace(); } } if("guide".equals(flag)) { StudentDAO studentDAO=new StudentDAO(); String userid=request.getParameter("userid"); System.out.println(userid); boolean res=true; String state=""; try { res=studentDAO.findCheck(userid); } catch (Exception e) { e.printStackTrace(); } if(res) { state="student"; } else{ TeacherDAO teacherDAO=new TeacherDAO(); try { res=teacherDAO.findCheck(userid); } catch (Exception e) { e.printStackTrace(); } if(res) { state="teacher"; } else { state="none"; } } String json = new Gson().toJson(state); //·µ»ØÖµ¸ø΢ÐÅС³ÌÐò Writer out = response.getWriter(); out.write(json); out.flush(); } if("myInfo".equals(flag)) { String userid=request.getParameter("userid"); StudentDAO studentDAO=new StudentDAO(); try { List<String> list=studentDAO.myInfo(userid); Map<String, String> result = new HashMap<String, String>(); result.put("backName",list.get(0)); result.put("backNum", list.get(1)); String json = new Gson().toJson(result); //·µ»ØÖµ¸ø΢ÐÅС³ÌÐò Writer out = response.getWriter(); out.write(json); out.flush(); } catch (Exception e) { e.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
这里的APPID和SECRET要使用你自己的。
关于“如何实现微信小程序签到考勤功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。