明梦爽
2022-03-03 25ab6ddae2902b6735d7641b1371b25587a3589c
src/components/Administrator/Main.vue
@@ -1,37 +1,44 @@
<template>
  <div>
    <!-- 级联选择器 -->
     <div class="block">
      <el-cascader
        v-model="value"
        :options="options"
        clearable
        filterable
        @change="handleChange">
      </el-cascader>
    </div>
    <el-row class="block">
      <el-col :span="4">
        <el-cascader
          v-model="value"
          :options="options"
          clearable
          filterable
          :getCheckedNodes="true"
          @change="handleChange">
        </el-cascader>
      </el-col>
    </el-row>
    <div>
      <el-card class="box-card">
        <el-row :gutter="40">
          <el-col :span="4">
            <!-- 添加新闻按钮 -->
            <el-button el-button class="add" type="success" icon="el-icon-plus" @click="goEdit()">点击在该列表下添加一条新闻</el-button>
            <el-button el-button class="add" type="success" icon="el-icon-plus" @click="goEdit(newsCategoryId)">点击在该列表下添加一条新闻</el-button>
          </el-col>
          <el-col :span="10">
            <!-- 新闻搜索 -->
            <el-input clearable @clear="qingKong()" placeholder="请输入所要查询的新闻标题" v-model="fuzzyForm.fuzzytitle" class="input-with-select">
              <el-button class="btn" slot="append" icon="el-icon-search" @click="fuzzyList(fuzzyForm.fuzzytitle)"></el-button>
            <el-input clearable placeholder="请输入所要查询的新闻标题" v-model="search" class="input-with-select">
            </el-input>
          </el-col>
        </el-row>
        <!-- 新闻表格区域 -->
        <el-table :data="tableData" border style="width: 100%" :header-cell-style="{textAlign: 'center'}" :cell-style="{ textAlign: 'center' }"
        <el-table
          :data="tableData.filter(data => !search || data.title.toLowerCase().includes(search.toLowerCase()))"
          border
          style="width: 100%"
          :header-cell-style="{textAlign: 'center'}"
          :cell-style="{ textAlign: 'center' }"
        >
          <!-- :header-cell-style="{textAlign: 'center'}"设置头部居中: -->
          <!-- :cell-style="{ textAlign: 'center' }"设置整个表格内容水平居中: -->
          <!-- 问题:怎么获取数组中每个对象的key值(每条新闻的id)?
               解决:通过作用域插槽 slot-scope 获取每个对象 -->
          <el-table-column type="index"></el-table-column>
          <el-table-column type="index" label="序号" width="50">
            <template slot-scope="scope">
              <span>{{ (currentPage-1)*pageSize+scope.$index+1 }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="releaseTime" label="日期" width="" >
          </el-table-column>
          <el-table-column prop="title" label="新闻标题" width="">
@@ -50,29 +57,18 @@
            
          </el-table-column>
        </el-table>
        <div class="fenye">
        <!-- 分页 -->
        <div>
          <!-- 获取新闻列表分页 -->
          <div class="get">
          <div>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[3,6,12, 18,]"
              :page-size="100"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
          </div>
          <!-- 模糊新闻列表分页 -->
          <div class="fuzzy">
            <el-pagination
              @size-change="handleSizeChangefuzzy"
              @current-change="handleCurrentChangefuzzy"
              :current-page="fuzzyForm.fuzzycurrent"
              :page-sizes="[6,12, 18,]"
              :page-size="100"
              layout="total, sizes, prev, pager, next, jumper"
              :total="fuzzyForm.fuzzytotal">
              :total="total">
            </el-pagination>
          </div>
        </div>
@@ -92,7 +88,13 @@
            <el-input v-model="editForm.title"></el-input>
          </el-form-item>
          <el-form-item label="发布日期:">
            <el-input v-model="editForm.releaseTime"></el-input>
            <el-date-picker
              v-model="editForm.releaseTime"
              type="date"
              placeholder="请选择发布日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
@@ -106,18 +108,25 @@
<script>
import { getNewsList,getnew,update,deleteNew,fuzzy } from '../../api/api'
import axios from 'axios'
export default {
  data() {
    return {
      search:'',
      getShow:true, //获取分页的显示(布尔值)
      gettable:true,  //获取表格的显示
      visible: false,
      editDialogVisible:false,  //控制修改对话框的布尔值
      AdDialogVisible:false,  //超管登录对话框
      Ad:{ username:'',password:'' }, //超管对象
      editForm:{},  //查询到的新闻对象,目前仅供修改使用
      newsCategoryId:1, //先存一个小标题id
      tableData:[],   //新闻列表对象
      pageSize:3,     //每页条数
      fuzzytableData:[],  //模糊查询列表对象
      pageSize:6,     //每页条数
      currentPage:1,  //当前页
      total:0,        //新闻总条数
      fuzzyShow:false,  //模糊查询分页的显示(布尔值)
      fuzzytable:false, //模糊查询表格的显示
      fuzzyForm:{ fuzzytitle:'',fuzzytotal:0,fuzzycurrent:1,fuzzysize:6 },  //模糊查询列表对象
      value: [],
      options: [{
@@ -134,6 +143,10 @@
        {
          value:44,
          label: '学院动态',
        },
        {
          value:45,
          label: '轮播图',
        }],
      },{
          value: 22,
@@ -216,8 +229,9 @@
    },
    //级联选择器的方法
    handleChange(value) {
      console.log(value);
      console.log(value[1]);
      // console.log(value);
      // console.log(value[1]);
      this.currentPage = 1;
      this.newsCategoryId = value[1]; //将得到的小标题id存放起来
      this.query(this.newsCategoryId);
    },
@@ -228,7 +242,7 @@
        size:this.pageSize
      };
      getNewsList(data).then(res=>{
        console.log(res);
        // console.log(res);
        if (res.code !== 200) {
          return this.$message.error('获取新闻列表失败')
        }else{
@@ -242,7 +256,7 @@
    //展示修改对话框
    showEditDialog(id){
      getnew(id).then(res => {
        console.log(res);
        // console.log(res);
        if (res.code == 200) {
          this.editForm = res.data
        }     
@@ -251,44 +265,41 @@
    },
    //确定修改表单提交,验证发起请求
    editNew(){
      // console.log(typeof(this.editForm.releaseTime));
      // console.log(this.editForm.releaseTime);
      let arr = this.editForm.releaseTime.split('')
      if(arr[4] !== '/' || arr[7] !== '/'){
        return this.$message.error('请按照“XXXX/XX/XX”的格式填写日期')
      }else{
        const data = {
          id: this.editForm.id,
          releaseTime: this.editForm.releaseTime,
          title:this.editForm.title
        }
        update(data).then(res=>{
          console.log('dsadasas'+res);
          if (res.code == 200) {
            this.editDialogVisible =false;
            this.$message.success('修改新闻成功!')
            this.query(this.newsCategoryId);
          }
        })
      const data = {
        id: this.editForm.id,
        releaseTime: this.editForm.releaseTime,
        title:this.editForm.title
      }
      update(data).then(res=>{
        console.log('dsadasas',res);
        if (res.code == 200) {
          this.editDialogVisible =false;
          this.$message.success('修改新闻成功!')
          this.query(this.newsCategoryId);
        }
      })
    },
    //模糊查询
    fuzzyList(title){
      console.log(title);
      // console.log(title);
      if(title == ''){
        return  this.$message.error('请先输入查询新闻标题!')
      }
      this.fuzzyShow = true //模糊分页显示
      this.getShow = false  //获取分页隐藏
      this.gettable = false
      this.fuzzytable = true
      const data = {
        current: this.fuzzyForm.fuzzycurrent,
        size: this.fuzzyForm.fuzzysize,
        title:title
      }
      fuzzy(data).then(res => {
        console.log(res);
        // console.log(res);
        if (res.code !== 200) {
          return this.$message.error('查询新闻列表失败')
        }else{
          this.tableData = res.data.records
          this.fuzzytableData = res.data.records
          this.fuzzyForm.fuzzytotal = res.data.total
        }
      }).catch(err => {
@@ -298,11 +309,16 @@
    //清空查询列表内容
    qingKong(){
      this.tableData = []
      this.fuzzytableData = []
      this.fuzzyForm.fuzzytotal = 0
      this.fuzzyShow = false
      this.getShow = true
      this.fuzzytable = false
      this.gettable = true
    },
    //根据id删除新闻
    async deleteNews(id){
      console.log(id);
      // console.log(id);
      const res = await this.$confirm('此操作将永久删除该条新闻, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
@@ -336,6 +352,10 @@
    look(id){
      this.$router.push({path:'news',query:{id:id}})
    },
    //超管登录对话框
    showAdDialog(){
      this.AdDialogVisible = true
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
@@ -355,18 +375,38 @@
      this.fuzzyList(this.fuzzyForm.fuzzytitle) //模糊查询
    },
    //去往添加编辑页面
    goEdit(){
      if (this.newsCategoryId==27||this.newsCategoryId==28||this.newsCategoryId==29||
      this.newsCategoryId==30||this.newsCategoryId==31) {
        this.$message.error('该新闻标题下不能添加新闻')
      }else if(this.newsCategoryId == 35||this.newsCategoryId==36||this.newsCategoryId==37||
    goEdit(id){
      let title = '';
      console.log(this.value);
      if (this.value.length = 2) {
        let t = this.value[0];
        let i = this.value[1];
        this.options.forEach( item => {
          if(item.value == t){
            title += item.label+'/'
            item.children.forEach( e => {
              if(e.value == i) title += e.label;
            })
          }
        })
      }
      if (this.newsCategoryId==27&&this.total==1||this.newsCategoryId==28&&this.total==1||this.newsCategoryId==29&&this.total==1||
      this.newsCategoryId==30&&this.total==1||this.newsCategoryId==31&&this.total==1) {
        this.$message.error('该新闻标题下只能存在一篇新闻')
      }else if(this.newsCategoryId==27||this.newsCategoryId==28||this.newsCategoryId==29||this.newsCategoryId==30||
      this.newsCategoryId==31||this.newsCategoryId == 35||this.newsCategoryId==36||this.newsCategoryId==37||
      this.newsCategoryId==38||this.newsCategoryId==39||this.newsCategoryId==40||
      this.newsCategoryId==41||this.newsCategoryId==42||this.newsCategoryId==43||
      this.newsCategoryId==44){
        this.$router.push('/administrator/edit')
        this.$router.push({path:'/administrator/edit',query:{id:id,title:title}})
        console.log(this.newsCategoryId);
      }else if(this.newsCategoryId==45){
        this.$router.push({path:'/administrator/editlunbo',query:{id:id,title:title}})
        // console.log(this.newsCategoryId);
      }else{
        this.$message.error('请先选择新闻标题')
      }
      }
    }
  },
}
@@ -376,11 +416,10 @@
.add{
  margin-bottom: 10px !important;
}
.block{
  margin-bottom: 10px;
}
.el-pagination{
  margin-top: 10px;
}
.fenye{
  display: flex;
  justify-content: space-between;
}
</style>