mirror of
https://github.com/dqjdda/Aurora.git
synced 2018-11-06 05:33:29 +00:00
新增图床管理,修复禁用账户后依然能登录的Bug,修复角色管理懒加载的Bug,部分代码优化,sql文件更新
This commit is contained in:
@@ -170,7 +170,7 @@ public class MenuController {
|
||||
@DeleteMapping(value = "/delete")
|
||||
public ResponseEntity delete(@RequestParam Long id){
|
||||
log.warn("REST request to delete menu");
|
||||
menuService.delete(id);
|
||||
menuService.delete(menuService.findById(id));
|
||||
return ResponseEntity.ok();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ public class PermissionController {
|
||||
@DeleteMapping(value = "/delete")
|
||||
public ResponseEntity delete(@RequestParam Long id) {
|
||||
log.warn("REST request to deletePermission");
|
||||
permissionService.delete(id);
|
||||
permissionService.delete(permissionService.findById(id));
|
||||
return ResponseEntity.ok();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ public class RoleController {
|
||||
@DeleteMapping(value = "/delete")
|
||||
public ResponseEntity delete(@RequestParam Long id) {
|
||||
log.warn("REST request to deleteRole");
|
||||
roleService.delete(id);
|
||||
roleService.delete(roleService.findById(id));
|
||||
return ResponseEntity.ok();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ import me.aurora.service.RoleService;
|
||||
import me.aurora.service.UserService;
|
||||
import me.aurora.service.mapper.UserMapper;
|
||||
import me.aurora.util.HttpContextUtils;
|
||||
import me.aurora.config.exception.AuroraException;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authz.annotation.Logical;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package me.aurora.app.rest.web;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.aurora.annotation.Log;
|
||||
import me.aurora.domain.ResponseEntity;
|
||||
import me.aurora.domain.User;
|
||||
import me.aurora.repository.spec.PictureSpec;
|
||||
import me.aurora.service.PictureService;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authz.annotation.Logical;
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:13:32
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/picture")
|
||||
public class PictureController {
|
||||
|
||||
@Autowired
|
||||
private PictureService pictureService;
|
||||
|
||||
/**
|
||||
* 去图床页面
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/index")
|
||||
public ModelAndView index(){
|
||||
log.warn("REST request to PicturePage");
|
||||
return new ModelAndView("/web/picture/index");
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询全部图片
|
||||
* @param username
|
||||
* @param createTime
|
||||
* @param endTime
|
||||
* @param page
|
||||
* @param limit
|
||||
* @return
|
||||
*/
|
||||
@RequiresPermissions(value={"admin", "picture:all","picture:select"}, logical= Logical.OR)
|
||||
@GetMapping(value = "/getPictureInfo")
|
||||
public Map getLogInfo(@RequestParam(value = "username",required = false) String username,
|
||||
@RequestParam(value = "createTime",required = false) Timestamp createTime,
|
||||
@RequestParam(value = "endTime",required = false) Timestamp endTime,
|
||||
@RequestParam(value = "page",defaultValue = "1")Integer page,
|
||||
@RequestParam(value = "limit",defaultValue = "10")Integer limit){
|
||||
|
||||
log.warn("REST request to findAll Log");
|
||||
Sort sort = new Sort(Sort.Direction.DESC,"createTime");
|
||||
Pageable pageable = PageRequest.of(page-1,limit,sort);
|
||||
return pictureService.getPictureInfo(new PictureSpec(username,createTime,endTime),pageable);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去上传页面
|
||||
* @return
|
||||
*/
|
||||
@RequiresPermissions (value={"admin", "picture:all","picture:add"}, logical= Logical.OR)
|
||||
@GetMapping(value = "/toAddPage")
|
||||
public ModelAndView toAddPage(){
|
||||
log.warn("REST request to addPicturePage");
|
||||
return new ModelAndView("/web/picture/add");
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
* @param file
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@RequiresPermissions (value={"admin", "picture:all","picture:upload"}, logical= Logical.OR)
|
||||
@PostMapping(value = "/upload")
|
||||
public ResponseEntity uploadAccessory(@RequestParam MultipartFile file) throws Exception {
|
||||
User user = (User) SecurityUtils.getSubject().getPrincipal();
|
||||
pictureService.upload(file,user);
|
||||
return ResponseEntity.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除图片
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Log("删除图片")
|
||||
@RequiresPermissions (value={"admin", "picture:all","picture:delete"}, logical= Logical.OR)
|
||||
@DeleteMapping(value = "/delete")
|
||||
public ResponseEntity delete(@RequestParam Long id) {
|
||||
log.warn("REST request to delete picture");
|
||||
pictureService.delete(pictureService.findById(id));
|
||||
return ResponseEntity.ok();
|
||||
}
|
||||
}
|
||||
@@ -22,9 +22,9 @@ public class GlobalExceptionHandler {
|
||||
* @return
|
||||
*/
|
||||
@ExceptionHandler(value = AuthorizationException.class)
|
||||
public ModelAndView handleAuthorizationException() {
|
||||
public String handleAuthorizationException() {
|
||||
log.error("没有权限访问");
|
||||
return new ModelAndView("exception/403");
|
||||
return "/exception/403";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -174,6 +174,7 @@ public class ShiroConfig {
|
||||
cookie.setMaxAge(auroraProperties.getShiro().getCookieTimeout());
|
||||
return cookie;
|
||||
}
|
||||
|
||||
/**
|
||||
* cookie管理对象
|
||||
* @return
|
||||
|
||||
@@ -26,7 +26,7 @@ import java.util.stream.Collectors;
|
||||
@Slf4j
|
||||
public class ShiroRealm extends AuthorizingRealm {
|
||||
|
||||
private final String enabled = "0";
|
||||
private final Integer enabled = 0;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@@ -2,18 +2,59 @@ package me.aurora.domain;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.checkerframework.checker.units.qual.C;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:08:31
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@Table(name = "picture")
|
||||
@Table(name = "zj_picture")
|
||||
public class Picture implements Serializable {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String filename;
|
||||
|
||||
private String url;
|
||||
|
||||
private String size;
|
||||
|
||||
private String height;
|
||||
|
||||
private String width;
|
||||
|
||||
/**
|
||||
* delete URl
|
||||
*/
|
||||
@Column(name = "deleteUrl")
|
||||
private String delete;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "user_id")
|
||||
private User user;
|
||||
|
||||
@CreationTimestamp
|
||||
private Timestamp createTime;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Picture{" +
|
||||
"id=" + id +
|
||||
", url='" + url + '\'' +
|
||||
", size='" + size + '\'' +
|
||||
", height='" + height + '\'' +
|
||||
", width='" + width + '\'' +
|
||||
", deleteUrl='" + delete + '\'' +
|
||||
", createTime=" + createTime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package me.aurora.domain;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@@ -53,6 +54,10 @@ public class User implements Serializable {
|
||||
@JoinTable(name = "zj_users_roles", joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")})
|
||||
private Set<Role> roles;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL)
|
||||
@JsonIgnore
|
||||
private Set<Picture> pictures;
|
||||
|
||||
public interface New{};
|
||||
public interface Update{};
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package me.aurora.repository;
|
||||
|
||||
import me.aurora.domain.Picture;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:16:05
|
||||
*/
|
||||
public interface PictureRepo extends JpaRepository<Picture,Long>, JpaSpecificationExecutor {
|
||||
}
|
||||
@@ -4,9 +4,12 @@ import me.aurora.domain.Role;
|
||||
import me.aurora.domain.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -29,4 +32,13 @@ public interface RoleRepo extends JpaRepository<Role,Long>,JpaSpecificationExecu
|
||||
* @return
|
||||
*/
|
||||
List<Role> findByUsers(Set<User> users);
|
||||
|
||||
/**
|
||||
* 解决懒加载异常
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
@Query("from Role r join fetch r.permissions where r.id = :id")
|
||||
Optional<Role> findById(@Param("id") Long id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package me.aurora.repository.spec;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import me.aurora.domain.Picture;
|
||||
import me.aurora.domain.User;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import javax.persistence.criteria.*;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:30:21
|
||||
*/
|
||||
public class PictureSpec implements Specification<Picture> {
|
||||
|
||||
private String username;
|
||||
private Timestamp createTime;
|
||||
private Timestamp endTime;
|
||||
|
||||
public PictureSpec(String username, Timestamp createTime, Timestamp endTime) {
|
||||
this.username = username;
|
||||
this.createTime = createTime;
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Predicate toPredicate(Root<Picture> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
|
||||
List<Predicate> list = new ArrayList<Predicate>();
|
||||
Join<User,Picture> userJoin = root.join("user",JoinType.LEFT);
|
||||
if (!StrUtil.isEmpty(username)) {
|
||||
list.add(cb.like(userJoin.get("username").as(String.class), "%" + username + "%" ));
|
||||
}
|
||||
|
||||
if(null!=createTime && null != endTime){
|
||||
list.add(cb.greaterThanOrEqualTo(root.get("createTime").as(Timestamp.class),createTime));
|
||||
list.add(cb.lessThanOrEqualTo(root.get("createTime").as(Timestamp.class),endTime));
|
||||
|
||||
}
|
||||
|
||||
Predicate[] p = new Predicate[list.size()];
|
||||
return cb.and(list.toArray(p));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PictureSpec{" +
|
||||
"username='" + username + '\'' +
|
||||
", createTime=" + createTime +
|
||||
", endTime=" + endTime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import me.aurora.domain.Role;
|
||||
import me.aurora.domain.vo.MenuVo;
|
||||
import me.aurora.repository.spec.MenuSpec;
|
||||
import me.aurora.service.dto.MenuDTO;
|
||||
import me.aurora.config.exception.AuroraException;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
@@ -70,10 +69,10 @@ public interface MenuService {
|
||||
|
||||
/**
|
||||
* 删除菜单
|
||||
* @param id
|
||||
* @param menu
|
||||
*/
|
||||
@CacheEvict(allEntries = true)
|
||||
void delete(Long id);
|
||||
void delete(Menu menu);
|
||||
|
||||
/**
|
||||
* 根据ID查询
|
||||
|
||||
@@ -54,11 +54,11 @@ public interface PermissionService {
|
||||
|
||||
/**
|
||||
* 删除权限
|
||||
* @param id
|
||||
* @param permission
|
||||
* @return
|
||||
*/
|
||||
@CacheEvict(allEntries = true)
|
||||
void delete(Long id);
|
||||
void delete(Permission permission);
|
||||
|
||||
/**
|
||||
* 查询所有的权限
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package me.aurora.service;
|
||||
|
||||
import me.aurora.domain.Picture;
|
||||
import me.aurora.domain.User;
|
||||
import me.aurora.repository.spec.PictureSpec;
|
||||
import org.springframework.cache.annotation.CacheConfig;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:14:16
|
||||
*/
|
||||
@CacheConfig(cacheNames = "picture")
|
||||
public interface PictureService {
|
||||
|
||||
/**
|
||||
* 查询图床
|
||||
* @param pictureSpec
|
||||
* @param pageable
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(keyGenerator = "keyGenerator")
|
||||
Map getPictureInfo(PictureSpec pictureSpec, Pageable pageable);
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
* @param file
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
@CacheEvict(allEntries = true)
|
||||
void upload(MultipartFile file, User user);
|
||||
|
||||
/**
|
||||
* 根据ID查询
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
@Cacheable(key = "#p0")
|
||||
Picture findById(Long id);
|
||||
|
||||
/**
|
||||
* 删除图片
|
||||
* @param picture
|
||||
*/
|
||||
@CacheEvict(allEntries = true)
|
||||
void delete(Picture picture);
|
||||
}
|
||||
@@ -71,11 +71,11 @@ public interface RoleService {
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
* @param id
|
||||
* @param role
|
||||
* @return
|
||||
*/
|
||||
@CacheEvict(allEntries = true)
|
||||
void delete(Long id);
|
||||
void delete(Role role);
|
||||
|
||||
/**
|
||||
* 组装树形结构数据
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package me.aurora.service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import me.aurora.domain.User;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
import javax.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Timestamp;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:08:31
|
||||
*/
|
||||
@Data
|
||||
public class PictureDto implements Serializable {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String filename;
|
||||
|
||||
private String url;
|
||||
|
||||
private String size;
|
||||
|
||||
private String height;
|
||||
|
||||
private String width;
|
||||
|
||||
private String userName;
|
||||
|
||||
private Timestamp createTime;
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import me.aurora.service.mapper.MenuMapper;
|
||||
import me.aurora.util.ListSortUtil;
|
||||
import me.aurora.util.PageUtil;
|
||||
import me.aurora.config.exception.AuroraException;
|
||||
import me.aurora.util.ValidationUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -72,7 +73,9 @@ public class MenuServiceImpl implements MenuService {
|
||||
|
||||
@Override
|
||||
public Menu findByPId(Integer pid) {
|
||||
return menuRepo.findById(pid.longValue()).get();
|
||||
Menu menu = menuRepo.findById(pid.longValue()).get();
|
||||
ValidationUtil.isNull(menu,"pid:"+pid+"is not find");
|
||||
return menu;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -96,18 +99,20 @@ public class MenuServiceImpl implements MenuService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
List<Menu> menus = menuRepo.findByPid(Integer.parseInt(id.toString()));
|
||||
public void delete(Menu menu) {
|
||||
List<Menu> menus = menuRepo.findByPid(menu.getPid());
|
||||
if(menus !=null && menus.size()!=0){
|
||||
menuRepo.deleteAll(menus);
|
||||
}
|
||||
menuRepo.deleteById(id);
|
||||
menuRepo.delete(menu);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Menu findById(Long id) {
|
||||
return menuRepo.findById(id).get();
|
||||
Menu menu = menuRepo.findById(id).get();
|
||||
ValidationUtil.isNull(menu,"id:"+id+"is not find");
|
||||
return menu;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -11,6 +11,7 @@ import me.aurora.service.dto.PermissionDTO;
|
||||
import me.aurora.service.mapper.PerMissionMapper;
|
||||
import me.aurora.util.PageUtil;
|
||||
import me.aurora.config.exception.AuroraException;
|
||||
import me.aurora.util.ValidationUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -50,8 +51,9 @@ public class PermissionServiceImpl implements PermissionService {
|
||||
|
||||
@Override
|
||||
public Permission findById(Long id) {
|
||||
Optional<Permission> permission = permissionRepo.findById(id);
|
||||
return permission.get();
|
||||
Permission permission = permissionRepo.findById(id).get();
|
||||
ValidationUtil.isNull(permission,"id:"+id+"is not find");
|
||||
return permission;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,11 +72,7 @@ public class PermissionServiceImpl implements PermissionService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
Permission permission = permissionRepo.findById(id).get();
|
||||
if(permission == null){
|
||||
throw new AuroraException(HttpStatus.HTTP_NOT_FOUND,"ID为"+id+"的权限没有找到");
|
||||
}
|
||||
public void delete(Permission permission) {
|
||||
if(permission.getRoles() != null && permission.getRoles().size()!=0){
|
||||
throw new AuroraException(HttpStatus.HTTP_BAD_REQUEST,"暂不支持删除使用中的权限,请先与角色解绑");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package me.aurora.service.impl;
|
||||
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.aurora.config.exception.AuroraException;
|
||||
import me.aurora.domain.Picture;
|
||||
import me.aurora.domain.User;
|
||||
import me.aurora.repository.PictureRepo;
|
||||
import me.aurora.repository.spec.PictureSpec;
|
||||
import me.aurora.service.PictureService;
|
||||
import me.aurora.service.dto.PictureDto;
|
||||
import me.aurora.service.mapper.PictureMapper;
|
||||
import me.aurora.util.FileUtil;
|
||||
import me.aurora.util.PageUtil;
|
||||
import me.aurora.util.SizeUtil;
|
||||
import me.aurora.util.ValidationUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:14:43
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class PictureServiceImpl implements PictureService {
|
||||
|
||||
private final String uploadUrl = "https://sm.ms/api/upload";
|
||||
|
||||
private final String SUCCESS = "success";
|
||||
|
||||
private final String CODE = "code";
|
||||
|
||||
private final String MSG = "msg";
|
||||
|
||||
@Autowired
|
||||
private PictureRepo pictureRepo;
|
||||
|
||||
@Autowired
|
||||
private PictureMapper pictureMapper;
|
||||
|
||||
@Override
|
||||
public Map getPictureInfo(PictureSpec pictureSpec, Pageable pageable) {
|
||||
Page<Picture> picturePage = pictureRepo.findAll(pictureSpec,pageable);
|
||||
Page<PictureDto> pictureDtos = picturePage.map(pictureMapper::toDto);
|
||||
return PageUtil.buildPage(pictureDtos.getContent(),picturePage.getTotalElements());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void upload(MultipartFile multipartFile, User user) {
|
||||
File file = FileUtil.toFile(multipartFile);
|
||||
//将参数合成一个请求
|
||||
HttpEntity requestEntity = new HttpEntity<>(file);
|
||||
RestTemplate rest = new RestTemplate();
|
||||
FileSystemResource resource = new FileSystemResource(file);
|
||||
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
|
||||
//sm.ms 固定格式
|
||||
param.add("smfile", resource);
|
||||
//执行HTTP请求
|
||||
String str = rest.postForObject(uploadUrl, param, String.class);
|
||||
JSONObject jsonObject = JSONUtil.parseObj(str);
|
||||
Picture picture = null;
|
||||
if(!jsonObject.get(CODE).toString().equals(SUCCESS)){
|
||||
throw new AuroraException(HttpStatus.HTTP_BAD_REQUEST,jsonObject.get(MSG).toString());
|
||||
}
|
||||
//转成实体类
|
||||
picture = JSON.parseObject(jsonObject.get("data").toString(), Picture.class);
|
||||
picture.setSize(SizeUtil.getSize(Integer.valueOf(picture.getSize())));
|
||||
picture.setUser(user);
|
||||
picture.setFilename(FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()));
|
||||
pictureRepo.save(picture);
|
||||
//删除临时文件
|
||||
FileUtil.deleteFile(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Picture findById(Long id) {
|
||||
Picture picture = pictureRepo.findById(id).get();
|
||||
ValidationUtil.isNull(picture,"id:"+id+"is not find");
|
||||
return picture;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Picture picture) {
|
||||
RestTemplate rest = new RestTemplate();
|
||||
try {
|
||||
ResponseEntity<String> str = rest.getForEntity(picture.getDelete(), String.class);
|
||||
if(str.getStatusCode().is2xxSuccessful()){
|
||||
pictureRepo.delete(picture);
|
||||
}
|
||||
//如果删除的地址出错,直接删除数据库数据
|
||||
} catch(Exception e){
|
||||
pictureRepo.delete(picture);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import me.aurora.service.dto.RoleDTO;
|
||||
import me.aurora.service.mapper.RoleMapper;
|
||||
import me.aurora.util.PageUtil;
|
||||
import me.aurora.config.exception.AuroraException;
|
||||
import me.aurora.util.ValidationUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -70,7 +71,9 @@ public class RoleServiceImpl implements RoleService {
|
||||
|
||||
@Override
|
||||
public Role findById(Long id) {
|
||||
return roleRepo.findById(id).get();
|
||||
Role role = roleRepo.findById(id).get();
|
||||
ValidationUtil.isNull(role,"id:"+id+"is not find");
|
||||
return role;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -92,15 +95,11 @@ public class RoleServiceImpl implements RoleService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
Role role = roleRepo.findById(id).get();
|
||||
if(role == null){
|
||||
throw new AuroraException(404,"ID为"+id+"的角色未找到");
|
||||
}
|
||||
public void delete(Role role) {
|
||||
if(role.getUsers() != null && role.getUsers().size() != 0){
|
||||
throw new AuroraException(400,"角色使用中,请先解绑用户");
|
||||
}
|
||||
roleRepo.deleteById(id);
|
||||
roleRepo.delete(role);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,6 +14,7 @@ import me.aurora.service.mapper.UserMapper;
|
||||
import me.aurora.util.MD5Utils;
|
||||
import me.aurora.util.PageUtil;
|
||||
import me.aurora.config.exception.AuroraException;
|
||||
import me.aurora.util.ValidationUtil;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -44,8 +45,9 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
public User findById(Long id) {
|
||||
Optional<User> user = userRepo.findById(id);
|
||||
return user.get();
|
||||
User user = userRepo.findById(id).get();
|
||||
ValidationUtil.isNull(user,"id:"+id+"is not find");
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package me.aurora.service.mapper;
|
||||
|
||||
import me.aurora.domain.Picture;
|
||||
import me.aurora.service.dto.PictureDto;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
/**
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:09:43
|
||||
*/
|
||||
@Mapper(componentModel = "spring",uses = {},unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||
public interface PictureMapper extends EntityMapper<PictureDto, Picture>{
|
||||
|
||||
@Mapping(target = "userName",source = "picture.user.username")
|
||||
PictureDto toDto(Picture picture);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package me.aurora.util;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* File工具类
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 16:55:45
|
||||
*/
|
||||
public class FileUtil {
|
||||
|
||||
/**
|
||||
* MultipartFile转File
|
||||
* @param multipartFile
|
||||
* @return
|
||||
*/
|
||||
public static File toFile(MultipartFile multipartFile){
|
||||
// 获取文件名
|
||||
String fileName = multipartFile.getOriginalFilename();
|
||||
// 获取文件后缀
|
||||
String prefix=getExtensionName(fileName);
|
||||
File file = null;
|
||||
try {
|
||||
// 用uuid作为文件名,防止生成的临时文件重复
|
||||
file = File.createTempFile(UUID.randomUUID().toString(), prefix);
|
||||
// MultipartFile to File
|
||||
multipartFile.transferTo(file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param files
|
||||
*/
|
||||
public static void deleteFile(File... files) {
|
||||
for (File file : files) {
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件扩展名
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
public static String getExtensionName(String filename) {
|
||||
if ((filename != null) && (filename.length() > 0)) {
|
||||
int dot = filename.lastIndexOf('.');
|
||||
if ((dot >-1) && (dot < (filename.length() - 1))) {
|
||||
return filename.substring(dot + 1);
|
||||
}
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Java文件操作 获取不带扩展名的文件名
|
||||
* @param filename
|
||||
* @return
|
||||
*/
|
||||
public static String getFileNameNoEx(String filename) {
|
||||
if ((filename != null) && (filename.length() > 0)) {
|
||||
int dot = filename.lastIndexOf('.');
|
||||
if ((dot >-1) && (dot < (filename.length()))) {
|
||||
return filename.substring(0, dot);
|
||||
}
|
||||
}
|
||||
return filename;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package me.aurora.util;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* 字节转换工具
|
||||
* @author 郑杰
|
||||
* @date 2018/09/20 14:01:28
|
||||
*/
|
||||
public class SizeUtil {
|
||||
|
||||
/**
|
||||
* 定义GB的计算常量
|
||||
*/
|
||||
private static final int GB = 1024 * 1024 * 1024;
|
||||
/**
|
||||
* 定义MB的计算常量
|
||||
*/
|
||||
private static final int MB = 1024 * 1024;
|
||||
/**
|
||||
* 定义KB的计算常量
|
||||
*/
|
||||
private static final int KB = 1024;
|
||||
|
||||
/**
|
||||
* 格式化小数
|
||||
*/
|
||||
private static final DecimalFormat df = new DecimalFormat("0.00");
|
||||
|
||||
public static String getSize(int size){
|
||||
String resultSize = "";
|
||||
if (size / GB >= 1) {
|
||||
//如果当前Byte的值大于等于1GB
|
||||
resultSize = df.format(size / (float) GB) + "GB ";
|
||||
} else if (size / MB >= 1) {
|
||||
//如果当前Byte的值大于等于1MB
|
||||
resultSize = df.format(size / (float) MB) + "MB ";
|
||||
} else if (size / KB >= 1) {
|
||||
//如果当前Byte的值大于等于1KB
|
||||
resultSize = df.format(size / (float) KB) + "KB ";
|
||||
} else {
|
||||
resultSize = size + "B ";
|
||||
}
|
||||
return resultSize;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package me.aurora.util;
|
||||
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import me.aurora.config.exception.AuroraException;
|
||||
|
||||
/**
|
||||
* 验证工具
|
||||
* @author 郑杰
|
||||
* @date 2018/09/21 10:35:03
|
||||
*/
|
||||
public class ValidationUtil {
|
||||
|
||||
/**
|
||||
* 验证空
|
||||
* @param object
|
||||
*/
|
||||
public static void isNull(Object object,String msg){
|
||||
System.out.println(msg);
|
||||
if(object == null){
|
||||
throw new AuroraException(HttpStatus.HTTP_NOT_FOUND,msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user