From 348c08c495ecda43411d1cf2f9b6f651e23a64f1 Mon Sep 17 00:00:00 2001 From: wucongxing8150 <815046773@qq.com> Date: Thu, 26 Jun 2025 14:44:49 +0800 Subject: [PATCH] 6666 --- .../sa/admin/extend/fangxinqian/Base.java | 124 +++++ .../sa/admin/extend/fangxinqian/Company.java | 7 + .../sa/admin/extend/fangxinqian/Personal.java | 7 + .../expert/controller/ExpertController.java | 6 +- .../app/medicalrecord/domain/DpmsAddForm.java | 1 - .../src/main/resources/dev/application.yaml | 14 +- sa-common/pom.xml | 1 - .../sa/common/common/code/UserErrorCode.java | 4 +- .../module/support/redis/RedisService.java | 445 +++++++++--------- 9 files changed, 379 insertions(+), 230 deletions(-) create mode 100644 sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Base.java create mode 100644 sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Company.java create mode 100644 sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Personal.java diff --git a/sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Base.java b/sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Base.java new file mode 100644 index 0000000..1ff751f --- /dev/null +++ b/sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Base.java @@ -0,0 +1,124 @@ +package net.lab1024.sa.admin.extend.fangxinqian; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import net.lab1024.sa.common.common.exception.BusinessException; +import net.lab1024.sa.common.module.support.redis.RedisService; +import org.springframework.beans.factory.annotation.Value; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; + +@Slf4j +public class Base { + private static final ObjectMapper objectMapper = new ObjectMapper(); + + @Value("${fxq.client-id}") + private static String clientId; + + @Value("${fxq.client-secret}") + private static String clientSecret; + + @Value("${fxq.client-url}") + private static String clientUrl; + + @Resource + private RedisService redisService; + + /** + * 发送 POST JSON 请求 + * @param url 请求地址 + * @param jsonData JSON 字符串 + * @param headers 请求头 + * @return 响应字符串 + */ + public String postJson(String url, String jsonData, Map headers) { + // 获取token + String tokenKey = "fangxinqian:" + clientId; + String token = redisService.get(tokenKey); + if (token == null) { + token = getAccessToken(); + } + + // 刷新token + HttpRequest request = HttpRequest.post(url) + .body(jsonData) + .header("Content-Type", "application/json") + .header("Authorization", "Bearer " + token); + + if (headers != null) { + headers.forEach(request::header); + } + + try (HttpResponse response = request.execute()) { + JSONObject json = JSONUtil.parseObj(response.body()); + + log.info("获取app数据返回:{}",json); + + int code = json.getInt("code", -1); + if (code == 10000) { + return response.body(); + } else if (code == 10005) { + // token 失效,重新获取 + getAccessToken(); + + return postJson(url,jsonData,headers); + }else{ + throw new BusinessException(json.getStr("message", "操作失败")); + } + } + } + + public String getAccessToken(){ + String url = String.format("%s?grant_type=client_credentials&client_id=%s&client_secret=%s", + "https://saasapi.fangxinqian.cn/oauth/token", clientId, clientSecret); + + HttpRequest request = HttpRequest.post(url); + + try (HttpResponse response = request.execute()) { + if (response.getStatus() != 200) { + throw new BusinessException("操作失败"); + } + + JSONObject json = JSONUtil.parseObj(response.body()); + + log.info("获取app数据返回:{}",json); + + String accessToken = json.getStr("access_token"); + if (accessToken == null || accessToken.isEmpty()) { + throw new BusinessException("操作失败"); + } + + Integer expiresIn = json.getInt("expires_in"); + if (expiresIn == null || expiresIn <= 0) { + throw new BusinessException("操作失败"); + } + + String tokenKey = "fangxinqian:" + clientId; + redisService.set(tokenKey,accessToken,(expiresIn - 120)); + + return accessToken; + } + } + + public class R + { + /** 接口调用状态。200:正常;其它值:调用出错 */ + private int code; + + /** 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok */ + private String msg; + + /** 接口是否调用成功 */ + private boolean success; + + /** 错误信息或提示信息 */ + private String message; + + } +} diff --git a/sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Company.java b/sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Company.java new file mode 100644 index 0000000..d708678 --- /dev/null +++ b/sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Company.java @@ -0,0 +1,7 @@ +package net.lab1024.sa.admin.extend.fangxinqian; + +/** + * 企业账号 + */ +public class Company { +} diff --git a/sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Personal.java b/sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Personal.java new file mode 100644 index 0000000..d55829a --- /dev/null +++ b/sa-admin/src/main/java/net/lab1024/sa/admin/extend/fangxinqian/Personal.java @@ -0,0 +1,7 @@ +package net.lab1024.sa.admin.extend.fangxinqian; + +/** + * 个人账号 + */ +public class Personal { +} diff --git a/sa-admin/src/main/java/net/lab1024/sa/admin/module/app/expert/controller/ExpertController.java b/sa-admin/src/main/java/net/lab1024/sa/admin/module/app/expert/controller/ExpertController.java index 4de0a77..2daf859 100644 --- a/sa-admin/src/main/java/net/lab1024/sa/admin/module/app/expert/controller/ExpertController.java +++ b/sa-admin/src/main/java/net/lab1024/sa/admin/module/app/expert/controller/ExpertController.java @@ -229,8 +229,7 @@ public class ExpertController { // 获取白名单用户-姓名、医院名称 ExpertWhiteEntity expertWhiteEntity = expertWhiteEntityService.getExpertWhiteEntityByNameAndHospitalName(name,hospital_name); if (expertWhiteEntity == null) { - responseDTO.setMsg("暂无登录权限"); - return responseDTO; + return ResponseDTO.error(NO_AUTH); } ExpertEntity expert = new ExpertEntity(); @@ -472,8 +471,7 @@ public class ExpertController { // 获取白名单用户-姓名、医院名称 ExpertWhiteEntity expertWhiteEntity = expertWhiteEntityService.getExpertWhiteEntityByNameAndHospitalName(name,hospital_name); if (expertWhiteEntity == null) { - responseDTO.setMsg("暂无登录权限"); - return responseDTO; + return ResponseDTO.error(NO_AUTH); } ExpertEntity expert = new ExpertEntity(); diff --git a/sa-admin/src/main/java/net/lab1024/sa/admin/module/app/medicalrecord/domain/DpmsAddForm.java b/sa-admin/src/main/java/net/lab1024/sa/admin/module/app/medicalrecord/domain/DpmsAddForm.java index cf31a85..c2ffe6a 100644 --- a/sa-admin/src/main/java/net/lab1024/sa/admin/module/app/medicalrecord/domain/DpmsAddForm.java +++ b/sa-admin/src/main/java/net/lab1024/sa/admin/module/app/medicalrecord/domain/DpmsAddForm.java @@ -9,7 +9,6 @@ import java.time.LocalDateTime; @Data public class DpmsAddForm { - @ApiModelProperty(value = "治疗时间", required = true) @NotNull(message = "治疗时间 不能为空") private LocalDateTime treatTime; diff --git a/sa-admin/src/main/resources/dev/application.yaml b/sa-admin/src/main/resources/dev/application.yaml index 1c323d3..45e0b30 100644 --- a/sa-admin/src/main/resources/dev/application.yaml +++ b/sa-admin/src/main/resources/dev/application.yaml @@ -12,6 +12,13 @@ server: spring: profiles: active: '@profiles.active@' + data: + redis: + host: '139.155.127.177' + port: 30002 + password: gdxz2022&dj. + database: 11 + timeout: 3000 # 项目配置 project: @@ -40,4 +47,9 @@ igandan: token: XUUHml5iQ9mlFsa8QqOwyBrLI2nGGGxJ platform: case doc: - host: https://dev-doc.igandan.com/app/ \ No newline at end of file + host: https://dev-doc.igandan.com/app/ + +fxq: + client-id: Md7w4eaZih + client-secret: 97acf8ebb09641cbb90accf06e74ccf5 + client-url: https://saasapi.fangxinqian.cn/openapi/v2/ \ No newline at end of file diff --git a/sa-common/pom.xml b/sa-common/pom.xml index 5359c9a..4ee221b 100644 --- a/sa-common/pom.xml +++ b/sa-common/pom.xml @@ -260,7 +260,6 @@ commons-io 2.11.0 - diff --git a/sa-common/src/main/java/net/lab1024/sa/common/common/code/UserErrorCode.java b/sa-common/src/main/java/net/lab1024/sa/common/common/code/UserErrorCode.java index 39869ea..fbc079f 100644 --- a/sa-common/src/main/java/net/lab1024/sa/common/common/code/UserErrorCode.java +++ b/sa-common/src/main/java/net/lab1024/sa/common/common/code/UserErrorCode.java @@ -37,7 +37,9 @@ public enum UserErrorCode implements ErrorCode { NO_PERMISSION_EDIT(30010, "该状态不允许修改"), NO_MODIFY(30011, "该账号无需补充资料"), PROJECT_CLOSE(30012, "本年度人工肝病例登记项目已结束,谢谢您的关注!"), - ExpertBankVerifyFail(30013, "银行卡认证失败"); + ExpertBankVerifyFail(30013, "银行卡认证失败"), + + NO_AUTH(30014, "对不起,您暂无权限访问该项目~"); private final int code; diff --git a/sa-common/src/main/java/net/lab1024/sa/common/module/support/redis/RedisService.java b/sa-common/src/main/java/net/lab1024/sa/common/module/support/redis/RedisService.java index f332192..4723113 100644 --- a/sa-common/src/main/java/net/lab1024/sa/common/module/support/redis/RedisService.java +++ b/sa-common/src/main/java/net/lab1024/sa/common/module/support/redis/RedisService.java @@ -1,222 +1,223 @@ -//package net.lab1024.sa.common.module.support.redis; -// -//import com.alibaba.fastjson.JSON; -//import net.lab1024.sa.common.common.domain.SystemEnvironment; -//import net.lab1024.sa.common.common.enumeration.SystemEnvironmentEnum; -//import net.lab1024.sa.common.common.util.SmartStringUtil; -//import net.lab1024.sa.common.constant.RedisKeyConst; -//import org.slf4j.Logger; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.data.redis.core.*; -//import org.springframework.stereotype.Component; -//import org.springframework.util.CollectionUtils; -// -//import java.time.LocalDate; -//import java.time.LocalDateTime; -//import java.time.LocalTime; -//import java.time.temporal.ChronoUnit; -//import java.util.Collection; -//import java.util.List; -//import java.util.concurrent.TimeUnit; -// -///** -// * redis 一顿操作 -// * -// * @Author 1024创新实验室: 罗伊 -// * @Date 2020/8/25 21:57 -// * @Wechat zhuoda1024 -// * @Email lab1024@163.com -// * @Copyright 1024创新实验室 ( https://1024lab.net ) -// */ -////@Component -//public class RedisService { -// -// private static final Logger log = org.slf4j.LoggerFactory.getLogger(RedisService.class); -// -// @Autowired -// private StringRedisTemplate stringRedisTemplate; -// -// @Autowired -// private RedisTemplate redisTemplate; -// -// @Autowired -// private ValueOperations redisValueOperations; -// -// @Autowired -// private HashOperations redisHashOperations; -// -// @Autowired -// private ListOperations redisListOperations; -// -// @Autowired -// private SetOperations redisSetOperations; -// -// @Autowired -// private SystemEnvironment systemEnvironment; -// -// -// /** -// * 生成redis key -// * @param prefix -// * @param key -// * @return -// */ -// public String generateRedisKey(String prefix, String key) { -// SystemEnvironmentEnum currentEnvironment = systemEnvironment.getCurrentEnvironment(); -// return systemEnvironment.getProjectName() + RedisKeyConst.SEPARATOR + currentEnvironment.getValue() + RedisKeyConst.SEPARATOR + prefix + key; -// } -// -// /** -// * redis key 解析成真实的内容 -// * @param redisKey -// * @return -// */ -// public static String redisKeyParse(String redisKey) { -// if(SmartStringUtil.isBlank(redisKey)){ -// return ""; -// } -// int index = redisKey.lastIndexOf(RedisKeyConst.SEPARATOR); -// if(index < 1){ -// return redisKey; -// } -// return redisKey.substring(index); -// } -// -// public boolean getLock(String key, long expire) { -// return redisValueOperations.setIfAbsent(key, String.valueOf(System.currentTimeMillis()), expire, TimeUnit.MILLISECONDS); -// } -// -// public void unLock(String key) { -// redisValueOperations.getOperations().delete(key); -// } -// -// /** -// * 指定缓存失效时间 -// * -// * @param key 键 -// * @param time 时间(秒) -// * @return -// */ -// public boolean expire(String key, long time) { -// return redisTemplate.expire(key, time, TimeUnit.SECONDS); -// } -// -// /** -// * 获取当天剩余的秒数 -// * -// * @return -// */ -// public static long currentDaySecond() { -// return ChronoUnit.SECONDS.between(LocalDateTime.now(), LocalDateTime.of(LocalDate.now(), LocalTime.MAX)); -// } -// -// /** -// * 根据key 获取过期时间 -// * -// * @param key 键 不能为null -// * @return 时间(秒) 返回0代表为永久有效 -// */ -// public long getExpire(String key) { -// return redisTemplate.getExpire(key, TimeUnit.SECONDS); -// } -// -// /** -// * 判断key是否存在 -// * -// * @param key 键 -// * @return true 存在 false不存在 -// */ -// public boolean hasKey(String key) { -// return redisTemplate.hasKey(key); -// } -// -// /** -// * 删除缓存 -// * -// * @param key 可以传一个值 或多个 -// */ -// @SuppressWarnings("unchecked") -// public void delete(String... key) { -// if (key != null && key.length > 0) { -// if (key.length == 1) { -// redisTemplate.delete(key[0]); -// } else { -// redisTemplate.delete((Collection) CollectionUtils.arrayToList(key)); -// } -// } -// } -// -// /** -// * 删除缓存 -// * -// * @param keyList -// */ -// public void delete(List keyList) { -// if (CollectionUtils.isEmpty(keyList)) { -// return; -// } -// redisTemplate.delete(keyList); -// } -// -// //============================String============================= -// -// /** -// * 普通缓存获取 -// * -// * @param key 键 -// * @return 值 -// */ -// public String get(String key) { -// return key == null ? null : redisValueOperations.get(key); -// } -// -// public T getObject(String key, Class clazz) { -// Object json = this.get(key); -// if (json == null) { -// return null; -// } -// T obj = JSON.parseObject(json.toString(), clazz); -// return obj; -// } -// -// -// /** -// * 普通缓存放入 -// */ -// public void set(String key, String value) { -// redisValueOperations.set(key, value); -// } -// public void set(Object key, Object value) { -// String jsonString = JSON.toJSONString(value); -// redisValueOperations.set(key.toString(), jsonString); -// } -// -// /** -// * 普通缓存放入 -// */ -// public void set(String key, String value, long second) { -// redisValueOperations.set(key, value, second, TimeUnit.SECONDS); -// } -// -// /** -// * 普通缓存放入并设置时间 -// */ -// public void set(Object key, Object value, long time) { -// String jsonString = JSON.toJSONString(value); -// if (time > 0) { -// redisValueOperations.set(key.toString(), jsonString, time, TimeUnit.SECONDS); -// } else { -// set(key.toString(), jsonString); -// } -// } -// -// //============================ map ============================= -// public void mset(String key, String hashKey, Object value) { -// redisHashOperations.put(key, hashKey, value); -// } -// -// public Object mget(String key, String hashKey) { -// return redisHashOperations.get(key, hashKey); -// } -// -//} \ No newline at end of file +package net.lab1024.sa.common.module.support.redis; + +import com.alibaba.fastjson.JSON; +import net.lab1024.sa.common.common.domain.SystemEnvironment; +import net.lab1024.sa.common.common.enumeration.SystemEnvironmentEnum; +import net.lab1024.sa.common.common.util.SmartStringUtil; +import net.lab1024.sa.common.constant.RedisKeyConst; +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.*; +import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; + +import javax.annotation.Resource; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.temporal.ChronoUnit; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * redis 一顿操作 + * + * @Author 1024创新实验室: 罗伊 + * @Date 2020/8/25 21:57 + * @Wechat zhuoda1024 + * @Email lab1024@163.com + * @Copyright 1024创新实验室 ( https://1024lab.net ) + */ +//@Component +public class RedisService { + + private static final Logger log = org.slf4j.LoggerFactory.getLogger(RedisService.class); + + @Resource + private StringRedisTemplate stringRedisTemplate; + + @Resource + private RedisTemplate redisTemplate; + + @Resource + private ValueOperations redisValueOperations; + + @Resource + private HashOperations redisHashOperations; + + @Resource + private ListOperations redisListOperations; + + @Resource + private SetOperations redisSetOperations; + + @Resource + private SystemEnvironment systemEnvironment; + + + /** + * 生成redis key + * @param prefix + * @param key + * @return + */ + public String generateRedisKey(String prefix, String key) { + SystemEnvironmentEnum currentEnvironment = systemEnvironment.getCurrentEnvironment(); + return systemEnvironment.getProjectName() + RedisKeyConst.SEPARATOR + currentEnvironment.getValue() + RedisKeyConst.SEPARATOR + prefix + key; + } + + /** + * redis key 解析成真实的内容 + * @param redisKey + * @return + */ + public static String redisKeyParse(String redisKey) { + if(SmartStringUtil.isBlank(redisKey)){ + return ""; + } + int index = redisKey.lastIndexOf(RedisKeyConst.SEPARATOR); + if(index < 1){ + return redisKey; + } + return redisKey.substring(index); + } + + public boolean getLock(String key, long expire) { + return redisValueOperations.setIfAbsent(key, String.valueOf(System.currentTimeMillis()), expire, TimeUnit.MILLISECONDS); + } + + public void unLock(String key) { + redisValueOperations.getOperations().delete(key); + } + + /** + * 指定缓存失效时间 + * + * @param key 键 + * @param time 时间(秒) + * @return + */ + public boolean expire(String key, long time) { + return redisTemplate.expire(key, time, TimeUnit.SECONDS); + } + + /** + * 获取当天剩余的秒数 + * + * @return + */ + public static long currentDaySecond() { + return ChronoUnit.SECONDS.between(LocalDateTime.now(), LocalDateTime.of(LocalDate.now(), LocalTime.MAX)); + } + + /** + * 根据key 获取过期时间 + * + * @param key 键 不能为null + * @return 时间(秒) 返回0代表为永久有效 + */ + public long getExpire(String key) { + return redisTemplate.getExpire(key, TimeUnit.SECONDS); + } + + /** + * 判断key是否存在 + * + * @param key 键 + * @return true 存在 false不存在 + */ + public boolean hasKey(String key) { + return redisTemplate.hasKey(key); + } + + /** + * 删除缓存 + * + * @param key 可以传一个值 或多个 + */ + @SuppressWarnings("unchecked") + public void delete(String... key) { + if (key != null && key.length > 0) { + if (key.length == 1) { + redisTemplate.delete(key[0]); + } else { + redisTemplate.delete((Collection) CollectionUtils.arrayToList(key)); + } + } + } + + /** + * 删除缓存 + * + * @param keyList + */ + public void delete(List keyList) { + if (CollectionUtils.isEmpty(keyList)) { + return; + } + redisTemplate.delete(keyList); + } + + //============================String============================= + + /** + * 普通缓存获取 + * + * @param key 键 + * @return 值 + */ + public String get(String key) { + return key == null ? null : redisValueOperations.get(key); + } + + public T getObject(String key, Class clazz) { + Object json = this.get(key); + if (json == null) { + return null; + } + T obj = JSON.parseObject(json.toString(), clazz); + return obj; + } + + + /** + * 普通缓存放入 + */ + public void set(String key, String value) { + redisValueOperations.set(key, value); + } + public void set(Object key, Object value) { + String jsonString = JSON.toJSONString(value); + redisValueOperations.set(key.toString(), jsonString); + } + + /** + * 普通缓存放入 + */ + public void set(String key, String value, long second) { + redisValueOperations.set(key, value, second, TimeUnit.SECONDS); + } + + /** + * 普通缓存放入并设置时间 + */ + public void set(Object key, Object value, long time) { + String jsonString = JSON.toJSONString(value); + if (time > 0) { + redisValueOperations.set(key.toString(), jsonString, time, TimeUnit.SECONDS); + } else { + set(key.toString(), jsonString); + } + } + + //============================ map ============================= + public void mset(String key, String hashKey, Object value) { + redisHashOperations.put(key, hashKey, value); + } + + public Object mget(String key, String hashKey) { + return redisHashOperations.get(key, hashKey); + } + +} \ No newline at end of file