InterfaceOauthController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package jnpf.base.controller;
  2. import cn.dev33.satoken.annotation.SaCheckPermission;
  3. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  4. import io.swagger.v3.oas.annotations.Operation;
  5. import io.swagger.v3.oas.annotations.Parameter;
  6. import io.swagger.v3.oas.annotations.Parameters;
  7. import io.swagger.v3.oas.annotations.tags.Tag;
  8. import jakarta.validation.Valid;
  9. import jnpf.base.ActionResult;
  10. import jnpf.base.entity.DataInterfaceEntity;
  11. import jnpf.base.entity.DataInterfaceLogEntity;
  12. import jnpf.base.entity.DataInterfaceUserEntity;
  13. import jnpf.base.entity.InterfaceOauthEntity;
  14. import jnpf.base.model.InterfaceOauth.*;
  15. import jnpf.base.model.datainterface.DataInterfaceVo;
  16. import jnpf.base.service.DataInterfaceLogService;
  17. import jnpf.base.service.DataInterfaceService;
  18. import jnpf.base.service.DataInterfaceUserService;
  19. import jnpf.base.service.InterfaceOauthService;
  20. import jnpf.base.vo.PageListVO;
  21. import jnpf.base.vo.PaginationVO;
  22. import jnpf.constant.MsgCode;
  23. import jnpf.exception.DataException;
  24. import jnpf.permission.entity.UserEntity;
  25. import jnpf.permission.service.UserService;
  26. import jnpf.util.JsonUtil;
  27. import jnpf.util.StringUtil;
  28. import jnpf.util.UserProvider;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.web.bind.annotation.*;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.List;
  34. import java.util.UUID;
  35. /**
  36. * 接口认证控制器
  37. *
  38. * @author JNPF开发平台组
  39. * @version V3.4.2
  40. * @copyright 引迈信息技术有限公司
  41. * @date 2022/6/8
  42. */
  43. @Tag(name = "接口认证", description = "interfaceoauth")
  44. @RestController
  45. @RequestMapping(value = "/api/system/InterfaceOauth")
  46. public class InterfaceOauthController extends SuperController<InterfaceOauthService, InterfaceOauthEntity> {
  47. @Autowired
  48. private DataInterfaceService dataInterfaceService;
  49. @Autowired
  50. private DataInterfaceLogService dataInterfaceLogService;
  51. @Autowired
  52. private InterfaceOauthService interfaceOauthService;
  53. @Autowired
  54. private UserService userService;
  55. @Autowired
  56. private DataInterfaceUserService dataInterfaceUserService;
  57. /**
  58. * 获取接口认证列表(分页)
  59. *
  60. * @param pagination 分页参数
  61. * @return ignore
  62. */
  63. @Operation(summary = "获取接口认证列表(分页)")
  64. @SaCheckPermission("dataCenter.interfaceOauth")
  65. @GetMapping
  66. public ActionResult<PageListVO<InterfaceIdentListVo>> getList(PaginationOauth pagination) {
  67. List<InterfaceOauthEntity> data = interfaceOauthService.getList(pagination);
  68. List<InterfaceIdentListVo> jsonToList = JsonUtil.getJsonToList(data, InterfaceIdentListVo.class);
  69. jsonToList.forEach(item -> {
  70. if (StringUtil.isNotEmpty(UserProvider.getUser().getTenantId())) {
  71. item.setTenantId(UserProvider.getUser().getTenantId());
  72. }
  73. if (item.getCreatorUserId() != null) {
  74. String creUser = userService.getInfo(item.getCreatorUserId()) != null ? userService.getInfo(item.getCreatorUserId()).getRealName() : "";
  75. item.setCreatorUser(creUser);
  76. }
  77. });
  78. PaginationVO paginationVO = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
  79. return ActionResult.page(jsonToList, paginationVO);
  80. }
  81. /**
  82. * 添加接口认证
  83. *
  84. * @param interfaceIdentForm 添加接口认证模型
  85. * @return ignore
  86. */
  87. @Operation(summary = "添加接口认证")
  88. @Parameter(name = "interfaceIdentForm", description = "添加接口认证模型", required = true)
  89. @SaCheckPermission("dataCenter.interfaceOauth")
  90. @PostMapping
  91. public ActionResult create(@RequestBody @Valid InterfaceIdentForm interfaceIdentForm) {
  92. InterfaceOauthEntity entity = JsonUtil.getJsonToBean(interfaceIdentForm, InterfaceOauthEntity.class);
  93. if (interfaceOauthService.isExistByAppName(entity.getAppName(), entity.getId())) {
  94. return ActionResult.fail(MsgCode.EXIST001.get());
  95. }
  96. if (interfaceOauthService.isExistByAppId(entity.getAppId(), entity.getId())) {
  97. return ActionResult.fail("AppId" + MsgCode.EXIST103.get());
  98. }
  99. interfaceOauthService.create(entity);
  100. return ActionResult.success(MsgCode.SU001.get());
  101. }
  102. /**
  103. * 修改接口认证
  104. *
  105. * @param interfaceIdentForm 添加接口认证模型
  106. * @return ignore
  107. */
  108. @Operation(summary = "修改接口认证")
  109. @Parameters({
  110. @Parameter(name = "interfaceIdentForm", description = "添加接口认证模型", required = true),
  111. @Parameter(name = "id", description = "主键", required = true)
  112. })
  113. @SaCheckPermission("dataCenter.interfaceOauth")
  114. @PutMapping("/{id}")
  115. public ActionResult update(@RequestBody @Valid InterfaceIdentForm interfaceIdentForm, @PathVariable("id") String id) throws DataException {
  116. InterfaceOauthEntity entity = JsonUtil.getJsonToBean(interfaceIdentForm, InterfaceOauthEntity.class);
  117. if (interfaceOauthService.isExistByAppName(entity.getAppName(), id)) {
  118. return ActionResult.fail(MsgCode.EXIST001.get());
  119. }
  120. if (interfaceOauthService.isExistByAppId(entity.getAppId(), id)) {
  121. return ActionResult.fail("AppId" + MsgCode.EXIST103.get());
  122. }
  123. boolean flag = interfaceOauthService.update(entity, id);
  124. if (flag == false) {
  125. return ActionResult.fail(MsgCode.FA002.get());
  126. }
  127. return ActionResult.success(MsgCode.SU004.get());
  128. }
  129. /**
  130. * 删除接口认证
  131. *
  132. * @param id 主键
  133. * @return
  134. */
  135. @Operation(summary = "删除接口")
  136. @Parameters({
  137. @Parameter(name = "id", description = "主键", required = true)
  138. })
  139. @SaCheckPermission("dataCenter.interfaceOauth")
  140. @DeleteMapping("/{id}")
  141. public ActionResult delete(@PathVariable String id) {
  142. InterfaceOauthEntity entity = interfaceOauthService.getInfo(id);
  143. if (entity != null) {
  144. interfaceOauthService.delete(entity);
  145. return ActionResult.success(MsgCode.SU003.get());
  146. }
  147. return ActionResult.fail(MsgCode.FA003.get());
  148. }
  149. /**
  150. * 获取秘钥
  151. *
  152. * @return
  153. */
  154. @Operation(summary = "获取接口认证密钥")
  155. @SaCheckPermission("dataCenter.interfaceOauth")
  156. @GetMapping("/getAppSecret")
  157. public ActionResult getAppSecret() {
  158. String uuid = UUID.randomUUID().toString().replace("-", "");
  159. return ActionResult.success(MsgCode.SU019.get(), uuid);
  160. }
  161. /**
  162. * 保存綁定认证接口
  163. *
  164. * @return
  165. */
  166. @Operation(summary = "保存綁定认证接口")
  167. @Parameters({
  168. @Parameter(name = "identInterfaceListModel", description = "授权接口列表模型", required = true)
  169. })
  170. @SaCheckPermission("dataCenter.interfaceOauth")
  171. @PostMapping("/saveInterfaceList")
  172. public ActionResult getInterfaceList(@RequestBody IdentInterfaceListModel identInterfaceListModel) {
  173. InterfaceOauthEntity entity = new InterfaceOauthEntity();
  174. entity.setId(identInterfaceListModel.getInterfaceIdentId());
  175. entity.setDataInterfaceIds(identInterfaceListModel.getDataInterfaceIds());
  176. boolean b = interfaceOauthService.updateById(entity);
  177. if (b) {
  178. return ActionResult.success(MsgCode.SU002.get());
  179. }
  180. return ActionResult.success(MsgCode.FA101.get());
  181. }
  182. /**
  183. * 获取接口授权绑定接口列表
  184. *
  185. * @param id 主键
  186. * @return
  187. */
  188. @Operation(summary = "获取认证基础信息及接口列表")
  189. @Parameters({
  190. @Parameter(name = "id", description = "主键", required = true)
  191. })
  192. @SaCheckPermission("dataCenter.interfaceOauth")
  193. @GetMapping("/{id}")
  194. public ActionResult getInterfaceList(@PathVariable("id") String id) {
  195. InterfaceOauthEntity entity = interfaceOauthService.getInfo(id);
  196. InterfaceIdentVo bean = JsonUtil.getJsonToBean(entity, InterfaceIdentVo.class);
  197. if (StringUtils.isNotEmpty(bean.getDataInterfaceIds())) {
  198. List<DataInterfaceVo> listDataInterfaceVo = new ArrayList<>();
  199. List<DataInterfaceEntity> list = dataInterfaceService.getList(false);
  200. list.forEach(item -> {
  201. if (bean.getDataInterfaceIds().contains(item.getId())) {
  202. DataInterfaceVo dataInterfaceVo = JsonUtil.getJsonToBean(item, DataInterfaceVo.class);
  203. listDataInterfaceVo.add(dataInterfaceVo);
  204. }
  205. });
  206. bean.setList(listDataInterfaceVo);
  207. }
  208. //添加授权用户信息
  209. List<InterfaceUserVo> listIuv = new ArrayList<>();
  210. List<DataInterfaceUserEntity> select = dataInterfaceUserService.select(id);
  211. for (DataInterfaceUserEntity diue : select) {
  212. String userId = diue.getUserId();
  213. UserEntity info = userService.getInfo(userId);
  214. InterfaceUserVo iuv = new InterfaceUserVo();
  215. iuv.setUserId(userId);
  216. iuv.setUserKey(diue.getUserKey());
  217. iuv.setUserName(info.getRealName() + "/" + info.getAccount());
  218. listIuv.add(iuv);
  219. }
  220. bean.setUserList(listIuv);
  221. return ActionResult.success(MsgCode.SU019.get(), bean);
  222. }
  223. /**
  224. * 获取日志列表
  225. *
  226. * @param id 主键
  227. * @param paginationIntrfaceLog 分页参数
  228. * @return
  229. */
  230. @Operation(summary = "获取日志列表")
  231. @Parameters({
  232. @Parameter(name = "id", description = "主键", required = true)
  233. })
  234. @SaCheckPermission("dataCenter.interfaceOauth")
  235. @GetMapping("/dataInterfaceLog/{id}")
  236. public ActionResult<PageListVO<IdentDataInterfaceLogVO>> getInterfaceList(@PathVariable("id") String id, PaginationIntrfaceLog paginationIntrfaceLog) {
  237. InterfaceOauthEntity entity = interfaceOauthService.getInfo(id);
  238. List<IdentDataInterfaceLogVO> voList = null;
  239. PaginationVO vo = null;
  240. if (entity != null && StringUtils.isNotEmpty(entity.getDataInterfaceIds())) {
  241. String dataInterfaceIds = entity.getDataInterfaceIds();
  242. String[] split = dataInterfaceIds.split(",");
  243. List<String> list = Arrays.asList(split);
  244. List<DataInterfaceLogEntity> listByIds = dataInterfaceLogService.getListByIds(entity.getAppId(), list, paginationIntrfaceLog);
  245. voList = JsonUtil.getJsonToList(listByIds, IdentDataInterfaceLogVO.class);
  246. List<DataInterfaceEntity> listDataInt = dataInterfaceService.getList(false);
  247. for (IdentDataInterfaceLogVO invo : voList) {
  248. if (StringUtil.isNotEmpty(UserProvider.getUser().getTenantId())) {
  249. invo.setTenantId(UserProvider.getUser().getTenantId());
  250. }
  251. //绑定用户
  252. UserEntity userEntity = userService.getInfo(invo.getUserId());
  253. if (userEntity != null) {
  254. invo.setUserId(userEntity.getRealName() + "/" + userEntity.getAccount());
  255. }
  256. //绑定接口基础数据
  257. listDataInt.forEach(item -> {
  258. if (invo.getInvokId().contains(item.getId())) {
  259. DataInterfaceVo dataInterfaceVo = JsonUtil.getJsonToBean(item, DataInterfaceVo.class);
  260. invo.setFullName(dataInterfaceVo.getFullName());
  261. invo.setEnCode(dataInterfaceVo.getEnCode());
  262. }
  263. });
  264. }
  265. vo = JsonUtil.getJsonToBean(paginationIntrfaceLog, PaginationVO.class);
  266. }
  267. return ActionResult.page(voList, vo);
  268. }
  269. @Operation(summary = "授权用户")
  270. @SaCheckPermission("dataCenter.interfaceOauth")
  271. @PostMapping("/SaveUserList")
  272. public ActionResult saveUserList(@RequestBody InterfaceUserForm interfaceUserForm) {
  273. dataInterfaceUserService.saveUserList(interfaceUserForm);
  274. return ActionResult.success(MsgCode.SU002.get());
  275. }
  276. }