您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何實現微信小程序簽到考勤功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
鑒于很多小伙伴給我私信,詢問關于后端代碼的事。很開心很幫助到這么多人。但之前由于某種原因沒能將其與客戶端代碼一并發布,這里將代碼發布到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"); /* éè???ìó|í·?êDíajax??óò·??ê */ response.setHeader("Access-Control-Allow-Origin", "*"); /* D?o?±íê??ùóDμ?òìóò???ó???éò??óêü£? */ 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("òμ??2ù×÷꧰ü"); e.printStackTrace(); } } else { System.out.println("code?TD§"); } System.out.println(session_key+" "+openid);*/ /*Map<String, Object> result = new HashMap<String, Object>(); result.put("res", "test"); result.put("msg", "oóì¨ò?ê?μ?");*/ // 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", "oóì¨ò?ê?μ?"); String json = new Gson().toJson(result); //·μ???μ???¢D?D?3ìDò 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("2?è?3é1|"); } } 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("2?è?3é1|"); } } 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); //·μ???μ???¢D?D?3ìDò 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); //·μ???μ???¢D?D?3ìDò 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。