whyclxw
2020-11-07 6453477077b622ef14f61e450236ddb47e134f77
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
package com.fgkj.services;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
 
import com.fgkj.actions.ActionUtil;
import com.fgkj.dao.BaseDAO;
import com.fgkj.dao.BaseDAOFactory;
import com.fgkj.dto.UserClient;
import com.fgkj.dto.User_inf;
 
public class User_infService extends ActionUtil {
    private ServiceModel model;
    private BaseDAO dao;
 
    public User_infService() {
        model = new ServiceModel();
        dao = BaseDAOFactory.getBaseDAO(BaseDAO.USER_INF);
    }
 
     //添加用户
    public ServiceModel add(Object obj) {
        Boolean bl=dao.add(obj);
        if(bl){
            model.setCode(1);
            model.setMsg("添加成功!");
        }
        else{
            model.setCode(0);
            model.setMsg("添加失败!");
        }
        return model;
    }
 
    //修改用户密码
    public ServiceModel update(Object obj) {
        Boolean bl=dao.update(obj);
        if(bl){
            model.setCode(1);
            model.setMsg("修改成功");
        }else{
            model.setCode(0);
            model.setMsg("修改失败");
        }
        return model;
    }
 
    //删除用户
    public ServiceModel del(Object obj) {
        Boolean bl = dao.del(obj);
        if (bl) {
            model.setCode(1);
            model.setMsg("删除成功!");
        } else {
            model.setMsg("删除失败!");
        }
        return model;
    }
    //查询所有的用户(不含密码)
    public ServiceModel searchAll() {
        List list = dao.searchAll();
        if (list != null && list.size() > 0) {
            model.setCode(1);
            model.setData(list);
            model.setMsg("查询成功");
        }else{
            model.setCode(0);
            model.setMsg("查询失败");
        }
        return model;
    }
 
    // 用户登录
    public ServiceModel login(Object obj) {
        User_inf uinf = (User_inf) obj;
        List list = dao.serchByCondition(uinf);//验证用户是否存在
        if (list == null || list.size() < 1) {
            model.setMsg(this.getText("用户名不存在"));
            // System.out.println("用户名不存在");
        } else {
            User_inf u = (User_inf) list.get(list.size()-1);
            /*if (!uinf.getUpassword().equals(u.getUpassword())) {
                model.setMsg(getText("密码错误"));
            } else {*/
                model.setCode(1);
                model.setMsg("index.jsp");
                //model.setMsg("index-outline.jsp");
                
                /*setMyCookie("user",u,30,uinf.getUId() == 1);                //用户记住密码功能
                
                // 将登陆成功的用户存入session
                u.setUSnId("");
                u.setUpassword("");*/
                ActionUtil.getSession().setAttribute("user", u);
                
                //设置session不活动时间为30分
                ActionUtil.getSession().setMaxInactiveInterval(60*30);
                ActionUtil.getSession().setAttribute("ip",ActionUtil.getRequest().getRemoteAddr());
                //权限
                /*User_permitgroupService upgs=new User_permitgroupService();
                ServiceModel model1=upgs.serchItem(u);
                if(model1.getCode()==1){
                    ActionUtil.getSession().setAttribute("permits", ActionUtil.tojson(model1.getData()));
                }else{
                    model.setCode(0);
                    model.setMsg("还未分配权限");
                }*/
                //将新登录的用户存入application
                setApplication(u);
            //}
        }
        return model;
    }
 
    /**
     *     保存指定的cookie值
     * @param key        cookie 键
     * @param obj        值
     * @param timelong    保存时长(秒)
     * @param flag        是否保存
     */
    public void setMyCookie(String key,Object obj,long timelong,boolean flag){
        Cookie c = null;
        if(flag){
            //记住密码
            c = new Cookie(key, ActionUtil.EncryptionBase64(ActionUtil.getGson().toJson(obj)).toString());
        }else{
            c = new Cookie(key, "");
        }
        //让Cookie保留30天
        c.setMaxAge(30*24*60*60);
        ActionUtil.getResponse().addCookie(c);
    }
 
    /**
     * 开始查看application中是否有另一用使用该账号登陆
     * 
     * @return
     */
    
    public ServiceModel checkUser() {
        Map<String, UserClient> map = (Map) ActionUtil.getApplication().getAttribute("users");
        // System.out.println(map);
        if (map != null && map.size() > 0) {
            User_inf user = (User_inf) ActionUtil.getSession().getAttribute("user");
            Long login_time = (Long) ActionUtil.getSession().getAttribute("login_time");
            if (user != null && login_time != null) {
                UserClient client = map.get(user.getUName());
                if (client != null) {
                    if (login_time == client.getLogin_times()) {
                        model.setCode(0);
                    }else {
                        model.setCode(1);
                        model.setMsg(getText("The landing on the account in another host, please log in again"));
                    }
                }else {
                    model.setCode(1);
                    model.setMsg(getText("You are not logged in, please log in"));
                }
            } else {
                model.setCode(1);
                model.setMsg(getText("You are not logged in, please log in"));
            }
        } else {
            model.setCode(1);
            model.setMsg(getText("You are not logged in, please log in"));
        }
        return model;
    }
    // 将所有登陆的用户的信息存到application中
    public void setApplication(User_inf user) {
        ServletContext application = ActionUtil.getApplication();
        Map<String, UserClient> map = (Map) application.getAttribute("users");
        if (map == null) {
            map = new HashMap<String, UserClient>();
        } else {
            UserClient client = map.get(user.getUName());
            if (client != null) {
                map.remove(user.getUName());
            }
        }
        Long login_time = new Date().getTime();
        ActionUtil.getSession().setAttribute("login_time", login_time);
        map.put(user.getUName(), new UserClient(ActionUtil.getRequest().getRemoteAddr(),user,login_time));
        application.setAttribute("users", map);
    }
    
}