Newer
Older
Nanping_sponge / src / views / system / newsManage / index.vue
@liyingjing liyingjing on 25 Oct 2023 12 KB 南平登录模块
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="" prop="newsTitle">
        <el-input
          v-model="queryParams.newsTitle"
          placeholder="请输入新闻标题"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="NewsList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="70" />
      <el-table-column
        label="新闻标题"
        align="center"
        prop="newsTitle"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="新闻类型"
        align="center"
        prop="newsType"
        width="100"
      >
        <template #default="scope">
          <dict-tag :options="news_type" :value="scope.row.newsType" />
        </template>
      </el-table-column>
      <el-table-column
        label="发布时间"
        align="center"
        prop="publishTime"
        width="100"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" align="center" prop="isTop" width="100">
        <template #default="scope">
          <dict-tag :options="is_state" :value="scope.row.isTop" />
        </template>
      </el-table-column>
      <el-table-column
        label="浏览次数"
        align="center"
        prop="browseNum"
        width="100"
      >
      </el-table-column>
      <el-table-column
        label="是否显示"
        align="center"
        prop="isShow"
        width="100"
      >
        <!-- <template #default="scope">
          <dict-tag :options="is_state" :value="scope.row.isShow" />
        </template> -->
        <template #default="scope">
          <el-switch
            v-model="scope.row.isShow"
            :active-value="1"
            :inactive-value="0"
            @click="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="创建人"
        align="center"
        prop="createBy"
        width="100"
      />
      <el-table-column
        label="发布时间"
        align="center"
        prop="publishTime"
        width="200"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="220"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleDetail(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改新闻对话框 -->
    <el-dialog :title="title" v-model="open" width="900px" append-to-body>
      <el-form ref="NewsRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="新闻标题" prop="newsTitle">
              <el-input v-model="form.newsTitle" placeholder="请输入新闻标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否置顶">
              <el-radio-group v-model="form.isTop">
                <el-radio
                  v-for="dict in is_state"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否显示">
              <el-radio-group v-model="form.isShow">
                <el-radio
                  v-for="dict in is_state"
                  :key="dict.value"
                  :label="dict.value"
                  >{{ dict.label }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发布时间" prop="publishTime">
              <el-date-picker
                v-model="form.publishTime"
                type="datetime"
                placeholder="请选择"
                value-format="YYYY-MM-DD HH:mm:ss"
            /></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="新闻类型" prop="newsType">
              <el-select v-model="form.newsType" placeholder="请选择">
                <el-option
                  v-for="dict in news_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="首页图片" prop="picture">
              <ImageUpload
                :isBackName="true"
                v-model:modelValue="form.picture"
              ></ImageUpload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容" prop="newsContent">
              <WangEditor v-model:modelValue="form.newsContent" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog
      title="新闻详情"
      v-model="detailOpen"
      width="900px"
      append-to-body
      class="dialog-detail-box scrollbar"
    >
      <newsDetail
        :detailOpen="detailOpen"
        :dialogFormDetail="dialogFormDetail"
      ></newsDetail>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="newsManage">
import {
  pageNews,
  getNews,
  delNews,
  addNews,
  updateNews,
} from "@/api/system/newsManage";
import { fileList } from "@/api/system/file.js";
import WangEditor from "@/components/WangEditor";
import newsDetail from "@/views/system/newsManage/newsDetail.vue";
const { proxy } = getCurrentInstance();
const is_state = proxy.fixDict["isState"]; //是否字典
const { news_type } = proxy.useDict("news_type");
console.log("news_type", news_type);
const NewsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const detailOpen = ref(false);
const data = reactive({
  form: {
    isTop: "0",
    isShow: "1",
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    newsTitle: undefined,
  },
  rules: {
    newsTitle: [
      { required: true, message: "新闻标题不能为空", trigger: "blur" },
    ],
    newsType: [
      { required: true, message: "新闻类型不能为空", trigger: "change" },
    ],
    publishTime: [
      { required: true, message: "发布时间不能为空", trigger: "change" },
    ],
  },
  dialogFormDetail: {}, //详情弹框
});

const { queryParams, form, rules, dialogFormDetail } = toRefs(data);

/** 查询新闻列表 */
function getList() {
  loading.value = true;
  pageNews(queryParams.value).then((response) => {
    NewsList.value = response.data;
    total.value = response.total;
    loading.value = false;
  });
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    newsTitle: undefined,
    newsType: undefined,
    newsContent: undefined,
    publishTime: "",
    isTop: "0",
    isShow: "1",
    picture: [],
  };
  proxy.resetForm("NewsRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加新闻";
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getNews(id).then((response) => {
    response.data.isTop = response.data.isTop.toString();
    response.data.isShow = response.data.isShow.toString();
    form.value = response.data;
    open.value = true;
    title.value = "修改新闻";
    getFileList(row);
  });
}

//获取图片
function getFileList(row) {
  fileList({ refId: row.id, refType: "news" }).then((res) => {
    form.value.picture = res.data;
    dialogFormDetail.value.picture = res.data;
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["NewsRef"].validate((valid) => {
    if (valid) {
      form.value.fileSaveRequestList = [];
      if (form.value.picture) {
        form.value.fileSaveRequestList = form.value.picture.map((item) => {
          return {
            name: item.name,
            refField: "home.picture",
            refType: "news",
            url: item.url,
          };
        });
      }

      if (form.value.id != undefined) {
        updateNews(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNews(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除新闻编号为"' + id + '"的数据项?')
    .then(function () {
      return delNews(id);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

//详情操作
function handleDetail(row) {
  getNews(row.id).then((response) => {
    response.data.isTop = response.data.isTop.toString();
    response.data.isShow = response.data.isShow.toString();
    dialogFormDetail.value = response.data;
    detailOpen.value = true;
    getFileList(row);
  });
}

//隐藏操作
function handleStatusChange(row) {
  let text = row.isShow == 0 ? "隐藏" : "显示";
  form.value = row;
  form.value.isShow = 0;
  fileList({ refId: row.id, refType: "news" }).then((res) => {
    form.value.picture = res.data;
    proxy.$modal
      .confirm(`是否确认${text}新闻?`)
      .then(function () {
        form.value.isShow = form.value.isShow == 0 ? 1 : 0;
        form.value.fileSaveRequestList = [];
        if (form.value.picture) {
          form.value.fileSaveRequestList = form.value.picture.map((item) => {
            return {
              name: item.name,
              refField: "home.picture",
              refType: "news",
              url: item.url,
            };
          });
        }
        return updateNews(form.value);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess(`${text}成功`);
      })
      .catch(() => {
        getList();
      });
  });
}
getList();
</script>