JM-admin/src/view/lbtManage/index.vue
2023-10-27 00:09:58 +08:00

317 lines
10 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup>
// 引入依赖
import api from '@/api/lbt'
// import { toSQLLine } from '@/utils/stringFun'
import WarningBar from '@/components/warningBar/warningBar.vue'
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const dialogFormVisible = ref(false)
import { useUserStore } from '@/pinia/modules/user'
const userStore = useUserStore()
import { parseTime } from '@/utils'
// 变量
const path = ref(import.meta.env.VITE_BASE_API)
const queryParams = ref({
page: 1,
pageSize: 10
})
const deleteVisible = ref(false)
const lbt_ids = ref([])
const dialogTitle = ref('')
const form = ref({ status: '1' })
const lbtType_options = ref([
{
label: '首页',
value: 1
}
])
const rules = ref({
// lbtName: [{ required: true, message: '请输入名称', trigger: 'blur' }],
imgUrl: [{ required: true, message: '请选择素材', trigger: 'blur' }],
link: [{ required: true, message: '请填写跳转链接', trigger: 'blur' }],
// lbtType: [{ required: true, message: '请选择展示位置', trigger: 'blur' }]
})
const ruleFormRef = ref(null)
const tableData = ref([])
const total = ref(0)
// 生命周期
onMounted(() => {
getLbtList()
})
// 方法
async function getLbtList() {
const res = await api.getLbtManageList(queryParams.value)
if (res.code === 0) {
tableData.value = res.data.list
total.value = res.data.total
}
}
function openDialog(type) {
dialogFormVisible.value = true
switch (type) {
case 'add':
dialogTitle.value = '新增轮播图'
break
case 'edit':
dialogTitle.value = '编辑轮播图'
break
}
}
async function onDelete() {
const ids = lbt_ids.value.map(item => item.banner_id)
const res = await api.delLbt({ ids })
if (res.code === 0) {
ElMessage({
type: 'success',
message: res.msg
})
// if (tableData.value.length === ids.length && page.value > 1) {
// page.value--
// }
deleteVisible.value = false
getLbtList()
}
}
function closeDialog() {
dialogFormVisible.value = false
form.value = { status: '1' }
}
function handleAvatarSuccess(res) {
console.log(res)
form.value.imgUrl = res.data.file.url
}
function beforeAvatarUpload(file) {
const isLt05M = file.size / 1024 / 1024 < 20
const isJPG = file.type.indexOf('image/') === -1
if (isJPG) {
ElMessage.error('文件格式错误,请上传图片类型,如JPGPNG后缀的文件')
}
if (!isLt05M) {
ElMessage.error('上传头像图片大小不能超过 2M!')
}
return !isJPG && isLt05M
}
function enterDialog() {
console.log(form.value)
submitForm(ruleFormRef.value)
}
const submitForm = async(formEl) => {
if (!formEl) return
await formEl.validate((valid, fields) => {
if (valid) {
console.log('submit!')
saveForm()
} else {
// console.log('error submit!', fields)
}
})
}
async function saveForm() {
form.value.status = parseInt(form.value.status)
form.value.linkType = parseInt(form.value.linkType)
form.value.lbtType = parseInt(form.value.lbtType)
const params = { // 新参数
imgUrl: form.value.imgUrl,
link: form.value.link
}
let func_name = ''
if (form.value.ID) { // 编辑
func_name = 'editLbtManage'
params.id = form.value.ID
} else {
func_name = 'addLbtManage'
}
const res = await api[func_name](params)
if (res.code === 0) {
ElMessage({
type: 'success',
message: '操作成功!'
})
getLbtList()
closeDialog()
}
}
const handleSelectionChange = (val) => {
lbt_ids.value = val
}
function editLbtFunc(item) {
form.value = item
openDialog('edit')
}
function delLbtFunc(item) {
ElMessageBox.confirm('此操作将永久删除该数据, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.then(async() => {
const res = await api.delLbt({
// ids: [item.ID]
id: item.ID
})
if (res.code === 0) {
ElMessage({
type: 'success',
message: '删除成功!'
})
// if (tableData.value.length === 1 && page.value > 1) {
// page.value--
// }
getLbtList()
}
}, () => {
})
}
function handleCurrentChange(val) {
queryParams.value.page = val
getLbtList()
}
function handleSizeChange(val) {
queryParams.value.pageSize = val
getLbtList()
}
</script>
<template>
<div>
<div class="gva-table-box">
<!-- 批量操作-->
<div class="gva-btn-list">
<el-button size="small" type="primary" icon="plus" @click="openDialog('add')">新增</el-button>
<el-popover v-model="deleteVisible" placement="top" width="160">
<p>确定要删除吗?</p>
<div style="text-align: right; margin-top: 8px;">
<el-button size="small" type="primary" link @click="deleteVisible = false">取消</el-button>
<el-button size="small" type="primary" @click="onDelete">确定</el-button>
</div>
<!-- <template #reference>
<el-button icon="delete" size="small" type="danger" :disabled="!lbt_ids.length" style="margin-left: 10px;" @click="deleteVisible = true">删除</el-button>
</template> -->
</el-popover>
</div>
<!-- 数据列表-->
<el-table border :data="tableData" @selection-change="handleSelectionChange">
<!-- <el-table-column
type="selection"
width="55"
/> -->
<!-- <el-table-column align="left" label="名称" min-width="150" prop="lbtName" /> -->
<el-table-column align="left" label="缩略图" min-width="150" prop="imgUrl">
<template #default="scope">
<img class="lbt-thumb" :src="scope.row.imgUrl" alt="">
</template>
</el-table-column>
<el-table-column align="left" label="是否启用" min-width="150" prop="status">
<template #default="scope">
{{ scope.row.status == '1'?'启用':'禁用' }}
</template>
</el-table-column>
<!-- <el-table-column align="left" label="说明" min-width="150" prop="lbtIntroduction" /> -->
<el-table-column align="left" label="更新时间" min-width="150" prop="UpdatedAt">
<template #default="scope">
{{ parseTime(scope.row.UpdatedAt) }}
</template>
</el-table-column>
<el-table-column align="left" label="操作" min-width="150" prop="status">
<template #default="scope">
<el-button
icon="edit"
size="small"
type="primary"
link
@click="editLbtFunc(scope.row)"
>修改</el-button>
<el-button
icon="delete"
size="small"
type="danger"
link
@click="delLbtFunc(scope.row)"
>删除</el-button>
</template>
</el-table-column>
</el-table>
<div class="gva-pagination">
<el-pagination
:current-page="queryParams.page"
:page-size="queryParams.pageSize"
:page-sizes="[10, 30, 50, 100]"
:total="total"
layout="total, sizes, prev, pager, next, jumper"
@current-change="handleCurrentChange"
@size-change="handleSizeChange"
/>
</div>
</div>
<!--新增、编辑 窗口-->
<el-dialog v-model="dialogFormVisible" :before-close="closeDialog" :title="dialogTitle">
<el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="80px">
<!-- <el-form-item label="名称" prop="lbtName">
<el-input v-model="form.lbtName" placeholder="请输入名称" autocomplete="off" />
</el-form-item> -->
<el-form-item label="素材" prop="imgUrl">
<el-upload
class="avatar-uploader"
:action="`${path}/fileUploadAndDownload/upload`"
:headers="{ 'x-token': userStore.token }"
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
>
<img v-if="form.imgUrl" :src="form.imgUrl" class="img-container">
<el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
</el-upload>
</el-form-item>
<!-- <el-form-item label="说明" prop="lbtIntroduction">
<el-input v-model="form.lbtIntroduction" placeholder="说明" autocomplete="off" />
</el-form-item>
<el-form-item label="链接状态" prop="method">
<el-switch v-model="form.linkType" :active-value="1" :inactive-value="2" active-text="外链" inactive-text="内链" />
</el-form-item> -->
<el-form-item label="链接地址" prop="link">
<el-input v-model="form.link" placeholder="链接地址" autocomplete="off" />
</el-form-item>
<!-- <el-form-item label="启用状态" prop="status">
<el-switch v-model="form.status" :active-value="1" :inactive-value="2" active-text="启用" inactive-text="禁用" />
</el-form-item> -->
<!-- <el-form-item label="展示位置" prop="lbtType">
<el-select v-model="form.lbtType" class="m-2" placeholder="请选择">
<el-option
v-for="item in lbtType_options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item> -->
<el-form-item style="display: none">
<el-button type="primary" @click="submitForm(ruleFormRef)">
Create
</el-button>
</el-form-item>
</el-form>
<template #footer>
<div class="dialog-footer">
<el-button size="small" @click="closeDialog">取 消</el-button>
<el-button size="small" type="primary" @click="enterDialog"> </el-button>
</div>
</template>
</el-dialog>
</div>
</template>
<style scoped>
.img-container{
width: 200px;
height: 150px;
}
.gva-btn-list{
justify-content: end;
}
.lbt-thumb{
width: 100px;
height: 50px;
}
</style>