首頁 > 微信小程式 > 小程式開發 > 分享微信小程式簽到考勤後端代碼

分享微信小程式簽到考勤後端代碼

藏色散人
發布: 2020-07-25 13:23:08
轉載
4470 人瀏覽過

相關推薦:《小程式開發教學

#服務端源碼

鑑於很多小夥伴給我私信,詢問關於後端程式碼的事。很開心很幫助這麼多人。但之前因為某些原因無法與客戶端程式碼一併發布,這裡將程式碼發佈到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要使用你自己的。由於最近很少關注博客,諮詢的人也很多,很多私信來不及回复,具體其它的就由大家自己探索嘍。本程序純屬個人興趣,切勿用於商業用途。

以上是分享微信小程式簽到考勤後端代碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板