From af5da28b4ff8526eaa29f41ef9837451f0c1f06f Mon Sep 17 00:00:00 2001 From: zjf <1799650030@qq.com> Date: Mon, 31 Mar 2025 15:49:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E6=94=B9=E6=B5=99=E6=94=BF?= =?UTF-8?q?=E9=92=89=E7=99=BB=E5=BD=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ThirdPartyLogController.java | 119 ++++++++++++++++++ .../controller/ThirdPartyOAth2Cotroller.java | 4 + .../entity/ThirdPartyLogEntity.java | 107 ++++++++++++++++ .../exception/ThirdPartyException.java | 14 +++ .../mapper/ThirdPartyLogMapper.java | 11 ++ .../service/ThirdPartyLogService.java | 12 ++ .../service/ThirdPartyOAuth2Service.java | 29 ++++- .../impl/ThirdPartyLogServiceImpl.java | 26 ++++ .../resources/mapper/ThirdPartyLogMapper.xml | 21 ++++ 9 files changed, 341 insertions(+), 2 deletions(-) create mode 100644 pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyLogController.java create mode 100644 pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/entity/ThirdPartyLogEntity.java create mode 100644 pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/exception/ThirdPartyException.java create mode 100644 pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/mapper/ThirdPartyLogMapper.java create mode 100644 pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/ThirdPartyLogService.java create mode 100644 pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/impl/ThirdPartyLogServiceImpl.java create mode 100644 pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyLogMapper.xml diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyLogController.java b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyLogController.java new file mode 100644 index 000000000..a5ee704b8 --- /dev/null +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyLogController.java @@ -0,0 +1,119 @@ +package com.bsd.pjcloud.thirdparty.controller; + +import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.collection.CollUtil; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.bsd.pjcloud.common.core.util.R; +import com.bsd.pjcloud.common.log.annotation.SysLog; +import com.bsd.pjcloud.thirdparty.entity.ThirdPartyLogEntity; +import com.bsd.pjcloud.thirdparty.service.ThirdPartyLogService; +import org.springframework.security.access.prepost.PreAuthorize; +import com.bsd.pjcloud.common.excel.annotation.ResponseExcel; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import org.springdoc.core.annotations.ParameterObject; +import org.springframework.http.HttpHeaders; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.Operation; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Objects; + +/** + * 单点日志记录 + * + * @author zjf + * @date 2025-03-31 13:48:37 + */ +@RestController +@RequiredArgsConstructor +@RequestMapping("/thirdPartyLog" ) +@Tag(description = "thirdPartyLog" , name = "单点日志记录管理" ) +@SecurityRequirement(name = HttpHeaders.AUTHORIZATION) +public class ThirdPartyLogController { + + private final ThirdPartyLogService thirdPartyLogService; + + /** + * 分页查询 + * @param page 分页对象 + * @param thirdPartyLog 单点日志记录 + * @return + */ + @Operation(summary = "分页查询" , description = "分页查询" ) + @GetMapping("/page" ) + @PreAuthorize("@pms.hasPermission('thirdparty_thirdPartyLog_view')" ) + public R getThirdPartyLogPage(@ParameterObject Page page, @ParameterObject ThirdPartyLogEntity thirdPartyLog) { + LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); + return R.ok(thirdPartyLogService.page(page, wrapper)); + } + + + /** + * 通过id查询单点日志记录 + * @param id id + * @return R + */ + @Operation(summary = "通过id查询" , description = "通过id查询" ) + @GetMapping("/{id}" ) + @PreAuthorize("@pms.hasPermission('thirdparty_thirdPartyLog_view')" ) + public R getById(@PathVariable("id" ) Long id) { + return R.ok(thirdPartyLogService.getById(id)); + } + + /** + * 新增单点日志记录 + * @param thirdPartyLog 单点日志记录 + * @return R + */ + @Operation(summary = "新增单点日志记录" , description = "新增单点日志记录" ) + @SysLog("新增单点日志记录" ) + @PostMapping + @PreAuthorize("@pms.hasPermission('thirdparty_thirdPartyLog_add')" ) + public R save(@RequestBody ThirdPartyLogEntity thirdPartyLog) { + return R.ok(thirdPartyLogService.save(thirdPartyLog)); + } + + /** + * 修改单点日志记录 + * @param thirdPartyLog 单点日志记录 + * @return R + */ + @Operation(summary = "修改单点日志记录" , description = "修改单点日志记录" ) + @SysLog("修改单点日志记录" ) + @PutMapping + @PreAuthorize("@pms.hasPermission('thirdparty_thirdPartyLog_edit')" ) + public R updateById(@RequestBody ThirdPartyLogEntity thirdPartyLog) { + return R.ok(thirdPartyLogService.updateById(thirdPartyLog)); + } + + /** + * 通过id删除单点日志记录 + * @param ids id列表 + * @return R + */ + @Operation(summary = "通过id删除单点日志记录" , description = "通过id删除单点日志记录" ) + @SysLog("通过id删除单点日志记录" ) + @DeleteMapping + @PreAuthorize("@pms.hasPermission('thirdparty_thirdPartyLog_del')" ) + public R removeById(@RequestBody Long[] ids) { + return R.ok(thirdPartyLogService.removeBatchByIds(CollUtil.toList(ids))); + } + + + /** + * 导出excel 表格 + * @param thirdPartyLog 查询条件 + * @param ids 导出指定ID + * @return excel 文件流 + */ + @ResponseExcel + @GetMapping("/export") + @PreAuthorize("@pms.hasPermission('thirdparty_thirdPartyLog_export')" ) + public List export(ThirdPartyLogEntity thirdPartyLog,Long[] ids) { + return thirdPartyLogService.list(Wrappers.lambdaQuery(thirdPartyLog).in(ArrayUtil.isNotEmpty(ids), ThirdPartyLogEntity::getId, ids)); + } +} \ No newline at end of file diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyOAth2Cotroller.java b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyOAth2Cotroller.java index 1a6f8584e..4f317bf99 100644 --- a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyOAth2Cotroller.java +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/controller/ThirdPartyOAth2Cotroller.java @@ -21,6 +21,7 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.apache.commons.lang3.StringUtils; import org.apache.seata.spring.annotation.GlobalTransactional; import org.springframework.http.HttpHeaders; import org.springframework.transaction.annotation.Transactional; @@ -74,6 +75,9 @@ public class ThirdPartyOAth2Cotroller { @GlobalTransactional @Transactional(rollbackFor = Exception.class) public R zzdsso(@PathVariable(name = "token") String token) { + if (StringUtils.isBlank(token)){ + return R.failed("token不能为空"); + } //获取数字政务token通过浙政钉前端获取的token,调试阶段先写死 // String json = "{" + diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/entity/ThirdPartyLogEntity.java b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/entity/ThirdPartyLogEntity.java new file mode 100644 index 000000000..c8dbb2b39 --- /dev/null +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/entity/ThirdPartyLogEntity.java @@ -0,0 +1,107 @@ +package com.bsd.pjcloud.thirdparty.entity; + +import com.baomidou.mybatisplus.annotation.*; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; +import com.bsd.pjcloud.common.core.util.TenantTable; +import java.time.LocalDateTime; + +/** + * 单点日志记录 + * + * @author zjf + * @date 2025-03-31 13:48:37 + */ +@Data +@TenantTable +@TableName("third_party_log") +@EqualsAndHashCode(callSuper = true) +@Schema(description = "单点日志记录") +public class ThirdPartyLogEntity extends Model { + + + /** + * id主键 + */ + @TableId(type = IdType.ASSIGN_ID) + @Schema(description="id主键") + private Long id; + + /** + * 接口名称 + */ + @Schema(description="接口名称") + private String apiName; + + /** + * 是否删除 + */ + @Schema(description="是否删除") + private Boolean deleted; + + /** + * 请求路径 + */ + @Schema(description="请求路径") + private String requestUrl; + + /** + * 状态码 + */ + @Schema(description="状态码") + private String httpStatus; + + /** + * 原始响应 + */ + @Schema(description="原始响应") + private String responseBody; + + /** + * 错误码(如-1) + */ + @Schema(description="错误码(如-1)") + private String errorCode; + + /** + * 错误描述 + */ + @Schema(description="错误描述") + private String errorMsg; + + /** + * 创建者 + */ + @TableField(fill = FieldFill.INSERT) + @Schema(description="创建者") + private String createBy; + + /** + * 创建时间 + */ + @TableField(fill = FieldFill.INSERT) + @Schema(description="创建时间") + private LocalDateTime createTime; + + /** + * 更新者 + */ + @TableField(fill = FieldFill.INSERT_UPDATE) + @Schema(description="更新者") + private String updateBy; + + /** + * 更新时间 + */ + @TableField(fill = FieldFill.INSERT_UPDATE) + @Schema(description="更新时间") + private LocalDateTime updateTime; + + /** + * 租户编号 + */ + @Schema(description="租户编号") + private Long tenantId; +} \ No newline at end of file diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/exception/ThirdPartyException.java b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/exception/ThirdPartyException.java new file mode 100644 index 000000000..1ee33e4ad --- /dev/null +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/exception/ThirdPartyException.java @@ -0,0 +1,14 @@ +package com.bsd.pjcloud.thirdparty.exception; + +import lombok.Data; + +@Data +public class ThirdPartyException extends RuntimeException { + private String code; // 前端识别码 + + public ThirdPartyException(String message, String code) { + super(message); + this.code = code; + } + +} \ No newline at end of file diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/mapper/ThirdPartyLogMapper.java b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/mapper/ThirdPartyLogMapper.java new file mode 100644 index 000000000..442098b09 --- /dev/null +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/mapper/ThirdPartyLogMapper.java @@ -0,0 +1,11 @@ +package com.bsd.pjcloud.thirdparty.mapper; + +import com.bsd.pjcloud.common.data.datascope.PjcloudBaseMapper; +import com.bsd.pjcloud.thirdparty.entity.ThirdPartyLogEntity; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface ThirdPartyLogMapper extends PjcloudBaseMapper { + + +} \ No newline at end of file diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/ThirdPartyLogService.java b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/ThirdPartyLogService.java new file mode 100644 index 000000000..abeb1b631 --- /dev/null +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/ThirdPartyLogService.java @@ -0,0 +1,12 @@ +package com.bsd.pjcloud.thirdparty.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.bsd.pjcloud.thirdparty.entity.ThirdPartyLogEntity; + +public interface ThirdPartyLogService extends IService { + + + + + void saveLog(ThirdPartyLogEntity thirdPartyLogEntity); +} \ No newline at end of file diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/ThirdPartyOAuth2Service.java b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/ThirdPartyOAuth2Service.java index c33870204..6e9873e56 100644 --- a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/ThirdPartyOAuth2Service.java +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/ThirdPartyOAuth2Service.java @@ -1,11 +1,14 @@ package com.bsd.pjcloud.thirdparty.service; +import com.alibaba.fastjson.JSON; import com.bsd.pjcloud.admin.api.entity.SysUser; import com.bsd.pjcloud.common.core.util.R; import com.bsd.pjcloud.thirdparty.config.OAuth2ClientConfig; import com.bsd.pjcloud.thirdparty.dto.*; import com.bsd.pjcloud.thirdparty.entity.ThirdPartyDeptEntity; +import com.bsd.pjcloud.thirdparty.entity.ThirdPartyLogEntity; import com.bsd.pjcloud.thirdparty.entity.ThirdPartyUserEntity; +import com.bsd.pjcloud.thirdparty.exception.ThirdPartyException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; @@ -19,6 +22,8 @@ import org.springframework.util.DigestUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; import java.nio.charset.StandardCharsets; @@ -47,6 +52,8 @@ public class ThirdPartyOAuth2Service { private final ThirdPartyUserService partyUserService; + private final ThirdPartyLogService thirdPartyLogService; + public TokenDTO getAccessToken() { TokenDTO cachedToken = tokenCache.get("access_token"); @@ -384,7 +391,9 @@ public class ThirdPartyOAuth2Service { * @return */ public ZZDUserDTO getZZDUserInfo(String zzdToken) { + // 构建请求URL + try { String url = baseUrl + config.getZzdShUserInfoUrl()+"?token=" + zzdToken; // 获取 Bearer Token String accessToken = getAccessToken().getAccess_token(); @@ -400,17 +409,33 @@ public class ThirdPartyOAuth2Service { ResponseEntity> response = restTemplate.exchange( url, HttpMethod.GET, requestEntity, responseType ); + if (response.getBody().getCode().equals("-1")){ log.error("获取浙政钉/诗画用户信息失败"); log.error("token",zzdToken); - log.error("response",response.getBody().getDescription()); log.error("response",response.getBody()); + ThirdPartyLogEntity thirdPartyLogEntity = new ThirdPartyLogEntity(); + thirdPartyLogEntity.setApiName("获取浙政钉/诗画用户信息失败"); + thirdPartyLogEntity.setRequestUrl(url); + thirdPartyLogEntity.setResponseBody(JSON.toJSONString(response.getBody())); + thirdPartyLogEntity.setErrorCode(response.getBody().getCode()); + thirdPartyLogEntity.setErrorMsg(response.getBody().getDescription()); + thirdPartyLogEntity.setHttpStatus(response.getStatusCode().toString()); + thirdPartyLogEntity.setCreateBy("thirdPartylog"); + thirdPartyLogService.saveLog(thirdPartyLogEntity); throw new RuntimeException("获取浙政钉用户信息失败"); } return response.getBody().getData(); - + } catch (HttpStatusCodeException e) { + log.error("第三方接口HTTP异常 状态码:{} 响应:{}", e.getRawStatusCode(), e.getResponseBodyAsString()); + throw new ThirdPartyException("第三方服务异常,请联系管理员", "THIRD_PARTY_500"); + } catch (ResourceAccessException e) { + log.error("第三方接口网络异常", e); + throw new ThirdPartyException("第三方服务暂时不可用,请稍后重试", "NETWORK_ERROR"); } + } + /** diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/impl/ThirdPartyLogServiceImpl.java b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/impl/ThirdPartyLogServiceImpl.java new file mode 100644 index 000000000..9a11fdabb --- /dev/null +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/java/com/bsd/pjcloud/thirdparty/service/impl/ThirdPartyLogServiceImpl.java @@ -0,0 +1,26 @@ +package com.bsd.pjcloud.thirdparty.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.bsd.pjcloud.thirdparty.entity.ThirdPartyLogEntity; +import com.bsd.pjcloud.thirdparty.mapper.ThirdPartyLogMapper; +import com.bsd.pjcloud.thirdparty.service.ThirdPartyLogService; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +/** + * 单点日志记录 + * + * @author zjf + * @date 2025-03-31 13:48:37 + */ +@Service +public class ThirdPartyLogServiceImpl extends ServiceImpl implements ThirdPartyLogService { + @Override + @Async + @Transactional(propagation = Propagation.REQUIRES_NEW) + public void saveLog(ThirdPartyLogEntity thirdPartyLogEntity) { + baseMapper.insert(thirdPartyLogEntity); + } +} \ No newline at end of file diff --git a/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyLogMapper.xml b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyLogMapper.xml new file mode 100644 index 000000000..cdbed5022 --- /dev/null +++ b/pjcloud-thirdparty/pjcloud-thirdparty-biz/src/main/resources/mapper/ThirdPartyLogMapper.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file