whycrzg
2021-02-03 f3e160c4c6690232da3258adb42c1f702cd8bb07
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.fgkj.controller;
 
import com.fgkj.util.*;
 
import com.fgkj.dto.*;
import com.fgkj.mapper.impl.User_ChartMapper;
import com.fgkj.services.User_battgroup_baojigroup_battgroupService;
import com.fgkj.services.User_infService;
import com.fgkj.services.User_logService;
import com.fgkj.services.Vip_userService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
@RequestMapping("login")
@RestController
@Api(tags = "login接口")
public class LoginController{
 
    // private User_inf uinf;
    ServiceModel model = new ServiceModel();
    //private String user;
    //private String password;
 
    @Resource
    private User_infService service;
    @Resource
    private Vip_userService vservice;
    @Resource
    private User_ChartMapper userChartMapper;
 
    //web版登录
    @PostMapping("login")
    @ApiOperation(notes = "",value="web版登录")
    public ServiceModel login(@RequestBody User_inf uinf) {
        model=vservice.login(uinf);
        //System.out.println(model);
        if(model.getCode()!=1){    
            model=service.login(uinf);
            if(model.code == 1){
                User_inf u = (User_inf) ActionUtil.getUser();
                //System.out.println(u);
                if(u!=null){    
                    //setUserBaoji(u);            //存储当前用户的包机组
                    
                    recordUserlogin(u);            //记录当前用户的登录记录
                    
                    setUserLogo(u);                //记录当前用户的所属部门的logo    
                }
            }
        }
 
 
        if(model.getCode()==1){
            Cookie c = null;
            //System.out.println(ActionUtil.DecryptionBase64());
            if(uinf.getuId() == 1){
                c = new Cookie("user", ActionUtil.EncryptionBase64(ActionUtil.getGson().toJson(uinf)).toString());
            }else{
                c = new Cookie("user", "");
            }
            //让Cookie保留30天
            c.setMaxAge(30*24*60*60);
            ActionUtil.getResponse().addCookie(c);
        }
        return model;
    }
    
    //手机端登录action
    @PostMapping("mobileLogin")
    @ApiOperation(notes = "",value="手机端登录action")
    public ServiceModel MobileLogin(@RequestBody User_inf uinf){
        System.out.println(uinf);
        String usnId64=(String) ActionUtil.EncryptionBase64(uinf.getUpassword());
        uinf.setUpassword(usnId64);
        //System.out.println(usnId64+"  : action");
        model=service.login(uinf);
        //System.out.println(model);
        if(model.code==1){
            User_logService logservice= new User_logService();
            User_log log = new User_log();
            User_inf uinf2 = (User_inf) ActionUtil.getUser();
            if(uinf!=null){                
                log.setuId(uinf2.getuId());
                log.setuOprateDay(new Date());
                log.setuOprateMsg("登录移动系统");
                log.setuOprateType(UserUtil.LOGIN);
                log.setuTerminalIp(ActionUtil.getRequest().getRemoteAddr());
                logservice.add(log);
            }
        }
        return model;
    }
    
    //检查超级管理员是否
    @GetMapping("checkVip")
    @ApiOperation(notes = "",value="检查超级管理员是否")
    public ServiceModel  checkVip(){
        Map<String, UserClient> map = (Map) ActionUtil.getApplication().getAttribute("vips");
        model=vservice.checkVip();
        
        return model;
    }
    
    /**
     * 判断是否有另一同一账号登陆的用户
     * @return
     */
    @GetMapping("check")
    @ApiOperation(notes = "",value="判断是否有另一同一账号登陆的用户")
    public ServiceModel check(){
        //System.out.println("开始检查");
        model=service.checkUser();
        //当前用户
        HttpSession session = ActionUtil.getSession();
        User_inf uinf=(User_inf) session.getAttribute("user");
        if(model.code == 1){
            session.invalidate();        //清除当前的session;                //清除session
        }
        
        return model;
    }
    
    /**
     * 退出登录
     * @return
     */
    @PutMapping("/userExit")
    @ApiOperation(notes = "",value="退出登录")
    public ServiceModel exitUser(){
        //将当前用户从所有的登陆用户中移除
        Map<String, UserClient> map = (Map) ActionUtil.getApplication().getAttribute("users");
        if(map!=null && map.size()>0){
            User_inf user = (User_inf) ActionUtil.getSession().getAttribute("user");
            if(user!=null){
                //System.out.println(user);
                UserClient client = map.get(user.getuName());
                //System.out.println(client);
                if(client!=null){
                    map.remove(user.getuName());
                }
                User_logService logservice= new User_logService();
                User_log log = new User_log();
                log.setuId(user.getuId());
                log.setuOprateDay(new Date());
                log.setuOprateMsg("登出系统");
                log.setuOprateType(UserUtil.LOGOUT);
                log.setuTerminalIp(ActionUtil.getRequest().getLocalAddr());
                logservice.add(log);
            }
            ActionUtil.getSession().invalidate();        //清除session
        }
        
        return model;
    }
    
    /**
     * 存储当前用户的包机组信息
     * @param u
     */
    public void setUserBaoji(User_inf u){
        User_battgroup_baojigroup_battgroupService se = new User_battgroup_baojigroup_battgroupService();
        try {
            Object batts = se.serchByCondition(u).getData();
            //System.out.println(batts.toString());
            Cookie b = new Cookie("batts",URLEncoder.encode(ActionUtil.tojson(batts),"UTF-8"));
            b.setMaxAge(30*24*60*60);
            ActionUtil.getResponse().addCookie(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 记录用户的登录记录
     * @param u
     */
    public void recordUserlogin(User_inf u){
        try {
            User_logService logservice= new User_logService();
            User_log log = new User_log();
            log.setuId(u.getuId());
            log.setuOprateDay(new Date());
            log.setuOprateMsg("登录PC系统");
            log.setuOprateType(UserUtil.LOGIN);
            log.setuTerminalIp(ActionUtil.getRequest().getRemoteAddr());
            logservice.add(log);
        } catch (Exception e) {
            System.out.println("记录用户的登录信息出错");
            e.printStackTrace();
        }
    }
    
    /**
     * 查询用户所述的部门 对应的logo
     */
    public void setUserLogo(User_inf u){
        try {
            List ls = userChartMapper.serchByCondition(u);
            String uc = "whyc";
            if(ls != null && ls.size()>0){
                uc = ((User_Chart)ls.get(0)).getChart_file();
            }        
            Cookie c = new Cookie("user_logo",uc);
            c.setMaxAge(30*24*60*60);
            ActionUtil.getResponse().addCookie(c);
        } catch (Exception e) {
            e.printStackTrace();
        }    
    }
    
    /**
     * 退出登录
     * @return
     */
    public ServiceModel exitVUser(){
        ActionUtil.getSession().invalidate();
        return model;
    }
    
    /*//通过制定的字符串获取session
    public ServiceModel getSessionByString(){
        Object obj = ActionUtil.getSession().getAttribute(json);
        if(obj != null){
            result = ActionUtil.tojson(obj);
        }else{
            result = "";
        }
        return model;
    }*/
    
    /**
     * 初始化数据库连接池
     * @return
     *//*
    public ServiceModel initDBPool(){
        DBUtil.initPool();
        return model;
    }*/
 
}