search
HomeWeChat AppletMini Program DevelopmentShare the WeChat applet check-in and attendance back-end code

##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);
	}}

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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.