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.dao.impl.User_infImpl;
|
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.setData(u.getUNote());
|
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);
|
}
|
|
//权限管理——————查出所有的用户标识是否有权限
|
public ServiceModel serchUserByGroup() {
|
List list=((User_infImpl)dao).serchUserByGroup();
|
if(list!=null&&list.size()>0){
|
model.setCode(1);
|
model.setData(list);
|
model.setMsg("查询成功!");
|
}
|
else{
|
model.setCode(0);
|
model.setMsg("查询失败!");
|
}
|
return model;
|
}
|
}
|