SysModController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package cn.aiyangniu.api.controller.system;
  2. import cn.aiyangniu.api.common.entity.system.AuthEntity;
  3. import cn.aiyangniu.api.common.entity.system.SysModEntity;
  4. import cn.aiyangniu.api.common.entity.system.SysModVo;
  5. import cn.aiyangniu.api.service.system.SysModService;
  6. import cn.aiyangniu.api.common.util.AuthUtil;
  7. import cn.aiyangniu.api.common.util.CharacterFiltUtil;
  8. import cn.aiyangniu.api.common.util.RandomUtil;
  9. import com.alibaba.fastjson.JSONObject;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiImplicitParam;
  12. import io.swagger.annotations.ApiImplicitParams;
  13. import io.swagger.annotations.ApiOperation;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.web.bind.annotation.RequestBody;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RequestMethod;
  18. import org.springframework.web.bind.annotation.RestController;
  19. import javax.annotation.Resource;
  20. import javax.servlet.http.HttpServletRequest;
  21. import java.util.*;
  22. /**
  23. * 模块业务接口类
  24. *
  25. * @author Henry Hall
  26. * @since 2020-08-08
  27. */
  28. @Slf4j
  29. @RestController
  30. @Api(tags="系统模块接口")
  31. @RequestMapping("/sysModule")
  32. public class SysModController {
  33. @Resource
  34. private SysModService modService;
  35. @Resource
  36. private RandomUtil randomUtil;
  37. @Resource
  38. private AuthUtil authUtil;
  39. /**
  40. * saveMod 新增/修改模块
  41. *
  42. * @param req 请求对象
  43. * @return 返回结果Json串
  44. */
  45. @ApiOperation(value = "新增/修改模块信息")
  46. @RequestMapping(value="/save", method= RequestMethod.POST)
  47. @ApiImplicitParam(name = "modEntity", value = "模块信息实体对象", paramType = "body", dataType="SysModEntity", dataTypeClass = SysModEntity.class, required = true)
  48. public Map<String, Object> saveMod(@RequestBody SysModEntity modEntity, HttpServletRequest req) {
  49. Map<String, Object> result = new HashMap<>();
  50. String retCode, retMsg, sysRandom, token, reqRandom, userId, modId, modName, code, parentId, logo, url;
  51. token = req.getHeader("Authorization");
  52. if(token == null || "null".equals(token)) {
  53. retCode = "1002";
  54. retMsg = "对不起,您的操作非法,请登录!";
  55. } else {
  56. AuthEntity auth = authUtil.getUserId(token);
  57. if(auth == null || !auth.isSuccess()) {
  58. retCode = "1002";
  59. retMsg = "对不起,您没有登录或会话超时,请重新登录!";
  60. } else {
  61. userId = auth.getUserId();
  62. sysRandom = randomUtil.getSysRandom(userId);
  63. modId = modEntity.getModId();
  64. modName = modEntity.getModName();
  65. code = modEntity.getModCode();
  66. parentId = modEntity.getParentId();
  67. logo = modEntity.getLogo();
  68. url = modEntity.getUrl();
  69. reqRandom = modEntity.getReqRandom();
  70. modId = CharacterFiltUtil.inputFilter(modId, true);
  71. modName = CharacterFiltUtil.inputFilter(modName, true);
  72. code = CharacterFiltUtil.inputFilter(code, true);
  73. parentId = CharacterFiltUtil.inputFilter(parentId, true);
  74. logo = CharacterFiltUtil.inputFilter(logo, true);
  75. url = CharacterFiltUtil.inputFilter(url, true);
  76. reqRandom = CharacterFiltUtil.inputFilter(reqRandom, true);
  77. if("".equals(modName) || "".equals(code) || "0".equals(parentId) || "".equals(reqRandom)) {
  78. retCode = "100110020101";
  79. retMsg = "对不起,您输入的信息有为空的必填项,请检查!";
  80. } else if(modName.length() > 30 || code.length() > 10 || logo.length() > 30 || url.length() > 60 || parentId.length() != 36) {
  81. retCode = "100110020102";
  82. retMsg = "对不起,您输入的信息有超出字数限制的项,请检查!";
  83. } else if(RandomUtil.verifySysRandom(reqRandom, sysRandom)) {
  84. retCode = "100110020103";
  85. retMsg = "对不起,您已经提交过了,请不要重复提交!";
  86. } else {
  87. randomUtil.setSysRandom(userId);
  88. modEntity.setModName(modName);
  89. modEntity.setModCode(code);
  90. modEntity.setParentId(parentId);
  91. modEntity.setUrl(url);
  92. modEntity.setLogo(logo);
  93. modEntity.setOptUser(userId);
  94. int r;
  95. if("".equals(modId)) {
  96. r = modService.addMod(modEntity);
  97. } else {
  98. modEntity.setModId(modId);
  99. r = modService.edtMod(modEntity);
  100. }
  101. if(r != 0) {
  102. retCode = "1001";
  103. retMsg = "恭喜您,模块保存成功!";
  104. } else {
  105. retCode = "100110020104";
  106. retMsg = "对不起,系统错误,请联系系统管理员!";
  107. }
  108. }
  109. }
  110. }
  111. result.put("code", retCode);
  112. result.put("msg", retMsg);
  113. return result;
  114. }
  115. /**
  116. * deleteMod 删除模块
  117. *
  118. * @param req 请求对象
  119. * @return 返回结果Json串
  120. */
  121. @ApiOperation(value = "删除模块信息")
  122. @RequestMapping(value="/delete", method= RequestMethod.POST)
  123. @ApiImplicitParam(name = "vo", value = "删除模块的请求对象", paramType = "body", dataType="SysModVo", dataTypeClass = SysModVo.class, required = true)
  124. public Map<String, Object> deleteMod(@RequestBody SysModVo vo, HttpServletRequest req) {
  125. Map<String, Object> result = new HashMap<>();
  126. String retCode, retMsg, token;
  127. token = req.getHeader("Authorization");
  128. if(token == null || "null".equals(token)) {
  129. retCode = "1002";
  130. retMsg = "对不起,您的操作非法,请登录!";
  131. } else {
  132. AuthEntity auth = authUtil.getUserId(token);
  133. if(auth == null || !auth.isSuccess()) {
  134. retCode = "1002";
  135. retMsg = "对不起,您没有登录或会话超时,请重新登录!";
  136. } else {
  137. List<String> ids = vo.getModIds();
  138. if(ids==null || ids.size()==0) {
  139. retCode = "100110020201";
  140. retMsg = "对不起,您提交的信息有为空的必填项,请检查!";
  141. } else {
  142. int r = modService.delMods(auth.getUserId(), ids);
  143. if(r != 0) {
  144. retCode = "1001";
  145. retMsg = "恭喜您,模块删除成功!";
  146. } else {
  147. retCode = "100110020204";
  148. retMsg = "对不起,系统错误,请联系系统管理员!";
  149. }
  150. }
  151. }
  152. }
  153. result.put("code", retCode);
  154. result.put("msg", retMsg);
  155. return result;
  156. }
  157. /**
  158. * getMod 获取单一模块
  159. *
  160. * @param req 请求对象
  161. * @return 返回结果Json串
  162. */
  163. @ApiOperation(value = "获取模块详情")
  164. @RequestMapping(value="/getById", method= RequestMethod.GET)
  165. @ApiImplicitParam(name = "modId", value = "模块编号", paramType = "query", dataType="string", dataTypeClass = String.class, required = true)
  166. public Map<String, Object> getMod(String modId, HttpServletRequest req) {
  167. Map<String, Object> result = new HashMap<>();
  168. String retCode, retMsg, token;
  169. SysModEntity modEntity;
  170. token = req.getHeader("Authorization");
  171. if(token == null || "null".equals(token)) {
  172. retCode = "1002";
  173. retMsg = "对不起,您的操作非法,请登录!";
  174. } else {
  175. boolean auth = authUtil.hasLogin(token);
  176. if(!auth) {
  177. retCode = "1002";
  178. retMsg = "对不起,您没有登录或会话超时,请重新登录!";
  179. } else {
  180. modId = CharacterFiltUtil.inputFilter(modId, true);
  181. if("".equals(modId) || modId.length() != 36) {
  182. retCode = "100110020301";
  183. retMsg = "对不起,您请求的参数非法,请重试!";
  184. } else {
  185. modEntity = modService.getMod(modId);
  186. if(modEntity != null) {
  187. result.put("data", modEntity);
  188. retCode = "1001";
  189. retMsg = "查询成功。";
  190. } else {
  191. retCode = "100110020305";
  192. retMsg = "暂无内容";
  193. }
  194. }
  195. }
  196. }
  197. result.put("code", retCode);
  198. result.put("msg", retMsg);
  199. return result;
  200. }
  201. /**
  202. * list 列表显示模块
  203. *
  204. * @param req 请求对象
  205. * @return 返回结果Json串
  206. */
  207. @ApiOperation(value = "获取模块树状列表")
  208. @RequestMapping(value="/list", method= RequestMethod.GET)
  209. @ApiImplicitParam(name = "parentId", value = "父级模块编号", paramType = "query", dataType="string", dataTypeClass = String.class)
  210. public Map<String, Object> listMods(String parentId, HttpServletRequest req) {
  211. Map<String, Object> result = new HashMap<>();
  212. String retCode, retMsg, token;
  213. token = req.getHeader("Authorization");
  214. if(token == null || "null".equals(token)) {
  215. retCode = "1002";
  216. retMsg = "对不起,您的操作非法,请登录!";
  217. } else {
  218. boolean auth = authUtil.hasLogin(token);
  219. if(!auth) {
  220. retCode = "1002";
  221. retMsg = "对不起,您没有登录或会话超时,请重新登录!";
  222. } else {
  223. parentId = parentId==null || "".equals(parentId) ? "00000000-0000-0000-0000-000000000000" : parentId; // 一级模块的父级编号
  224. List<SysModEntity> modEntities = modService.listMods(parentId);
  225. if(modEntities != null && modEntities.size() > 0) {
  226. result.put("data", modEntities);
  227. retCode = "1001";
  228. retMsg = "查询成功。";
  229. } else {
  230. retCode = "100110020405";
  231. retMsg = "暂无内容";
  232. }
  233. }
  234. }
  235. result.put("code", retCode);
  236. result.put("msg", retMsg);
  237. return result;
  238. }
  239. /**
  240. * select 选择显示模块
  241. *
  242. * @param req 请求对象
  243. * @return 返回结果Json串
  244. */
  245. @ApiOperation(value = "获取模块下拉列表")
  246. @RequestMapping(value="/select", method= RequestMethod.GET)
  247. @ApiImplicitParams({
  248. @ApiImplicitParam(name = "kind", value = "种类,1为全部,2为当前用户所拥有的", paramType = "query", dataType="string", dataTypeClass = String.class),
  249. @ApiImplicitParam(name = "schName", value = "关键字", paramType = "query", dataType="string", dataTypeClass = String.class)
  250. })
  251. public Map<String, Object> selMods(String kind, String schName, HttpServletRequest req) {
  252. Map<String, Object> result = new HashMap<>();
  253. String retCode, retMsg, token, parentId;
  254. token = req.getHeader("Authorization");
  255. if(token == null || "null".equals(token)) {
  256. retCode = "1002";
  257. retMsg = "对不起,您的操作非法,请登录!";
  258. } else {
  259. JSONObject userSession = authUtil.getUserSession(token);
  260. if(userSession == null) {
  261. retCode = "1002";
  262. retMsg = "对不起,您没有登录或会话超时,请重新登录!";
  263. } else {
  264. kind = CharacterFiltUtil.inputFilter(kind, true);
  265. schName = CharacterFiltUtil.inputFilter(schName, true);
  266. String userMods = userSession.get("modCodes").toString();
  267. parentId = "00000000-0000-0000-0000-000000000000"; // 一级模块的父级编号
  268. List<SysModEntity> modEntities = modService.selMods(kind, parentId, schName, userMods);
  269. if(modEntities != null && modEntities.size() > 0) {
  270. result.put("data", modEntities);
  271. retCode = "1001";
  272. retMsg = "查询成功。";
  273. } else {
  274. retCode = "100110020505";
  275. retMsg = "暂无内容";
  276. }
  277. }
  278. }
  279. result.put("code", retCode);
  280. result.put("msg", retMsg);
  281. return result;
  282. }
  283. /**
  284. * sortMods 统计某模块下的子模块数量,用于自动排序
  285. */
  286. @ApiOperation(value = "获取模块数量")
  287. @RequestMapping(value="/sort", method= RequestMethod.GET)
  288. @ApiImplicitParam(name = "parentId", value = "父级模块编号", paramType = "query", dataType="string", dataTypeClass = String.class)
  289. private Map<String, Object> sortMods(String parentId, HttpServletRequest req) {
  290. Map<String, Object> result = new HashMap<>();
  291. String retCode, retMsg, token;
  292. token = req.getHeader("Authorization");
  293. if(token == null || "null".equals(token)) {
  294. retCode = "1002";
  295. retMsg = "对不起,您的操作非法,请登录!";
  296. } else {
  297. boolean auth = authUtil.hasLogin(token);
  298. if(!auth) {
  299. retCode = "1002";
  300. retMsg = "对不起,您没有登录或会话超时,请重新登录!";
  301. } else {
  302. parentId = CharacterFiltUtil.inputFilter(parentId, true);
  303. if("".equals(parentId) || parentId.length() != 36) {
  304. retCode = "100110020601";
  305. retMsg = "对不起,您请求的参数不正确,请重试!";
  306. } else {
  307. int modNum = modService.sortMods(parentId);
  308. result.put("data", modNum);
  309. retCode = "1001";
  310. retMsg = "查询成功。";
  311. }
  312. }
  313. }
  314. result.put("code", retCode);
  315. result.put("msg", retMsg);
  316. return result;
  317. }
  318. /**
  319. * getModBtns 获取模块及按钮信息
  320. *
  321. * @param req 请求对象
  322. * @return 返回结果Json串
  323. */
  324. @ApiOperation(value = "获取模块及数量")
  325. @RequestMapping(value="/getModBtns", method= RequestMethod.GET)
  326. public Map<String, Object> getModBtns(HttpServletRequest req) {
  327. Map<String, Object> result = new HashMap<>();
  328. String retCode, retMsg, token;
  329. token = req.getHeader("Authorization");
  330. if(token == null || "null".equals(token)) {
  331. retCode = "1002";
  332. retMsg = "对不起,您的操作非法,请登录!";
  333. } else {
  334. boolean auth = authUtil.hasLogin(token);
  335. if(!auth) {
  336. retCode = "1002";
  337. retMsg = "对不起,您没有登录或会话超时,请重新登录!";
  338. } else {
  339. List<SysModEntity> modEntities = modService.getModBtns();
  340. if(modEntities != null && modEntities.size() > 0) {
  341. result.put("data", modEntities);
  342. retCode = "1001";
  343. retMsg = "查询成功。";
  344. } else {
  345. retCode = "100110020905";
  346. retMsg = "暂无内容";
  347. }
  348. }
  349. }
  350. result.put("code", retCode);
  351. result.put("msg", retMsg);
  352. return result;
  353. }
  354. }