AppApiEndPoint.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package com.yihu.ehr.basic.apps.controller;
  2. import com.yihu.ehr.basic.apps.model.AppApi;
  3. import com.yihu.ehr.basic.apps.model.AppApiParameter;
  4. import com.yihu.ehr.basic.apps.model.AppApiResponse;
  5. import com.yihu.ehr.basic.apps.service.*;
  6. import com.yihu.ehr.basic.user.entity.RoleApiRelation;
  7. import com.yihu.ehr.basic.user.entity.RoleAppRelation;
  8. import com.yihu.ehr.basic.user.entity.Roles;
  9. import com.yihu.ehr.basic.user.service.RoleApiRelationService;
  10. import com.yihu.ehr.basic.user.service.RolesService;
  11. import com.yihu.ehr.constants.ApiVersion;
  12. import com.yihu.ehr.constants.ErrorCode;
  13. import com.yihu.ehr.constants.ServiceApi;
  14. import com.yihu.ehr.controller.EnvelopRestEndPoint;
  15. import com.yihu.ehr.entity.api.AppApiCategory;
  16. import com.yihu.ehr.entity.api.AppApiErrorCode;
  17. import com.yihu.ehr.exception.ApiException;
  18. import com.yihu.ehr.model.app.MAppApi;
  19. import com.yihu.ehr.model.app.MAppApiDetail;
  20. import com.yihu.ehr.model.app.MAppApiParameter;
  21. import com.yihu.ehr.model.app.OpenAppApi;
  22. import com.yihu.ehr.util.rest.Envelop;
  23. import io.swagger.annotations.Api;
  24. import io.swagger.annotations.ApiOperation;
  25. import io.swagger.annotations.ApiParam;
  26. import javafx.beans.binding.ObjectExpression;
  27. import org.springframework.aop.aspectj.annotation.LazySingletonAspectInstanceFactoryDecorator;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.http.MediaType;
  30. import org.springframework.web.bind.annotation.*;
  31. import javax.servlet.http.HttpServletRequest;
  32. import javax.servlet.http.HttpServletResponse;
  33. import java.text.ParseException;
  34. import java.util.*;
  35. /**
  36. * @author linz
  37. * @version 1.0
  38. * @created 2016年7月7日21:04:13
  39. */
  40. @RestController
  41. @RequestMapping(ApiVersion.Version1_0)
  42. @Api(value = "AppApi", description = "平台应用接口管理", tags = {"平台应用-接口管理"})
  43. public class AppApiEndPoint extends EnvelopRestEndPoint {
  44. @Autowired
  45. private AppApiService appApiService;
  46. @Autowired
  47. private AppApiParameterService appApiParameterService;
  48. @Autowired
  49. private AppApiResponseService appApiResponseService;
  50. @Autowired
  51. private AppApiCategoryService appApiCategoryService;
  52. @Autowired
  53. private AppApiErrorCodeService appApiErrorCodeService;
  54. @RequestMapping(value = ServiceApi.AppApi.AppApis, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  55. @ApiOperation(value = "创建AppApi")
  56. public MAppApi createAppApi(
  57. @ApiParam(name = "appApi", value = "对象JSON结构体", allowMultiple = true)
  58. @RequestBody String appApiJson) throws Exception {
  59. AppApi appApi = toEntity(appApiJson, AppApi.class);
  60. appApi = appApiService.createAppApi(appApi);
  61. return convertToModel(appApi, MAppApi.class);
  62. }
  63. @RequestMapping(value = ServiceApi.AppApi.AppApis, method = RequestMethod.GET)
  64. @ApiOperation(value = "获取AppApi列表")
  65. public Collection<MAppApi> getAppApis(
  66. @ApiParam(name = "fields", value = "返回的字段,为空返回全部字段", defaultValue = "")
  67. @RequestParam(value = "fields", required = false) String fields,
  68. @ApiParam(name = "filters", value = "过滤器,为空检索所有条件")
  69. @RequestParam(value = "filters", required = false) String filters,
  70. @ApiParam(name = "sorts", value = "排序,规则参见说明文档", defaultValue = "")
  71. @RequestParam(value = "sorts", required = false) String sorts,
  72. @ApiParam(name = "size", value = "分页大小", defaultValue = "15")
  73. @RequestParam(value = "size", required = false) int size,
  74. @ApiParam(name = "page", value = "页码", defaultValue = "1")
  75. @RequestParam(value = "page", required = false) int page,
  76. HttpServletRequest request,
  77. HttpServletResponse response) throws Exception {
  78. List<AppApi> appApiList = appApiService.search(fields, filters, sorts, page, size);
  79. pagedResponse(request, response, appApiService.getCount(filters), page, size);
  80. return convertToModels(appApiList, new ArrayList<>(appApiList.size()), MAppApi.class, fields);
  81. }
  82. @RequestMapping(value = ServiceApi.AppApi.AppApis, method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  83. @ApiOperation(value = "更新appApi")
  84. public MAppApi updateAppApi(
  85. @ApiParam(name = "appApi", value = "对象JSON结构体", allowMultiple = true)
  86. @RequestBody String appJson) throws Exception {
  87. AppApi appApi = toEntity(appJson, AppApi.class);
  88. if (appApiService.retrieve(appApi.getId()) == null) {
  89. throw new ApiException(ErrorCode.NOT_FOUND, "应用API不存在");
  90. }
  91. appApiService.save(appApi);
  92. return convertToModel(appApi, MAppApi.class);
  93. }
  94. @RequestMapping(value = ServiceApi.AppApi.AppApi, method = RequestMethod.GET)
  95. @ApiOperation(value = "获取AppApi")
  96. public MAppApi getAppApi(
  97. @ApiParam(name = "id", value = "id")
  98. @PathVariable(value = "id") int id) throws Exception {
  99. AppApi appApi = appApiService.retrieve(id);
  100. return convertToModel(appApi, MAppApi.class);
  101. }
  102. @RequestMapping(value = ServiceApi.AppApi.AppApi, method = RequestMethod.DELETE)
  103. @ApiOperation(value = "删除AppApi")
  104. public boolean deleteAppApi(
  105. @ApiParam(name = "id", value = "id")
  106. @PathVariable(value = "id") int id) throws Exception {
  107. appApiService.delete(id);
  108. return true;
  109. }
  110. @RequestMapping(value = ServiceApi.AppApi.AppApisNoPage, method = RequestMethod.GET)
  111. @ApiOperation(value = "获取过滤App列表")
  112. public Collection<MAppApi> getAppApiNoPage(
  113. @ApiParam(name = "filters", value = "过滤器,为空检索所有条件")
  114. @RequestParam(value = "filters", required = false) String filters
  115. ) throws Exception {
  116. List<AppApi> appApiList = appApiService.search(filters);
  117. return convertToModels(appApiList, new ArrayList<>(appApiList.size()), MAppApi.class, "");
  118. }
  119. @RequestMapping(value = ServiceApi.AppApi.AppApiSearch, method = RequestMethod.GET)
  120. @ApiOperation(value = "获取AppApi列表")
  121. Collection<MAppApiDetail> searchApi(
  122. @ApiParam(name = "fields", value = "返回的字段,为空返回全部字段", defaultValue = "")
  123. @RequestParam(value = "fields", required = false) String fields,
  124. @ApiParam(name = "filters", value = "过滤器,规则参见说明文档", defaultValue = "")
  125. @RequestParam(value = "filters", required = false) String filters,
  126. @ApiParam(name = "sorts", value = "排序,规则参见说明文档", defaultValue = "")
  127. @RequestParam(value = "sorts", required = false) String sorts,
  128. @ApiParam(name = "size", value = "分页大小", defaultValue = "15")
  129. @RequestParam(value = "size", required = false) int size,
  130. @ApiParam(name = "page", value = "页码", defaultValue = "1")
  131. @RequestParam(value = "page", required = false) int page,
  132. HttpServletRequest request,
  133. HttpServletResponse response) throws Exception {
  134. List<AppApi> appApiList = appApiService.search(fields, filters, sorts, page, size);
  135. pagedResponse(request, response, appApiService.getCount(filters), page, size);
  136. Collection<MAppApiDetail> mAppApiDetails = convertToModels(appApiList, new ArrayList<>(appApiList.size()), MAppApiDetail.class, "");
  137. mAppApiDetails.forEach(appApi -> {
  138. try {
  139. List apiParams = appApiParameterService.search("appApiId=" + appApi.getId());
  140. Collection<MAppApiParameter> mAppApiParameters = convertToModels(apiParams, new ArrayList<>(apiParams.size()), MAppApiParameter.class, "");
  141. appApi.setParameters(mAppApiParameters);
  142. } catch (ParseException e) {
  143. e.printStackTrace();
  144. }
  145. });
  146. return mAppApiDetails;
  147. }
  148. //------------------------ 开放平台基本请求部分 -------------------------
  149. @RequestMapping(value = ServiceApi.AppApi.CheckName, method = RequestMethod.GET)
  150. @ApiOperation(value = "检查名称")
  151. public Boolean checkName(
  152. @ApiParam(name = "name", value = "api名称", required = true)
  153. @RequestParam(value = "name") String name,
  154. @ApiParam(name = "appApiId", value = "appApiId")
  155. @RequestParam(value = "appApiId", required = false) Integer appApiId) throws Exception {
  156. if (null == appApiId) {
  157. if (appApiService.findByName(name).size() > 0) {
  158. return true;
  159. }
  160. return false;
  161. } else {
  162. AppApi appApi = appApiService.findById(appApiId);
  163. if (!appApi.getName().equals(name) && appApiService.findByName(name).size() > 0) {
  164. return true;
  165. }
  166. return false;
  167. }
  168. }
  169. @RequestMapping(value = ServiceApi.AppApi.Save, method = RequestMethod.POST)
  170. @ApiOperation(value = "新增Api")
  171. public Envelop save(
  172. @ApiParam(name = "appApi", value = "对象JSON结构体", required = true, allowMultiple = true)
  173. @RequestParam(value = "appApi") String appApi,
  174. @ApiParam(name = "apiParam", value = "api请求参数集合")
  175. @RequestParam(value = "apiParam", required = false) String apiParam,
  176. @ApiParam(name = "apiResponse", value = "api响应参数集合")
  177. @RequestParam(value = "apiResponse", required = false) String apiResponse,
  178. @ApiParam(name = "apiErrorCode", value = "api错误码集合")
  179. @RequestParam(value = "apiErrorCode", required = false) String apiErrorCode) throws Exception {
  180. AppApi appApi1 = appApiService.completeSave(appApi, apiParam, apiResponse, apiErrorCode);
  181. List<AppApiParameter> appApiParameters = appApiParameterService.search("appApiId=" + appApi1.getId());
  182. List<AppApiResponse> appApiResponses = appApiResponseService.search("appApiId=" + appApi1.getId());
  183. List<AppApiErrorCode> appApiErrorCodes = appApiErrorCodeService.search("appApiId=" + appApi1.getId());
  184. List resultLis = new ArrayList();
  185. Map<String, Object> dataMap = new HashMap<>();
  186. dataMap.put("param", appApiParameters);
  187. dataMap.put("response", appApiResponses);
  188. dataMap.put("errorCode", appApiErrorCodes);
  189. resultLis.add(dataMap);
  190. return success(appApi1, resultLis);
  191. }
  192. @RequestMapping(value = ServiceApi.AppApi.Delete, method = RequestMethod.POST)
  193. @ApiOperation(value = "删除Api")
  194. public Boolean delete(
  195. @ApiParam(name = "id", value = "ID", required = true)
  196. @RequestParam(value = "id") Integer id) throws Exception {
  197. appApiService.completeDelete(id);
  198. return true;
  199. }
  200. @RequestMapping(value = ServiceApi.AppApi.Update, method = RequestMethod.POST)
  201. @ApiOperation(value = "更新Api")
  202. public Envelop update(
  203. @ApiParam(name = "appApi", value = "对象JSON结构体", required = true, allowMultiple = true)
  204. @RequestParam(value = "appApi") String appApi,
  205. @ApiParam(name = "apiParam", value = "api请求参数集合")
  206. @RequestParam(value = "apiParam", required = false) String apiParam,
  207. @ApiParam(name = "apiResponse", value = "api响应参数集合")
  208. @RequestParam(value = "apiResponse", required = false) String apiResponse,
  209. @ApiParam(name = "apiErrorCode", value = "api错误码集合")
  210. @RequestParam(value = "apiErrorCode", required = false) String apiErrorCode) throws Exception {
  211. AppApi appApi1 = toEntity(appApi, AppApi.class);
  212. if (null == appApiService.findById(appApi1.getId()) ) {
  213. return failed("操作的API不存在", ErrorCode.OBJECT_NOT_FOUND.value());
  214. }
  215. AppApi newAppApi = appApiService.completeSave(appApi, apiParam, apiResponse, apiErrorCode);
  216. List<AppApiParameter> appApiParameters = appApiParameterService.search("appApiId=" + newAppApi.getId());
  217. List<AppApiResponse> appApiResponses = appApiResponseService.search("appApiId=" + newAppApi.getId());
  218. List<AppApiErrorCode> appApiErrorCodes = appApiErrorCodeService.search("appApiId=" + appApi1.getId());
  219. List resultLis = new ArrayList();
  220. Map<String, Object> dataMap = new HashMap<>();
  221. dataMap.put("param", appApiParameters);
  222. dataMap.put("response", appApiResponses);
  223. dataMap.put("errorCode", appApiErrorCodes);
  224. resultLis.add(dataMap);
  225. return success(appApi1, resultLis);
  226. }
  227. @RequestMapping(value = ServiceApi.AppApi.Page, method = RequestMethod.GET)
  228. @ApiOperation(value = "Api分页")
  229. public Envelop page(
  230. @ApiParam(name = "fields", value = "返回的字段,为空返回全部字段")
  231. @RequestParam(value = "fields", required = false) String fields,
  232. @ApiParam(name = "filters", value = "过滤器,规则参见说明文档")
  233. @RequestParam(value = "filters", required = false) String filters,
  234. @ApiParam(name = "sorts", value = "排序,规则参见说明文档")
  235. @RequestParam(value = "sorts", required = false) String sorts,
  236. @ApiParam(name = "page", value = "页码", defaultValue = "1")
  237. @RequestParam(value = "page", required = false) int page,
  238. @ApiParam(name = "size", value = "分页大小", defaultValue = "15")
  239. @RequestParam(value = "size", required = false) int size) throws Exception {
  240. List<AppApi> appApiList = appApiService.search(fields, filters, sorts, page, size);
  241. int count = (int)appApiService.getCount(filters);
  242. Envelop envelop = getPageResult(appApiList, count, page, size);
  243. List<AppApi> appApiList1 = envelop.getDetailModelList();
  244. List<AppApi> appApiList2 = new ArrayList<>(appApiList1.size());
  245. for (AppApi appApi : appApiList1) {
  246. if (appApi.getCategory() != null) {
  247. AppApiCategory appApiCategory = appApiCategoryService.findOne(appApi.getCategory());
  248. if (appApiCategory != null) {
  249. appApi.setCategoryName(appApiCategory.getName());
  250. }
  251. }
  252. appApiList2.add(appApi);
  253. }
  254. envelop.setDetailModelList(appApiList2);
  255. return envelop;
  256. }
  257. // -------------------------------- 接入授权部分 --------------------------------
  258. @RequestMapping(value = ServiceApi.AppApi.AuthList, method = RequestMethod.GET)
  259. @ApiOperation(value = "获取AppApi列表")
  260. public List<AppApi> authApiList(
  261. @ApiParam(name = "appId", value = "应用ID", required = true)
  262. @RequestParam(value = "appId") String appId) throws Exception{
  263. List<AppApi> appApiList = appApiService.authApiList(appId);
  264. List<AppApi> appApiList2 = new ArrayList<>(appApiList.size());
  265. for (AppApi appApi : appApiList) {
  266. if (appApi.getCategory() != null) {
  267. AppApiCategory appApiCategory = appApiCategoryService.findOne(appApi.getCategory());
  268. if (appApiCategory != null) {
  269. appApi.setCategoryName(appApiCategory.getName());
  270. }
  271. }
  272. appApiList2.add(appApi);
  273. }
  274. return appApiList2;
  275. }
  276. @RequestMapping(value = ServiceApi.AppApi.AuthApi, method = RequestMethod.POST)
  277. @ApiOperation(value = "授权AppApi")
  278. public Boolean authApi(
  279. @ApiParam(name = "appId", value = "角色appId", required = true)
  280. @RequestParam(value = "appId") String appId,
  281. @ApiParam(name = "code", value = "角色编码", required = true)
  282. @RequestParam(value = "code") String code,
  283. @ApiParam(name = "apiId", value = "apiId", required = true)
  284. @RequestParam(value = "apiId") String apiId) throws Exception{
  285. appApiService.authApi(code, appId, apiId);
  286. return true;
  287. }
  288. @RequestMapping(value = ServiceApi.AppApi.AuthApi, method = RequestMethod.DELETE)
  289. @ApiOperation(value = "取消AppApi授权")
  290. public Boolean unAuthApi(
  291. @ApiParam(name = "appId", value = "角色appId", required = true)
  292. @RequestParam(value = "appId") String appId,
  293. @ApiParam(name = "code", value = "角色编码", required = true)
  294. @RequestParam(value = "code") String code,
  295. @ApiParam(name = "apiId", value = "apiId", required = true)
  296. @RequestParam(value = "apiId") String apiId) throws Exception{
  297. appApiService.unAuthApi(code, appId, apiId);
  298. return true;
  299. }
  300. }