增加文件删除功能,修复了文章和文件分页当前页数的BUG,

This commit is contained in:
sysnix 2023-09-24 23:55:09 +08:00
parent a2d868d354
commit 0a9605a74c
7 changed files with 139 additions and 16 deletions

View File

@ -2,6 +2,7 @@ package controller
import (
"blog/internal/service"
"net/http"
"net/url"
"github.com/kataras/iris/v12"
@ -26,7 +27,7 @@ func (ctrl *FileController) ViewFile() {
func (ctrl *FileController) BeforeActivation(activation mvc.BeforeActivation) {
// log.Println("before")
activation.Handle("GET", "{id}", "ViewFile")
activation.Handle(http.MethodGet, "{id}", "ViewFile")
}
func (ctrl *FileController) AfterActivation(activation mvc.AfterActivation) {

View File

@ -1,14 +1,20 @@
package admin
import (
"blog/internal/consts"
"blog/internal/model/AjaxResult"
"blog/internal/model/admin"
"blog/internal/model/vo"
"blog/internal/service"
"blog/third_party/SessionUtil"
"blog/third_party/database"
"context"
"net/http"
"time"
"github.com/google/uuid"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
"github.com/kataras/iris/v12/sessions"
"gorm.io/gorm"
)
@ -84,3 +90,35 @@ func (ctrl *FileController) PostUpload() {
Location: "/file/" + fileId,
})
}
func (ctrl *FileController) DelFile() {
id := ctrl.Ctx.Params().Get("id")
if id == "" {
ctrl.Ctx.JSON(AjaxResult.Error("ID为空"))
return
}
ctx := context.Background()
err := database.GormTemplate.Transaction(func(tx *gorm.DB) error {
txErr := tx.Table("common_files").Delete(&vo.CommonFiles{Id: id}).Error
if txErr != nil {
return txErr
}
database.RedisTemplate.Del(ctx, consts.REDIS_FILE+id, consts.REDIS_FILE_BYTES+id).Err()
return txErr
})
if err != nil {
ctrl.Ctx.JSON(AjaxResult.Error("删除失败"))
}
ctrl.Ctx.JSON(AjaxResult.OkMsg("删除完成", nil))
}
func (ctrl *FileController) BeforeActivation(activation mvc.BeforeActivation) {
// log.Println("before")
activation.Handle(http.MethodDelete, "/del/{id}", "DelFile")
}
func (ctrl *FileController) AfterActivation(activation mvc.AfterActivation) {
// log.Println("after")
}

View File

@ -84,7 +84,7 @@ func (*articleService) PageAdminArticle(page int, itemsPerPage int) vo.Page[admi
}
var totalPages int = int(totalElements)/itemsPerPage + pre
return vo.Page[admin.AdminArticle]{TotalElements: totalElements, TotalPages: totalPages, Number: totalPages, Content: content}
return vo.Page[admin.AdminArticle]{TotalElements: totalElements, TotalPages: totalPages, Number: page, Content: content}
}
func (*articleService) CreateArticle(articel *admin.AdminArticle) (string, error) {

View File

@ -56,5 +56,5 @@ func (*fileService) PageSysFiles(page int, itemsPerPage int) vo.Page[admin.SysFi
}
var totalPages int = int(totalElements)/itemsPerPage + pre
return vo.Page[admin.SysFile]{TotalElements: totalElements, TotalPages: totalPages, Number: totalPages, Content: content}
return vo.Page[admin.SysFile]{TotalElements: totalElements, TotalPages: totalPages, Number: page, Content: content}
}

View File

@ -111,7 +111,6 @@
el: '#app',
template: '#app-template',
data: {
group: null,
drawer: false,
loading: false,
dialog: {
@ -176,7 +175,7 @@
ths.articleList = data.content;
ths.pageable.totalElements = data.totalElements
ths.pageable.totalPages = data.totalPages
ths.pageable.page = data.number + 1
ths.pageable.page = data.number
ths.loading = false;
// console.log(ths.pageable)
@ -191,7 +190,7 @@
this.dialog.obj = item
this.dialog.title = '发布文章'
this.dialog.context = '确认要发布标题为《' + item.title + '》的文章吗?'
this.dialog.context = `确认要发布标题为《${item.title}》的文章吗?`
this.dialog.show = true;
this.confirm = () => {

View File

@ -102,7 +102,7 @@
axios.post('/admin/diary/submit', formData, {headers: {"Content-Type": "multipart/form-data"}}).then(res => {
let data = res.data
console.log(data)
// location.href = '/diary'
location.href = '/diary'
this.sending = false
}).catch(err => {
ths.error = err

View File

@ -51,19 +51,30 @@
<v-main class="grey lighten-3">
<v-container style="height: 100%">
<v-row style="height: 100%">
<!-- <v-col cols="12" sm="2" class="">
<v-col cols="12" sm="2" class="">
<v-sheet min-height="100%">
<v-btn class="ma-2 white--text" :loading="uploading" :disabled="uploading" color="blue-grey" @click="upload">
上传
<v-icon right dark>mdi-cloud-upload</v-icon>
</v-btn>
<v-form ref="fileForm">
<v-file-input show-size counter multiplelabel="请选择文件" v-model="fileInfo" ref="file"></v-file-input>
</v-form >
<v-alert color="error" type="error" color="red lighten-2" v-if="errorMsg">
{{errorMsg}}
</v-alert>
</v-sheet>
</v-col> -->
</v-col>
<v-col cols="12" sm="12" md="12">
<v-col cols="12" sm="12" md="10">
<v-sheet min-height="100%">
<v-data-table class="elevation-1"
:headers="headers"
:items="itemList"
height="700px"
:height="tableHeight"
:page.sync="pageable.page"
fixed-header="true"
:items-per-page="pageable.itemsPerPage"
:loading="loading"
loading-text="数据加载中"
@ -80,6 +91,7 @@
<template #item.actions="{ item }">
<v-btn text color="primary" :href="`/file/${item.id}`" target="_blank">下载</v-btn>
<v-btn text color="error" @click="delFile(item)">删除</v-btn>
</template>
</v-data-table>
<div class="pt-2 float-right" style="display: flex">
@ -112,10 +124,24 @@
new Vue({
el: '#app',
template: '#app-template',
computed: {
// tableHeight: {
// get() {
// if (window.innerHeight > 1000) {
// return window.innerHeight - 120
// }
// return window.innerHeight - 300
// }
// }
},
data: {
group: null,
tableHeight: 0,
drawer: false,
loading: false,
uploading: false,
fileInfo: null,
errorMsg: null,
dialog: {
show: false,
title: '',
@ -173,7 +199,7 @@
ths.itemList = data.content;
ths.pageable.totalElements = data.totalElements
ths.pageable.totalPages = data.totalPages
ths.pageable.page = data.number + 1
ths.pageable.page = data.number
ths.loading = false;
}).catch(function (error) {
@ -182,15 +208,74 @@
});
},
download(id) {
console.log(id);
delFile(item) {
// console.log(id);
let ths = this;
let id = item.id
this.dialog.obj = item
this.dialog.title = '删除文件'
this.dialog.context = `确认要删除名为《${item.fileName}》的文章吗?`
this.dialog.show = true;
this.confirm = () => {
ths.dialog.show = false;
axios.delete(`/admin/file/del/${id}`).then(res => {
let data = res.data
if (data.code === 200) {
ths.list()
return
}
}).catch(err => {
});
}
},
upload() {
let ths = this;
if (!this.fileInfo) {
console.log("请选择文件");
return
}
let formData = new FormData();
formData.append("file", this.fileInfo);
this.uploading = true
axios.post('/admin/file/upload', formData, { headers: { "Content-Type": "multipart/form-data" } }).then(res => {
let data = res.data
console.log(data)
// location.href = '/diary'
ths.uploading = false
if (data.success === 1) {
ths.$refs.fileForm.reset()
ths.list()
ths.errorMsg = null
return
}
ths.errorMsg = data.message
}).catch(err => {
console.error(err)
ths.uploading = false
ths.errorMsg = err
});
}
},
mounted() {
this.list();
let ths = this
function resize(){
if (window.innerHeight > 900) {
ths.tableHeight = window.innerHeight - 150
} else {
ths.tableHeight = window.innerHeight - 300
}
}
window.addEventListener("resize", resize)
resize()
},
vuetify: new Vuetify(),