完成分享查询的接口

This commit is contained in:
LittleBoy 2022-05-24 11:51:32 +08:00
parent 77191b4d3a
commit 0be5fdd979
6 changed files with 184 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.generator.RandomGenerator; import cn.hutool.captcha.generator.RandomGenerator;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import xyz.longicorn.driver.dto.ApiResult; import xyz.longicorn.driver.dto.ApiResult;
import xyz.longicorn.driver.pojo.ShareInfo; import xyz.longicorn.driver.pojo.ShareInfo;
@ -16,30 +17,36 @@ import javax.annotation.Resource;
public class ShareController { public class ShareController {
@Resource @Resource
private ShareService shareService; private ShareService shareService;
/** /**
* 创建分享 * 创建分享
*
* @return * @return
*/ */
@RequestMapping("/create") @RequestMapping("/create")
public ApiResult create(@RequestBody ShareInfo info){ public ApiResult create(@RequestBody ShareInfo info) {
String id = StpUtil.getLoginId().toString(); String id = StpUtil.getLoginId().toString();
// 设置分享者 // 设置分享者
info.setUid(Integer.parseInt(id)); info.setUid(Integer.parseInt(id));
// 设置密码 // 设置密码
if("yes".equalsIgnoreCase(info.getPassword())){ if ("yes".equalsIgnoreCase(info.getPassword())) {
RandomGenerator generator = new RandomGenerator("0123456789abcdefg", 4); RandomGenerator generator = new RandomGenerator("0123456789abcdefg", 4);
info.setPassword(generator.generate()); info.setPassword(generator.generate());
}else{ } else {
info.setPassword(null); // 不需要密码 info.setPassword(null); // 不需要密码
} }
info.setId((new RandomGenerator(10)).generate()); info.setId((new RandomGenerator(10)).generate());
return ApiResult.success(shareService.create(info)); return ApiResult.success(shareService.create(info));
} }
@RequestMapping("/info") @RequestMapping("/info")
public ApiResult info(String id){ public ApiResult info(String id) {
return ApiResult.success(shareService.getById(id)); return ApiResult.success(shareService.getById(id));
} }
public ApiResult list(ShareInfo info){
return ApiResult.success(shareService.create(info)); @RequestMapping("/list")
public ApiResult list(@RequestParam(required = true) String id, String parent,String password) {
return ApiResult.success(shareService.listFiles(id, parent,password));
} }
} }

View File

@ -1,14 +1,99 @@
package xyz.longicorn.driver.service; package xyz.longicorn.driver.service;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.longicorn.driver.config.BizException;
import xyz.longicorn.driver.dao.FileInfoMapper;
import xyz.longicorn.driver.dao.FolderInfoMapper;
import xyz.longicorn.driver.dao.ShareInfoMapper; import xyz.longicorn.driver.dao.ShareInfoMapper;
import xyz.longicorn.driver.dto.FileItem;
import xyz.longicorn.driver.pojo.FileInfo;
import xyz.longicorn.driver.pojo.FolderInfo;
import xyz.longicorn.driver.pojo.ShareInfo; import xyz.longicorn.driver.pojo.ShareInfo;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service @Service
public class ShareService extends ServiceImpl<ShareInfoMapper, ShareInfo> { public class ShareService extends ServiceImpl<ShareInfoMapper, ShareInfo> {
@Resource
private FileInfoMapper fileInfoMapper;
@Resource
private FolderService folderService;
public ShareInfo create(ShareInfo info) { public ShareInfo create(ShareInfo info) {
this.save(info); this.save(info);
return info; return info;
} }
public ShareInfo getById(String id) {
// 查询到分享信息
QueryWrapper q = new QueryWrapper();
q.eq("id", id);
q.eq("status", 1); // 只查询状态为1 0:删除 2:违规 3xxx
final ShareInfo one = getOne(q);
// 为null或者长期有效则不需要判断
if (one != null && one.getLive() != -1) {
// 判断有效期
long diffTime = (new Date()).getTime() - one.getCreateTime().getTime(); // 毫秒
if (diffTime < 0) return one; // 说明有效期远远还没有到
// 计算当前时间和创建时间的时间差
long betweenTime = DateUtil.between(new Date(), one.getCreateTime(), DateUnit.SECOND);
if (betweenTime > one.getLive()) { // 大于有效期
return null;
}
}
return one;
}
public List<FileItem> listFiles(String id, String parent, String password) {
ShareInfo info = this.getById(id);
if (null == info) {
throw BizException.create("分享信息有误");
}
// 需要先验证密码
if (info.getPassword() != null) {
if (!info.getPassword().equalsIgnoreCase(password)) {
throw BizException.create("提取密码不正确");
}
}
// 是否是文件
if (info.getType() == 1) {
// 文件
return getFileById(info.getFileId());
}
return getFolderFilesById(info, parent);
}
private List<FileItem> getFolderFilesById(ShareInfo info, String path) {
if(StringUtils.hasLength(path)){
// path = path.substring(1); /
}
if(path == null || path.equals("/")){
path = "";
}
int uid = info.getUid();
// 查询根目录信息
FolderInfo folder = folderService.getById(info.getFileId()); //
String rootPath = folder.getPath(); // 获取真实的逻辑路径
// 查询目录信息
System.out.println(rootPath + path);
return folderService.listFolder(uid,rootPath + path);
}
private List<FileItem> getFileById(Long fileId) {
final FileInfo fileInfo = fileInfoMapper.selectById(fileId);
FileItem item = FileItem.build(fileInfo);
List<FileItem> list = new ArrayList<>();
list.add(item);
return list;
}
} }

74
web/src/pages/Share.vue Normal file
View File

@ -0,0 +1,74 @@
<template>
<div class="page-share">
<div v-if="loadingData">
加载中...
</div>
<div v-else class="share-content-wrapper">
<!-- 分享信息有误 -->
<el-result v-if="shareError" icon="warning"
sub-title="啊哦!没有找到分享数据,请打开正确的分享链接!"
>
</el-result>
<div v-else class="share-main">
<!-- 显示分享信息 -->
</div>
</div>
</div>
</template>
<script>
import {useRoute} from "vue-router";
import api from "../service/api";
import {ref} from "vue";
export default {
name: "Share",
setup(props) {
const route = useRoute();
const id = route.params.id // : this.$route.params.id
/**
* 分享信息有误
*/
const shareError = ref(false)
/**
* 是否加载中
*/
const loadingData = ref(true)
/**
* 分享信息
* @type {Ref<UnwrapRef<ShareInfo>>}
*/
const shareInfo = ref({})
const showShareFiles = () => {
api.share.list(id).then(ret => {
console.log(ret)
})
}
// 使
api.share.info(id).then(ret => {
if (null == ret) shareError.value = true
else {
//
shareInfo.value = ret
//
showShareFiles()
}
}).finally(() => {
// false
loadingData.value = false
});
return {
shareError, loadingData, shareInfo
}
}
}
</script>
<style scoped>
</style>

View File

@ -14,7 +14,7 @@ const loginPath = '/login', // 登录路径
// 全局导航守卫 // 全局导航守卫
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
const toPath = to.path; // 获取要展示得路径 const toPath = to.path; // 获取要展示得路径
if (anonymousPaths.includes(toPath)) next() // 判断是否可匿名访问 if (anonymousPaths.includes(toPath) || /^\/s\/*/i.test(toPath)) next() // 判断是否可匿名访问
else { else {
// 验证token // 验证token
if (!store.getters.userToken) next(loginPath) // 没有登录直接打开登录页面 if (!store.getters.userToken) next(loginPath) // 没有登录直接打开登录页面

View File

@ -52,6 +52,11 @@ const routes = [
name: 'Login', name: 'Login',
path: '/login', path: '/login',
component: () => import('../pages/Login.vue') component: () => import('../pages/Login.vue')
},
{
name: 'Share',
path: '/s/:id', // 动态路由
component: () => import('../pages/Share.vue')
} }
]; ];
export default routes export default routes

View File

@ -187,6 +187,12 @@ export default {
*/ */
create(info) { create(info) {
return request('/api/share/create', 'POST', info) return request('/api/share/create', 'POST', info)
},
info(id){
return request('/api/share/info', 'GET', {id})
},
list(id,parent='/'){
return request('/api/share/list', 'GET', {id,parent})
} }
}, },
code() { code() {