首页>代码>Struts、spring、myBatis增删改查新闻后台管理案例>/共享代码/第四次备份/newsItem/src/controller/UserController.java
package controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import dto.User_Dto;

import service.User_Service;
import util.PageUtil;

/**
 * @author: 张进
 * @createTime: 2017-3-9 下午9:59:00
 * @description:
 * TODO:
 */
public class UserController extends ActionSupport {
	private User_Service service;
	private User_Dto user;
	private PageUtil page;
	private String show_one;//前台UserShow.jsp传递的参数
	public String getShow_one()
    {
    	return show_one;
    }

	public void setShow_one(String show_one)
    {
    	this.show_one = show_one;
    }

	private int a;
	private int b;
	private int checks;

	private JSONObject json;
	private File photo;
	private String photoFileName;
	private String photoContentType;
	
	public int getChecks() {
		return checks;
	}

	public void setChecks(int checks) {
		this.checks = checks;
	}

	public int getA() {
		return a;
	}

	public void setA(int a) {
		this.a = a;
	}

	public int getB() {
		return b;
	}

	public void setB(int b) {
		this.b = b;
	}


	public PageUtil getPage() {
		return page;
	}

	public void setPage(PageUtil page) {
		this.page = page;
	}

	
	
	

	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}

	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}

	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	public JSONObject getJson() {
		return json;
	}

	public void setJson(JSONObject json) {
		this.json = json;
	}

	public User_Service getService() {
		return service;
	}

	public void setService(User_Service service) {
		this.service = service;
	}

	public User_Dto getUser() {
		return user;
	}

	public void setUser(User_Dto user) {
		this.user = user;
	}
	
	/**
	 * 
	 * @描述 登录
	 * @日期 2017-3-7
	 * @参数 newsItem
	 * @返回类型 String
	 */
	public String Login(){		
		User_Dto us = null;
		Map<String, String>  outMap = new HashMap<String, String>();
		us = service.Login(user);
		
		// 验证登录
		if (us == null){	//us为空,数据库没有查到对应的用户
			outMap.put("result", "without_user");
		}else if (us !=null&&us.getChecks()!=2){	//us不为空,审核状态不为2,数据库中有查到对应的用户数据
			if(us.getChecks()==1){	//用户为已经审核通过的
				if(us.getRole_id().equals("1001")){	//权限为管理员的
					outMap.put("result", "userShow");//将管理员结果GM传到前台,展示所用用户
					ServletActionContext.getRequest().getSession().setAttribute("sonInfo", us);
				}else /*if(us.getRole_id().equals("1002"))*/{	//权限为非管理员的(记者和普通员工)
					outMap.put("result", "newsShow");//则可以登录,展示所用新闻
					ServletActionContext.getRequest().getSession().setAttribute("sonInfo", us);
				}
				//cookie记住密码
				//从前台获取  记住密码   的value属性值,
				String remem_value = ServletActionContext.getRequest().getParameter("rememberMe");
				if ("yes".equals(remem_value)) {				
					// 放入cookie
					Cookie cookieUsername = new Cookie("userName",us.getUserName());// 获取用户名的值,放入cookie
					Cookie cookiePwd = new Cookie("pwd", us.getPwd());// 获取密码,放入cookie
					cookieUsername.setMaxAge(3600 * 24 * 30);// 设置用户名cookie一个月后失效
					cookiePwd.setMaxAge(3600 * 24 * 30);// 设置密码cookie一个月后失效
					// 将cookie写入response
					ServletActionContext.getResponse().addCookie(cookieUsername);
					ServletActionContext.getResponse().addCookie(cookiePwd);
				} else { /* 清除“用户名”和“密码”的Cookie */
					Cookie cookieUsername = new Cookie("userName","");// 获取用户名cookie
					Cookie cookiePwd = new Cookie("pwd", "");// 获取密码cookie
					cookieUsername.setMaxAge(0);
					cookiePwd.setMaxAge(0);
					// 将cookie写入response
					ServletActionContext.getResponse().addCookie(cookieUsername);
					ServletActionContext.getResponse().addCookie(cookiePwd);
				}				
			}else if(us.getChecks()==3){	//审核不通过的用户
				outMap.put("result", "user_refuse");				
			}	
		}else if(us !=null&&us.getChecks()==2){	//待审核的用户,设置session属性对象us,再去到用户详细页面
			outMap.put("result", "waitCheck_user");			
		}	
		json = JSONObject.fromObject(outMap);//将数据放进json,给ajax
		return "success";
	}
	
	/**
	 * 描述:待审核者登录方法
	 * 返回值类型:String
	 */
	public String Login_two(){	
		try{
			String userName = ServletActionContext.getRequest().getParameter("userName");
			String pwd = ServletActionContext.getRequest().getParameter("pwd");
		
			User_Dto wait_check_user = new User_Dto();
			wait_check_user.setUserName(userName);
			wait_check_user.setPwd(pwd);
			wait_check_user = service.Login(wait_check_user);
			if(wait_check_user !=null ){
				//设置"serTino"到前台,用户详细页面
				ServletActionContext.getRequest().getSession().setAttribute("serTino", wait_check_user);
				return "wait_check_user";
			}else{
				return "Login_page";
			}
			
		}catch (Exception e) {
			e.printStackTrace();
			return "Login_page";
		}
	}

	/**
	 * 
	 * @描述 用户注册方法
	 * @日期 2017-3-7
	 * @参数 newsItem
	 * @返回类型 String
	 */
	public String userRegister() throws Exception {
		System.out.println(user);
		User_Dto dtoBack = service.Login(user);
		HttpSession session = ServletActionContext.getRequest().getSession();
		try{
			//获取项目的部署目录
		    String path =	ServletActionContext.getRequest().getRealPath("/img");
			InputStream in = new FileInputStream(photo);  
			OutputStream os = new FileOutputStream(new File( path+"/"+photoFileName));
			byte b[]=new byte[1024];
			int res = 0;
			while( ( res =in.read(b)) != -1 ){
				os.write(b);
			}
			os.close();
			in.close();
			user.setPic(photoFileName); 
			service.userRegister(user);			
			session.setAttribute("userInfo", dtoBack);//拦截器用
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();		
		}
		return "success";
	}

	/**
	 * @描述 展示所有用户
	 * @日期 2017-3-7
	 * @参数 newsItem
	 * @返回类型 String
	 */
	public String userShow() {
		List<User_Dto> usList = new ArrayList<User_Dto>();
		if (page == null) {
			page = new PageUtil();
		}
		page.setPageSize(15);
		Map<String, Object> usMap = new HashMap<String, Object>();
		if (user != null) {
			usMap.put("nickName", user.getNickName());
			usMap.put("userName", user.getUserName());
		}
		usMap.put("a", a);
		usMap.put("b", b);
		usMap.put("checks", checks);
		try {
			usList = service.userShow(page, usMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		HttpServletRequest request = ServletActionContext.getRequest();
		if (usList != null && usList.size() > 0) {
			request.getSession().setAttribute("usList", usList);
			return "success";
		} else {
			return "error";
		}
	}
	/**
	 * @描述  管理员查看审核一个用户,和注册时,用户不重复的验证
	 * @日期 2017-3-7
	 * @参数 user
	 * @返回类型 String
	 */
	public String oneUser_detail() {		
		try {				
			if(user.getId() !=null){//用户id不为空就是超级用户审核,依靠id验证
				User_Dto us = service.oneUser_detail(user);
				ServletActionContext.getRequest().getSession().setAttribute("serTino", us);//设置"serTino"到前台
			}else{	//id为空时候,注册用户不重复的验证,依靠名字验证
				Map<String, String>  outMap = new HashMap<String, String>();
				User_Dto us = service.oneUser_detail(user);
				if(us != null){
					outMap.put("result", "already_exist");
				}
				json = JSONObject.fromObject(outMap);//将数据放进json,给ajax								
			}			
					
		} catch (Exception e) {
			
			e.printStackTrace();
			;
		}
		return "success";
	}
	
	/**
	 * @描述  注册时,用户不重复的验证
	 * @日期 2017-3-7
	 * @参数 user
	 * @返回类型 String
	 */
	public String check_name() {		
		try {						
				Map<String, String>  outMap = new HashMap<String, String>();
				User_Dto us = service.oneUser_detail(user);
				if(us != null){
					outMap.put("result", "already_exist");
				}
				json = JSONObject.fromObject(outMap);//将数据放进json,给ajax												
		} catch (Exception e) {
			
			e.printStackTrace();
			;
		}
		return "success";
	}

	/**
	 * @描述 	管理员审核方法
	 * @日期 2017-3-7
	 * @参数 newsItem
	 * @返回类型 String
	 */
	public String review_check() {
		int sal = 0;
		Map<String,String> usMap = new HashMap<String, String>();
		try {
			sal = service.review_check(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(sal == 1){
			usMap.put("reult", "yesGod");
		}else{
			usMap.put("reult", "noGod");
		}
		json=JSONObject.fromObject(usMap);
		return "success";
	}
}
最近下载更多
1029162867  LV13 2022年7月7日
52321641  LV1 2022年7月4日
阿布屋脊  LV7 2022年6月7日
猫巷^  LV1 2021年12月7日
543666826  LV33 2021年11月20日
chongmingsang  LV1 2021年6月30日
clz123456  LV6 2021年6月24日
panbiao  LV11 2021年3月31日
1435633469  LV1 2021年1月5日
江辞123456  LV1 2020年12月15日
最近浏览更多
zilatan 2023年11月25日
暂无贡献等级
飞呀飞呀飞不放  LV7 2023年8月9日
gecongkai  LV8 2023年6月22日
shuidajiao 2023年6月18日
暂无贡献等级
微信网友_6509705287716864  LV2 2023年6月8日
yangbulx  LV3 2023年6月7日
微信网友_6509053506113536  LV1 2023年6月7日
975525508  LV1 2023年6月5日
lpclpclpc  LV4 2023年5月30日
微信网友_6465435620184064  LV6 2023年5月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友