Newer
Older
HuangJiPC / src / pages / views / publicService / messagePush.vue
@zhangdeliang zhangdeliang on 21 Jun 15 KB update
<template>
  <!-- 消息推送 -->
  <div class="messagePush">
    <div class="searchBoxs">
      <n-space>
        <n-select v-model:value="searchVal1" placeholder="请选择任务状态" :options="successList" style="width: 180px" clearable> </n-select>
        <n-select v-model:value="searchVal2" placeholder="请选择任务类型" :options="taskList" style="width: 180px" clearable> </n-select>
        <n-button type="primary" @click="handleClick('search')">
          <template #icon>
            <n-icon><Search /></n-icon>
          </template>
          搜索
        </n-button>
        <n-button type="primary" @click="handleClick('add')">
          <template #icon>
            <n-icon><Add /></n-icon>
          </template>
          新增消息推送
        </n-button>
      </n-space>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <n-data-table
        :bordered="false"
        :max-height="700"
        striped
        :columns="columns"
        :data="tableData"
        :loading="tableLoading"
        :remote="true"
        :pagination="pagination"
      >
      </n-data-table>
    </div>
    <!-- 新增修改弹窗 -->
    <n-modal
      :title="modalTitle"
      :mask-closable="false"
      preset="dialog"
      :show-icon="false"
      :style="{ width: '800px' }"
      v-model:show="modalShow"
    >
      <n-form ref="formRef" :label-width="120" :rules="formInfo.rules" :model="formInfo.data" label-placement="left">
        <n-form-item label="消息类型:" path="articleType">
          <n-select v-model:value="formInfo.data.articleType" placeholder="请选择消息类型" :options="infoList" clearable> </n-select>
        </n-form-item>
        <n-form-item label="消息标题:" path="title">
          <n-input v-model:value="formInfo.data.title" placeholder="请输入消息标题" />
        </n-form-item>
        <n-form-item label="消息简介:" path="degist">
          <n-input v-model:value="formInfo.data.degist" placeholder="请输入消息简介" />
        </n-form-item>
        <n-form-item label="消息内容:" path="content">
          <Editor @editorContent="editorContent" ref="richEditor" />
        </n-form-item>
        <n-form-item label="缩略图:" class="uploadWarns" path="mediaUrl">
          <n-upload v-model:file-list="uploadList" accept=".jpg,.png,.jpeg,.svg,.gif" :max="1" list-type="image-card" @change="changeFile">
          </n-upload>
          <span style="margin-top: 10px">提示:格式为.jpg,.png,.jpeg,.svg,.gif,大小小于640KB</span>
        </n-form-item>
        <n-form-item label="任务类型:" path="msgType">
          <n-select v-model:value="formInfo.data.msgType" placeholder="请选择任务类型" :options="taskList" clearable> </n-select>
        </n-form-item>
        <n-form-item label="推送时间:" v-if="formInfo.data.msgType == 1" path="pushTaskTime">
          <n-date-picker v-model:value="formInfo.data.pushTaskTime" style="width: 100%" type="datetime" clearable />
        </n-form-item>
        <n-form-item label="推送对象:" path="pushGroupId">
          <n-select
            v-model:value="formInfo.data.pushGroupId"
            multiple
            filterable
            placeholder="请选择推送对象"
            :options="groupList"
            clearable
          >
          </n-select>
        </n-form-item>
        <n-form-item label="推送对象性别:" path="pushGroupSex">
          <n-select v-model:value="formInfo.data.pushGroupSex" placeholder="请选择推送对象性别" :options="sexList" clearable> </n-select>
        </n-form-item>
      </n-form>
      <template #action>
        <n-space>
          <n-button @click="() => (modalShow = false)">取消</n-button>
          <n-button type="primary" @click="handleClick('submit')">确定 </n-button>
        </n-space>
      </template>
    </n-modal>
  </div>
</template>

<script>
import { toRefs, onMounted, reactive, h, ref, nextTick } from 'vue';
import { Search, Add } from '@vicons/ionicons5';
import { NButton, NImage } from 'naive-ui';
import { messagePushImage, messagePushSearch, messagePushSave, messagePushUpdate, messagePushDelete, userGroupSearch } from '@/services';
import { resetForm, formatDate } from '@/utils/util';
import Editor from './editor.vue';

export default {
  name: 'messagePush',
  components: { Search, Add, NButton, Editor },
  setup() {
    const allData = reactive({
      searchVal1: null,
      searchVal2: null,
      modalTitle: '新增',
      modalShow: false,
      uploadList: [],
      successList: [
        { value: 0, label: '待处理' },
        { value: 1, label: '完成' },
        { value: 2, label: '失败' },
      ],
      taskList: [
        { value: '1', label: '定时任务' },
        { value: '0', label: '即时任务' },
      ],
      sexList: [
        { value: 2, label: '全部' },
        { value: 0, label: '男' },
        { value: 1, label: '女' },
      ],
      infoList: [
        { value: '0', label: '新闻资讯' },
        { value: '1', label: '政策法规' },
        { value: '2', label: '专题知识' },
        { value: '3', label: '历史文化' },
        { value: '4', label: '旅游信息' },
      ],
      groupList: [],
      formInfo: {
        data: {
          articleType: '',
          title: '',
          pushTaskTime: null,
          content: '',
          degist: '',
          msgType: 1,
          pushGroupId: [],
          mediaUrl: '',
          mediaId: '',
          pushGroupSex: '',
          id: '',
        },
        rules: {
          articleType: {
            required: true,
            trigger: ['change'],
            message: '请选择消息类型',
          },
          degist: {
            required: true,
            trigger: ['blur'],
            message: '请输入消息简介',
          },
          content: {
            required: true,
            trigger: ['blur'],
            message: '请输入消息内容',
          },
          title: {
            required: true,
            trigger: ['blur'],
            message: '请输入消息标题',
          },
          mediaUrl: {
            type: 'string',
            required: true,
            trigger: ['blur'],
            message: '请上传图片',
          },
        },
      },
      tableLoading: true,
      tableData: [],
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80',
          render(row, index) {
            return (paginationReactive.page - 1) * paginationReactive.pageSize + index + 1;
          },
        },
        {
          title: '消息标题',
          align: 'center',
          key: 'title',
          ellipsis: {
            tooltip: true,
          },
        },
        {
          title: '消息简介',
          align: 'center',
          key: 'degist',
          ellipsis: {
            tooltip: true,
          },
        },
        {
          title: '消息类型',
          align: 'center',
          key: 'articleType',
          render(row) {
            let arr = allData.infoList.filter((item) => item.value == row.articleType);
            return arr[0].label;
          },
        },
        {
          title: '任务状态',
          align: 'center',
          key: 'pushSucessState',
          render(row) {
            return row.pushSucessState == '0' ? '待处理' : row.pushSucessState == 1 ? '完成' : '失败';
          },
        },
        {
          title: '任务类型',
          align: 'center',
          key: 'msgType',
          render(row) {
            return row.msgType == '1' ? '定时任务' : '即时任务';
          },
        },
        {
          title: '缩略图',
          align: 'center',
          key: 'mediaUrl',
          render(row) {
            return h(NImage, {
              width: 48,
              src: row.mediaUrl,
              style: {
                margin: '5px',
              },
            });
          },
        },
        { title: '推送时间', align: 'center', key: 'pushTaskTime' },
        {
          title: '操作',
          key: 'actions',
          align: 'center',
          render(row) {
            let arrs = [...allData.actionColumn];
            let obj = {
              size: 'small',
              btnType: 'primary',
              type: 'edit',
              default: '修改',
            };
            // 待处理状态才能修改
            if (row.pushSucessState == '0') {
              arrs.unshift(obj);
            }
            const btn = arrs.map((item, index) => {
              return h(
                NButton,
                {
                  text: true,
                  size: item.size,
                  style: {
                    margin: '10px',
                  },
                  type: item.btnType,
                  onClick: () => handleClick(item.type, row),
                },
                { default: () => item.default }
              );
            });
            return btn;
          },
        },
      ],
      actionColumn: [
        {
          size: 'small',
          btnType: 'error',
          type: 'delete',
          default: '删除',
        },
      ],
    });
    //分页
    const paginationReactive = reactive({
      page: 1,
      pageSize: 10,
      showSizePicker: true,
      pageSizes: [10, 20, 50],
      showQuickJumper: true,
      pageCount: 0,
      itemCount: 0,
      prefix: () => {
        return '共 ' + paginationReactive.itemCount + ' 项';
      },
      onChange: (page) => {
        paginationReactive.page = page;
        getTableData();
      },
      onPageSizeChange: (pageSize) => {
        paginationReactive.pageSize = pageSize;
        paginationReactive.page = 1;
        getTableData();
      },
    });
    const getTableData = async () => {
      allData.tableLoading = true;
      let pramas = {
        current: paginationReactive.page,
        size: paginationReactive.pageSize,
        pushSucessState: allData.searchVal1,
        msgType: allData.searchVal2,
      };
      let res = await messagePushSearch(pramas);
      if (res && res.code == 200) {
        allData.tableData = res.data.records;
        paginationReactive.pageCount = res.data.pages;
        paginationReactive.itemCount = res.data.total;
      }
      allData.tableLoading = false;
    };
    const formRef = ref(null);
    const richEditor = ref(null);
    // 点击事件
    const handleClick = async (type, row) => {
      switch (type) {
        case 'search':
          paginationReactive.page = 1;
          getTableData();
          break;
        case 'add':
          allData.modalTitle = '新增';
          resetForm(allData.formInfo.data);
          allData.modalShow = true;
          allData.formInfo.data.msgType = '1';
          allData.formInfo.data.pushGroupId = [];
          allData.formInfo.data.pushTaskTime = Date.now() + 5 * 60 * 1000;
          allData.formInfo.data.pushGroupSex = 2;
          allData.uploadList = [];
          break;
        case 'edit':
          allData.modalTitle = '修改';
          allData.formInfo.data = { ...row };
          allData.modalShow = true;
          allData.formInfo.data.articleType = row.articleType != null ? String(row.articleType) : '';
          allData.formInfo.data.msgType = row.msgType != null ? String(row.msgType) : '';
          allData.formInfo.data.pushTaskTime = row.pushTaskTime ? Number(new Date(String(row.pushTaskTime)).getTime()) : null;
          // 富文本默认值
          nextTick(() => {
            richEditor.value.content = row.content;
          });
          // 图片文件默认填充
          allData.uploadList = [];
          let param = {
            id: row.id,
            name: '图片',
            status: 'finished',
            url: row.mediaUrl,
          };
          allData.uploadList.push(param);
          break;
        case 'submit':
          formRef.value.validate((errors) => {
            if (!errors) {
              submitData();
            } else {
              $message.error('验证失败,请检查必填项');
            }
          });
          break;
        case 'delete':
          $dialog.info({
            title: '提示',
            content: `确定删除该数据吗?`,
            positiveText: '确定',
            negativeText: '取消',
            onPositiveClick: () => {
              let ids = [row.id];
              dataDel(ids);
            },
          });
          break;
      }
    };
    // 删除数据
    async function dataDel(ids) {
      let res = await messagePushDelete({ ids: ids });
      if (res && res.code === 200) {
        $message.success('删除成功');
        getTableData();
      }
    }
    // 提交数据
    async function submitData() {
      let params = { ...allData.formInfo.data };
      if (allData.formInfo.data.msgType == 1) {
        params.pushTaskTime = formatDate(allData.formInfo.data.pushTaskTime);
      } else {
        params.pushTaskTime = null;
      }
      if (allData.modalTitle == '新增') {
        if (params.id) delete params.id;
        let res = await messagePushSave(params);
        if (res && res.code == 200) {
          $message.success('操作成功');
          getTableData();
          allData.modalShow = false;
        }
      } else {
        let res = await messagePushUpdate(params);
        if (res && res.code == 200) {
          $message.success('操作成功');
          getTableData();
          allData.modalShow = false;
        } else {
          $message.error(res.msg);
        }
      }
    }
    // 文件上传和删除
    const changeFile = async (file) => {
      console.log(file, '文件上传');
      $loadingBar.start();
      if (file.event) {
        // 文件上传大小判断
        let size = file.file.file.size / 1024;
        if (size > 640) {
          allData.formInfo.data.mediaId = '';
          allData.formInfo.data.mediaUrl = '';
          $message.error('图片大小大于640KB,请删除后重新上传');
          setTimeout(() => {
            allData.uploadList = [
              {
                id: file.file.id,
                name: file.file.name,
                status: 'error',
              },
            ];
          });
          return false;
        }
        // 文件上传
        let formdata = new FormData();
        formdata.append('file', file.file.file);
        let config = {
          headers: { 'Content-Type': 'multipart/form-data' },
        };
        let res = await messagePushImage(formdata, config);
        if (res && res.code === 200) {
          allData.formInfo.data.mediaId = res.data.media_id;
          allData.formInfo.data.mediaUrl = res.data.url;
        } else {
          allData.uploadList = [];
          let param = {
            id: file.file.id,
            name: file.file.name,
            status: 'error',
          };
          allData.uploadList.push(param);
        }
      } else {
        // 文件删除
        allData.formInfo.data.mediaId = '';
        allData.formInfo.data.mediaUrl = '';
      }
      $loadingBar.finish();
    };
    // 获取组信息
    async function getGroup() {
      allData.groupList = [];
      let params = { current: 0, size: 100 };
      let res = await userGroupSearch(params);
      if (res && res.code == 200) {
        let datas = res.data.records;
        if (datas.length == 0) return;
        datas.map((item) => {
          allData.groupList.push({
            value: item.id,
            label: item.groupName,
          });
        });
      }
    }
    // 获取从富文本传过来的值
    const editorContent = (obj) => {
      allData.formInfo.data.content = obj;
    };
    onMounted(() => {
      getGroup();
      getTableData();
    });
    return {
      ...toRefs(allData),
      pagination: paginationReactive,
      handleClick,
      getTableData,
      formRef,
      editorContent,
      richEditor,
      changeFile,
    };
  },
};
</script>

<style lang="less" scoped>
.messagePush {
  width: 100%;
  .searchBoxs {
    margin: 10px;
  }
}
</style>