AppKeyInfoController.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package com.zhiqiyun.open.mvc.controller;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  6. import com.baomidou.mybatisplus.core.metadata.OrderItem;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.dliyun.oap.framework.security.algorithm.Rsa;
  9. import com.dliyun.oap.framework.service.RouterService;
  10. import com.dliyun.oap.framework.service.ServiceMethodHandler;
  11. import com.zhiqiyun.open.annotation.Permission;
  12. import com.zhiqiyun.open.core.enmus.YN;
  13. import com.zhiqiyun.open.core.models.app.ApiRequestLog;
  14. import com.zhiqiyun.open.core.models.app.AppKeyInfo;
  15. import com.zhiqiyun.open.core.models.user.OauthInfo;
  16. import com.zhiqiyun.open.core.service.ApiRequestLogService;
  17. import com.zhiqiyun.open.core.service.AppKeyInfoService;
  18. import com.zhiqiyun.open.core.service.OauthService;
  19. import com.zhiqiyun.open.core.service.SequenceService;
  20. import com.zhiqiyun.open.mvc.Result;
  21. import com.zhiqiyun.open.mvc.params.QueryApiRequestLogParams;
  22. import com.zhiqiyun.open.mvc.params.QueryAppKeyInfoParams;
  23. import com.zhiqiyun.open.mvc.params.SaveAppKeyInfoParam;
  24. import com.zhiqiyun.open.utils.DateUtil;
  25. import com.zhiqiyun.open.utils.ServletContext;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.apache.commons.lang3.StringUtils;
  28. import org.springframework.beans.BeanUtils;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.web.bind.annotation.PostMapping;
  31. import org.springframework.web.bind.annotation.RequestBody;
  32. import org.springframework.web.bind.annotation.RequestMapping;
  33. import org.springframework.web.bind.annotation.RestController;
  34. import javax.annotation.Resource;
  35. import javax.validation.Valid;
  36. import java.util.*;
  37. import java.util.stream.Collectors;
  38. @Slf4j
  39. @RestController
  40. @RequestMapping("/appKey")
  41. public class AppKeyInfoController {
  42. @Autowired
  43. private AppKeyInfoService appKeyInfoService;
  44. @Autowired
  45. private SequenceService sequenceService;
  46. @Autowired
  47. private ApiRequestLogService apiRequestLogService;
  48. @Autowired
  49. private RouterService routerService;
  50. @Resource
  51. private OauthService oauthService;
  52. @Permission(value = "app.key.find", tags = "查询AppKey")
  53. @PostMapping("/findPage")
  54. public Result findPage(@RequestBody QueryAppKeyInfoParams params) {
  55. QueryWrapper<AppKeyInfo> queryWrapper = new QueryWrapper<>();
  56. String accessToken = ServletContext.getAccessToken();
  57. OauthInfo oauthInfo = this.oauthService.getAuth(accessToken);
  58. log.info("当前用户信息:{}", JSONObject.toJSONString(oauthInfo));
  59. if (StrUtil.isNotBlank(oauthInfo.getOpenKey())) {
  60. queryWrapper.in("id", Arrays.stream(oauthInfo.getOpenKey().split(","))
  61. .map(Long::parseLong).collect(Collectors.toList()));
  62. }
  63. if (params.getId() != null) {
  64. queryWrapper.eq("id", params.getId());
  65. }
  66. if (params.getIsEnable() != null) {
  67. queryWrapper.eq("is_enable", params.getIsEnable());
  68. }
  69. if (StringUtils.isNotBlank(params.getRemark())) {
  70. queryWrapper.like("remark", params.getRemark());
  71. }
  72. Page<AppKeyInfo> page = params.getPage();
  73. page.addOrder(OrderItem.desc("created_time"));
  74. Page<AppKeyInfo> resultData = this.appKeyInfoService.page(page, queryWrapper);
  75. return Result.instance(Result.Code.SUCCESS).setData(resultData);
  76. }
  77. @Permission(value = "app.key.statistics", tags = "Api接口统计")
  78. @PostMapping("/statistics")
  79. public Result statistics() {
  80. QueryWrapper<ApiRequestLog> wrapper = new QueryWrapper<>();
  81. wrapper.select("app_key appKey", "count(id) value");
  82. wrapper.groupBy("app_key");
  83. wrapper.orderByDesc("value");
  84. wrapper.last("limit 8");
  85. List<Map<String, Object>> requestAppKeyData = this.apiRequestLogService.listMaps(wrapper);
  86. for (Map<String, Object> dataMap : requestAppKeyData) {
  87. AppKeyInfo appKeyInfo = this.appKeyInfoService.findByAppKey(dataMap.get("appKey").toString());
  88. if (appKeyInfo == null) {
  89. dataMap.put("name", "未知应用");
  90. } else {
  91. dataMap.put("name", appKeyInfo.getName());
  92. }
  93. }
  94. wrapper = new QueryWrapper<>();
  95. wrapper.select("DATE_FORMAT(service_begin_time, '%Y-%m-%d') d", "count(id) value");
  96. wrapper.groupBy("d");
  97. wrapper.orderByAsc("d");
  98. wrapper.last("limit 15");
  99. List<Map<String, Object>> requestDayData = this.apiRequestLogService.listMaps(wrapper);
  100. wrapper = new QueryWrapper<>();
  101. wrapper.select("method", "count(id) value");
  102. wrapper.groupBy("method");
  103. wrapper.orderByDesc("value");
  104. wrapper.last("limit 15");
  105. List<Map<String, Object>> requestMethodData = this.apiRequestLogService.listMaps(wrapper);
  106. for (Map<String, Object> dataMap : requestMethodData) {
  107. String method = dataMap.getOrDefault("method", "").toString();
  108. if (StringUtils.isBlank(method)) {
  109. dataMap.put("name", "未知应用");
  110. } else {
  111. dataMap.put("name", method);
  112. }
  113. }
  114. Map<String, Object> dataMap = new HashMap<>();
  115. dataMap.put("requestAppKeyData", requestAppKeyData);
  116. dataMap.put("requestDayData", requestDayData);
  117. dataMap.put("requestMethodData", requestMethodData);
  118. return Result.instance(Result.Code.MESSAGE_SUCCESS).setData(dataMap);
  119. }
  120. @Permission(value = "app.key.find", tags = "Api调用日志查询")
  121. @PostMapping("/findRequestLogsPage")
  122. public Result findRequestLogsPage(@RequestBody QueryApiRequestLogParams params) {
  123. QueryWrapper<ApiRequestLog> queryWrapper = new QueryWrapper<>();
  124. if (StringUtils.isNotBlank(params.getAppKey())) {
  125. queryWrapper.eq("app_key", params.getAppKey());
  126. }
  127. if (StringUtils.isNotBlank(params.getMethod())) {
  128. queryWrapper.eq("method", params.getMethod());
  129. }
  130. if (StringUtils.isNotBlank(params.getVersion())) {
  131. queryWrapper.eq("version", params.getVersion());
  132. }
  133. if (StringUtils.isNotBlank(params.getClientIp())) {
  134. queryWrapper.eq("client_ip", params.getClientIp());
  135. }
  136. if (params.getServiceBeginTime() != null) {
  137. queryWrapper.between("service_begin_time", params.getServiceBeginTime().getStatDate(), params.getServiceBeginTime().getEndDate());
  138. }
  139. Page<ApiRequestLog> page = params.getPage();
  140. page.addOrder(OrderItem.desc("service_begin_time"));
  141. Page<ApiRequestLog> resultData = this.apiRequestLogService.page(page, queryWrapper);
  142. return Result.instance(Result.Code.SUCCESS).setData(resultData);
  143. }
  144. @Permission(value = "app.key.add", tags = "新建AppKey")
  145. @PostMapping("/save")
  146. public Result save(@Valid @RequestBody SaveAppKeyInfoParam param) throws Exception {
  147. Map<String, ServiceMethodHandler> serviceMethodHandlerMap = this.routerService.getOapContext().getAllServiceMethodHandlers();
  148. List<String> listAllServices = new ArrayList<>();
  149. for (ServiceMethodHandler handler : serviceMethodHandlerMap.values()) {
  150. listAllServices.add(String.format("%s:%s", handler.getServiceMethodDefinition().getMethod(), handler.getServiceMethodDefinition().getVersion()));
  151. }
  152. AppKeyInfo appKeyInfo = new AppKeyInfo();
  153. BeanUtils.copyProperties(param, appKeyInfo);
  154. Long id = this.sequenceService.nextId();
  155. appKeyInfo.setId(id);
  156. Rsa.RsaKeys rsaKeys = Rsa.genKeyPair();
  157. appKeyInfo.setPublicKey(rsaKeys.getPublicKey());
  158. appKeyInfo.setPrivateKey(rsaKeys.getPrivateKey());
  159. appKeyInfo.setCreatedTime(DateUtil.current());
  160. appKeyInfo.setIsEnable(YN.Y);
  161. appKeyInfo.getPermissions().removeIf(permission -> !listAllServices.contains(permission));
  162. this.appKeyInfoService.save(appKeyInfo);
  163. return Result.instance(Result.Code.MESSAGE_SUCCESS);
  164. }
  165. @Permission(value = "app.key.edit", tags = "更新AppKey")
  166. @PostMapping("/updateById")
  167. public Result updateById(Long id, @Valid @RequestBody SaveAppKeyInfoParam param) {
  168. Map<String, ServiceMethodHandler> serviceMethodHandlerMap = this.routerService.getOapContext().getAllServiceMethodHandlers();
  169. List<String> listAllServices = new ArrayList<>();
  170. for (ServiceMethodHandler handler : serviceMethodHandlerMap.values()) {
  171. listAllServices.add(String.format("%s:%s", handler.getServiceMethodDefinition().getMethod(), handler.getServiceMethodDefinition().getVersion()));
  172. }
  173. AppKeyInfo appKeyInfo = new AppKeyInfo();
  174. BeanUtils.copyProperties(param, appKeyInfo);
  175. appKeyInfo.setId(id);
  176. appKeyInfo.getPermissions().removeIf(permission -> !listAllServices.contains(permission));
  177. this.appKeyInfoService.refreshCache(id.toString());
  178. this.appKeyInfoService.updateById(appKeyInfo);
  179. return Result.instance(Result.Code.MESSAGE_SUCCESS);
  180. }
  181. @Permission(value = "app.key.disable", tags = "禁用AppKey")
  182. @PostMapping("/disableByIds")
  183. public Result disableByIds(@RequestBody List<Long> ids) {
  184. UpdateWrapper<AppKeyInfo> updateWrapper = new UpdateWrapper<>();
  185. updateWrapper.in("id", ids);
  186. updateWrapper.set("is_enable", YN.N);
  187. this.appKeyInfoService.update(updateWrapper);
  188. return Result.instance(Result.Code.MESSAGE_SUCCESS);
  189. }
  190. @Permission(value = "app.key.enable", tags = "启用AppKey")
  191. @PostMapping("/enableByIds")
  192. public Result enableByIds(@RequestBody List<Long> ids) {
  193. UpdateWrapper<AppKeyInfo> updateWrapper = new UpdateWrapper<>();
  194. updateWrapper.in("id", ids);
  195. updateWrapper.set("is_enable", YN.Y);
  196. this.appKeyInfoService.update(updateWrapper);
  197. return Result.instance(Result.Code.MESSAGE_SUCCESS);
  198. }
  199. }