新增了oss签名接口
This commit is contained in:
parent
bd5a9f819a
commit
e3b3cca738
@ -97,6 +97,7 @@ public class CaseExchangeController {
|
||||
page,
|
||||
request.getKeyword(),
|
||||
request.getUserId(),
|
||||
request.getIsSelected(),
|
||||
request.handleOrder()
|
||||
);
|
||||
|
||||
|
||||
@ -1,22 +1,37 @@
|
||||
package com.example.caseData.controller;
|
||||
|
||||
import com.example.caseData.common.Response;
|
||||
import com.example.caseData.dto.PublicDto;
|
||||
import com.example.caseData.request.PublicRequest;
|
||||
import com.example.caseData.config.EnvConfig;
|
||||
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 jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class PublicController {
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@Resource
|
||||
private HttpServletRequest httpServletRequest;
|
||||
|
||||
@Resource
|
||||
private EnvConfig envConfig;
|
||||
|
||||
// 登陆
|
||||
@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
|
||||
@ -25,8 +40,28 @@ public class PublicController {
|
||||
String phone = "18221234167";
|
||||
|
||||
// 用户登陆
|
||||
PublicDto g = userService.UserLogin(phone);
|
||||
LoginDto g = userService.UserLogin(phone);
|
||||
|
||||
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,
|
||||
@Param("keyword") String keyword,
|
||||
@Param("userId") String userId,
|
||||
@Param("isSelected") Integer isSelected,
|
||||
@Param("order") Map<String, String> order
|
||||
);
|
||||
|
||||
|
||||
@ -52,6 +52,12 @@ public class CaseExchangeDto {
|
||||
@JsonProperty("collect_num")
|
||||
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 lombok.AllArgsConstructor;
|
||||
@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class PublicDto {
|
||||
public class LoginDto {
|
||||
@JsonProperty("user_id")
|
||||
private String userId; // 主键id
|
||||
|
||||
@ -3,32 +3,48 @@ package com.example.caseData.extend.aliyun;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
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.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class Oss {
|
||||
private static final String ENDPOINT = "your-endpoint";
|
||||
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 String BUCKET_NAME = "your-bucket";
|
||||
private static final String CALLBACK_URL = "your-callback-url";
|
||||
private static final String HOST = "https://your-bucket.your-endpoint";
|
||||
private static OssConfig ossConfig;
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(LogRequestInterceptor.class);
|
||||
|
||||
public Oss(OssConfig ossConfig ){
|
||||
Oss.ossConfig = ossConfig;
|
||||
}
|
||||
|
||||
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) {
|
||||
OSS client = createClient();
|
||||
try (InputStream input = new ByteArrayInputStream(content)) {
|
||||
client.putObject(BUCKET_NAME, fileName, input);
|
||||
client.putObject(ossConfig.getBucket(), fileName, input);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.info(e.getMessage());
|
||||
return false;
|
||||
} finally {
|
||||
client.shutdown();
|
||||
@ -38,11 +54,11 @@ public class Oss {
|
||||
public static String getObjectToString(String fileName) {
|
||||
OSS client = createClient();
|
||||
try {
|
||||
OSSObject ossObject = client.getObject(BUCKET_NAME, fileName);
|
||||
OSSObject ossObject = client.getObject(ossConfig.getBucket(), fileName);
|
||||
InputStream content = ossObject.getObjectContent();
|
||||
return new String(content.readAllBytes());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.info(e.getMessage());
|
||||
return null;
|
||||
} finally {
|
||||
client.shutdown();
|
||||
@ -52,17 +68,18 @@ public class Oss {
|
||||
public static boolean downloadObjectToFile(String fileName, String localPath) {
|
||||
OSS client = createClient();
|
||||
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;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.info(e.getMessage());
|
||||
return false;
|
||||
} finally {
|
||||
client.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<String, Object> getOssSign(String dir) {
|
||||
// 获取oss签名
|
||||
public static GetOssSignDto getOssSign(String dir) {
|
||||
long expireTime = 30;
|
||||
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
|
||||
Date expiration = new Date(expireEndTime);
|
||||
@ -74,21 +91,22 @@ public class Oss {
|
||||
OSS client = createClient();
|
||||
try {
|
||||
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 signature = client.calculatePostSignature(postPolicy);
|
||||
|
||||
Map<String, Object> respMap = new HashMap<>();
|
||||
respMap.put("access_id", ACCESS_KEY_ID);
|
||||
respMap.put("policy", encodedPolicy);
|
||||
respMap.put("signature", signature);
|
||||
respMap.put("dir", dir);
|
||||
respMap.put("host", HOST);
|
||||
respMap.put("expire", expireEndTime / 1000);
|
||||
respMap.put("callback", CALLBACK_URL);
|
||||
return respMap;
|
||||
GetOssSignDto g = new GetOssSignDto();
|
||||
g.setAccessId(ossConfig.getAccessKey());
|
||||
g.setPolicy(encodedPolicy);
|
||||
g.setSignature(signature);
|
||||
g.setDir(dir);
|
||||
g.setHost(ossConfig.getCustomDomainName());
|
||||
g.setExpire( expireEndTime / 1000);
|
||||
g.setCallback("");
|
||||
|
||||
return g;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.info(e.getMessage());
|
||||
return null;
|
||||
} finally {
|
||||
client.shutdown();
|
||||
|
||||
@ -37,6 +37,12 @@ public class CaseExchangeModel {
|
||||
@TableField("exchange_status")
|
||||
private Integer exchangeStatus;
|
||||
|
||||
/**
|
||||
* 是否被精选(0:否 1:是)
|
||||
*/
|
||||
@TableField("is_selected")
|
||||
private Integer isSelected;
|
||||
|
||||
/**
|
||||
* 阅读量
|
||||
*/
|
||||
|
||||
@ -27,6 +27,10 @@ public class getCaseExchangeSearchPage {
|
||||
@JsonProperty("user_id")
|
||||
private String userId;
|
||||
|
||||
// 是否被精选(0:否 1:是)
|
||||
@JsonProperty("is_selected")
|
||||
private Integer isSelected;
|
||||
|
||||
// 排序字段
|
||||
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 lombok.Data;
|
||||
|
||||
@Data
|
||||
public class PublicRequest {
|
||||
public class LoginRequest {
|
||||
// ✅ 自定义校验分组
|
||||
public interface Login {}
|
||||
|
||||
@ -1,13 +1,11 @@
|
||||
package com.example.caseData.service;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
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.CaseClinicalDoctorDao;
|
||||
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.extend.app.Hospital.GetHospitalByUuidResponse;
|
||||
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.StringToInt;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.el.parser.Token;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static com.baomidou.mybatisplus.extension.toolkit.Db.save;
|
||||
@ -56,7 +50,7 @@ public class UserService {
|
||||
* @return UserModel
|
||||
*/
|
||||
@Transactional
|
||||
public PublicDto UserLogin(String phone) throws BusinessException {
|
||||
public LoginDto UserLogin(String phone) throws BusinessException {
|
||||
// 获取app用户数据
|
||||
UserModel user = GetAppUserInfoByPhone(phone);
|
||||
|
||||
@ -69,7 +63,7 @@ public class UserService {
|
||||
CaseClinicalDoctorModel caseClinicalDoctor = caseClinicalDoctorDao.selectOne(caseClinicalDoctorWrapper);
|
||||
|
||||
// 处理返回值
|
||||
PublicDto g = new PublicDto();
|
||||
LoginDto g = new LoginDto();
|
||||
g.setUserId(String.valueOf(user.getUserId()));
|
||||
g.setUserName(user.getUserName());
|
||||
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 user c ON c.user_id = a.user_id
|
||||
WHERE a.exchange_status = 1
|
||||
<if test="isSelected != null and isSelected != ''">
|
||||
AND c.is_selected = ${isSelected}
|
||||
</if>
|
||||
<if test="keyword != null and keyword != ''">
|
||||
AND (
|
||||
a.exchange_title LIKE CONCAT('%', #{keyword}, '%')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user