ADD file via upload

main
pjhmizn49 1 year ago
parent 42ba345c19
commit 54d57a8fc7

@ -0,0 +1,466 @@
<template>
<div>
<div class="tableTop">
<div>
<el-input @input="handleSearch" suffix-icon="el-icon-search" style="width: 300px" placeholder="请输入搜索关键字" v-model="input" clearable></el-input>
</div>
<div>
<el-button @click="handleAdd" type="primary">添加赠品</el-button>
<el-button @click="handleDeleteMul" type="danger">批量删除</el-button>
</div>
<!-- 表单 -->
<el-dialog title="赠品信息" :visible.sync="dialogVisible" :before-close="handleClose">
<el-form align="left" ref="dialogForm" :rules="rules" :model="dialogForm" label-width="130px" style="margin-right: 150px">
<el-form-item label="赠品名称" prop="gift_name">
<el-input v-model="dialogForm.gift_name" placeholder="请输入赠品名称"></el-input>
</el-form-item>
<el-form-item label="兑换积分" required>
<el-input-number v-model="dialogForm.gift_point" :step="100" :min="0"></el-input-number>
</el-form-item>
<el-form-item label="库存(件)" required>
<el-input-number v-model="dialogForm.gift_stock" :step="50" :min="0"></el-input-number>
</el-form-item>
<el-form-item label="展示图">
<el-upload
class="upload-demo"
:on-success="onSuccess"
:on-error="onError"
:action="request"
:headers="headers"
:before-remove="beforeRemove"
:limit="1"
:on-exceed="handleExceed"
:file-list="fileList">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件且不超过500kb</div>
</el-upload>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="handleClose"> </el-button>
<el-button type="primary" @click="submitForm"> </el-button>
</div>
</el-dialog>
</div>
<el-table
ref="multipleTable"
:data="tableData"
tooltip-effect="dark"
style="width: 100%"
@selection-change="handleSelectionChange">
<el-table-column type="selection" width="60"></el-table-column>
<el-table-column prop="gift_id" label="id" width="100"></el-table-column>
<el-table-column prop="gift_name" label="赠品名" width="200"></el-table-column>
<el-table-column prop="gift_point" label="兑换积分" width="150"></el-table-column>
<el-table-column prop="gift_stock" label="库存(件)" width="150"></el-table-column>
<el-table-column prop="gift_pic" label="展示图" width="180">
<template slot-scope="scope">
<img :src="require('/Users/zhangjiadi/IdeaProjects/毕业设计/picFiles'+scope.row.gift_pic)" style="height: 80px;width: 80px" alt="">
</template>
</el-table-column>
<el-table-column label="操作" >
<template slot-scope="scope">
<el-button @click="handleModify(scope.row)" type="primary" size="small" plain>修改</el-button>
<el-button @click="handleDelete(scope.row)" type="danger" size="small" plain>删除</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
background
:current-page="currentPage"
:page-size="pageSize"
layout="total, prev, pager, next, jumper"
:total="total"
:page-count="page_number"
style="margin-top: 30px"
@current-change="handleCurrentChange">
</el-pagination>
</div>
</template>
<script>
import Cookie from "js-cookie";
import {http} from "@/js/http";
export default {
data() {
return {
//
tableData: [{
gift_id: 1,
gift_name: '',
gift_point: 1,
gift_stock: 1,
gift_pic:''
}],
dialogForm:[{
gift_id: 1,
gift_name: '',
gift_point: 1,
gift_stock: 1,
gift_pic:'/10001/20240307123830248.jpg'
}],
//
rules:{
gift_name:[
{ required:true, trigger:'blur', message:'请输入名称'}
],
gift_point:[
{ required:true, trigger:'blur', message:'请输入兑换积分'}
],
gift_stock:[
{ required:true, trigger:'blur', message:'请输入库存'}
]
},
//
multipleSelection: [],
input: '',
dialogVisible:false,
modelType:0, //01
//
currentPage:1,
pageSize:8,
page_number:1,//
total:10,//
//
request:http+'/upload',
headers:{ Authorization: Cookie.get('token') },
fileList: []
}
},
created() {
this.currentPage=1;
this.fetchData();
},
methods: {
fetchData(){
//
const _this=this;
try {
axios({
method: 'post',
url: 'http://localhost:8181/gift/list',
headers: {
Authorization: Cookie.get('token'),
'Content-Type': 'application/json'
},
data:{
page:_this.currentPage,
page_size:_this.pageSize
}
}).then(function (response){
if (response.data.code===200){
//
_this.tableData=response.data.data.gifts;
_this.page_number=response.data.data.page_number;
_this.total=response.data.data.total;
}
if (response.data.code===401){
_this.$message.warning('身份验证失败,请重新登录!')
}
if (response.data.code===403){
const message = response.data.msg;
this.$alert("页面加载出错,具体原因如下:\n"+message, '页面加载失败', {
confirmButtonText: '确定'
})
}
});
}catch (error){
this.$message.warning('页面加载出错,请重试!')
}
},
//
handleSearch(){
const _this = this
try {
axios({
method: 'post',
url: 'http://localhost:8181/gift/query',
headers: {
Authorization: Cookie.get('token'),
'Content-Type': 'application/json'
},
data:{
page:_this.currentPage,
page_size:_this.pageSize,
gift_name:_this.input
}
}).then(function (response){
if (response.data.code===200){
//
_this.tableData=response.data.data.gifts;
_this.page_number=response.data.data.page_number;
_this.total=response.data.data.total;
}
if (response.data.code===401){
_this.$message.warning('身份验证失败,请重新登录!')
}
if (response.data.code===403){
const message = response.data.msg;
this.$alert("页面加载出错,具体原因如下:\n"+message, '页面加载失败', {
confirmButtonText: '确定'
})
}
});
}catch (error){
this.$message.warning('页面加载出错,请重试!')
}
},
//
handleCurrentChange(page) {
this.currentPage = page;
this.fetchData();
},
//
handleSelectionChange(val) {
this.multipleSelection = val;
},
//
//
handleAdd(){
this.modelType = 0
this.dialogVisible = true
},
//
handleModify(row){
const _this = this
this.modelType = 1
axios({
method: 'post',
url: 'http://localhost:8181/gift/info',
headers: {
Authorization: Cookie.get('token'),
'Content-Type': 'application/json'
},
data:{
gift_id: row.gift_id
}
}).then(function (response){
if (response.data.code===200) {
//
_this.dialogForm = response.data.data
}
if (response.data.code===401){
_this.$message.warning('身份验证失败,请重新登录!')
}
if (response.data.code===403){
const message = response.data.msg;
this.$alert("任务出错,具体原因如下:\n"+message, '页面加载失败', {
confirmButtonText: '确定'
})
}
});
this.dialogVisible = true
},
//
submitForm(){ //
const _this=this;
this.$refs.dialogForm.validate((valid) =>{
if(valid){
if(_this.modelType===0){
//
axios({
method: 'post',
url: 'http://localhost:8181/gift/add',
headers: {
Authorization: Cookie.get('token'),
'Content-Type': 'application/json'
},
data:{
gift_name:_this.dialogForm.gift_name,
gift_point:_this.dialogForm.gift_point,
gift_stock:_this.dialogForm.gift_stock,
gift_pic:_this.dialogForm.gift_pic
}
}).then(function (response){
if (response.data.code===200) {
//
_this.$message.success('添加成功!')
//
_this.fetchData()
}
if (response.data.code===401){
_this.$message.warning('身份验证失败,请重新登录!')
}
if (response.data.code===403){
const message = response.data.msg;
this.$alert("任务出错,具体原因如下:\n"+message, '页面加载失败', {
confirmButtonText: '确定'
})
}
});
}
else if(_this.modelType===1){
//
axios({
method: 'post',
url: 'http://localhost:8181/gift/modify',
headers: {
Authorization: Cookie.get('token'),
'Content-Type': 'application/json'
},
data:{
gift_id:_this.dialogForm.gift_id,
gift_name:_this.dialogForm.gift_name,
gift_point:_this.dialogForm.gift_point,
gift_stock:_this.dialogForm.gift_stock,
gift_pic:_this.dialogForm.gift_pic
}
}).then(function (response){
if (response.data.code===200) {
//
_this.$message.success('修改成功!')
//
_this.handleSearch()
}
if (response.data.code===401){
_this.$message.warning('身份验证失败,请重新登录!')
}
if (response.data.code===403){
const message = response.data.msg;
this.$alert("任务出错,具体原因如下:\n"+message, '页面加载失败', {
confirmButtonText: '确定'
})
}
});
}
//
this.handleClose();
}
})
},
//
handleDelete(row){
const _this = this;
this.$confirm('此操作将永久删除该赠品信息, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
//
axios({
method: 'post',
url: 'http://localhost:8181/gift/deletePer',
headers: {
Authorization: Cookie.get('token'),
'Content-Type': 'application/json'
},
data:{
gift_id:row.gift_id
}
}).then(function (response){
if (response.data.code===200) {
//
_this.handleSearch()
}
if (response.data.code===401){
_this.$message.warning('身份验证失败,请重新登录!')
}
if (response.data.code===403){
const message = response.data.msg;
this.$alert("页面加载出错,具体原因如下:\n"+message, '页面加载失败', {
confirmButtonText: '确定'
})
}
});
this.$message({type: 'success', message: '删除成功!'});
}).catch(() => {
this.$message({type: 'info', message: '已取消删除'});
});
},
handleDeleteMul(){
const _this = this
if(_this.multipleSelection.length===0)
this.$message({type: 'warning', message: '请选择赠品'});
else{
let ids = []
for (let i=0; i<_this.multipleSelection.length; i++){
ids[i] = _this.multipleSelection[i].gift_id
}
this.$confirm('此操作将永久删除赠品信息, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
//
axios({
method: 'post',
url: 'http://localhost:8181/gift/deleteMul',
headers: {
Authorization: Cookie.get('token'),
'Content-Type': 'application/json'
},
data:{
gift_ids:ids
}
}).then(function (response){
if (response.data.code===200) {
//
_this.handleSearch()
}
if (response.data.code===401){
_this.$message.warning('身份验证失败,请重新登录!')
}
if (response.data.code===403){
const message = response.data.msg;
this.$alert("任务出错,具体原因如下:\n"+message, '页面加载失败', {
confirmButtonText: '确定'
})
}
});
this.$message({type: 'success', message: '批量删除成功!'});
}).catch(() => {
this.$message({type: 'info', message: '已取消删除'});
});
}
},
//dialog
handleClose(){
this.$refs.dialogForm.resetFields()
//
this.dialogForm.gift_id = 1;
this.dialogForm.gift_name='';
this.dialogForm.gift_point=1;
this.dialogForm.gift_stock=1;
this.dialogForm.gift_pic='';
this.fileList=[]
this.dialogVisible = false;
},
//
//
handleExceed(files, fileList) {
this.$message.warning(`当前限制选择 1 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`);
},
beforeRemove(file) {
return this.$confirm(`确定移除 ${ file.name }`);
},
onSuccess(response){
if(response.code===200){
this.dialogForm.gift_pic = response.data.url;
}else if (response.code===401){
this.$message.warning('身份验证失败,请重新登录!')
}else if (response.code===403)
{
this.$alert("上传失败,具体原因如下:\n"+response.msg, '上传失败', {
confirmButtonText: '确定'
})
}
},
onError(){
this.$alert("网络请求失败", '上传失败', {
confirmButtonText: '确定'
})
},
}
}
</script>
<style>
.tableTop{
width: 1200px;
display: flex;
justify-content: space-between;
align-items: center;
margin: 10px 0 20px 50px;
}
</style>
Loading…
Cancel
Save