管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-09-29 fa944da5443f02c135ae63f1f7e50170d04a97cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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;
    }
}