這篇文章將為大家詳細(xì)講解有關(guān)如何實(shí)現(xiàn)微信小程序簽到考勤功能,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司于2013年成立,先為通江等服務(wù)建站,通江等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為通江企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
鑒于很多小伙伴給我私信,詢問(wèn)關(guān)于后端代碼的事。很開心很幫助到這么多人。但之前由于某種原因沒能將其與客戶端代碼一并發(fā)布,這里將代碼發(fā)布到GitHub上,讓大家方便下載學(xué)習(xí)。這里用的是Java Servlet ,運(yùn)行在 Web 服務(wù)器或應(yīng)用服務(wù)器上的程序,作為來(lái)自 Web 瀏覽器或其他 HTTP 客戶端的請(qǐng)求和 HTTP 服務(wù)器上的數(shù)據(jù)庫(kù)或應(yīng)用程序之間的中間層。數(shù)據(jù)庫(kù)使用的是MySQL,持久層使用了JDBC,Java的原生API。沒有使用框架,方便新手學(xué)習(xí),也更能理解web的運(yùn)行機(jī)制和原理。
GitHub地址:傳送門
這里要說(shuō)明一下關(guān)鍵的代碼:
/** * 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要使用你自己的。
關(guān)于“如何實(shí)現(xiàn)微信小程序簽到考勤功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
本文題目:如何實(shí)現(xiàn)微信小程序簽到考勤功能
轉(zhuǎn)載來(lái)于:http://aaarwkj.com/article22/gdeojc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、企業(yè)建站、網(wǎng)站制作、面包屑導(dǎo)航、App設(shè)計(jì)、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)