• 月木学途开发 5.轮播图模块


    概述

    效果图

     

    数据库设计

    轮播图表
    1. DROP TABLE IF EXISTS `banner`;
    2. CREATE TABLE `banner` (
    3.   `bannerId` int(11) NOT NULL AUTO_INCREMENT,
    4.   `bannerUrl` longtext,
    5.   `bannerDesc` varchar(255) DEFAULT NULL,
    6.   `bannerTypeId` int(11) DEFAULT NULL,
    7.   PRIMARY KEY (`bannerId`)
    8. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

    轮播图类型表
    1. DROP TABLE IF EXISTS `banner_type`;
    2. CREATE TABLE `banner_type` (
    3.   `bannerTypeId` int(11) NOT NULL AUTO_INCREMENT,
    4.   `bannerTypeName` varchar(255) DEFAULT NULL,
    5.   PRIMARY KEY (`bannerTypeId`)
    6. ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4;

    实体类设计

    轮播图
    1. package jkw.pojo;
    2. import com.baomidou.mybatisplus.annotation.TableField;
    3. import com.baomidou.mybatisplus.annotation.TableId;
    4. import lombok.Data;
    5. import java.io.Serializable;
    6. /**
    7.  * 轮播图
    8.  */
    9. @Data
    10. public class Banner implements Serializable {
    11.     @TableId
    12.     private Integer bannerId;
    13.     private String bannerUrl;//图片
    14.     private String bannerDesc;//描述
    15.     private Integer bannerTypeId;//类型
    16.     @TableField(exist = false)
    17.     private BannerType bannerType;
    18. }

    轮播图类型
    1. package jkw.pojo;
    2. import com.baomidou.mybatisplus.annotation.TableId;
    3. import lombok.Data;
    4. import java.io.Serializable;
    5. /**
    6.  * 轮播图类型
    7.  */
    8. @Data
    9. public class BannerType implements Serializable {
    10.     @TableId
    11.     private Integer bannerTypeId;
    12.     private String bannerTypeName;//类型名
    13. }

    Mapper层开发

    BannerTypeMapper
    1. package jkw.mapper;
    2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    3. import jkw.pojo.BannerType;
    4. public interface BannerTypeMapper extends BaseMapper<BannerType> {
    5. }

    BannerMapper
    1. package jkw.mapper;
    2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    3. import jkw.pojo.Banner;
    4. public interface BannerMapper extends BaseMapper<Banner> {
    5. }

    Service层开发

    BannerTypeService
    1. package jkw.service;
    2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    3. import jkw.pojo.BannerType;
    4. import java.util.List;
    5. public interface BannerTypeService {
    6.     void add(BannerType BannerType);
    7.     void update(BannerType BannerType);
    8.     void delete(Integer id);
    9.     BannerType findById(Integer id);
    10.     List findAll();
    11.     Page search(String search, int page, int size);
    12. }

    BannerTypeServiceImpl
    1. package jkw.service.impl;
    2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    4. import jkw.mapper.BannerTypeMapper;
    5. import jkw.pojo.BannerType;
    6. import jkw.service.BannerTypeService;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.stereotype.Service;
    9. import org.springframework.transaction.annotation.Transactional;
    10. import java.util.List;
    11. @Service
    12. @Transactional
    13. public class BannerTypeServiceImpl implements BannerTypeService {
    14.     @Autowired
    15.     private BannerTypeMapper bannerTypeMapper;
    16.     @Override
    17.     public void add(BannerType BannerType) {
    18.         bannerTypeMapper.insert(BannerType);
    19.     }
    20.     @Override
    21.     public void update(BannerType BannerType) {
    22.         bannerTypeMapper.updateById(BannerType);
    23.     }
    24.     @Override
    25.     public void delete(Integer id) {
    26.         bannerTypeMapper.deleteById(id);
    27.     }
    28.     @Override
    29.     public BannerType findById(Integer id) {
    30.         return bannerTypeMapper.selectById(id);
    31.     }
    32.     @Override
    33.     public List<BannerType> findAll() {
    34.         return bannerTypeMapper.selectList(null);
    35.     }
    36.     @Override
    37.     public Page<BannerType> search(String search, int page, int size) {
    38.         QueryWrapper queryWrapper = new QueryWrapper();
    39.         if (search != null) {
    40.         }
    41.         return bannerTypeMapper.selectPage(new Page<>(page, size), queryWrapper);
    42.     }
    43. }

    BannerService
    1. package jkw.service;
    2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    3. import jkw.pojo.Banner;
    4. import java.util.List;
    5. public interface BannerService {
    6.     void add(Banner Banner);
    7.     void update(Banner Banner);
    8.     void delete(Integer id);
    9.     Banner findById(Integer id);
    10.     List findAll();
    11.     Page search(String search, int page, int size);
    12. }

    BannerServiceImpl
    1. package jkw.service.impl;
    2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    3. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    4. import jkw.mapper.BannerMapper;
    5. import jkw.pojo.Banner;
    6. import jkw.service.BannerService;
    7. import org.springframework.beans.factory.annotation.Autowired;
    8. import org.springframework.stereotype.Service;
    9. import org.springframework.transaction.annotation.Transactional;
    10. import java.util.List;
    11. @Service
    12. @Transactional
    13. public class BannerServiceImpl implements BannerService {
    14.     @Autowired
    15.     private BannerMapper bannerMapper;
    16.     @Override
    17.     public void add(Banner Banner) {
    18.         bannerMapper.insert(Banner);
    19.     }
    20.     @Override
    21.     public void update(Banner Banner) {
    22.         bannerMapper.updateById(Banner);
    23.     }
    24.     @Override
    25.     public void delete(Integer id) {
    26.         bannerMapper.deleteById(id);
    27.     }
    28.     @Override
    29.     public Banner findById(Integer id) {
    30.         return bannerMapper.selectById(id);
    31.     }
    32.     @Override
    33.     public List<Banner> findAll() {
    34.         return bannerMapper.selectList(null);
    35.     }
    36.     @Override
    37.     public Page<Banner> search(String search, int page, int size) {
    38.         QueryWrapper queryWrapper = new QueryWrapper();
    39.         if (search != null) {
    40.         }
    41.         return bannerMapper.selectPage(new Page<>(page, size), queryWrapper);
    42.     }
    43. }

    控制层开发

    BannerTypeCon
    1. package jkw.controller.back;
    2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    3. import jkw.pojo.BannerType;
    4. import jkw.service.BannerTypeService;
    5. import jkw.vo.BaseResult;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.security.access.prepost.PreAuthorize;
    8. import org.springframework.web.bind.annotation.*;
    9. import java.util.List;
    10. @CrossOrigin
    11. @RestController
    12. @RequestMapping("/back/bannerType")
    13. public class BannerTypeCon {
    14.     @Autowired
    15.     private BannerTypeService bannerTypeService;
    16.     /**
    17.      * 新增
    18.      *
    19.      * @param bannerType
    20.      * @return
    21.      */
    22.     @PostMapping("/add")
    23.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    24.     public BaseResult add(BannerType bannerType) {
    25.         bannerTypeService.add(bannerType);
    26.         return BaseResult.ok();
    27.     }
    28.     /**
    29.      * 修改
    30.      *
    31.      * @param bannerType
    32.      * @return
    33.      */
    34.     @PostMapping("/update")
    35.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    36.     public BaseResult update(BannerType bannerType) {
    37.         bannerTypeService.update(bannerType);
    38.         return BaseResult.ok();
    39.     }
    40.     /**
    41.      * 删除
    42.      *
    43.      * @param bannerTypeId
    44.      * @return
    45.      */
    46.     @DeleteMapping("/delete")
    47.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    48.     public BaseResult delete(Integer bannerTypeId) {
    49.         bannerTypeService.delete(bannerTypeId);
    50.         return BaseResult.ok();
    51.     }
    52.     /**
    53.      * 根据id查询
    54.      *
    55.      * @param bannerTypeId
    56.      * @return
    57.      */
    58.     @GetMapping("/findById")
    59.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    60.     public BaseResult findById(Integer bannerTypeId) {
    61.         BannerType bannerType = bannerTypeService.findById(bannerTypeId);
    62.         return BaseResult.ok(bannerType);
    63.     }
    64.     /**
    65.      * 查询所有
    66.      *
    67.      * @return
    68.      */
    69.     @GetMapping("/findAll")
    70.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    71.     public BaseResult findAll() {
    72.         List<BannerType> all = bannerTypeService.findAll();
    73.         return BaseResult.ok(all);
    74.     }
    75.     /**
    76.      * 分页查询
    77.      *
    78.      * @param page
    79.      * @param size
    80.      * @return
    81.      */
    82.     @GetMapping("/search")
    83.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    84.     public BaseResult search(String search, int page, int size) {
    85.         Page<BannerType> brandPage = bannerTypeService.search(search, page, size);
    86.         return BaseResult.ok(brandPage);
    87.     }
    88. }

    BannerCon
    1. package jkw.controller.back;
    2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    3. import jkw.pojo.Banner;
    4. import jkw.service.BannerService;
    5. import jkw.vo.BaseResult;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.security.access.prepost.PreAuthorize;
    8. import org.springframework.web.bind.annotation.*;
    9. import java.util.List;
    10. @RequestMapping("/back/banner")
    11. @CrossOrigin
    12. @RestController
    13. public class BannerCon {
    14.     @Autowired
    15.     private BannerService bannerService;
    16.     /**
    17.      * 新增
    18.      *
    19.      * @param banner
    20.      * @return
    21.      */
    22.     @PostMapping("/add")
    23.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    24.     public BaseResult add(Banner banner) {
    25.         bannerService.add(banner);
    26.         return BaseResult.ok();
    27.     }
    28.     /**
    29.      * 修改
    30.      *
    31.      * @param banner
    32.      * @return
    33.      */
    34.     @PostMapping("/update")
    35.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    36.     public BaseResult update(Banner banner) {
    37.         bannerService.update(banner);
    38.         return BaseResult.ok();
    39.     }
    40.     /**
    41.      * 删除
    42.      *
    43.      * @param bannerId
    44.      * @return
    45.      */
    46.     @DeleteMapping("/delete")
    47.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    48.     public BaseResult delete(Integer bannerId) {
    49.         bannerService.delete(bannerId);
    50.         return BaseResult.ok();
    51.     }
    52.     /**
    53.      * 根据id查询
    54.      *
    55.      * @param bannerId
    56.      * @return
    57.      */
    58.     @GetMapping("/findById")
    59.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    60.     public BaseResult findById(Integer bannerId) {
    61.         Banner banner = bannerService.findById(bannerId);
    62.         return BaseResult.ok(banner);
    63.     }
    64.     /**
    65.      * 查询所有
    66.      *
    67.      * @return
    68.      */
    69.     @GetMapping("/findAll")
    70.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    71.     public BaseResult findAll() {
    72.         List<Banner> all = bannerService.findAll();
    73.         return BaseResult.ok(all);
    74.     }
    75.     /**
    76.      * 分页查询
    77.      *
    78.      * @param page
    79.      * @param size
    80.      * @return
    81.      */
    82.     @GetMapping("/search")
    83.     @PreAuthorize("hasAnyAuthority('/website/banner')")
    84.     public BaseResult search(String search, int page, int size) {
    85.         Page<Banner> brandPage = bannerService.search(search, page, size);
    86.         return BaseResult.ok(brandPage);
    87.     }
    88. }

    后台ui设计

    BannerType.vue
    1. <template>
    2.   <div class="data-container">
    3.     <!--添加 start-->
    4.     <div class="data-header">
    5.       <el-input class="input" @keyup.enter="searchHandle" v-model="searchInfo" size="large"
    6.                 placeholder="请输入关键字"></el-input>
    7.       <el-button @click="searchHandle" class="button" size="large" type="primary" plain>搜索</el-button>
    8.       <el-button round @click="addHander" size="large" type="primary">
    9.         <el-icon>
    10.           <DocumentAdd/>
    11.         </el-icon>
    12.         <span>新增</span>
    13.       </el-button>
    14.     </div>
    15.     <!--添加 end-->
    16.     <!--表格数据展示 start-->
    17.     <div class="data-table">
    18.       <el-table :data="dataList.list" style="width: 550px;">
    19.         <el-table-column show-overflow-tooltip label="类型名" prop="bannerTypeName" align="center" width="300"></el-table-column>
    20.         <el-table-column label="操作" align="center" width="220">
    21.           <template #default="scope">
    22.             <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
    23.             <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
    24.           </template>
    25.         </el-table-column>
    26.       </el-table>
    27.       <!--分页 start-->
    28.       <div class="page">
    29.         <el-pagination background
    30.                        layout="prev,pager,next,jumper"
    31.                        :default-page-size="defaultPageSize"
    32.                        :total="totalData"
    33.                        @current-change="currentChangeHaddler"></el-pagination>
    34.       </div>
    35.       <!--分页 end-->
    36.     </div>
    37.     <!--表格数据展示 end-->
    38.     <!--添加对话框 start-->
    39.     <el-dialog draggable destroy-on-close v-model="dialogAddVisible" title="添加" width="35%" center>
    40.       <el-form inline :model="addFormInfo" label-width="150px">
    41.         <el-form-item label="类型名">
    42.           <el-input v-model="addFormInfo.bannerTypeName"></el-input>
    43.         </el-form-item>
    44.       </el-form>
    45.       <template #footer>
    46.             <span class="dialog-footer">
    47.                 <el-button @click="dialogAddVisible = false">取消</el-button>
    48.                 <el-button type="primary" @click="sureHandler">确定</el-button>
    49.             </span>
    50.       </template>
    51.     </el-dialog>
    52.     <!--添加对话框 end-->
    53.     <!--编辑对话框 start-->
    54.     <!--destroy-on-close:每次关闭对话框时直接销毁对话框,没有缓存-->
    55.     <el-dialog
    56.         draggable
    57.         destroy-on-close
    58.         v-model="dialogEditorVisible"
    59.         title="编辑"
    60.         width="35%"
    61.         center>
    62.       <el-form inline :model="editorFormInfo" label-width="150px">
    63.         <el-form-item label="类型名">
    64.           <el-input v-model="editorFormInfo.bannerTypeName"></el-input>
    65.         </el-form-item>
    66.       </el-form>
    67.       <template #footer>
    68.             <span class="dialog-footer">
    69.                 <el-button @click="dialogEditorVisible = false">取消</el-button>
    70.                 <el-button type="primary" @click="sureEditorHandler">确定</el-button>
    71.             </span>
    72.       </template>
    73.     </el-dialog>
    74.     <!--编辑对话框 end-->
    75.   </div>
    76. </template>
    77. <script setup>
    78. import axios from "../../api/index.js"
    79. import {onMounted, reactive, ref} from "vue";
    80. import {ElMessage} from "element-plus";
    81. //初始化分页查询数据
    82. const dataList = reactive({
    83.   list: []
    84. })
    85. //初始化总条数
    86. const totalData = ref(0)
    87. //当前页
    88. const currentPage = ref(1)
    89. //初始化分页显示条数
    90. const defaultPageSize = ref(10)
    91. //搜索初始化状态
    92. const searchInfo = ref("")
    93. //添加添加对话框控制器
    94. const dialogAddVisible = ref(false)
    95. //初始化添加对话框状态
    96. const addFormInfo = reactive({
    97.   bannerTypeName: "",
    98. })
    99. //编辑对话框控制器
    100. const dialogEditorVisible = ref(false)
    101. //初始化编辑对话框状态
    102. const editorFormInfo = reactive({
    103.   bannerTypeId: '',
    104.   bannerTypeName: "",
    105. })
    106. /**
    107.  * 网路请求:分页查询
    108.  *  */
    109. const http = (search, page, size) => {
    110.   axios.banner_type_search({
    111.     search: search,
    112.     page: page,
    113.     size: size
    114.   }).then(res => {
    115.     if (res.data.code == 200) {
    116.       dataList.list = res.data.data.records
    117.       totalData.value = res.data.data.total
    118.     } else {
    119.       ElMessage.error(res.data.message)
    120.     }
    121.   })
    122. }
    123. onMounted(() => {
    124.   http(searchInfo.value, currentPage.value, defaultPageSize.value)
    125. })
    126. /**
    127.  * 分页
    128.  */
    129. const currentChangeHaddler = (nowPage) => {
    130.   http(searchInfo.value, nowPage, defaultPageSize.value)
    131.   currentPage.value = nowPage
    132. }
    133. /**
    134.  * 搜索按钮
    135.  */
    136. const searchHandle = () => {
    137.   http(searchInfo.value, currentPage.value, defaultPageSize.value)
    138. }
    139. /**
    140.  * 添加对话框弹出事件
    141.  */
    142. const addHander = () => {
    143.   dialogAddVisible.value = true
    144. }
    145. /**
    146.  * 添加对话框 确定事件
    147.  */
    148. const sureHandler = () => {
    149.   axios.banner_type_add({
    150.     bannerTypeName: addFormInfo.bannerTypeName,
    151.   }).then(res => {
    152.     if (res.data.code == 200) {
    153.       dialogAddVisible.value = false
    154.       http(searchInfo.value, currentPage.value, defaultPageSize.value)
    155.     } else {
    156.       ElMessage.error(res.data.message)
    157.     }
    158.   })
    159. }
    160. /**
    161.  * 编辑对话框 弹出事件
    162.  *  */
    163. const handleEdit = (index, row) => {
    164.   dialogEditorVisible.value = true
    165.   axios.banner_type_findById({
    166.     bannerTypeId: row.bannerTypeId
    167.   }).then(res => {
    168.     if (res.data.code == 200) {
    169.       editorFormInfo.bannerTypeId = res.data.data.bannerTypeId;
    170.       editorFormInfo.bannerTypeName = res.data.data.bannerTypeName;
    171.     } else {
    172.       ElMessage.error(res.data.data.message)
    173.     }
    174.   })
    175. }
    176. /**
    177.  * 编辑对话框 确定事件
    178.  */
    179. const sureEditorHandler = () => {
    180.   axios.banner_type_update({
    181.     bannerTypeId: editorFormInfo.bannerTypeId,
    182.     bannerTypeName: editorFormInfo.bannerTypeName,
    183.   }).then(res => {
    184.     if (res.data.code == 200) {
    185.       dialogEditorVisible.value = false
    186.       http(searchInfo.value, currentPage.value, defaultPageSize.value)
    187.     } else {
    188.       ElMessage.error(res.data.message)
    189.     }
    190.   })
    191. }
    192. /**删除 */
    193. const handleDelete = (index, row) => {
    194.   ElMessageBox.confirm(
    195.       '确定删除么',
    196.       '删除',
    197.       {
    198.         confirmButtonText: '确定',
    199.         cancelButtonText: '取消',
    200.         type: 'warning',
    201.       }
    202.   ).then(() => {
    203.     //确认删除
    204.     axios.banner_type_delete({
    205.       bannerTypeId: row.bannerTypeId
    206.     }).then(res => {
    207.       if (res.data.code == 200) {
    208.         ElMessage({
    209.           type: 'success',
    210.           message: "删除成功!!!",
    211.         })
    212.         //刷新
    213.         http(searchInfo.value, currentPage.value, defaultPageSize.value)
    214.       } else {
    215.         ElMessage({
    216.           type: 'error',
    217.           message: res.data.message,
    218.         })
    219.       }
    220.     })
    221.   }).catch(error => {
    222.     ElMessage({
    223.       type: 'info',
    224.       message: "取消删除",
    225.     })
    226.   })
    227. }
    228. </script>
    229. <style scoped>
    230. .data-container {
    231.   background: linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
    232.   background: -o-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
    233.   background: -ms-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
    234.   background: -moz-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
    235.   background: -webkit-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
    236.   height: 800px;
    237. }
    238. .data-header {
    239.   padding: 20px;
    240. }
    241. .data-header .input {
    242.   width: 300px;
    243. }
    244. .data-table {
    245.   padding: 20px;
    246. }
    247. .page {
    248.   position: fixed;
    249.   right: 10px;
    250.   bottom: 10px;
    251. }
    252. </style>

    Banner.vue
    1. <template>
    2.   <div class="data-container">
    3.     <!--添加 start-->
    4.     <div class="data-header">
    5.       <el-input class="input" @keyup.enter="searchHandle" v-model="searchInfo" size="large"
    6.                 placeholder="请输入关键字"></el-input>
    7.       <el-button @click="searchHandle" class="button" size="large" type="primary" plain>搜索</el-button>
    8.       <el-button round @click="addHander" size="large" type="primary">
    9.         <el-icon>
    10.           <DocumentAdd/>
    11.         </el-icon>
    12.         <span>新增</span>
    13.       </el-button>
    14.     </div>
    15.     <!--添加 end-->
    16.     <!--表格数据展示 start-->
    17.     <div class="data-table">
    18.       <el-table :data="dataList.list" style="width: 800px;">
    19.         <el-table-column label="图片" prop="bannerUrl" align="center">
    20.           <template #default="scope">
    21.             <img :src="scope.row.bannerUrl" style="height:60px"/>
    22.           </template>
    23.         </el-table-column>
    24.         <el-table-column label="类型" prop="bannerTypeId" align="center">
    25.           <template #default="scope">
    26.                   <span v-for="item in typeList.list" :key="item.bannerTypeId">
    27.                       {{ scope.row.bannerTypeId == item.bannerTypeId ? item.bannerTypeName : '' }}
    28.                   </span>
    29.           </template>
    30.         </el-table-column>
    31.         <el-table-column show-overflow-tooltip label="描述" prop="bannerDesc" align="center"></el-table-column>
    32.         <el-table-column label="操作" align="center" width="220">
    33.           <template #default="scope">
    34.             <el-button size="small" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
    35.             <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
    36.           </template>
    37.         </el-table-column>
    38.       </el-table>
    39.       <!--分页 start-->
    40.       <div class="page">
    41.         <el-pagination background
    42.                        layout="prev,pager,next,jumper"
    43.                        :default-page-size="defaultPageSize"
    44.                        :total="totalData"
    45.                        @current-change="currentChangeHaddler"></el-pagination>
    46.       </div>
    47.       <!--分页 end-->
    48.     </div>
    49.     <!--表格数据展示 end-->
    50.     <!--添加对话框 start-->
    51.     <el-dialog draggable destroy-on-close v-model="dialogAddVisible" title="添加" width="35%" center>
    52.       <el-form inline :model="addFormInfo" label-width="150px">
    53.         <el-form-item label="图片">
    54.           <el-upload list-type="picture-card"
    55.                      :action="uploadStore.fastdfsUploadUrl"
    56.                      :on-success="onHeadImageAddSuccess"
    57.                      :on-remove="onHeadImageAddRemove">
    58.             <el-icon>
    59.             </el-icon>
    60.           </el-upload>
    61.         </el-form-item>
    62.         <el-form-item label="类型">
    63.           <el-select v-model="addFormInfo.bannerTypeId" placeholder="请选择类型" size="large">
    64.             <el-option v-for="item in typeList.list" :key="item.bannerTypeId"
    65.                        :label="item.bannerTypeName"
    66.                        :value="item.bannerTypeId">
    67.             </el-option>
    68.           </el-select>
    69.         </el-form-item>
    70.         <el-form-item label="描述">
    71.           <el-input type="textarea" rows="6" style="width: 200px" v-model="addFormInfo.bannerDesc"></el-input>
    72.         </el-form-item>
    73.       </el-form>
    74.       <template #footer>
    75.             <span class="dialog-footer">
    76.                 <el-button @click="dialogAddVisible = false">取消</el-button>
    77.                 <el-button type="primary" @click="sureHandler">确定</el-button>
    78.             </span>
    79.       </template>
    80.     </el-dialog>
    81.     <!--添加对话框 end-->
    82.     <!--编辑对话框 start-->
    83.     <!--destroy-on-close:每次关闭对话框时直接销毁对话框,没有缓存-->
    84.     <el-dialog
    85.         draggable
    86.         destroy-on-close
    87.         v-model="dialogEditorVisible"
    88.         title="编辑"
    89.         width="35%"
    90.         center>
    91.       <el-form inline :model="editorFormInfo" label-width="150px">
    92.         <el-form-item label="头图">
    93.           <el-upload list-type="picture-card"
    94.                      :action="uploadStore.fastdfsUploadUrl"
    95.                      :on-success="onHeadImageEditSuccess"
    96.           >
    97.             <el-image :src="editorFormInfo.bannerUrl"/>
    98.           </el-upload>
    99.         </el-form-item>
    100.         <el-form-item label="类型">
    101.           <el-select v-model="editorFormInfo.bannerTypeId" placeholder="请选择类型" size="large">
    102.             <el-option v-for="item in typeList.list" :key="item.bannerTypeId"
    103.                        :label="item.bannerTypeName"
    104.                        :value="item.bannerTypeId">
    105.             </el-option>
    106.           </el-select>
    107.         </el-form-item>
    108.         <el-form-item label="描述">
    109.           <el-input type="textarea" rows="6" style="width: 200px" v-model="editorFormInfo.bannerDesc"></el-input>
    110.         </el-form-item>
    111.       </el-form>
    112.       <template #footer>
    113.             <span class="dialog-footer">
    114.                 <el-button @click="dialogEditorVisible = false">取消</el-button>
    115.                 <el-button type="primary" @click="sureEditorHandler">确定</el-button>
    116.             </span>
    117.       </template>
    118.     </el-dialog>
    119.     <!--编辑对话框 end-->
    120.   </div>
    121. </template>
    122. <script setup>
    123. import axios from "../../api/index.js"
    124. import {onMounted, reactive, ref} from "vue";
    125. import {ElMessage} from "element-plus";
    126. import {useUploadStore} from "../../stores/uploadStore.js"
    127. const uploadStore = useUploadStore()
    128. //初始化分页查询数据
    129. const dataList = reactive({
    130.   list: []
    131. })
    132. //初始化类型列表
    133. const typeList = reactive({
    134.   list: []
    135. })
    136. //初始化总条数
    137. const totalData = ref(0)
    138. //当前页
    139. const currentPage = ref(1)
    140. //初始化分页显示条数
    141. const defaultPageSize = ref(10)
    142. //搜索初始化状态
    143. const searchInfo = ref("")
    144. //添加添加对话框控制器
    145. const dialogAddVisible = ref(false)
    146. //初始化添加对话框状态
    147. const addFormInfo = reactive({
    148.   bannerUrl: "",
    149.   bannerDesc: "",
    150.   bannerTypeId: ''
    151. })
    152. //编辑对话框控制器
    153. const dialogEditorVisible = ref(false)
    154. //初始化编辑对话框状态
    155. const editorFormInfo = reactive({
    156.   bannerId: '',
    157.   bannerUrl: "",
    158.   bannerDesc: "",
    159.   bannerTypeId: ''
    160. })
    161. /**
    162.  * 网路请求:分页查询
    163.  *  */
    164. const http = (search, page, size) => {
    165.   axios.banner_search({
    166.     search: search,
    167.     page: page,
    168.     size: size
    169.   }).then(res => {
    170.     if (res.data.code == 200) {
    171.       dataList.list = res.data.data.records
    172.       totalData.value = res.data.data.total
    173.     } else {
    174.       ElMessage.error(res.data.message)
    175.     }
    176.   })
    177. }
    178. onMounted(() => {
    179.   http(searchInfo.value, currentPage.value, defaultPageSize.value)
    180.   axios.banner_type_findAll().then(res => {
    181.     if (res.data.code == 200) {
    182.       typeList.list = res.data.data
    183.     }
    184.   })
    185. })
    186. /**
    187.  * 分页
    188.  */
    189. const currentChangeHaddler = (nowPage) => {
    190.   http(searchInfo.value, nowPage, defaultPageSize.value)
    191.   currentPage.value = nowPage
    192. }
    193. /**
    194.  * 搜索按钮
    195.  */
    196. const searchHandle = () => {
    197.   http(searchInfo.value, currentPage.value, defaultPageSize.value)
    198. }
    199. /**
    200.  * 添加对话框弹出事件
    201.  */
    202. const addHander = () => {
    203.   dialogAddVisible.value = true
    204. }
    205. //上传成功的钩子
    206. const onHeadImageAddSuccess = (response, uploadFile) => {
    207.   addFormInfo.bannerUrl = response.data
    208.   ElMessage.success("上传成功")
    209. }
    210. //移除图片的钩子
    211. const onHeadImageAddRemove = (response, uploadFile) => {
    212.   axios.fastdfs_delete({
    213.     filePath: addFormInfo.bannerUrl
    214.   }).then(res => {
    215.     if (res.data.code == 200) {
    216.       ElMessage.success("移除成功")
    217.     }
    218.   })
    219. }
    220. /**
    221.  * 添加对话框 确定事件
    222.  */
    223. const sureHandler = () => {
    224.   axios.banner_add({
    225.     bannerUrl: addFormInfo.bannerUrl,
    226.     bannerDesc: addFormInfo.bannerDesc,
    227.     bannerTypeId: addFormInfo.bannerTypeId,
    228.   }).then(res => {
    229.     if (res.data.code == 200) {
    230.       dialogAddVisible.value = false
    231.       http(searchInfo.value, currentPage.value, defaultPageSize.value)
    232.     } else {
    233.       ElMessage.error(res.data.message)
    234.     }
    235.   })
    236. }
    237. /**
    238.  * 编辑对话框 弹出事件
    239.  *  */
    240. const handleEdit = (index, row) => {
    241.   dialogEditorVisible.value = true
    242.   axios.banner_findById({
    243.     bannerId: row.bannerId
    244.   }).then(res => {
    245.     if (res.data.code == 200) {
    246.       editorFormInfo.bannerId = res.data.data.bannerId;
    247.       editorFormInfo.bannerUrl = res.data.data.bannerUrl;
    248.       editorFormInfo.bannerDesc = res.data.data.bannerDesc;
    249.       editorFormInfo.bannerTypeId = res.data.data.bannerTypeId;
    250.     } else {
    251.       ElMessage.error(res.data.data.message)
    252.     }
    253.   })
    254. }
    255. /**
    256.  * 编辑对话框 头图修改时图片上传成功事件
    257.  */
    258.     //上传成功的钩子
    259. const onHeadImageEditSuccess = (response, uploadFile) => {
    260.       //删除以前图片
    261.       axios.fastdfs_delete({
    262.         filePath: editorFormInfo.bannerUrl
    263.       }).then(res => {
    264.         if (res.data.code == 200) {
    265.           ElMessage.success("fastdfs移除原图片成功")
    266.         }
    267.       })
    268.       //替换为现在的图片
    269.       editorFormInfo.bannerUrl = response.data
    270.       ElMessage.success("上传成功")
    271.     }
    272. /**
    273.  * 编辑对话框 确定事件
    274.  */
    275. const sureEditorHandler = () => {
    276.   axios.banner_update({
    277.     bannerId: editorFormInfo.bannerId,
    278.     bannerUrl: editorFormInfo.bannerUrl,
    279.     bannerDesc: editorFormInfo.bannerDesc,
    280.     bannerTypeId: editorFormInfo.bannerTypeId,
    281.   }).then(res => {
    282.     if (res.data.code == 200) {
    283.       dialogEditorVisible.value = false
    284.       http(searchInfo.value, currentPage.value, defaultPageSize.value)
    285.     } else {
    286.       ElMessage.error(res.data.message)
    287.     }
    288.   })
    289. }
    290. /**删除 */
    291. const handleDelete = (index, row) => {
    292.   ElMessageBox.confirm(
    293.       '确定删除么',
    294.       '删除',
    295.       {
    296.         confirmButtonText: '确定',
    297.         cancelButtonText: '取消',
    298.         type: 'warning',
    299.       }
    300.   ).then(() => {
    301.     //确认删除
    302.     axios.banner_delete({
    303.       bannerId: row.bannerId
    304.     }).then(res => {
    305.       if (res.data.code == 200) {
    306.         ElMessage({
    307.           type: 'success',
    308.           message: "删除成功!!!",
    309.         })
    310.         //刷新
    311.         http(searchInfo.value, currentPage.value, defaultPageSize.value)
    312.       } else {
    313.         ElMessage({
    314.           type: 'error',
    315.           message: res.data.message,
    316.         })
    317.       }
    318.     })
    319.   }).catch(error => {
    320.     ElMessage({
    321.       type: 'info',
    322.       message: "取消删除",
    323.     })
    324.   })
    325. }
    326. </script>
    327. <style scoped>
    328. .data-container {
    329.   background: linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
    330.   background: -o-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
    331.   background: -ms-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
    332.   background: -moz-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
    333.   background: -webkit-linear-gradient(left, rgb(89, 234, 233) 27%, rgb(131, 231, 218) 44%, rgb(69, 150, 216) 88%);
    334.   height: 800px;
    335. }
    336. .data-header {
    337.   padding: 20px;
    338. }
    339. .data-header .input {
    340.   width: 300px;
    341. }
    342. .data-table {
    343.   padding: 20px;
    344. }
    345. .page {
    346.   position: fixed;
    347.   right: 10px;
    348.   bottom: 10px;
    349. }
    350. </style>

  • 相关阅读:
    一次性分清zip、gzip、bzip2、tar命令
    (二)kafka的事务性和幂等性
    全流量安全分析的重要性
    【树莓派不吃灰】搭建emqx MQTT Broker
    巴特沃斯滤波器的设计
    依赖倒转原则&接口隔离原则&迪米特法则&合成复用原则
    解决 net core 3.x 跨域问题
    第一个vue-cli程序
    关于wParam和lParam
    【C#】vs2022 .net8
  • 原文地址:https://blog.csdn.net/m0_63040701/article/details/133134187