bonus-ui/src/views/swiper-manage/index.vue

282 lines
7.5 KiB
Vue
Raw Normal View History

<template>
<div class="app-container">
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button
type="primary"
plain
icon="el-icon-plus"
size="mini"
@click="handleAdd"
>
新增轮播图
</el-button>
</el-col>
</el-row>
2025-01-16 09:24:53 +08:00
<el-table :data="swiperList" border>
<el-table-column label="序号" align="center" width="100" type="index" />
2025-01-16 09:24:53 +08:00
<el-table-column
prop="slidePicture"
label="图片链接"
align="center"
show-overflow-tooltip
/>
<el-table-column prop="isShow" label="是否禁用" align="center">
2025-01-15 13:16:34 +08:00
<template slot-scope="{ row }">
<el-switch
active-text="激活"
inactive-text="禁用"
2025-01-16 09:24:53 +08:00
v-model="row.delFlag"
@change="onSwitchChange($event, row)"
2025-01-15 13:16:34 +08:00
/>
</template>
</el-table-column>
2025-01-16 09:24:53 +08:00
<el-table-column
prop="slideLink"
label="外部链接"
align="center"
show-overflow-tooltip
/>
2025-01-15 13:16:34 +08:00
<el-table-column label="操作" align="center">
2025-01-16 09:24:53 +08:00
<template slot-scope="{ row }">
<el-button size="mini" type="danger" @click="onClickDelete(row)">
2025-01-15 13:16:34 +08:00
删除
</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog
2025-01-15 13:16:34 +08:00
width="50%"
append-to-body
title="新增轮播图"
:visible.sync="addSwiperVisible"
>
<el-form
ref="addSwiperFormRef"
:model="addSwiperForm"
:rules="addSwiperFormRules"
label-width="120px"
>
<el-row>
<el-col :span="24">
2025-01-16 09:24:53 +08:00
<el-form-item label="外部链接:" prop="slideLink">
<el-input
placeholder="请输入外部链接"
v-model="addSwiperForm.slideLink"
/>
</el-form-item>
</el-col>
<el-col :span="24">
2025-01-16 09:24:53 +08:00
<el-form-item label="是否激活:" prop="delFlag">
<el-switch
active-text="激活"
inactive-text="禁用"
2025-01-16 09:24:53 +08:00
v-model="addSwiperForm.delFlag"
></el-switch>
</el-form-item>
</el-col>
<el-col :span="24">
2025-01-16 09:24:53 +08:00
<el-form-item label="轮播图上传:" prop="fileList">
2025-01-15 10:07:56 +08:00
<el-upload
:limit="1"
2025-01-15 13:16:34 +08:00
:headers="headers"
:show-file-list="true"
2025-01-15 10:07:56 +08:00
:action="uploadFileUrl"
list-type="picture-card"
2025-01-15 13:16:34 +08:00
:on-exceed="handleExceed"
2025-01-15 10:07:56 +08:00
:on-remove="handleRemove"
:on-preview="handlePreview"
:on-success="handleSuccess"
:before-upload="handleBeforeUpload"
2025-01-15 13:16:34 +08:00
:file-list="addSwiperForm.fileList"
2025-01-15 10:07:56 +08:00
>
<i class="el-icon-plus"></i>
</el-upload>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitForm"> </el-button>
<el-button @click="cancel"> </el-button>
</div>
2025-01-15 13:16:34 +08:00
<el-dialog
width="30%"
append-to-body
:before-close="
() => {
dialogInnerVisible = false;
}
"
:visible.sync="dialogInnerVisible"
>
<img
:src="previewUrl"
style="display: block; max-width: 100%; margin: 0 auto"
/>
</el-dialog>
</el-dialog>
</div>
</template>
<script>
2025-01-15 13:16:34 +08:00
import { getToken } from "@/utils/auth";
import {
addHomeSwiperApi,
2025-01-16 09:24:53 +08:00
getSwiperListApi,
2025-01-15 13:16:34 +08:00
deleteHomeSwiperApi,
editHomeSwiperTypeApi,
} from "@/api/swiper-manage/index.js";
export default {
data() {
return {
2025-01-15 13:16:34 +08:00
uploadFileUrl: process.env.VUE_APP_BASE_API + "/file/upload",
dialogInnerVisible: false,
fileType: ["png", "jpg", "jpeg"],
previewUrl: "",
addSwiperVisible: false,
value1: true,
value2: true,
2025-01-15 13:16:34 +08:00
addSwiperFormRules: {},
addSwiperForm: {
fileList: [],
2025-01-16 09:24:53 +08:00
slidePicture: "",
slideLink: "",
delFlag: true,
2025-01-15 13:16:34 +08:00
},
headers: {
Authorization: "Bearer " + getToken(),
},
2025-01-16 09:24:53 +08:00
swiperList: [
{
imgSrc: "xxxx",
isShow: true,
outLink: "xxxx999",
},
{
imgSrc: "xxxx",
isShow: true,
outLink: "xxxx999",
},
{
imgSrc: "xxxx",
isShow: false,
outLink: "xxxx999",
},
],
};
},
2025-01-16 09:24:53 +08:00
created() {
this.getSwiperListData();
},
methods: {
handleAdd() {
this.addSwiperVisible = true;
},
2025-01-16 09:24:53 +08:00
// 获取列表
async getSwiperListData() {
const res = await getSwiperListApi();
this.swiperList = res.rows;
this.swiperList.forEach((e) => {
e.delFlag == 0 ? (e.delFlag = true) : (e.delFlag = false);
});
// console.log(res, "列表数据--");
},
async submitForm() {
// 组装参数
const { slidePicture, slideLink, delFlag } = this.addSwiperForm;
const params = {
slidePicture,
slideLink,
delFlag: delFlag ? 0 : 2,
};
const res = await addHomeSwiperApi(params);
if (res.code === 200) {
this.$modal.msgSuccess("新增成功");
this.$refs.addSwiperFormRef.resetFields();
this.addSwiperVisible = false;
this.getSwiperListData();
}
// console.log(res, "上传结果--");
// this.addSwiperVisible = false;
},
cancel() {
this.addSwiperVisible = false;
},
2025-01-15 10:09:35 +08:00
// 上传之前
handleBeforeUpload(file) {
2025-01-15 13:16:34 +08:00
const isSvg = this.fileType.some((e) => file.type.includes(e));
if (!isSvg) {
this.$modal.msgError(
`文件格式不正确, 请上传${this.fileType.join("、")}格式的文件!`
);
return false;
}
const isLt = file.size / 1024 / 1024 < 10;
if (!isLt) {
this.$modal.msgError(`图片大小不能超过 10 MB`);
return false;
}
this.$modal.loading("图片正在上传,请稍候...");
2025-01-15 10:09:35 +08:00
},
// 移除
handleRemove(file) {
2025-01-16 09:24:53 +08:00
this.addSwiperForm.fileList = this.addSwiperForm.fileList.filter(
(item) => item.uid !== file.uid
);
this.addSwiperForm.slidePicture = "";
2025-01-15 10:09:35 +08:00
},
2025-01-15 13:16:34 +08:00
// 预览 图片
2025-01-15 10:09:35 +08:00
handlePreview(file) {
2025-01-15 13:16:34 +08:00
this.dialogInnerVisible = true;
this.previewUrl = file.url;
},
// 文件个数超出限制
handleExceed() {
this.$modal.msgError(`上传的图片数量不能超过 1 个`);
2025-01-15 10:09:35 +08:00
},
// 上传成功
handleSuccess(res) {
2025-01-15 13:16:34 +08:00
if (res.code === 200) {
2025-01-16 09:24:53 +08:00
this.addSwiperForm.slidePicture = res.data.url;
2025-01-15 13:16:34 +08:00
} else {
2025-01-16 09:24:53 +08:00
this.addSwiperForm.fileList = [];
this.addSwiperForm.slidePicture = "";
2025-01-15 13:16:34 +08:00
}
this.$modal.closeLoading();
},
2025-01-16 09:24:53 +08:00
onClickDelete(row) {
2025-01-15 13:16:34 +08:00
this.$confirm("确定删除该轮播图吗?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
})
2025-01-16 09:24:53 +08:00
.then(() => {
console.log(row, "row-");
})
2025-01-15 13:16:34 +08:00
.catch(() => {});
2025-01-15 10:09:35 +08:00
},
2025-01-16 09:24:53 +08:00
onSwitchChange(val, row) {
if (!val) {
this.$confirm("确定禁用该轮播图吗?", "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
})
.then(() => {})
.catch(() => {
row.isShow = true;
});
}
console.log(row, "row---");
},
},
};
</script>