SystemController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. package jnpf.base.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import cn.dev33.satoken.annotation.SaMode;
  4. import cn.hutool.core.bean.BeanUtil;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import io.swagger.v3.oas.annotations.Operation;
  10. import io.swagger.v3.oas.annotations.Parameter;
  11. import io.swagger.v3.oas.annotations.Parameters;
  12. import io.swagger.v3.oas.annotations.tags.Tag;
  13. import jnpf.base.ActionResult;
  14. import jnpf.base.Pagination;
  15. import jnpf.base.UserInfo;
  16. import jnpf.base.entity.ModuleEntity;
  17. import jnpf.base.entity.SystemEntity;
  18. import jnpf.base.model.AppAuthorizationModel;
  19. import jnpf.base.model.AppAuthorizeVo;
  20. import jnpf.base.model.UserAuthorize;
  21. import jnpf.base.model.base.*;
  22. import jnpf.base.service.CommonWordsService;
  23. import jnpf.base.service.ModuleService;
  24. import jnpf.base.service.PortalManageService;
  25. import jnpf.base.service.SystemService;
  26. import jnpf.base.vo.ListVO;
  27. import jnpf.base.vo.PaginationVO;
  28. import jnpf.constant.JnpfConst;
  29. import jnpf.constant.MsgCode;
  30. import jnpf.constant.PermissionConst;
  31. import jnpf.constant.model.MCode;
  32. import jnpf.emnus.SysParamEnum;
  33. import jnpf.message.util.OnlineUserModel;
  34. import jnpf.message.util.OnlineUserProvider;
  35. import jnpf.permission.entity.RoleEntity;
  36. import jnpf.permission.entity.RoleRelationEntity;
  37. import jnpf.permission.entity.UserEntity;
  38. import jnpf.permission.model.authorize.AuthorizeVO;
  39. import jnpf.permission.model.user.vo.BaseInfoVo;
  40. import jnpf.permission.service.AuthorizeService;
  41. import jnpf.permission.service.RoleRelationService;
  42. import jnpf.permission.service.RoleService;
  43. import jnpf.permission.service.UserService;
  44. import jnpf.util.JsonUtil;
  45. import jnpf.util.StringUtil;
  46. import jnpf.util.UploaderUtil;
  47. import jnpf.util.UserProvider;
  48. import org.apache.commons.collections4.CollectionUtils;
  49. import org.jetbrains.annotations.NotNull;
  50. import org.springframework.beans.factory.annotation.Autowired;
  51. import org.springframework.web.bind.annotation.*;
  52. import java.util.*;
  53. import java.util.stream.Collectors;
  54. /**
  55. * 应用中心
  56. *
  57. * @author :JNPF开发平台组
  58. * @version: V3.1.0
  59. * @copyright 引迈信息技术有限公司
  60. * @date :2022/6/21 15:33
  61. */
  62. @Tag(name = "应用中心", description = "system")
  63. @RestController
  64. @RequestMapping("/api/system/System")
  65. public class SystemController extends SuperController<SystemService, SystemEntity> {
  66. @Autowired
  67. private RoleService roleService;
  68. @Autowired
  69. private RoleRelationService roleRelationService;
  70. @Autowired
  71. private UserService userService;
  72. @Autowired
  73. private SystemService systemService;
  74. @Autowired
  75. private CommonWordsService commonWordsService;
  76. @Autowired
  77. private PortalManageService portalManageService;
  78. @Autowired
  79. private ModuleService moduleService;
  80. @Autowired
  81. private AuthorizeService authorizeService;
  82. @Operation(summary = "应用列表")
  83. @SaCheckPermission(value = {"appCenter", "workFlow.flowQuickLaunch", "teamwork.printTemplate", "monitor.flowMonitor", "monitor.userOnline"}, mode = SaMode.OR)
  84. @GetMapping
  85. public ActionResult<ListVO<SystemListVO>> list(SystemPageVO page) {
  86. Boolean enabledMark = false;
  87. if (ObjectUtil.equal(page.getEnabledMark(), "0")) {
  88. enabledMark = null;
  89. }
  90. if (ObjectUtil.equal(page.getEnabledMark(), "1")) {
  91. enabledMark = true;
  92. }
  93. List<SystemEntity> list = systemService.getList(page.getKeyword(), enabledMark, true, true, true, new ArrayList<>());
  94. List<SystemListVO> jsonToList = JsonUtil.getJsonToList(list, SystemListVO.class);
  95. //有修改权限列表
  96. List<String> authList = systemService.getAuthListByUser(UserProvider.getUser().getUserId(), true).stream().map(SystemEntity::getId).collect(Collectors.toList());
  97. UserInfo user = UserProvider.getUser();
  98. for (SystemListVO vo : jsonToList) {
  99. vo.setHasDelete(user.getIsAdministrator() || user.getUserId().equals(vo.getUserId()) ? 1 : 0);
  100. vo.setHasUpdate(user.getIsAdministrator() || authList.contains(vo.getId()) ? 1 : 0);
  101. }
  102. return ActionResult.success(new ListVO<>(jsonToList));
  103. }
  104. @Operation(summary = "获取应用授权信息")
  105. @SaCheckPermission(value = {"appConfig.appAuth"})
  106. @GetMapping("/getAppAuthorization/{systemId}")
  107. public ActionResult<AppAuthorizeVo> getAppAuthorization(@PathVariable String systemId) {
  108. Pagination pagination = new Pagination();
  109. AppAuthorizeVo appAuthorizeVo = new AppAuthorizeVo();
  110. SystemEntity info = systemService.getInfo(systemId);
  111. if (BeanUtil.isEmpty(info)) {
  112. return ActionResult.fail(MsgCode.FA001.getDesc());
  113. }
  114. String userId = "";
  115. String authorizeId = "";
  116. List<BaseInfoVo> baseInfoVos = new ArrayList<>();
  117. if (null == info.getUserId() || info.getUserId().isEmpty()) {
  118. appAuthorizeVo.setDevUsers(new ArrayList<>());
  119. appAuthorizeVo.setIsAllDevUser(1);
  120. appAuthorizeVo.setCreateUserId(info.getCreatorUserId());
  121. return ActionResult.success(appAuthorizeVo);
  122. } else {
  123. userId = info.getUserId();
  124. List<String> strings = new ArrayList<>();
  125. strings.add(userId);
  126. baseInfoVos = getBaseInfoVos(pagination, strings);
  127. List<UserAuthorize> userAuthorizes = baseInfoVos.stream()
  128. .map(item -> BeanUtil.copyProperties(item, UserAuthorize.class)).collect(Collectors.toList());
  129. appAuthorizeVo.setCreateUserId(userAuthorizes.get(0).getId());
  130. appAuthorizeVo.setFullName(baseInfoVos.get(0).getFullName());
  131. }
  132. if (null == info.getAuthorizeId()) {
  133. appAuthorizeVo.setDevUsers(new ArrayList<>());
  134. appAuthorizeVo.setIsAllDevUser(0);
  135. return ActionResult.success(appAuthorizeVo);
  136. }
  137. authorizeId = info.getAuthorizeId();
  138. if (PermissionConst.ALL_DEV_USER.equals(authorizeId)) {
  139. List<String> collect = getUserIdListByRoleId();
  140. baseInfoVos = getBaseInfoVos(pagination, collect);
  141. appAuthorizeVo.setIsAllDevUser(1);
  142. } else {
  143. String[] split = authorizeId.split(",");
  144. List<String> collect = Arrays.stream(split).distinct().collect(Collectors.toList());
  145. baseInfoVos = getBaseInfoVos(pagination, collect);
  146. appAuthorizeVo.setIsAllDevUser(0);
  147. }
  148. List<UserAuthorize> userAuthorizes = baseInfoVos.stream()
  149. .map(item -> BeanUtil.copyProperties(item, UserAuthorize.class)).collect(Collectors.toList());
  150. appAuthorizeVo.setDevUsers(userAuthorizes.stream().map(UserAuthorize::getId).collect(Collectors.toList()));
  151. return ActionResult.success(appAuthorizeVo);
  152. }
  153. @Operation(summary = "保存应用授权信息")
  154. @SaCheckPermission(value = {"appConfig.appAuth"})
  155. @PostMapping("/saveAppAuthorization")
  156. public ActionResult<MCode> appAuthorization(@RequestBody AppAuthorizationModel model) {
  157. systemService.saveSystemAuthorizion(model);
  158. return ActionResult.success(MsgCode.SU005.get());
  159. }
  160. @Operation(summary = "移交应用创建者")
  161. @SaCheckPermission(value = {"appConfig.appAuth"})
  162. @PostMapping("/changeAppAuthorization")
  163. public ActionResult<MCode> changeAppAuthorization(@RequestBody AppAuthorizationModel model) {
  164. systemService.changeSystemAuthorizion(model);
  165. return ActionResult.success(MsgCode.SU005.get());
  166. }
  167. @Operation(summary = "获取开发人员")
  168. @SaCheckPermission(value = {"appConfig.appAuth", "permission.user"}, mode = SaMode.OR)
  169. @GetMapping("/getDevUser")
  170. public ActionResult getDevRole(Pagination pagination) {
  171. List<String> collect = getUserIdListByRoleId();
  172. List<BaseInfoVo> baseInfoVoList = getBaseInfoVos(pagination, collect, true);
  173. PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
  174. return ActionResult.page(baseInfoVoList, paginationVO);
  175. }
  176. /**
  177. * 根据开发者角色获取用户id集合
  178. *
  179. * @return 返回用户id集合
  180. */
  181. private @NotNull List<String> getUserIdListByRoleId() {
  182. LambdaQueryWrapper<RoleEntity> queryWrapper = new LambdaQueryWrapper<>();
  183. queryWrapper.eq(RoleEntity::getEnCode, PermissionConst.DEVELOPER_CODE);
  184. List<RoleEntity> list = roleService.list(queryWrapper);
  185. String id = list.get(0).getId();
  186. //获取用户id集合
  187. List<String> collect = roleRelationService.getListByRoleId(id, PermissionConst.USER)
  188. .stream()
  189. .map(RoleRelationEntity::getObjectId)
  190. .collect(Collectors.toList());
  191. return collect;
  192. }
  193. /**
  194. * 根据关键词查询用户信息
  195. *
  196. * @param pagination 关键词
  197. * @param collect 用户id集合
  198. * @return 用户信息集合
  199. */
  200. private @NotNull List<BaseInfoVo> getBaseInfoVos(Pagination pagination, List<String> collect) {
  201. return getBaseInfoVos(pagination, collect, false);
  202. }
  203. private @NotNull List<BaseInfoVo> getBaseInfoVos(Pagination pagination, List<String> collect, Boolean filter) {
  204. if (CollectionUtils.isEmpty(collect)) {
  205. return Collections.emptyList();
  206. }
  207. QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
  208. if (filter) {
  209. queryWrapper.lambda().ne(UserEntity::getEnabledMark, 0);
  210. }
  211. if (!StringUtil.isEmpty(pagination.getKeyword())) {
  212. queryWrapper.lambda().and(
  213. t -> t.like(UserEntity::getAccount, pagination.getKeyword())
  214. .or().like(UserEntity::getRealName, pagination.getKeyword())
  215. .or().like(UserEntity::getMobilePhone, pagination.getKeyword())
  216. );
  217. }
  218. queryWrapper.lambda().in(UserEntity::getId, collect);
  219. queryWrapper.lambda().orderByAsc(UserEntity::getId);
  220. Page<UserEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
  221. page.setOptimizeCountSql(true);
  222. Page<UserEntity> actionEntityPage = userService.page(page, queryWrapper);
  223. List<UserEntity> entities = pagination.setData(actionEntityPage.getRecords(), page.getTotal());
  224. return entities.stream()
  225. .map(item -> {
  226. BaseInfoVo vo = JsonUtil.getJsonToBean(item, BaseInfoVo.class);
  227. vo.setType(SysParamEnum.USER.getCode());
  228. vo.setHeadIcon(UploaderUtil.uploaderImg(vo.getHeadIcon()));
  229. vo.setFullName(vo.getRealName() + "/" + vo.getAccount());
  230. return vo;
  231. }).collect(Collectors.toList());
  232. }
  233. @Operation(summary = "应用基础设置详情")
  234. @Parameters({
  235. @Parameter(name = "id", description = "主键", required = true)
  236. })
  237. @SaCheckPermission(value = {"appConfig.baseConfig"})
  238. @GetMapping("/{id}")
  239. public ActionResult<SystemVO> info(@PathVariable("id") String id) {
  240. SystemEntity entity = systemService.getInfo(id);
  241. if (entity == null) {
  242. return ActionResult.fail(MsgCode.FA001.get());
  243. }
  244. SystemVO jsonToBean = JsonUtil.getJsonToBean(entity, SystemVO.class);
  245. return ActionResult.success(jsonToBean);
  246. }
  247. @Operation(summary = "应用主题配置保存")
  248. @Parameters({
  249. @Parameter(name = "id", description = "主键", required = true)
  250. })
  251. @SaCheckPermission(value = {"appConfig.baseConfig"})
  252. @PostMapping("/preferenceSave/{id}")
  253. public ActionResult<SystemVO> preferenceSave(@PathVariable("id") String id, @RequestBody SystemUpModel upModel) {
  254. SystemEntity entity = systemService.getInfo(id);
  255. if (entity == null) {
  256. return ActionResult.fail(MsgCode.FA001.get());
  257. }
  258. entity.setPreferenceJson(upModel.getPreferenceJson());
  259. systemService.saveOrUpdate(entity);
  260. return ActionResult.success(MsgCode.SU005.get());
  261. }
  262. @Operation(summary = "新建应用")
  263. @Parameters({
  264. @Parameter(name = "systemCrModel", description = "新建模型", required = true)
  265. })
  266. @SaCheckPermission(value = {"appCenter"})
  267. @PostMapping
  268. public ActionResult create(@RequestBody SystemCrModel systemCrModel) {
  269. SystemEntity entity = JsonUtil.getJsonToBean(systemCrModel, SystemEntity.class);
  270. if (systemService.isExistEnCode(entity.getId(), entity.getEnCode())) {
  271. return ActionResult.fail(MsgCode.EXIST002.get());
  272. }
  273. systemService.create(entity);
  274. return ActionResult.success(MsgCode.SU001.get(), entity.getEnCode());
  275. }
  276. @Operation(summary = "修改应用")
  277. @Parameters({
  278. @Parameter(name = "id", description = "主键", required = true),
  279. @Parameter(name = "systemCrModel", description = "修改模型", required = true)
  280. })
  281. @SaCheckPermission(value = {"appConfig.baseConfig"})
  282. @PutMapping("/{id}")
  283. public ActionResult update(@PathVariable("id") String id, @RequestBody SystemUpModel systemUpModel) {
  284. SystemEntity systemEntity = systemService.getInfo(id);
  285. if (systemEntity == null) {
  286. return ActionResult.fail(MsgCode.FA002.get());
  287. }
  288. SystemEntity entity = JsonUtil.getJsonToBean(systemUpModel, SystemEntity.class);
  289. entity.setIsMain(systemEntity.getIsMain());
  290. if (systemService.isExistEnCode(id, entity.getEnCode())) {
  291. return ActionResult.fail(MsgCode.EXIST002.get());
  292. }
  293. systemService.update(id, entity);
  294. // 如果禁用了系统,则需要将系统
  295. if (systemEntity.getEnabledMark() == 1 && entity.getEnabledMark() == 0) {
  296. sentMessage("应用已被禁用,正为您切换应用", systemEntity);
  297. }
  298. return ActionResult.success(MsgCode.SU004.get());
  299. }
  300. @Operation(summary = "删除应用")
  301. @Parameters({
  302. @Parameter(name = "id", description = "主键", required = true)
  303. })
  304. @SaCheckPermission(value = {"appCenter"})
  305. @DeleteMapping("/{id}")
  306. public ActionResult<String> delete(@PathVariable("id") String id) {
  307. SystemEntity entity = systemService.getInfo(id);
  308. if (entity == null) {
  309. return ActionResult.fail(MsgCode.FA003.get());
  310. }
  311. // 查询平台下菜单
  312. List<ModuleEntity> webData = moduleService.getList(entity.getEnCode(), JnpfConst.WEB, null, null, null, null, false);
  313. List<ModuleEntity> appData = moduleService.getList(entity.getEnCode(), JnpfConst.APP, null, null, null, null, false);
  314. if (CollectionUtils.isNotEmpty(webData) || CollectionUtils.isNotEmpty(appData)) {
  315. return ActionResult.fail(MsgCode.SYS040.get());
  316. }
  317. if (entity.getIsMain() != null && entity.getIsMain() == 1) {
  318. return ActionResult.fail(MsgCode.SYS102.get());
  319. }
  320. // 系统绑定审批常用语时不允许被删除
  321. if (commonWordsService.existSystem(id)) {
  322. return ActionResult.fail(MsgCode.SYS103.get());
  323. } else {
  324. systemService.delete(id);
  325. sentMessage("应用已被删除,正为您切换应用", entity);
  326. }
  327. return ActionResult.success(MsgCode.SU003.get());
  328. }
  329. private void sentMessage(String message, SystemEntity entity) {
  330. // 如果禁用了系统,则需要将系统
  331. List<OnlineUserModel> onlineUserList = OnlineUserProvider.getOnlineUserList();
  332. for (OnlineUserModel item : onlineUserList) {
  333. String systemId = item.getSystemId();
  334. if (entity.getId().equals(systemId)) {
  335. if (item.getWebSocket().isOpen()) {
  336. Map<String, String> maps = new HashMap<>(1);
  337. maps.put("method", "logout");
  338. maps.put("msg", "应用已被禁用或删除");
  339. if (StringUtil.isNotEmpty(UserProvider.getUser().getTenantId())) {
  340. if (UserProvider.getUser().getTenantId().equals(item.getTenantId())) {
  341. item.getWebSocket().getAsyncRemote().sendText(JsonUtil.getObjectToString(maps));
  342. }
  343. } else {
  344. item.getWebSocket().getAsyncRemote().sendText(JsonUtil.getObjectToString(maps));
  345. }
  346. }
  347. }
  348. }
  349. }
  350. @Operation(summary = "获取当前用户有权限的应用")
  351. @GetMapping("/userAuthList")
  352. public ActionResult<List<SystemListVO>> userAuthList(Pagination pagination) {
  353. AuthorizeVO authorizeByUser = authorizeService.getAuthorizeByUser(false);
  354. List<String> systemIds = authorizeByUser.getSystemList().stream().filter(t -> !Objects.equals(t.getIsMain(), 1))
  355. .map(SystemBaeModel::getId).collect(Collectors.toList());
  356. List<SystemEntity> list = systemService.getListByIdsKey(systemIds, pagination.getKeyword());
  357. List<SystemListVO> listVo = JsonUtil.getJsonToList(list, SystemListVO.class);
  358. return ActionResult.success(listVo);
  359. }
  360. @Operation(summary = "获取所有应用")
  361. @GetMapping("/Selector")
  362. public ActionResult<List<SystemListVO>> getSelector() {
  363. List<SystemEntity> list = systemService.getList().stream().filter(t -> !Objects.equals(t.getIsMain(), 1)).collect(Collectors.toList());
  364. List<SystemListVO> listVo = JsonUtil.getJsonToList(list, SystemListVO.class);
  365. return ActionResult.success(listVo);
  366. }
  367. }