User优化

This commit is contained in:
Xubx 2024-09-04 23:17:57 +08:00
parent 0742b8eab8
commit 83a08e579d
2 changed files with 16 additions and 4 deletions

View File

@ -46,14 +46,21 @@ public class OrderService extends ServiceImpl<OrderMapper, Order> {
if (order == null) { if (order == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "参数错误"); return new JSONObject().fluentPut("success", false).fluentPut("message", "参数错误");
} }
List<User> userList = userService.list();
User userTemp = userList.stream().filter(e -> Objects.equals(e.getOpenId(), jsonObject.get("openId"))).findFirst().orElse(null);
order.setUserId(userTemp.getId());
if (!StringUtils.hasLength(order.getAddress())) { if (!StringUtils.hasLength(order.getAddress())) {
User user = userService.getById(order.getUserId()); User user = userService.getById(order.getUserId());
order.setAddress(user.getAddress()); order.setAddress(user.getAddress());
} }
// TODO 判空 // TODO 判空
boolean success = saveOrUpdate(order); boolean success = saveOrUpdate(order);
return new JSONObject().fluentPut("message", "success").fluentPut("success", success);
} if (success) {
return new JSONObject().fluentPut("message", "success").fluentPut("success", success).fluentPut("id", order.getId().toString());
} else {
return new JSONObject().fluentPut("message", "failure").fluentPut("success", success);
} }
public JSONObject list(JSONObject jsonObject) { public JSONObject list(JSONObject jsonObject) {
String openId = jsonObject.getString("openId"); String openId = jsonObject.getString("openId");

View File

@ -26,7 +26,8 @@ import java.util.stream.Collectors;
@Service @Service
public class UserService extends ServiceImpl<UserMapper, User> { public class UserService extends ServiceImpl<UserMapper, User> {
private static final String UPLOAD_DIR = "uploads/"; private static final String UPLOAD_DIR = "static/uploads/";
private static final String GET_URL = "uploads/";
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public JSONObject addOrUpdate(JSONObject jsonObject) { public JSONObject addOrUpdate(JSONObject jsonObject) {
@ -88,6 +89,9 @@ public class UserService extends ServiceImpl<UserMapper, User> {
List<User> userList = list(); List<User> userList = list();
User user = userList.stream().filter(e -> Objects.equals(e.getOpenId(), openId)).findFirst().orElse(null); User user = userList.stream().filter(e -> Objects.equals(e.getOpenId(), openId)).findFirst().orElse(null);
if (user == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "用户不存在");
}
return new JSONObject().fluentPut("data", user); return new JSONObject().fluentPut("data", user);
} }
@ -105,10 +109,11 @@ public class UserService extends ServiceImpl<UserMapper, User> {
// 保存文件到服务器 // 保存文件到服务器
String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename(); String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
Path path = Paths.get(UPLOAD_DIR + fileName); Path path = Paths.get(UPLOAD_DIR + fileName);
Files.createDirectories(path.getParent());
Files.write(path, file.getBytes()); Files.write(path, file.getBytes());
// 构建文件的访问URL // 构建文件的访问URL
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath() String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/" + UPLOAD_DIR) .path("/" + GET_URL)
.path(fileName) .path(fileName)
.toUriString(); .toUriString();
return new JSONObject().fluentPut("success", true).fluentPut("fileDownloadUri", fileDownloadUri); return new JSONObject().fluentPut("success", true).fluentPut("fileDownloadUri", fileDownloadUri);