package com.fh.controller.system.user; import java.io.PrintWriter; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import org.apache.shiro.crypto.hash.SimpleHash; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import com.fh.controller.base.BaseController; import com.fh.entity.Page; import com.fh.entity.system.Role; import com.fh.service.system.fhlog.FHlogManager; import com.fh.service.system.menu.MenuManager; import com.fh.service.system.role.RoleManager; import com.fh.service.system.user.UserManager; import com.fh.util.AppUtil; import com.fh.util.Const; import com.fh.util.FileDownload; import com.fh.util.FileUpload; import com.fh.util.GetPinyin; import com.fh.util.Jurisdiction; import com.fh.util.ObjectExcelRead; import com.fh.util.PageData; import com.fh.util.ObjectExcelView; import com.fh.util.PathUtil; import com.fh.util.Tools; /** * 类名称:UserController * 更新时间:2015年11月3日 * @version */ @Controller @RequestMapping(value="/user") public class UserController extends BaseController { String menuUrl = "user/listUsers.do"; //菜单地址(权限用) @Resource(name="userService") private UserManager userService; @Resource(name="roleService") private RoleManager roleService; @Resource(name="menuService") private MenuManager menuService; @Resource(name="fhlogService") private FHlogManager FHLOG; /**显示用户列表 * @param page * @return * @throws Exception */ @RequestMapping(value="/listUsers") public ModelAndView listUsers(Page page)throws Exception{ ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); pd = this.getPageData(); String keywords = pd.getString("keywords"); //关键词检索条件 if(null != keywords && !"".equals(keywords)){ pd.put("keywords", keywords.trim()); } String lastLoginStart = pd.getString("lastLoginStart"); //开始时间 String lastLoginEnd = pd.getString("lastLoginEnd"); //结束时间 if(lastLoginStart != null && !"".equals(lastLoginStart)){ pd.put("lastLoginStart", lastLoginStart+" 00:00:00"); } if(lastLoginEnd != null && !"".equals(lastLoginEnd)){ pd.put("lastLoginEnd", lastLoginEnd+" 00:00:00"); } page.setPd(pd); List userList = userService.listUsers(page); //列出用户列表 pd.put("ROLE_ID", "1"); List roleList = roleService.listAllRolesByPId(pd);//列出所有系统用户角色 mv.setViewName("system/user/user_list"); mv.addObject("userList", userList); mv.addObject("roleList", roleList); mv.addObject("pd", pd); mv.addObject("QX",Jurisdiction.getHC()); //按钮权限 return mv; } /**删除用户 * @param out * @throws Exception */ @RequestMapping(value="/deleteU") public void deleteU(PrintWriter out) throws Exception{ if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return;} //校验权限 logBefore(logger, Jurisdiction.getUsername()+"删除user"); PageData pd = new PageData(); pd = this.getPageData(); userService.deleteU(pd); FHLOG.save(Jurisdiction.getUsername(), "删除系统用户:"+pd); out.write("success"); out.close(); } /**去新增用户页面 * @return * @throws Exception */ @RequestMapping(value="/goAddU") public ModelAndView goAddU()throws Exception{ if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限 ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); pd = this.getPageData(); pd.put("ROLE_ID", "1"); List roleList = roleService.listAllRolesByPId(pd);//列出所有系统用户角色 mv.setViewName("system/user/user_edit"); mv.addObject("msg", "saveU"); mv.addObject("pd", pd); mv.addObject("roleList", roleList); return mv; } /**保存用户 * @return * @throws Exception */ @RequestMapping(value="/saveU") public ModelAndView saveU() throws Exception{ if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限 logBefore(logger, Jurisdiction.getUsername()+"新增user"); ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); pd = this.getPageData(); pd.put("USER_ID", this.get32UUID()); //ID 主键 pd.put("LAST_LOGIN", ""); //最后登录时间 pd.put("IP", ""); //IP pd.put("STATUS", "0"); //状态 pd.put("SKIN", "default"); pd.put("RIGHTS", ""); pd.put("PASSWORD", new SimpleHash("SHA-1", pd.getString("USERNAME"), pd.getString("PASSWORD")).toString()); //密码加密 if(null == userService.findByUsername(pd)){ //判断用户名是否存在 userService.saveU(pd); //执行保存 FHLOG.save(Jurisdiction.getUsername(), "新增系统用户:"+pd.getString("USERNAME")); mv.addObject("msg","success"); }else{ mv.addObject("msg","failed"); } mv.setViewName("save_result"); return mv; } /**判断用户名是否存在 * @return */ @RequestMapping(value="/hasU") @ResponseBody public Object hasU(){ Map map = new HashMap(); String errInfo = "success"; PageData pd = new PageData(); try{ pd = this.getPageData(); if(userService.findByUsername(pd) != null){ errInfo = "error"; } } catch(Exception e){ logger.error(e.toString(), e); } map.put("result", errInfo); //返回结果 return AppUtil.returnObject(new PageData(), map); } /**判断邮箱是否存在 * @return */ @RequestMapping(value="/hasE") @ResponseBody public Object hasE(){ Map map = new HashMap(); String errInfo = "success"; PageData pd = new PageData(); try{ pd = this.getPageData(); if(userService.findByUE(pd) != null){ errInfo = "error"; } } catch(Exception e){ logger.error(e.toString(), e); } map.put("result", errInfo); //返回结果 return AppUtil.returnObject(new PageData(), map); } /**判断编码是否存在 * @return */ @RequestMapping(value="/hasN") @ResponseBody public Object hasN(){ Map map = new HashMap(); String errInfo = "success"; PageData pd = new PageData(); try{ pd = this.getPageData(); if(userService.findByUN(pd) != null){ errInfo = "error"; } } catch(Exception e){ logger.error(e.toString(), e); } map.put("result", errInfo); //返回结果 return AppUtil.returnObject(new PageData(), map); } /**去修改用户页面(系统用户列表修改) * @return * @throws Exception */ @RequestMapping(value="/goEditU") public ModelAndView goEditU() throws Exception{ if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限 ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); pd = this.getPageData(); if("1".equals(pd.getString("USER_ID"))){return null;} //不能修改admin用户 pd.put("ROLE_ID", "1"); List roleList = roleService.listAllRolesByPId(pd); //列出所有系统用户角色 mv.addObject("fx", "user"); pd = userService.findById(pd); //根据ID读取 List froleList = new ArrayList(); //存放副职角色 String ROLE_IDS = pd.getString("ROLE_IDS"); //副职角色ID if(Tools.notEmpty(ROLE_IDS)){ String arryROLE_ID[] = ROLE_IDS.split(",fh,"); for(int i=0;i roleList = roleService.listAllRolesByPId(pd); //列出所有系统用户角色 pd.put("USERNAME", Jurisdiction.getUsername()); pd = userService.findByUsername(pd); //根据用户名读取 mv.setViewName("system/user/user_edit"); mv.addObject("msg", "editU"); mv.addObject("pd", pd); mv.addObject("roleList", roleList); return mv; } /**查看用户 * @return * @throws Exception */ @RequestMapping(value="/view") public ModelAndView view() throws Exception{ if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限 ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); pd = this.getPageData(); if("admin".equals(pd.getString("USERNAME"))){return null;} //不能查看admin用户 pd.put("ROLE_ID", "1"); List roleList = roleService.listAllRolesByPId(pd); //列出所有系统用户角色 pd = userService.findByUsername(pd); //根据ID读取 mv.setViewName("system/user/user_view"); mv.addObject("msg", "editU"); mv.addObject("pd", pd); mv.addObject("roleList", roleList); return mv; } /**去修改用户页面(在线管理页面打开) * @return * @throws Exception */ @RequestMapping(value="/goEditUfromOnline") public ModelAndView goEditUfromOnline() throws Exception{ ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); pd = this.getPageData(); if("admin".equals(pd.getString("USERNAME"))){return null;} //不能查看admin用户 pd.put("ROLE_ID", "1"); List roleList = roleService.listAllRolesByPId(pd); //列出所有系统用户角色 pd = userService.findByUsername(pd); //根据ID读取 List froleList = new ArrayList(); //存放副职角色 String ROLE_IDS = pd.getString("ROLE_IDS"); //副职角色ID if(Tools.notEmpty(ROLE_IDS)){ String arryROLE_ID[] = ROLE_IDS.split(",fh,"); for(int i=0;i map = new HashMap(); pd = this.getPageData(); List pdList = new ArrayList(); String USER_IDS = pd.getString("USER_IDS"); if(null != USER_IDS && !"".equals(USER_IDS)){ String ArrayUSER_IDS[] = USER_IDS.split(","); userService.deleteAllU(ArrayUSER_IDS); pd.put("msg", "ok"); }else{ pd.put("msg", "no"); } pdList.add(pd); map.put("list", pdList); return AppUtil.returnObject(pd, map); } /**导出用户信息到EXCEL * @return * @throws Exception */ @RequestMapping(value="/excel") public ModelAndView exportExcel() throws Exception{ FHLOG.save(Jurisdiction.getUsername(), "导出用户信息到EXCEL"); ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); pd = this.getPageData(); try{ if(Jurisdiction.buttonJurisdiction(menuUrl, "cha")){ String keywords = pd.getString("keywords"); //关键词检索条件 if(null != keywords && !"".equals(keywords)){ pd.put("keywords", keywords.trim()); } String lastLoginStart = pd.getString("lastLoginStart"); //开始时间 String lastLoginEnd = pd.getString("lastLoginEnd"); //结束时间 if(lastLoginStart != null && !"".equals(lastLoginStart)){ pd.put("lastLoginStart", lastLoginStart+" 00:00:00"); } if(lastLoginEnd != null && !"".equals(lastLoginEnd)){ pd.put("lastLoginEnd", lastLoginEnd+" 00:00:00"); } Map dataMap = new HashMap(); List titles = new ArrayList(); titles.add("用户名"); //1 titles.add("编号"); //2 titles.add("姓名"); //3 titles.add("职位"); //4 titles.add("手机"); //5 titles.add("邮箱"); //6 titles.add("最近登录"); //7 titles.add("上次登录IP"); //8 dataMap.put("titles", titles); List userList = userService.listAllUser(pd); List varList = new ArrayList(); for(int i=0;i listPd = (List)ObjectExcelRead.readExcel(filePath, fileName, 2, 0, 0); //执行读EXCEL操作,读出的数据导入List 2:从第3行开始;0:从第A列开始;0:第0个sheet /*存入数据库操作======================================*/ pd.put("RIGHTS", ""); //权限 pd.put("LAST_LOGIN", ""); //最后登录时间 pd.put("IP", ""); //IP pd.put("STATUS", "0"); //状态 pd.put("SKIN", "default"); //默认皮肤 pd.put("ROLE_ID", "1"); List roleList = roleService.listAllRolesByPId(pd);//列出所有系统用户角色 pd.put("ROLE_ID", roleList.get(0).getROLE_ID()); //设置角色ID为随便第一个 /** * var0 :编号 * var1 :姓名 * var2 :手机 * var3 :邮箱 * var4 :备注 */ for(int i=0;i userList = userService.listUsersBystaff(page); //列出用户列表(弹窗选择用) pd.put("ROLE_ID", "1"); List roleList = roleService.listAllRolesByPId(pd);//列出所有系统用户角色 mv.setViewName("system/user/window_user_list"); mv.addObject("userList", userList); mv.addObject("roleList", roleList); mv.addObject("pd", pd); mv.addObject("QX",Jurisdiction.getHC()); //按钮权限 return mv; } @InitBinder public void initBinder(WebDataBinder binder){ DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true)); } }