SpringBoot框架实现签到

前几天有人问我平台的签到功能是怎么实现的,今天我们就浅谈一下如何实现签到功能

首先,做功能最重要的是要懂业务逻辑,然后再写代码。我们先理清业务流程和业务逻辑

业务流程(发个截图):

用户登录

用户在个人主页中查看自己是否签到

          1. 如果未签到点击签到按钮,点击之后就到下一步

           

   2. 如果签到了就显示已签到,啥事都不用干

           

业务逻辑:

1. 简单版签到业务逻辑

          用户登录,获取用户id和用户账号,保存为全局变量

          通过用户id和系统时间查询用户今天是否签到,在界面中分别显示签到按钮或已签到按钮

          点击签到按钮后增加一条用户今日签到记录,界面显示已签到按钮

2. 专业版签到业务逻辑

          用户登录,获取用户id和用户账号,保存为全局变量

          通过用户id和系统时间查询用户今天是否签到,在界面中显示签到按钮和已签到按钮,增加 一条用户今日签到记录,但签              到记录里的签到字段值为0

         点击签到按钮后修改用户今日签到记录的签到字段值为1

这两种业务逻辑都可以实现签到功能,第一种逻辑简单,功能简化,第二种逻辑稍微复杂,但签到字段的加入使它扩展性变强

因为第一种逻辑已经能满足大部分的签到功能需求,所以楼主就先用第一种(还不是因为懒......嘤嘤嘤)

数据库结构

user表结构:

SpringBoot框架实现签到_第1张图片

signin表结构:

SpringBoot框架实现签到_第2张图片

head头文件代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ request.getContextPath() + "/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>





个人中心

foot尾文件代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

个人中心界面代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="common/head.jsp"%>	
${userSession.username}   



<%@ include file="common/foot.jsp"%>

UserController代码:

package com.kuyun.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuyun.entity.SignIn;
import com.kuyun.entity.User;
import com.kuyun.service.SignInService;
import com.kuyun.service.UserService;
import com.kuyun.tools.Constants;

@Controller
@RequestMapping(value = "/")
public class UserController {

	@Resource
	private UserService userService;
	@Resource
	private SignInService signInService;

	// 用户登录
	@RequestMapping(value = "/")
	public String toIndex(HttpSession session, Model model) {
		String username = "沐风";
		String password = "123456";
		User user = userService.findUser(username, password);
		// 验证用户是否存在
		if (user != null) {
			user.setId(user.getId());
			user.setUsername(user.getUsername());
			session.setAttribute(Constants.USER_SESSION, user);
		}
		return "index";
	}

	// 验证用户是否签到
	@RequestMapping(value = "/signIn", method = RequestMethod.POST)
	@ResponseBody
	public Map signIn(HttpSession session, Model model) {
		Map resultMap = new HashMap();
		User userSession = (User) session.getAttribute(Constants.USER_SESSION);
		Integer userId = userSession.getId();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date time = new Date();
		try {
			time = sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SignIn signIn = signInService.findSignIn(userId, time);
		if (signIn == null) {
			resultMap.put("result", "false");
		} else {
			resultMap.put("result", "true");
		}
		return resultMap;
	}
}

SignInController代码:

package com.kuyun.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuyun.entity.User;
import com.kuyun.service.SignInService;
import com.kuyun.tools.Constants;

@Controller
@RequestMapping(value = "/user")
public class SignInController {
	
	@Resource
	private SignInService signInService;

	// 签到
	@RequestMapping(value = "/toSignIn", method = RequestMethod.POST)
	@ResponseBody
	public Map toSignIn(HttpSession session) {
		Map resultMap = new HashMap();
		User userSession = (User) session.getAttribute(Constants.USER_SESSION);
		Integer userId = userSession.getId();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date time = new Date();
		try {
			time = sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		boolean flag=signInService.insSignIn(userId, time);
		if(flag) {
			resultMap.put("result", "true");
		}
		else {
			resultMap.put("result", "false");
		}
		return resultMap;
	}
}

本博客到此完毕......

你可能感兴趣的:(SpringBoot框架实现签到)