增加文件删除功能,修复了文章和文件分页当前页数的BUG,
This commit is contained in:
parent
a2d868d354
commit
0a9605a74c
|
@ -2,6 +2,7 @@ package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"blog/internal/service"
|
"blog/internal/service"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
|
@ -26,7 +27,7 @@ func (ctrl *FileController) ViewFile() {
|
||||||
|
|
||||||
func (ctrl *FileController) BeforeActivation(activation mvc.BeforeActivation) {
|
func (ctrl *FileController) BeforeActivation(activation mvc.BeforeActivation) {
|
||||||
// log.Println("before")
|
// log.Println("before")
|
||||||
activation.Handle("GET", "{id}", "ViewFile")
|
activation.Handle(http.MethodGet, "{id}", "ViewFile")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctrl *FileController) AfterActivation(activation mvc.AfterActivation) {
|
func (ctrl *FileController) AfterActivation(activation mvc.AfterActivation) {
|
||||||
|
|
|
@ -1,14 +1,20 @@
|
||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"blog/internal/consts"
|
||||||
|
"blog/internal/model/AjaxResult"
|
||||||
"blog/internal/model/admin"
|
"blog/internal/model/admin"
|
||||||
|
"blog/internal/model/vo"
|
||||||
"blog/internal/service"
|
"blog/internal/service"
|
||||||
"blog/third_party/SessionUtil"
|
"blog/third_party/SessionUtil"
|
||||||
"blog/third_party/database"
|
"blog/third_party/database"
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/kataras/iris/v12"
|
"github.com/kataras/iris/v12"
|
||||||
|
"github.com/kataras/iris/v12/mvc"
|
||||||
"github.com/kataras/iris/v12/sessions"
|
"github.com/kataras/iris/v12/sessions"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
@ -84,3 +90,35 @@ func (ctrl *FileController) PostUpload() {
|
||||||
Location: "/file/" + fileId,
|
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")
|
||||||
|
}
|
||||||
|
|
|
@ -84,7 +84,7 @@ func (*articleService) PageAdminArticle(page int, itemsPerPage int) vo.Page[admi
|
||||||
}
|
}
|
||||||
var totalPages int = int(totalElements)/itemsPerPage + pre
|
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) {
|
func (*articleService) CreateArticle(articel *admin.AdminArticle) (string, error) {
|
||||||
|
|
|
@ -56,5 +56,5 @@ func (*fileService) PageSysFiles(page int, itemsPerPage int) vo.Page[admin.SysFi
|
||||||
}
|
}
|
||||||
var totalPages int = int(totalElements)/itemsPerPage + pre
|
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}
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,7 +111,6 @@
|
||||||
el: '#app',
|
el: '#app',
|
||||||
template: '#app-template',
|
template: '#app-template',
|
||||||
data: {
|
data: {
|
||||||
group: null,
|
|
||||||
drawer: false,
|
drawer: false,
|
||||||
loading: false,
|
loading: false,
|
||||||
dialog: {
|
dialog: {
|
||||||
|
@ -176,7 +175,7 @@
|
||||||
ths.articleList = data.content;
|
ths.articleList = data.content;
|
||||||
ths.pageable.totalElements = data.totalElements
|
ths.pageable.totalElements = data.totalElements
|
||||||
ths.pageable.totalPages = data.totalPages
|
ths.pageable.totalPages = data.totalPages
|
||||||
ths.pageable.page = data.number + 1
|
ths.pageable.page = data.number
|
||||||
|
|
||||||
ths.loading = false;
|
ths.loading = false;
|
||||||
// console.log(ths.pageable)
|
// console.log(ths.pageable)
|
||||||
|
@ -191,7 +190,7 @@
|
||||||
|
|
||||||
this.dialog.obj = item
|
this.dialog.obj = item
|
||||||
this.dialog.title = '发布文章'
|
this.dialog.title = '发布文章'
|
||||||
this.dialog.context = '确认要发布标题为《' + item.title + '》的文章吗?'
|
this.dialog.context = `确认要发布标题为《${item.title}》的文章吗?`
|
||||||
this.dialog.show = true;
|
this.dialog.show = true;
|
||||||
|
|
||||||
this.confirm = () => {
|
this.confirm = () => {
|
||||||
|
|
|
@ -102,7 +102,7 @@
|
||||||
axios.post('/admin/diary/submit', formData, {headers: {"Content-Type": "multipart/form-data"}}).then(res => {
|
axios.post('/admin/diary/submit', formData, {headers: {"Content-Type": "multipart/form-data"}}).then(res => {
|
||||||
let data = res.data
|
let data = res.data
|
||||||
console.log(data)
|
console.log(data)
|
||||||
// location.href = '/diary'
|
location.href = '/diary'
|
||||||
this.sending = false
|
this.sending = false
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
ths.error = err
|
ths.error = err
|
||||||
|
|
|
@ -51,19 +51,30 @@
|
||||||
<v-main class="grey lighten-3">
|
<v-main class="grey lighten-3">
|
||||||
<v-container style="height: 100%">
|
<v-container style="height: 100%">
|
||||||
<v-row 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-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-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-sheet min-height="100%">
|
||||||
|
|
||||||
<v-data-table class="elevation-1"
|
<v-data-table class="elevation-1"
|
||||||
:headers="headers"
|
:headers="headers"
|
||||||
:items="itemList"
|
:items="itemList"
|
||||||
height="700px"
|
:height="tableHeight"
|
||||||
:page.sync="pageable.page"
|
:page.sync="pageable.page"
|
||||||
|
fixed-header="true"
|
||||||
:items-per-page="pageable.itemsPerPage"
|
:items-per-page="pageable.itemsPerPage"
|
||||||
:loading="loading"
|
:loading="loading"
|
||||||
loading-text="数据加载中"
|
loading-text="数据加载中"
|
||||||
|
@ -80,6 +91,7 @@
|
||||||
|
|
||||||
<template #item.actions="{ item }">
|
<template #item.actions="{ item }">
|
||||||
<v-btn text color="primary" :href="`/file/${item.id}`" target="_blank">下载</v-btn>
|
<v-btn text color="primary" :href="`/file/${item.id}`" target="_blank">下载</v-btn>
|
||||||
|
<v-btn text color="error" @click="delFile(item)">删除</v-btn>
|
||||||
</template>
|
</template>
|
||||||
</v-data-table>
|
</v-data-table>
|
||||||
<div class="pt-2 float-right" style="display: flex">
|
<div class="pt-2 float-right" style="display: flex">
|
||||||
|
@ -112,10 +124,24 @@
|
||||||
new Vue({
|
new Vue({
|
||||||
el: '#app',
|
el: '#app',
|
||||||
template: '#app-template',
|
template: '#app-template',
|
||||||
|
computed: {
|
||||||
|
// tableHeight: {
|
||||||
|
// get() {
|
||||||
|
// if (window.innerHeight > 1000) {
|
||||||
|
// return window.innerHeight - 120
|
||||||
|
// }
|
||||||
|
// return window.innerHeight - 300
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
},
|
||||||
data: {
|
data: {
|
||||||
group: null,
|
tableHeight: 0,
|
||||||
drawer: false,
|
drawer: false,
|
||||||
loading: false,
|
loading: false,
|
||||||
|
uploading: false,
|
||||||
|
fileInfo: null,
|
||||||
|
errorMsg: null,
|
||||||
dialog: {
|
dialog: {
|
||||||
show: false,
|
show: false,
|
||||||
title: '',
|
title: '',
|
||||||
|
@ -173,7 +199,7 @@
|
||||||
ths.itemList = data.content;
|
ths.itemList = data.content;
|
||||||
ths.pageable.totalElements = data.totalElements
|
ths.pageable.totalElements = data.totalElements
|
||||||
ths.pageable.totalPages = data.totalPages
|
ths.pageable.totalPages = data.totalPages
|
||||||
ths.pageable.page = data.number + 1
|
ths.pageable.page = data.number
|
||||||
|
|
||||||
ths.loading = false;
|
ths.loading = false;
|
||||||
}).catch(function (error) {
|
}).catch(function (error) {
|
||||||
|
@ -182,15 +208,74 @@
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
download(id) {
|
delFile(item) {
|
||||||
console.log(id);
|
// 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() {
|
mounted() {
|
||||||
this.list();
|
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(),
|
vuetify: new Vuetify(),
|
||||||
|
|
Loading…
Reference in New Issue