Rumah >applet WeChat >Pembangunan program mini >分享微信小程序签到考勤后端代码
相关推荐:《小程序开发教程》
鉴于很多小伙伴给我私信,询问关于后端代码的事。很开心很帮助到这么多人。但之前由于某种原因没能将其与客户端代码一并发布,这里将代码发布到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要使用你自己的。由于最近很少关注博客,咨询的人也很多,很多私信来不及回复,具体其它的就由大家自己探索喽。本程序纯属个人兴趣,切勿用于商业用途。
Atas ialah kandungan terperinci 分享微信小程序签到考勤后端代码. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!