SysController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. package com.iamberry.rst.controllers.sys;
  2. import com.iamberry.app.tool.des.MD5;
  3. import com.iamberry.rst.core.cm.CompanyInfo;
  4. import com.iamberry.rst.core.cm.StoreInfo;
  5. import com.iamberry.rst.core.order.OrderDepart;
  6. import com.iamberry.rst.core.page.PageRequest;
  7. import com.iamberry.rst.core.page.PagedResult;
  8. import com.iamberry.rst.core.sys.*;
  9. import com.iamberry.rst.faces.cm.CompanyInfoService;
  10. import com.iamberry.rst.faces.cm.StoreInfoService;
  11. import com.iamberry.rst.faces.order.OrderDepartService;
  12. import com.iamberry.rst.faces.sys.DepartService;
  13. import com.iamberry.rst.faces.sys.SysService;
  14. import com.iamberry.rst.utils.AdminUtils;
  15. import com.iamberry.rst.utils.StaticModelUtil;
  16. import com.iamberry.wechat.autiXSS.XSSHandler;
  17. import com.iamberry.wechat.tools.ResponseJson;
  18. import com.iamberry.wechat.tools.StrUtils;
  19. import com.iamberry.wechat.tools.ValidationResult;
  20. import com.iamberry.wechat.tools.ValidationUtils;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.apache.shiro.SecurityUtils;
  23. import org.apache.shiro.authz.annotation.Logical;
  24. import org.apache.shiro.authz.annotation.RequiresPermissions;
  25. import org.apache.shiro.authz.annotation.RequiresRoles;
  26. import org.apache.shiro.authz.annotation.RequiresUser;
  27. import org.apache.shiro.subject.Subject;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.stereotype.Controller;
  32. import org.springframework.web.bind.annotation.PathVariable;
  33. import org.springframework.web.bind.annotation.RequestMapping;
  34. import org.springframework.web.bind.annotation.RequestParam;
  35. import org.springframework.web.bind.annotation.ResponseBody;
  36. import org.springframework.web.servlet.ModelAndView;
  37. import javax.servlet.http.HttpServletRequest;
  38. import java.lang.reflect.InvocationTargetException;
  39. import java.util.ArrayList;
  40. import java.util.List;
  41. import java.util.Random;
  42. /**
  43. * @author 献
  44. *
  45. * @company 深圳爱贝源科技有限公司
  46. * @website www.iamberry.com
  47. * @tel 18271840547
  48. * @date 2017/5/15
  49. */
  50. @Controller
  51. @RequestMapping("/admin/sys")
  52. public class SysController {
  53. @Autowired
  54. private SysService sysService;
  55. @Autowired
  56. private DepartService departService;
  57. @Autowired
  58. private CompanyInfoService companyInfoService;
  59. @Autowired
  60. private StoreInfoService storeInfoService;
  61. @Autowired
  62. private OrderDepartService orderDepartService;
  63. private static Logger logger = LoggerFactory.getLogger(SysController.class);
  64. private Dept dept = new Dept();
  65. private StrUtils strUtils = new StrUtils();
  66. private MD5 md5;
  67. /**
  68. * 后台首页,自动判断来源
  69. * @param request
  70. * @return
  71. */
  72. @RequiresUser
  73. @RequestMapping("/_index")
  74. public ModelAndView indexUI(HttpServletRequest request) {
  75. Subject currentUser = SecurityUtils.getSubject();
  76. Admin admin = (Admin) currentUser.getPrincipal();
  77. Depart depart = departService.getDepartById(admin.getAdminDept());
  78. // 判断来源
  79. String userAgent = request.getHeader("User-Agent").toLowerCase();
  80. /* if (userAgent.contains("ipad") || userAgent.contains("iphone os") || userAgent.contains("android")) {
  81. // 来自手机端
  82. logger.info("用户{}来源于手机端", currentUser.getPrincipal());
  83. // 传递部门信息
  84. return new ModelAndView("wap/wap_index")
  85. .addObject("dept_name", depart.getDepartName())
  86. .addObject("dept_role", admin.getAdminManager() == 2);
  87. }*/
  88. logger.info("用户{}来源于PC端", currentUser.getPrincipal());
  89. // 注入数据:管理员名称、管理员部门
  90. String dept = depart.getDepartName();
  91. List<Menu> list = sysService.listMenu(AdminUtils.getLoginAdminId());
  92. return new ModelAndView("home/pc_index")
  93. .addObject("adminName", admin.getAdminName())
  94. .addObject("deptName", dept)
  95. .addObject("menus", sysService.listMenu(AdminUtils.getLoginAdminId()));
  96. }
  97. /**
  98. * 查看我的信息
  99. * @return
  100. */
  101. @ResponseBody
  102. @RequestMapping("/get/my_info")
  103. public ResponseJson getMyInfo() {
  104. // 获取当前用户信息
  105. Integer adminId = AdminUtils.getLoginAdminId();
  106. Admin temp = new Admin();
  107. temp.setAdminId(adminId);
  108. // 查询数据 & 清空敏感数据
  109. Admin admin = sysService.get(temp);
  110. admin.setAdminId(null);
  111. admin.setAdminPassword(null);
  112. admin.setAdminSalt(null);
  113. admin.setAdminTel(StrUtils.phoneFormat(admin.getAdminTel()));
  114. return new ResponseJson(200, "SUCCESS", 200).addResponseKeyValue("admin", admin);
  115. }
  116. /**
  117. * 进入修改密码页面
  118. * @return
  119. */
  120. @RequiresPermissions("sys:editMyPwd")
  121. @RequestMapping("/edit/_my_pwd")
  122. public ModelAndView editMyPasswordUI() {
  123. return new ModelAndView("sys/edit_pwd");
  124. }
  125. /**
  126. * 执行修改密码
  127. * @param pwd
  128. * @return
  129. */
  130. @RequiresPermissions("sys:editMyPwd")
  131. @RequestMapping("/edit/my_pwd")
  132. @ResponseBody
  133. public ResponseJson editMyPassword(@RequestParam("pwd") String pwd) {
  134. // 获取当前用户信息
  135. Integer adminId = AdminUtils.getLoginAdminId();
  136. Admin admin = new Admin();
  137. admin.setAdminPassword(pwd);
  138. admin.setAdminId(adminId);
  139. Integer res = sysService.editAdmin(admin);
  140. if (res == 1) {
  141. return new ResponseJson(200, "SUCCESS", 200);
  142. }
  143. return new ResponseJson(200, "SUCCESS", 400);
  144. }
  145. /**
  146. * 编辑自己的用户信息页面
  147. * @return
  148. */
  149. @RequiresPermissions("sys:editMyInfo")
  150. @RequestMapping("/_edit_myinfo")
  151. public ModelAndView editMeInfoUI() {
  152. // 获取当前用户信息
  153. Integer adminId = AdminUtils.getLoginAdminId();
  154. Admin temp = new Admin();
  155. temp.setAdminId(adminId);
  156. Admin admin = sysService.get(temp);
  157. // 返回用户信息
  158. ModelAndView mv = new ModelAndView("admin/edit");
  159. mv.addObject("admin", admin);
  160. return mv;
  161. }
  162. /**
  163. * 编辑我的信息
  164. * @param admin
  165. * @return
  166. */
  167. @ResponseBody
  168. @RequestMapping("/edit_myinfo")
  169. @RequiresPermissions("sys:editMyinfo")
  170. public ResponseJson editMeInfo(Admin admin) {
  171. // 校验
  172. ValidationResult result = ValidationUtils.validateEntity(admin);
  173. if (result.isHasErrors()) {
  174. return new ResponseJson(200, "VALIDATION_ERROR", 500).addResponseKeyValue(result.getErrorMsgList().get(0));
  175. }
  176. // 获取当前用户信息
  177. Integer adminId = AdminUtils.getLoginAdminId();
  178. admin.setAdminId(adminId);
  179. Integer res = sysService.editAdmin(admin);
  180. if (res == 1) {
  181. return new ResponseJson(200, "SUCCESS", 200);
  182. }
  183. return new ResponseJson(200, "EDIT_ERROR", 400);
  184. }
  185. @RequiresRoles(value = {"ROOT", "MANAGER"}, logical = Logical.OR)
  186. @RequiresPermissions("sys:listAdmin")
  187. @RequestMapping("/_list_admin")
  188. public ModelAndView listAdminUI(Admin admin, HttpServletRequest request,
  189. @RequestParam(value = "pageNO", defaultValue = "1", required = false) int pageNO,
  190. @RequestParam(value = "pageTotal", required = false) Integer pageTotal,
  191. @RequestParam(value = "pageSize", defaultValue = "20", required = false) int pageSize) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
  192. // 如果是ROOT,查询所有,否则查询部门信息
  193. Integer managerDept = AdminUtils.getLoginAdmin().getAdminDept();
  194. if (managerDept != 1) {
  195. admin.setAdminDept(managerDept);
  196. }
  197. // 封装请求数据
  198. PageRequest<Admin> pageRequest = new PageRequest<>(admin, pageNO, pageSize, pageTotal == null);
  199. // 查询
  200. PagedResult<Admin> result = sysService.listAdmin(pageRequest);
  201. if (pageTotal != null) {
  202. result.setPages(pageTotal);
  203. }
  204. // 返回
  205. ModelAndView mv = new ModelAndView("sys/list_admin");
  206. mv.addObject("page", result);
  207. mv.addObject("url", request.getRequestURI().replace(request.getContextPath(), "") +
  208. "?pageTotal=" + result.getPages() + "&pageSize=" + pageSize +
  209. "&adminName=" + (admin.getAdminName() == null ? "" : admin.getAdminName()) +
  210. "&adminSex=" + (admin.getAdminSex() == null ? "" : admin.getAdminSex()) +
  211. "&adminTel=" + (admin.getAdminTel() == null ? "" : admin.getAdminTel()) +
  212. "&adminMail=" + (admin.getAdminMail() == null ? "" : admin.getAdminMail()) +
  213. "&pageNO=");
  214. // 向页面传递数据,注意防止XSS
  215. XSSHandler.escapeObject(admin, new String[] {"adminName", "adminSex", "adminTel", "adminMail"});
  216. mv.addObject("admin", admin);
  217. mv.addObject("deptUtil", StaticModelUtil.useStaticPacker(Dept.class.getName()));
  218. mv.addObject("strUtil", StaticModelUtil.useStaticPacker(StrUtils.class.getName()));
  219. return mv;
  220. }
  221. @ResponseBody
  222. @RequestMapping("/edit_status")
  223. @RequiresPermissions("sys:editStatus")
  224. @RequiresRoles(value = {"ROOT", "MANAGER"}, logical = Logical.OR)
  225. public ResponseJson editStatus(@RequestParam("adminId") Integer adminId,
  226. @RequestParam("status") Integer status) {
  227. Admin admin = new Admin();
  228. admin.setAdminId(adminId);
  229. admin.setAdminStatus(status);
  230. Integer managerDept = AdminUtils.getLoginAdmin().getAdminDept();
  231. if (managerDept != 1) {
  232. admin.setAdminDept(managerDept);
  233. }
  234. Integer res = sysService.editAdmin(admin);
  235. if (res == null || res <= 0) {
  236. return new ResponseJson(200, "SUCCESS", 400);
  237. }
  238. return new ResponseJson(200, "SUCCESS", 200);
  239. }
  240. /**
  241. * 超级管理员 前往 编辑其他用户的权限
  242. * @return
  243. */
  244. @RequiresRoles(value = {"ROOT", "MANAGER"}, logical = Logical.OR)
  245. @RequiresPermissions("sys:editPermission")
  246. @RequestMapping("/{adminId}/_edit_permission")
  247. public ModelAndView editPermissionUI(@PathVariable("adminId") Integer adminId) {
  248. List<Menu> menus = sysService.listPermissions(adminId);
  249. Integer[] storeIds = orderDepartService.getDepartStoreIds(adminId);
  250. List<CompanyInfo> companyInfoList = companyInfoService.listCompanyInfo(new CompanyInfo());
  251. for (CompanyInfo companyInfo : companyInfoList) {
  252. StoreInfo storeInfo = new StoreInfo();
  253. storeInfo.setCompanyId(companyInfo.getCompanyId());
  254. List<StoreInfo> storeInfoList = storeInfoService.listStore(storeInfo);
  255. if(storeIds.length > 0){
  256. for (StoreInfo si : storeInfoList) {
  257. for (int i = 0; i < storeIds.length; i++) {
  258. if(si.getStoreId() == storeIds[i]){
  259. si.setIsSelectDepart(1); // 1:选中 2:未选中
  260. }
  261. }
  262. }
  263. }
  264. companyInfo.setStoreInfoList(storeInfoList);
  265. }
  266. return new ModelAndView("sys/edit_permission")
  267. .addObject("companyInfoList", companyInfoList)
  268. .addObject("list", menus)
  269. .addObject("adminId", adminId);
  270. }
  271. @RequiresRoles(value = {"ROOT", "MANAGER"}, logical = Logical.OR)
  272. @RequiresPermissions("sys:editPermission")
  273. @RequestMapping("/edit_permission")
  274. @ResponseBody
  275. public ResponseJson editPermission(@RequestParam("adminId") Integer adminId,
  276. @RequestParam("perms[]") String [] perms,
  277. @RequestParam("companyIds") String companyIds,
  278. @RequestParam("storeIds") String storeIds) {
  279. // perms["一级菜单:二级菜单:权限id:权限操作符", "一级菜单:二级菜单:权限id:权限操作符"]
  280. List<AdminPocess> pocesses = new ArrayList<>();
  281. for (String perm : perms) {
  282. String[] temp = StringUtils.split(perm, "-");
  283. if (temp.length != 4) {
  284. continue;
  285. }
  286. Integer menuId = Integer.parseInt(temp[0]);
  287. Integer subMenuId = Integer.parseInt(temp[1]);
  288. Integer permissionId = Integer.parseInt(temp[2]);
  289. AdminPocess pocess = new AdminPocess();
  290. pocess.setPocessAdminId(adminId);
  291. pocess.setPocessMenuId(menuId);
  292. pocess.setPocessSubId(subMenuId);
  293. pocess.setPocessSymbol(temp[3]);
  294. pocess.setPocessPermId(permissionId);
  295. pocesses.add(pocess);
  296. }
  297. int res = 0;
  298. try {
  299. res = sysService.editAdminPermission(pocesses, adminId);
  300. } catch (Exception e) {
  301. logger.info("", e);
  302. }
  303. List<OrderDepart> orderDepartList = new ArrayList<>();
  304. if(companyIds != null && !"".equals(companyIds)){
  305. String[] companyArray = companyIds.split(",");
  306. for (String company : companyArray) {
  307. OrderDepart orderDepart = new OrderDepart();
  308. orderDepart.setAdminId(adminId);
  309. orderDepart.setCompanyId(Integer.valueOf(company));
  310. orderDepart.setOrderDepartType(1);
  311. orderDepartList.add(orderDepart);
  312. }
  313. }
  314. if(storeIds != null && !"".equals(storeIds)){
  315. String[] storeArray = storeIds.split(",");
  316. for (String store : storeArray) {
  317. OrderDepart orderDepart = new OrderDepart();
  318. orderDepart.setAdminId(adminId);
  319. orderDepart.setStoreId(Integer.valueOf(store));
  320. orderDepart.setOrderDepartType(2);
  321. orderDepartList.add(orderDepart);
  322. }
  323. }
  324. if(orderDepartList.size() > 0){
  325. orderDepartService.update(orderDepartList,adminId);
  326. }
  327. if (res == 0) {
  328. return new ResponseJson(200, "ERROR", 400);
  329. }
  330. return new ResponseJson(200, "SUCCESS", 200);
  331. }
  332. /**
  333. * 判断是否是业务经理(有审核权限,即是否是小曾)
  334. * @date 2017年6月2日
  335. * @return
  336. */
  337. @ResponseBody
  338. @RequestMapping("/is_check_admin")
  339. public ResponseJson isCheckManager(){
  340. ResponseJson rj = new ResponseJson();
  341. rj.setResultCode(200);
  342. rj.setResultMsg("SUCCESS");
  343. Integer loginAdminId = AdminUtils.getLoginAdminId();
  344. if(loginAdminId == null || loginAdminId != 8){
  345. rj.setReturnCode(400);
  346. rj.addResponseKeyValue("该用户不是具有审核权限的管理员!");
  347. return rj;
  348. }
  349. rj.setReturnCode(200);
  350. return rj;
  351. }
  352. /**
  353. * 进入添加管理员页面
  354. * @param request
  355. * @return
  356. * @throws Exception
  357. */
  358. @RequiresPermissions("sys:add:admin")
  359. @RequestMapping("/_add_admin")
  360. public ModelAndView goAddAdmin(HttpServletRequest request) throws Exception {
  361. ModelAndView mv = new ModelAndView("sys/add_admin");
  362. Depart depart = new Depart();
  363. depart.setDepartStatus(1);
  364. List<Depart> departList = departService.getDepartList(depart);
  365. mv.addObject("departList",departList);
  366. return mv;
  367. }
  368. /**
  369. * 添加管理员信息
  370. * @param admin
  371. * @param request
  372. * @return
  373. * @throws Exception
  374. */
  375. @ResponseBody
  376. @RequiresPermissions("sys:add:admin")
  377. @RequestMapping("/add_admin_info")
  378. public ResponseJson addAdminInfo(Admin admin,HttpServletRequest request) throws Exception {
  379. md5 = new MD5("inlongadMD5");
  380. StringBuilder salt = new StringBuilder();
  381. String adminSalt = "";
  382. if (admin.getAdminAccount() == null || admin.getAdminPassword() == null) {
  383. return new ResponseJson(200, "账号和密码不能为空", 500);
  384. }
  385. StringBuilder passWord = new StringBuilder(admin.getAdminPassword());
  386. Random rd = new Random();
  387. for (int i = 0;i < 10;i++) {
  388. salt.append(String.valueOf(rd.nextInt(10)));
  389. }
  390. int adminId = AdminUtils.getLoginAdminId();
  391. Admin adminInfo = new Admin();
  392. adminInfo.setAdminId(adminId);
  393. //查询登录人信息
  394. adminInfo = sysService.get(adminInfo);
  395. if (adminInfo == null) {
  396. return new ResponseJson(200, "账号信息有误,请重新登录", 500);
  397. }
  398. adminSalt = md5.stringToMD5(salt.toString());
  399. admin.setAdminSalt(adminSalt);
  400. admin.setAdminSaleNum(0);
  401. admin.setAdminStatus(1);
  402. passWord.append(adminSalt);
  403. Admin admin1 = new Admin();
  404. admin1.setAdminAccount(admin.getAdminAccount());
  405. admin.setAdminPassword(md5.stringToMD5(passWord.toString()));
  406. List<Admin> adminList = sysService.listGetAdmin(admin1);
  407. if (adminList != null && adminList.size() > 0) {
  408. return new ResponseJson(200, "该账号密码已存在,请重新输入", 500);
  409. }
  410. int num = sysService.saveAdmin(admin);
  411. if (num > 0) {
  412. return new ResponseJson(200, "恭喜您,添加成功!", 200);
  413. } else {
  414. return new ResponseJson(200, "添加失败", 500);
  415. }
  416. }
  417. }