新增了oss签名接口
This commit is contained in:
parent
bd5a9f819a
commit
e3b3cca738
@ -97,6 +97,7 @@ public class CaseExchangeController {
|
|||||||
page,
|
page,
|
||||||
request.getKeyword(),
|
request.getKeyword(),
|
||||||
request.getUserId(),
|
request.getUserId(),
|
||||||
|
request.getIsSelected(),
|
||||||
request.handleOrder()
|
request.handleOrder()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -1,22 +1,37 @@
|
|||||||
package com.example.caseData.controller;
|
package com.example.caseData.controller;
|
||||||
|
|
||||||
import com.example.caseData.common.Response;
|
import com.example.caseData.common.Response;
|
||||||
import com.example.caseData.dto.PublicDto;
|
import com.example.caseData.config.EnvConfig;
|
||||||
import com.example.caseData.request.PublicRequest;
|
import com.example.caseData.dto.publicDto.GetOssSignDto;
|
||||||
|
import com.example.caseData.dto.publicDto.LoginDto;
|
||||||
|
import com.example.caseData.dto.user.UserDto;
|
||||||
|
import com.example.caseData.extend.aliyun.Oss;
|
||||||
|
import com.example.caseData.request.publicRequest.GetOssSignRequest;
|
||||||
|
import com.example.caseData.request.publicRequest.LoginRequest;
|
||||||
|
import com.example.caseData.request.UserRequest.UserRequest;
|
||||||
import com.example.caseData.service.UserService;
|
import com.example.caseData.service.UserService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
public class PublicController {
|
public class PublicController {
|
||||||
@Resource
|
@Resource
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private HttpServletRequest httpServletRequest;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private EnvConfig envConfig;
|
||||||
|
|
||||||
// 登陆
|
// 登陆
|
||||||
@PostMapping("/login/wechat/mobile")
|
@PostMapping("/login/wechat/mobile")
|
||||||
public Response<PublicDto> login(@Validated({PublicRequest.Login.class}) @ModelAttribute PublicRequest request) {
|
public Response<LoginDto> login(@Validated({LoginRequest.Login.class}) @ModelAttribute LoginRequest request) {
|
||||||
// 微信手机号授权登录
|
// 微信手机号授权登录
|
||||||
// 获取手机号
|
// 获取手机号
|
||||||
// 获取用户openid
|
// 获取用户openid
|
||||||
@ -25,8 +40,28 @@ public class PublicController {
|
|||||||
String phone = "18221234167";
|
String phone = "18221234167";
|
||||||
|
|
||||||
// 用户登陆
|
// 用户登陆
|
||||||
PublicDto g = userService.UserLogin(phone);
|
LoginDto g = userService.UserLogin(phone);
|
||||||
|
|
||||||
return Response.success(g);
|
return Response.success(g);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取签名
|
||||||
|
@GetMapping("/sign/oss")
|
||||||
|
public Response<GetOssSignDto> GetOssSign(
|
||||||
|
@Validated()
|
||||||
|
@ModelAttribute GetOssSignRequest request
|
||||||
|
) {
|
||||||
|
String userId = (String) httpServletRequest.getAttribute("userId");
|
||||||
|
|
||||||
|
String ossPath = "dev/";
|
||||||
|
if (Objects.equals(envConfig.getActive(), "prod")){
|
||||||
|
ossPath = "prod/";
|
||||||
|
}
|
||||||
|
|
||||||
|
ossPath = ossPath + "static/images/exchange/";
|
||||||
|
|
||||||
|
// 生成签名
|
||||||
|
GetOssSignDto g = Oss.getOssSign(ossPath);
|
||||||
|
return Response.success(g);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,6 +26,7 @@ public interface CaseExchangeDao extends BaseMapper<CaseExchangeModel> {
|
|||||||
Page<?> page,
|
Page<?> page,
|
||||||
@Param("keyword") String keyword,
|
@Param("keyword") String keyword,
|
||||||
@Param("userId") String userId,
|
@Param("userId") String userId,
|
||||||
|
@Param("isSelected") Integer isSelected,
|
||||||
@Param("order") Map<String, String> order
|
@Param("order") Map<String, String> order
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -52,6 +52,12 @@ public class CaseExchangeDto {
|
|||||||
@JsonProperty("collect_num")
|
@JsonProperty("collect_num")
|
||||||
private Integer collectNum;
|
private Integer collectNum;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 评论量
|
||||||
|
*/
|
||||||
|
@JsonProperty("comment_num")
|
||||||
|
private Integer commentNum;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发表时间
|
* 发表时间
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -0,0 +1,49 @@
|
|||||||
|
package com.example.caseData.dto.publicDto;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class GetOssSignDto {
|
||||||
|
/**
|
||||||
|
* 临时访问密钥 ID
|
||||||
|
*/
|
||||||
|
@JsonProperty("access_id")
|
||||||
|
private String accessId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传目标地址 Host
|
||||||
|
*/
|
||||||
|
@JsonProperty("host")
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 策略 Policy
|
||||||
|
*/
|
||||||
|
@JsonProperty("policy")
|
||||||
|
private String policy;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 签名结果
|
||||||
|
*/
|
||||||
|
@JsonProperty("signature")
|
||||||
|
private String signature;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 签名过期时间(秒级时间戳)
|
||||||
|
*/
|
||||||
|
@JsonProperty("expire")
|
||||||
|
private Long expire;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回调配置
|
||||||
|
*/
|
||||||
|
@JsonProperty("callback")
|
||||||
|
private String callback;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传目录路径
|
||||||
|
*/
|
||||||
|
@JsonProperty("dir")
|
||||||
|
private String dir;
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.example.caseData.dto;
|
package com.example.caseData.dto.publicDto;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
|
|||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class PublicDto {
|
public class LoginDto {
|
||||||
@JsonProperty("user_id")
|
@JsonProperty("user_id")
|
||||||
private String userId; // 主键id
|
private String userId; // 主键id
|
||||||
|
|
||||||
@ -3,32 +3,48 @@ package com.example.caseData.extend.aliyun;
|
|||||||
import com.aliyun.oss.OSS;
|
import com.aliyun.oss.OSS;
|
||||||
import com.aliyun.oss.OSSClientBuilder;
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
import com.aliyun.oss.model.*;
|
import com.aliyun.oss.model.*;
|
||||||
|
import com.example.caseData.config.JwtConfig;
|
||||||
|
import com.example.caseData.config.OssConfig;
|
||||||
|
import com.example.caseData.dto.publicDto.GetOssSignDto;
|
||||||
|
import com.example.caseData.middlewares.LogRequestInterceptor;
|
||||||
|
import com.example.caseData.utils.JwtUtil;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
public class Oss {
|
public class Oss {
|
||||||
private static final String ENDPOINT = "your-endpoint";
|
private static OssConfig ossConfig;
|
||||||
private static final String ACCESS_KEY_ID = "your-access-key-id";
|
|
||||||
private static final String ACCESS_KEY_SECRET = "your-access-key-secret";
|
private static final Logger logger = LoggerFactory.getLogger(LogRequestInterceptor.class);
|
||||||
private static final String BUCKET_NAME = "your-bucket";
|
|
||||||
private static final String CALLBACK_URL = "your-callback-url";
|
public Oss(OssConfig ossConfig ){
|
||||||
private static final String HOST = "https://your-bucket.your-endpoint";
|
Oss.ossConfig = ossConfig;
|
||||||
|
}
|
||||||
|
|
||||||
public static OSS createClient() {
|
public static OSS createClient() {
|
||||||
return new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
|
return new OSSClientBuilder().build(ossConfig.getEndpoint(), ossConfig.getAccessKey(), ossConfig.getAccessKeySecret());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean putObject(String fileName, byte[] content) {
|
public static boolean putObject(String fileName, byte[] content) {
|
||||||
OSS client = createClient();
|
OSS client = createClient();
|
||||||
try (InputStream input = new ByteArrayInputStream(content)) {
|
try (InputStream input = new ByteArrayInputStream(content)) {
|
||||||
client.putObject(BUCKET_NAME, fileName, input);
|
client.putObject(ossConfig.getBucket(), fileName, input);
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
logger.info(e.getMessage());
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
client.shutdown();
|
client.shutdown();
|
||||||
@ -38,11 +54,11 @@ public class Oss {
|
|||||||
public static String getObjectToString(String fileName) {
|
public static String getObjectToString(String fileName) {
|
||||||
OSS client = createClient();
|
OSS client = createClient();
|
||||||
try {
|
try {
|
||||||
OSSObject ossObject = client.getObject(BUCKET_NAME, fileName);
|
OSSObject ossObject = client.getObject(ossConfig.getBucket(), fileName);
|
||||||
InputStream content = ossObject.getObjectContent();
|
InputStream content = ossObject.getObjectContent();
|
||||||
return new String(content.readAllBytes());
|
return new String(content.readAllBytes());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
logger.info(e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
client.shutdown();
|
client.shutdown();
|
||||||
@ -52,17 +68,18 @@ public class Oss {
|
|||||||
public static boolean downloadObjectToFile(String fileName, String localPath) {
|
public static boolean downloadObjectToFile(String fileName, String localPath) {
|
||||||
OSS client = createClient();
|
OSS client = createClient();
|
||||||
try {
|
try {
|
||||||
client.getObject(new GetObjectRequest(BUCKET_NAME, fileName), new java.io.File(localPath));
|
client.getObject(new GetObjectRequest(ossConfig.getBucket(), fileName), new java.io.File(localPath));
|
||||||
return true;
|
return true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
logger.info(e.getMessage());
|
||||||
return false;
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
client.shutdown();
|
client.shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, Object> getOssSign(String dir) {
|
// 获取oss签名
|
||||||
|
public static GetOssSignDto getOssSign(String dir) {
|
||||||
long expireTime = 30;
|
long expireTime = 30;
|
||||||
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
|
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
|
||||||
Date expiration = new Date(expireEndTime);
|
Date expiration = new Date(expireEndTime);
|
||||||
@ -74,21 +91,22 @@ public class Oss {
|
|||||||
OSS client = createClient();
|
OSS client = createClient();
|
||||||
try {
|
try {
|
||||||
String postPolicy = client.generatePostPolicy(expiration, conditions);
|
String postPolicy = client.generatePostPolicy(expiration, conditions);
|
||||||
byte[] binaryData = postPolicy.getBytes("utf-8");
|
byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
|
||||||
String encodedPolicy = Base64.getEncoder().encodeToString(binaryData);
|
String encodedPolicy = Base64.getEncoder().encodeToString(binaryData);
|
||||||
String signature = client.calculatePostSignature(postPolicy);
|
String signature = client.calculatePostSignature(postPolicy);
|
||||||
|
|
||||||
Map<String, Object> respMap = new HashMap<>();
|
GetOssSignDto g = new GetOssSignDto();
|
||||||
respMap.put("access_id", ACCESS_KEY_ID);
|
g.setAccessId(ossConfig.getAccessKey());
|
||||||
respMap.put("policy", encodedPolicy);
|
g.setPolicy(encodedPolicy);
|
||||||
respMap.put("signature", signature);
|
g.setSignature(signature);
|
||||||
respMap.put("dir", dir);
|
g.setDir(dir);
|
||||||
respMap.put("host", HOST);
|
g.setHost(ossConfig.getCustomDomainName());
|
||||||
respMap.put("expire", expireEndTime / 1000);
|
g.setExpire( expireEndTime / 1000);
|
||||||
respMap.put("callback", CALLBACK_URL);
|
g.setCallback("");
|
||||||
return respMap;
|
|
||||||
|
return g;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
logger.info(e.getMessage());
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
client.shutdown();
|
client.shutdown();
|
||||||
|
|||||||
@ -37,6 +37,12 @@ public class CaseExchangeModel {
|
|||||||
@TableField("exchange_status")
|
@TableField("exchange_status")
|
||||||
private Integer exchangeStatus;
|
private Integer exchangeStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否被精选(0:否 1:是)
|
||||||
|
*/
|
||||||
|
@TableField("is_selected")
|
||||||
|
private Integer isSelected;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 阅读量
|
* 阅读量
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -27,6 +27,10 @@ public class getCaseExchangeSearchPage {
|
|||||||
@JsonProperty("user_id")
|
@JsonProperty("user_id")
|
||||||
private String userId;
|
private String userId;
|
||||||
|
|
||||||
|
// 是否被精选(0:否 1:是)
|
||||||
|
@JsonProperty("is_selected")
|
||||||
|
private Integer isSelected;
|
||||||
|
|
||||||
// 排序字段
|
// 排序字段
|
||||||
private OrderRequest order;
|
private OrderRequest order;
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,15 @@
|
|||||||
|
package com.example.caseData.request.publicRequest;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.Max;
|
||||||
|
import jakarta.validation.constraints.Min;
|
||||||
|
import jakarta.validation.constraints.NotEmpty;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class GetOssSignRequest {
|
||||||
|
@NotNull(message = "错误请求")
|
||||||
|
@Min(value = 1, message = "错误请求")
|
||||||
|
@Max(value = 1, message = "错误请求")
|
||||||
|
private String scene;
|
||||||
|
}
|
||||||
@ -1,10 +1,10 @@
|
|||||||
package com.example.caseData.request;
|
package com.example.caseData.request.publicRequest;
|
||||||
|
|
||||||
import jakarta.validation.constraints.*;
|
import jakarta.validation.constraints.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class PublicRequest {
|
public class LoginRequest {
|
||||||
// ✅ 自定义校验分组
|
// ✅ 自定义校验分组
|
||||||
public interface Login {}
|
public interface Login {}
|
||||||
|
|
||||||
@ -1,13 +1,11 @@
|
|||||||
package com.example.caseData.service;
|
package com.example.caseData.service;
|
||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
|
||||||
import cn.hutool.crypto.SecureUtil;
|
import cn.hutool.crypto.SecureUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
||||||
import com.example.caseData.dao.BasicHospitalDao;
|
import com.example.caseData.dao.BasicHospitalDao;
|
||||||
import com.example.caseData.dao.CaseClinicalDoctorDao;
|
import com.example.caseData.dao.CaseClinicalDoctorDao;
|
||||||
import com.example.caseData.dao.UserDao;
|
import com.example.caseData.dao.UserDao;
|
||||||
import com.example.caseData.dto.PublicDto;
|
import com.example.caseData.dto.publicDto.LoginDto;
|
||||||
import com.example.caseData.exception.BusinessException;
|
import com.example.caseData.exception.BusinessException;
|
||||||
import com.example.caseData.extend.app.Hospital.GetHospitalByUuidResponse;
|
import com.example.caseData.extend.app.Hospital.GetHospitalByUuidResponse;
|
||||||
import com.example.caseData.extend.app.Hospital.Hospital;
|
import com.example.caseData.extend.app.Hospital.Hospital;
|
||||||
@ -20,13 +18,9 @@ import com.example.caseData.utils.JwtUtil;
|
|||||||
import com.example.caseData.utils.Replace;
|
import com.example.caseData.utils.Replace;
|
||||||
import com.example.caseData.utils.StringToInt;
|
import com.example.caseData.utils.StringToInt;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.apache.el.parser.Token;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import static com.baomidou.mybatisplus.extension.toolkit.Db.save;
|
import static com.baomidou.mybatisplus.extension.toolkit.Db.save;
|
||||||
@ -56,7 +50,7 @@ public class UserService {
|
|||||||
* @return UserModel
|
* @return UserModel
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
public PublicDto UserLogin(String phone) throws BusinessException {
|
public LoginDto UserLogin(String phone) throws BusinessException {
|
||||||
// 获取app用户数据
|
// 获取app用户数据
|
||||||
UserModel user = GetAppUserInfoByPhone(phone);
|
UserModel user = GetAppUserInfoByPhone(phone);
|
||||||
|
|
||||||
@ -69,7 +63,7 @@ public class UserService {
|
|||||||
CaseClinicalDoctorModel caseClinicalDoctor = caseClinicalDoctorDao.selectOne(caseClinicalDoctorWrapper);
|
CaseClinicalDoctorModel caseClinicalDoctor = caseClinicalDoctorDao.selectOne(caseClinicalDoctorWrapper);
|
||||||
|
|
||||||
// 处理返回值
|
// 处理返回值
|
||||||
PublicDto g = new PublicDto();
|
LoginDto g = new LoginDto();
|
||||||
g.setUserId(String.valueOf(user.getUserId()));
|
g.setUserId(String.valueOf(user.getUserId()));
|
||||||
g.setUserName(user.getUserName());
|
g.setUserName(user.getUserName());
|
||||||
g.setAvatar(Replace.addOssDomain(user.getAvatar()));
|
g.setAvatar(Replace.addOssDomain(user.getAvatar()));
|
||||||
|
|||||||
@ -13,6 +13,9 @@
|
|||||||
LEFT JOIN case_exchange_label b ON a.exchange_id = b.exchange_id
|
LEFT JOIN case_exchange_label b ON a.exchange_id = b.exchange_id
|
||||||
LEFT JOIN user c ON c.user_id = a.user_id
|
LEFT JOIN user c ON c.user_id = a.user_id
|
||||||
WHERE a.exchange_status = 1
|
WHERE a.exchange_status = 1
|
||||||
|
<if test="isSelected != null and isSelected != ''">
|
||||||
|
AND c.is_selected = ${isSelected}
|
||||||
|
</if>
|
||||||
<if test="keyword != null and keyword != ''">
|
<if test="keyword != null and keyword != ''">
|
||||||
AND (
|
AND (
|
||||||
a.exchange_title LIKE CONCAT('%', #{keyword}, '%')
|
a.exchange_title LIKE CONCAT('%', #{keyword}, '%')
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user