后端分享功能实现

This commit is contained in:
LittleBoy 2022-05-24 10:39:03 +08:00
parent 47f79ff458
commit 77191b4d3a
9 changed files with 115 additions and 48 deletions

View File

@ -1,5 +1,8 @@
package xyz.longicorn.driver.controller; package xyz.longicorn.driver.controller;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.generator.RandomGenerator;
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.RestController; import org.springframework.web.bind.annotation.RestController;
import xyz.longicorn.driver.dto.ApiResult; import xyz.longicorn.driver.dto.ApiResult;
@ -18,7 +21,18 @@ public class ShareController {
* @return * @return
*/ */
@RequestMapping("/create") @RequestMapping("/create")
public ApiResult create(ShareInfo info){ public ApiResult create(@RequestBody ShareInfo info){
String id = StpUtil.getLoginId().toString();
// 设置分享者
info.setUid(Integer.parseInt(id));
// 设置密码
if("yes".equalsIgnoreCase(info.getPassword())){
RandomGenerator generator = new RandomGenerator("0123456789abcdefg", 4);
info.setPassword(generator.generate());
}else{
info.setPassword(null); // 不需要密码
}
info.setId((new RandomGenerator(10)).generate());
return ApiResult.success(shareService.create(info)); return ApiResult.success(shareService.create(info));
} }
@RequestMapping("/info") @RequestMapping("/info")

View File

@ -1,5 +1,8 @@
package xyz.longicorn.driver.pojo; package xyz.longicorn.driver.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -17,7 +20,7 @@ import java.util.Date;
@Accessors(chain = true) @Accessors(chain = true)
public class FileInfo implements Serializable { public class FileInfo implements Serializable {
@TableId(type = IdType.AUTO)
private Long id; private Long id;
private Integer uid; private Integer uid;
private String name; private String name;

View File

@ -1,6 +1,7 @@
package xyz.longicorn.driver.pojo; package xyz.longicorn.driver.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
@ -16,7 +17,9 @@ import java.util.Date;
@NoArgsConstructor @NoArgsConstructor
@Accessors(chain = true) @Accessors(chain = true)
public class ShareInfo { public class ShareInfo {
@TableId
@TableId(type= IdType.AUTO)
private Integer no;
private String id; private String id;
private String title; private String title;
private Integer uid; private Integer uid;

View File

@ -9,6 +9,6 @@ import xyz.longicorn.driver.pojo.ShareInfo;
public class ShareService extends ServiceImpl<ShareInfoMapper, ShareInfo> { public class ShareService extends ServiceImpl<ShareInfoMapper, ShareInfo> {
public ShareInfo create(ShareInfo info) { public ShareInfo create(ShareInfo info) {
this.save(info); this.save(info);
return null; return info;
} }
} }

View File

@ -62,7 +62,7 @@ spring:
redis: redis:
database: 1 database: 1
rabbitmq: rabbitmq:
host: 127.0.0.1 host: mq.1688cd.cn
port: 5672 port: 5672
username: client username: client
password: 123123 password: 123123

View File

@ -36,13 +36,13 @@ create table share_info
id varchar(20) not null primary key, id varchar(20) not null primary key,
title varchar(20) not null, title varchar(20) not null,
uid int(10) not null, uid int(10) not null,
file_id bigint(20) not null, file_id bigint not null comment '要分享文件的真是数据编号',
type tinyint(1) default 1 comment '分类类型 1:文件 2文件夹', type tinyint(1) default 1 null comment '分类类型 1:文件 2文件夹',
password varchar(20) null, password varchar(20) null comment '分享密码',
live int default 0 comment '有效期', live int default -1 null comment '有效期 时长-单位是秒,-1表示长期有效',
create_time datetime default current_timestamp, create_time datetime default CURRENT_TIMESTAMP null,
update_time datetime on update current_timestamp, update_time datetime null on update CURRENT_TIMESTAMP,
status tinyint(1) default 1, status tinyint(1) default 1 null,
index ix_file_name (title) index ix_file_name (title)
); );
CREATE TABLE user_info CREATE TABLE user_info

View File

@ -99,6 +99,11 @@
<!-- 分享 --> <!-- 分享 -->
<el-dialog v-model="share.visible" title="分享文件" width="600px"> <el-dialog v-model="share.visible" title="分享文件" width="600px">
<div class="share-create-dialog"> <div class="share-create-dialog">
<div v-if="share.showResult">
<h3 style="margin-bottom: 10px;">分享成功</h3>
<el-input v-model="share.shareData" />
</div>
<div v-else>
<h2>{{ share.current?.name }}</h2> <h2>{{ share.current?.name }}</h2>
<div class="live d-flex item"> <div class="live d-flex item">
<div class="title">有效期:</div> <div class="title">有效期:</div>
@ -107,6 +112,7 @@
<el-radio-button label="1">1</el-radio-button> <el-radio-button label="1">1</el-radio-button>
<el-radio-button label="7">7</el-radio-button> <el-radio-button label="7">7</el-radio-button>
<el-radio-button label="30">30</el-radio-button> <el-radio-button label="30">30</el-radio-button>
<el-radio-button label="-1">长期有效</el-radio-button>
</el-radio-group> </el-radio-group>
</div> </div>
</div> </div>
@ -120,10 +126,12 @@
</div> </div>
</div> </div>
</div> </div>
</div>
<template #footer> <template #footer>
<div class="dialog-footer"> <div class="dialog-footer">
<span style="color: #999;font-size: 12px;float: left;">配合净网行动, 网盘严厉打击色情低俗等不良信息的传播行为</span> <span style="color: #999;font-size: 12px;float: left;">配合净网行动, 网盘严厉打击色情低俗等不良信息的传播行为</span>
<el-button type="primary" :loading="share.loading" @click="handleCreateFolder">创建分享</el-button> <el-button type="primary" v-if="share.showResult">关闭</el-button>
<el-button v-else type="primary" :loading="share.loading" @click="handleCreateShare">创建分享</el-button>
</div> </div>
</template> </template>
</el-dialog> </el-dialog>
@ -203,7 +211,9 @@ export default {
*/ */
info: null, info: null,
live: 1, live: 1,
password: 'yes' password: 'yes',
showResult: false,
shareData: ''
} }
} }
}, },
@ -284,13 +294,40 @@ export default {
if (option.slug == 'share') { if (option.slug == 'share') {
// //
this.share.current = item this.share.current = item
//
this.share.info = {}
this.share.showResult = false
this.share.visible = true; this.share.visible = true;
} else if (option.slug == 'rename') { } else if (option.slug == 'rename') {
this.renameFile(item); this.renameFile(item);
} else if (option.slug == 'delete') { } else if (option.slug == 'delete') {
this.deleteFile(item); this.deleteFile(item);
} }
}, },
handleCreateShare() {
this.share.info = {
title: this.share.current.name,
fileId: this.share.current.id,
//1: 2
type: this.share.current.type == 'folder' ? 2 : 1,
password: this.share.password,
live: this.share.live === '-1' ? -1 : (this.share.live * 24 * 3600),
}
api.share.create(this.share.info).then(
/**
*
* @param {ShareInfo} ret
*/
(ret) => {
console.log(ret)
this.share.info = ret;
this.share.showResult = true
this.share.shareData = 'http://localhost:3000/s/' + ret.id + (
ret.password ? ' 提取码:' + ret.password : ''
)
}).catch(e => ElMessage.error('分享失败(' + e.message + ')'));
},
/** /**
* *
* @param {FileItem} file * @param {FileItem} file
@ -375,11 +412,13 @@ export default {
line-height: 40px; line-height: 40px;
height: 40px; height: 40px;
} }
.list-file-icon { .list-file-icon {
width: 40px; width: 40px;
display: flex; display: flex;
align-items: center; align-items: center;
} }
.list-file-name { .list-file-name {
margin-left: 5px; margin-left: 5px;
} }

View File

@ -53,12 +53,16 @@ function request(api, method = 'GET', postData = {}, progressChange = null) {
} }
let Authorization = store.getters.userToken let Authorization = store.getters.userToken
if (Authorization) { if (Authorization) {
options = { if(!options.headers) {
headers: { options.headers = {}
Authorization
},
...options
} }
options.headers.Authorization = Authorization;
// options = {
// ...options,
// headers: {
// Authorization
// }
// }
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -1,4 +1,8 @@
declare type ApiResult = { code: number, message: string, data: any } interface ApiResult<T> {
code: number
message: string
data: T
}
declare type FileItem = { declare type FileItem = {
createTime: string, createTime: string,
id: number, id: number,
@ -8,8 +12,8 @@ declare type FileItem = {
* *
*/ */
thumb: string, thumb: string,
size: number, size: number
type: 'folder' | string, type: 'folder' | string
updateTime: string updateTime: string
} }
declare type FileIconInstance = { declare type FileIconInstance = {