UserController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. package com.zjt.web;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.google.gson.JsonArray;
  6. import com.google.gson.JsonObject;
  7. import com.zjt.entity.*;
  8. import com.zjt.service.MaterialType;
  9. import com.zjt.service.TmenuService;
  10. import com.zjt.service.TroleService;
  11. import com.zjt.service.TuserService;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.apache.shiro.SecurityUtils;
  14. import org.apache.shiro.authc.UsernamePasswordToken;
  15. import org.apache.shiro.subject.Subject;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.ui.Model;
  18. import org.springframework.validation.BindingResult;
  19. import org.springframework.web.bind.annotation.*;
  20. import tk.mybatis.mapper.entity.Example;
  21. import javax.annotation.Resource;
  22. import javax.servlet.http.HttpSession;
  23. import javax.validation.Valid;
  24. import javax.validation.constraints.Size;
  25. import java.net.URLEncoder;
  26. import java.text.SimpleDateFormat;
  27. import java.util.Date;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.function.Consumer;
  32. import java.util.function.Predicate;
  33. import java.util.stream.Collectors;
  34. import static org.apache.commons.lang3.StringUtils.length;
  35. /**
  36. * @Description: 当前登录用户控制器
  37. * @Date: Created in 2018/2/8 19:28
  38. * @param
  39. */
  40. @Controller
  41. @RequestMapping("/user")
  42. public class UserController {
  43. @Resource
  44. private com.zjt.service.MaterialType MaterialType;
  45. @Resource
  46. private TroleService troleService;
  47. @Resource
  48. private TuserService tuserService;
  49. @Resource
  50. private TmenuService tmenuService;
  51. /*@Resource
  52. private LogService logService;*/
  53. @RequestMapping("list")
  54. public String select(String id,String storeCode, String createtime,String receive, HttpSession session, Model model){
  55. Tuser users= (Tuser) session.getAttribute("currentUser");
  56. List<cgmaterial> collect1;
  57. String id1="";
  58. if(!"".equals(id)){
  59. id1=id.substring(1);
  60. }
  61. if(!"".equals(createtime)){
  62. String[] split1 = createtime.split(" - ");
  63. String strDateFormat1 = "yyyy-MM-dd HH:mm:ss";
  64. SimpleDateFormat sdf1 = new SimpleDateFormat(strDateFormat1);
  65. if(!"".equals(storeCode)){
  66. if("admin".equals(users.getTrueName())){
  67. collect1=MaterialType.selectDataBy(id1,storeCode,split1[0],split1[1],receive);
  68. }else{
  69. collect1=MaterialType.selectDataBy(id1,users.getUserName(),split1[0],split1[1],receive);
  70. }
  71. }else{
  72. if("admin".equals(users.getTrueName())){
  73. collect1=MaterialType.selectDataBy(id1,"",split1[0],split1[1],receive);
  74. }else{
  75. collect1=MaterialType.selectDataBy(id1,users.getUserName(),split1[0],split1[1],receive);
  76. }
  77. }
  78. }else{
  79. if(!"".equals(storeCode)){
  80. if("admin".equals(users.getTrueName())){
  81. collect1=MaterialType.selectDataBy(id1,storeCode,"","",receive);
  82. }else{
  83. collect1=MaterialType.selectDataBy(id1,users.getUserName(),"","",receive);
  84. }
  85. }else{
  86. if("admin".equals(users.getTrueName())){
  87. collect1=MaterialType.selectDataBy(id1,"","","",receive);
  88. }else {
  89. collect1 = MaterialType.selectDataBy(id1, users.getUserName(), "", "", receive);
  90. }
  91. }
  92. }
  93. model.addAttribute("listcg",collect1);
  94. //List<cgmaterial> collect1 = MaterialType.selectDataBy(id,storeCode,split1[0],split1[1],receive);
  95. Predicate<cgmaterial> predicate = a->{
  96. if(!"admin".equals(users.getTrueName())){
  97. if(a.getUsername().equals(users.getTrueName())){
  98. return true;
  99. }else{
  100. return false;
  101. }
  102. }else {
  103. return true;
  104. }
  105. };
  106. Predicate<cgmaterial> predicate2=a->{
  107. if(createtime.length()>0){
  108. String[] split = createtime.split(" - ");
  109. String strDateFormat = "yyyy-MM-dd HH:mm:ss";
  110. SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
  111. if(sdf.format(a.getCreatetime()).compareTo(split[0])>0&&sdf.format(a.getCreatetime()).compareTo(split[1])<0){
  112. return true;
  113. }else{
  114. return false;
  115. }
  116. }else{
  117. return true;
  118. }
  119. };
  120. /* Predicate<cgmaterial> predicate3=a->{
  121. if(receive.length()>0){
  122. if(a.getLy().equals(receive)){
  123. return true;
  124. }else{
  125. return false;
  126. }
  127. }else {
  128. return true;
  129. }
  130. };*/
  131. Predicate<cgmaterial> predicate4=a->{
  132. if(storeCode.length()>0){
  133. if(a.getStore().equals(storeCode)){
  134. return true;
  135. }else{
  136. return false;
  137. }
  138. }else {
  139. return true;
  140. }
  141. };
  142. Predicate<cgmaterial> predicate5=a->{
  143. if(id.length()>0){
  144. String replace = id.replace(",", "");
  145. if(("S"+a.getId()).equals(replace)){
  146. return true;
  147. }else{
  148. return false;
  149. }
  150. }else {
  151. return true;
  152. }
  153. };
  154. /* List<cgmaterial> collect = MaterialType.selectData().stream()
  155. .filter(predicate)
  156. .filter(predicate2)
  157. .filter(predicate3)
  158. .filter(predicate4)
  159. .filter(predicate5)
  160. .collect(Collectors.toList());*/
  161. return "mypackage/selectcg";
  162. }
  163. @RequestMapping("input")
  164. public String input(){
  165. return "mypackage/input";
  166. }
  167. @RequestMapping("test")
  168. public String test(){
  169. return "mypackage/test";
  170. }
  171. @RequestMapping("insert")
  172. public String insert(@RequestParam("list")String userList,HttpSession session){
  173. //List<requestDate> requestDates = JSON.parseArray(userList, requestDate.class);
  174. List<cgmaterial> cgmaterial = JSON.parseArray(userList, cgmaterial.class);
  175. Tuser user= (Tuser) session.getAttribute("currentUser");
  176. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
  177. String s=user.getId()+""+df.format(new Date());
  178. Consumer<cgmaterial> consumer= a->MaterialType.insertCg(a);
  179. Predicate<cgmaterial> predicate=a->{
  180. if(a.getMaterialcode()!= null&&a.getWz()!=null&&a.getLy()!=null){
  181. return true;
  182. }else{
  183. return false;
  184. }
  185. };
  186. cgmaterial.stream().filter(a->a.getMaterialcode()!= null&&a.getWz()!=null&&a.getLy()!=null).map(
  187. a->{
  188. a.setId(Long.parseLong(s));
  189. a.setUsername(user.getTrueName());
  190. a.setCreatetime(new Date());
  191. a.setStore(user.getUserName());
  192. return a;
  193. }
  194. ).collect(Collectors.toList()).forEach(consumer);
  195. return "mypackage/test";
  196. }
  197. /**
  198. * 用户登录请求
  199. * @param user
  200. * @return
  201. */
  202. @ResponseBody
  203. @PostMapping("/login")
  204. public Map<String,Object> login(String imageCode, @Valid Tuser user, BindingResult bindingResult, HttpSession session){
  205. Map<String,Object> map=new HashMap<String,Object>();
  206. if(StringUtils.isEmpty(imageCode)){
  207. map.put("success", false);
  208. map.put("errorInfo", "请输入验证码!");
  209. return map;
  210. }
  211. if(!session.getAttribute("checkcode").equals(imageCode)){
  212. map.put("success", false);
  213. map.put("errorInfo", "验证码输入错误!");
  214. return map;
  215. }
  216. if(bindingResult.hasErrors()){
  217. map.put("success", false);
  218. map.put("errorInfo", bindingResult.getFieldError().getDefaultMessage());
  219. return map;
  220. }
  221. Subject subject=SecurityUtils.getSubject();
  222. UsernamePasswordToken token=new UsernamePasswordToken(user.getUserName(), user.getPassword());
  223. try{
  224. subject.login(token); // 登录认证
  225. String userName=(String) SecurityUtils.getSubject().getPrincipal();
  226. //Tuser currentUser=tuserService.findByUserName(userName);
  227. Example tuserExample=new Example(Tuser.class);
  228. tuserExample.or().andEqualTo("userName",userName);
  229. Tuser currentUser=tuserService.selectByExample(tuserExample).get(0);
  230. session.setAttribute("currentUser", currentUser);
  231. //List<Trole> roleList=troleService.findByUserId(currentUser.getId());
  232. List<Trole> roleList=troleService.selectRolesByUserId(currentUser.getId());
  233. map.put("roleList", roleList);
  234. map.put("roleSize", roleList.size());
  235. map.put("success", true);
  236. //logService.save(new Log(Log.LOGIN_ACTION,"用户登录")); // 写入日志
  237. return map;
  238. }catch(Exception e){
  239. e.printStackTrace();
  240. map.put("success", false);
  241. map.put("errorInfo", "用户名或者密码错误!");
  242. return map;
  243. }
  244. }
  245. /**
  246. * 保存角色信息
  247. * @param roleId
  248. * @param session
  249. * @return
  250. * @throws Exception
  251. */
  252. @ResponseBody
  253. @PostMapping("/saveRole")
  254. public Map<String,Object> saveRole(Integer roleId,HttpSession session)throws Exception{
  255. Map<String,Object> map=new HashMap<String,Object>();
  256. Trole currentRole=troleService.selectByKey(roleId);
  257. session.setAttribute("currentRole", currentRole); // 保存当前角色信息
  258. putTmenuOneClassListIntoSession(session);
  259. map.put("success", true);
  260. return map;
  261. }
  262. /**
  263. * 安全退出
  264. *
  265. * @return
  266. * @throws Exception
  267. */
  268. @GetMapping("/logout")
  269. public String logout() throws Exception {
  270. // logService.save(new Log(Log.LOGOUT_ACTION,"用户注销"));
  271. SecurityUtils.getSubject().logout();
  272. return "redirect:/tologin";
  273. }
  274. /**
  275. * 加载权限菜单
  276. * @param session
  277. * @return
  278. * @throws Exception
  279. * 这里传入的parentId是1
  280. */
  281. @ResponseBody
  282. @GetMapping("/loadMenuInfo")
  283. public String loadMenuInfo(HttpSession session, Integer parentId)throws Exception{
  284. putTmenuOneClassListIntoSession(session);
  285. Trole currentRole=(Trole) session.getAttribute("currentRole");
  286. //根据当前用户的角色id和父节点id查询所有菜单及子集json
  287. String json=getAllMenuByParentId(parentId,currentRole.getId()).toString();
  288. //System.out.println(json);
  289. return json;
  290. }
  291. /**
  292. * 获取根频道所有菜单信息
  293. * @param parentId
  294. * @param roleId
  295. * @return
  296. */
  297. private JsonObject getAllMenuByParentId(Integer parentId,Integer roleId){
  298. JsonObject resultObject=new JsonObject();
  299. JsonArray jsonArray=this.getMenuByParentId(parentId, roleId);//得到所有一级菜单
  300. for(int i=0;i<jsonArray.size();i++){
  301. JsonObject jsonObject=(JsonObject) jsonArray.get(i);
  302. //判断该节点下时候还有子节点
  303. Example example=new Example(Tmenu.class);
  304. example.or().andEqualTo("pId",jsonObject.get("id").getAsString());
  305. //if("true".equals(jsonObject.get("spread").getAsString())){
  306. if (tmenuService.selectCountByExample(example)==0) {
  307. continue;
  308. }else{
  309. //由于后台模板的规定,一级菜单以title最为json的key
  310. resultObject.add(jsonObject.get("title").getAsString(), getAllMenuJsonArrayByParentId(jsonObject.get("id").getAsInt(),roleId));
  311. }
  312. }
  313. return resultObject;
  314. }
  315. //获取根频道下子频道菜单列表集合
  316. private JsonArray getAllMenuJsonArrayByParentId(Integer parentId,Integer roleId){
  317. JsonArray jsonArray=this.getMenuByParentId(parentId, roleId);
  318. for(int i=0;i<jsonArray.size();i++){
  319. JsonObject jsonObject=(JsonObject) jsonArray.get(i);
  320. //判断该节点下是否还有子节点
  321. Example example=new Example(Tmenu.class);
  322. example.or().andEqualTo("pId",jsonObject.get("id").getAsString());
  323. //if("true".equals(jsonObject.get("spread").getAsString())){
  324. if (tmenuService.selectCountByExample(example)==0) {
  325. continue;
  326. }else{
  327. //二级或三级菜单
  328. jsonObject.add("children", getAllMenuJsonArrayByParentId(jsonObject.get("id").getAsInt(),roleId));
  329. }
  330. }
  331. return jsonArray;
  332. }
  333. /**
  334. * 根据父节点和用户角色id查询菜单
  335. * @param parentId
  336. * @param roleId
  337. * @return
  338. */
  339. private JsonArray getMenuByParentId(Integer parentId,Integer roleId){
  340. //List<Menu> menuList=menuService.findByParentIdAndRoleId(parentId, roleId);
  341. HashMap<String,Object> paraMap=new HashMap<String,Object>();
  342. paraMap.put("pid",parentId);
  343. paraMap.put("roleid",roleId);
  344. List<Tmenu> menuList=tmenuService.selectByParentIdAndRoleId(paraMap);
  345. JsonArray jsonArray=new JsonArray();
  346. for(Tmenu menu:menuList){
  347. JsonObject jsonObject=new JsonObject();
  348. jsonObject.addProperty("id", menu.getId()); // 节点id
  349. jsonObject.addProperty("title", menu.getName()); // 节点名称
  350. jsonObject.addProperty("spread", false); // 不展开
  351. jsonObject.addProperty("icon", menu.getIcon());
  352. if(StringUtils.isNotEmpty(menu.getUrl())){
  353. jsonObject.addProperty("href", menu.getUrl()); // 菜单请求地址
  354. }
  355. jsonArray.add(jsonObject);
  356. }
  357. return jsonArray;
  358. }
  359. public void putTmenuOneClassListIntoSession(HttpSession session){
  360. //用来在welcome.ftl中获取主菜单列表
  361. Example example=new Example(Tmenu.class);
  362. example.or().andEqualTo("pId",1);
  363. List<Tmenu> tmenuOneClassList=tmenuService.selectByExample(example);
  364. session.setAttribute("tmenuOneClassList", tmenuOneClassList);
  365. }
  366. }