whyclj
2019-10-29 1c0469e45346d464e0c5672ee68f9ecd4fb6be7c
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
252
253
254
255
256
257
258
259
260
261
262
263
package com.fgkj.actions;
 
 
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.Cookie;
 
import com.fgkj.dao.BaseDAO;
import com.fgkj.dao.BaseDAOFactory;
import com.fgkj.db.DBUtil;
import com.fgkj.db.IDatabaseName;
import com.fgkj.dto.ServiceModel;
import com.fgkj.dto.User;
import com.fgkj.dto.UserClient;
import com.fgkj.dto.User_Chart;
import com.fgkj.dto.User_inf;
import com.fgkj.dto.User_log;
import com.fgkj.services.UserService;
import com.fgkj.services.User_ChartService;
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 com.opensymphony.xwork2.ActionSupport;
 
 
public class LoginAction extends ActionSupport{
    private User_inf uinf;
    private ServiceModel model;    
    //private String user;
    //private String password;
    private String json;
    private String result;
    
    private User_infService service=new User_infService();
    private Vip_userService vservice=new Vip_userService();
 
    //web版登录
    public String login() {
        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);
        }
        result=ActionUtil.tojson(model);    
        return "success";
    }
    
    //手机端登录action
    public String MobileLogin(){
        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 uinf = (User_inf) ActionUtil.getUser();
            if(uinf!=null){                
                log.setuId(uinf.getUId());
                log.setUOprateDay(new Date());
                log.setuOprateMsg("登录移动系统");
                log.setUOprateType(UserUtil.LOGIN);
                log.setuTerminalIp(ActionUtil.getRequest().getRemoteAddr());
                logservice.add(log);
            }
        }
        //System.out.println(model);
        result=ActionUtil.tojson(model);    
        //System.out.println(result);
        return "success";
    }
    
    //检查超级管理员是否
    public String  checkVip(){
        Map<String, UserClient> map = (Map) ActionUtil.getApplication().getAttribute("vips");
        model=vservice.checkVip();
        result=ActionUtil.tojson(model);
        return SUCCESS;
    }
    
    /**
     * 判断是否有另一同一账号登陆的用户
     * @return
     */
    public String check(){
        //System.out.println("开始检查");
        model=service.checkUser();
        uinf=(User_inf)ActionUtil.getSession().getAttribute("user");        
        if(model.code == 1){
            ActionUtil.getSession().invalidate();        //清除当前的session;                //清除session
        }
        result=ActionUtil.tojson(model);
        return "success";
    }
    
    /**
     * 退出登录
     * @return
     */
    
    public String 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 "success";
    }
    
    /**
     * 存储当前用户的包机组信息
     * @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 {
            BaseDAO dao = BaseDAOFactory.getBaseDAO(BaseDAO.USER_CHART);
            List ls = dao.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 String exitVUser(){
        ActionUtil.getSession().invalidate();
        return SUCCESS;
    }
    
    //通过制定的字符串获取session
    public String getSessionByString(){
        Object obj = ActionUtil.getSession().getAttribute(json);
        if(obj != null){
            result = ActionUtil.tojson(obj);
        }else{
            result = "";
        }
        return SUCCESS;
    }
    
    /**
     * 初始化数据库连接池
     * @return
     */
    public String initDBPool(){
        DBUtil.initPool();
        return SUCCESS;
    }
 
    public String getResult() {
        return result;
    }
 
    public void setResult(String result) {
        this.result = result;
    }
 
 
    public User_inf getUinf() {
        return uinf;
    }
    
    public void setJson(String json) {
        this.json = json;
    }
 
    public void setUinf(User_inf uinf) {
        this.uinf = uinf;
    }
        
}