package com.lf.server.controller.sys;
|
|
import com.lf.server.controller.BaseController;
|
import com.lf.server.entity.all.ResponseMsg;
|
import com.lf.server.entity.data.LoginEntity;
|
import com.lf.server.entity.data.TokenEntity;
|
import com.lf.server.entity.data.UsersEntity;
|
import com.lf.server.entity.sys.LoginInfo;
|
import com.lf.server.entity.sys.Result;
|
import com.lf.server.entity.sys.User;
|
import com.lf.server.helper.StringHelper;
|
import com.lf.server.service.data.LoginService;
|
import com.lf.server.service.data.TokenService;
|
import com.lf.server.service.data.UsersService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.servlet.ModelAndView;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
/**
|
* 签名控制器
|
* @author www
|
* @date 2022-09-21
|
*/
|
@RestController
|
@RequestMapping("/sign")
|
public class SignController extends BaseController {
|
@Autowired
|
UsersService userService;
|
|
@Autowired
|
LoginService loginService;
|
|
@Autowired
|
TokenService tokenService;
|
|
/**
|
* 跳到首页
|
*
|
* @return String
|
*/
|
@GetMapping({"/", "/toIndex"})
|
public ModelAndView toIndex(ModelAndView mv) {
|
mv.setViewName("index");
|
|
User user = null;
|
if (user != null) {
|
mv.addObject("msg", "Hello " + user.getLoginName() + " !");
|
}
|
|
return mv;
|
}
|
|
/**
|
* 跳到登录页
|
*
|
* @return String
|
*/
|
@GetMapping("/toLogin")
|
public ModelAndView toLogin(ModelAndView mv) {
|
mv.setViewName("login");
|
|
return mv;
|
}
|
|
/**
|
* 登录认证
|
*
|
* @return String
|
*/
|
@PostMapping(value = "/login", produces = "application/json; charset=UTF-8")
|
public ResponseMsg<TokenEntity> login(@RequestBody UsersEntity user, HttpServletRequest req, HttpServletResponse res) {
|
try {
|
if (user == null) {
|
return fail("请输入用户名和密码!", null);
|
}
|
if (StringHelper.isEmpty(user.getUid())) {
|
return fail("用户名不能为空!", null);
|
}
|
if (StringHelper.isEmpty(user.getPwd())) {
|
return fail("密码不能为空!", null);
|
}
|
|
UsersEntity ue = userService.selectByUid(user.getUid());
|
if (ue == null) {
|
return fail("用户名不存在!", null);
|
}
|
|
LoginEntity le = loginService.getNewLogin(user.getId(), req);
|
if (!user.getPwd().equals(ue.getPwd())) {
|
le.setStatus(0);
|
le.setDescr("密码错误");
|
loginService.insertLogin(le);
|
return fail("密码不正确!", null);
|
}
|
le.setStatus(1);
|
loginService.insertLogin(le);
|
|
TokenEntity te = tokenService.getNewToken(ue.getId(), req);
|
tokenService.insertToken(te);
|
|
return success(te);
|
} catch (Exception ex) {
|
return fail(ex.getMessage(), null);
|
}
|
}
|
|
/**
|
* 注销
|
*
|
* @return String
|
*/
|
@GetMapping("/logout")
|
public ModelAndView logout() {
|
ModelAndView mv = new ModelAndView();
|
mv.setViewName("login");
|
|
return mv;
|
}
|
}
|