Home > WeChat Applet > Mini Program Development > Share the WeChat applet check-in and attendance back-end code

Share the WeChat applet check-in and attendance back-end code

藏色散人
Release: 2020-07-25 13:23:08
forward
4449 people have browsed it

##Related recommendations: "

小program Development Tutorial"

Server source code

In view of the fact that many friends sent me private messages asking about the back-end code. Very happy to have helped so many people. However, due to some reasons, it could not be released together with the client code. Here, the code is released on GitHub so that everyone can download and study conveniently. What is used here is Java Servlet, a program that runs on a Web server or application server as an intermediary layer between requests from a Web browser or other HTTP client and a database or application on the HTTP server. The database uses MySQL, and the persistence layer uses JDBC, Java's native API. No framework is used, which makes it easier for novices to learn and better understand the operating mechanism and principles of the web.

GitHub address: Portal
Here is the key code:

/**
 * 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);
	}}
Copy after login

Use your own APPID and SECRET here. Since I rarely pay attention to the blog recently, and there are many people asking for advice, I have no time to reply to many private messages. I will leave the rest to you to explore on your own. This program is purely for personal interest and must not be used for commercial purposes.

The above is the detailed content of Share the WeChat applet check-in and attendance back-end code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template