fix:修改浙政钉登录接口

This commit is contained in:
zjf committed 2025-03-31 15:49:41 +08:00
1 parent 733c44c05b
commit af5da28b4f
9 files changed
+341 -2

No files matched your search

@@ -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<ThirdPartyLogEntity> 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<ThirdPartyLogEntity> export(ThirdPartyLogEntity thirdPartyLog,Long[] ids) {
return thirdPartyLogService.list(Wrappers.lambdaQuery(thirdPartyLog).in(ArrayUtil.isNotEmpty(ids), ThirdPartyLogEntity::getId, ids));
}
}
@@ -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 = "{" +
@@ -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<ThirdPartyLogEntity> {
/**
* 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;
}
@@ -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;
}
}
@@ -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<ThirdPartyLogEntity> {
}
@@ -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<ThirdPartyLogEntity> {
void saveLog(ThirdPartyLogEntity thirdPartyLogEntity);
}
@@ -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<ApiResponse<ZZDUserDTO>> 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");
}
}
/**
@@ -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<ThirdPartyLogMapper, ThirdPartyLogEntity> implements ThirdPartyLogService {
@Override
@Async
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveLog(ThirdPartyLogEntity thirdPartyLogEntity) {
baseMapper.insert(thirdPartyLogEntity);
}
}
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bsd.pjcloud.thirdparty.mapper.ThirdPartyLogMapper">
<resultMap id="thirdPartyLogMap" type="com.bsd.pjcloud.thirdparty.entity.ThirdPartyLogEntity">
<id property="id" column="id"/>
<result property="apiName" column="api_name"/>
<result property="deleted" column="deleted"/>
<result property="requestUrl" column="request_url"/>
<result property="httpStatus" column="http_status"/>
<result property="responseBody" column="response_body"/>
<result property="errorCode" column="error_code"/>
<result property="errorMsg" column="error_msg"/>
<result property="createBy" column="create_by"/>
<result property="createTime" column="create_time"/>
<result property="updateBy" column="update_by"/>
<result property="updateTime" column="update_time"/>
<result property="tenantId" column="tenant_id"/>
</resultMap>
</mapper>