index.vue 6.83 KB
<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-container style="height: 100%;">
        <el-header style="height: max-content">
          <!-- 列表操作 -->
          <el-row :gutter="10" class="mb8" justify="end">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="Plus"
                @click="btn_add()"
              >{{ $t('advertisement.dialogTitleAdd') }}</el-button>
            </el-col>
          </el-row>
        </el-header>
        <el-main id="max-height">
          <!-- 列表内容 -->
          <el-table
            v-loading="status.loading"
            :data="list"
            :max-height="windowSize.height - 70"
            :header-cell-style="{textAlign:'center'}"
            :cell-style="{textAlign:'center'}"
            border
          >
            <el-table-column :label="$t('advertisement.tableAdName')" prop="name" />
            <el-table-column :label="$t('advertisement.tableSortName')" prop="sort" />
            <el-table-column :label="$t('advertisement.tablePhotoName')" >
              <template #default="{ row }">
                <el-image style="width: 100px; height: 100px" :src="row.ossUrl" />
              </template>
            </el-table-column>
            <el-table-column :label="$t('advertisement.tableStateName')" prop="status">
              <template #default="{ row }">
                <dict-tag
                  :options="advert_state"
                  :value="row.status"
                ></dict-tag>
              </template>
            </el-table-column>
            <el-table-column :label="$t('advertisement.tableControlsName')" class-name="small-padding fixed-width">
              <template #default="{ row }">
                <el-button
                  link
                  type="info"
                  @click="handleEdit(row)"
                >{{ $t('advertisement.controlsEditName') }}</el-button>
                <el-button
                  link
                  v-if="row.status == 1"
                  type="info"
                  @click="handleChangeStatus(row, 2)"
                >{{ $t('advertisement.controlsDelistName') }}</el-button>
                <el-button
                  link
                  v-if="row.status == 0 || row.status == 2"
                  type="info"
                  @click="handleChangeStatus(row, 1)"
                >{{ $t('advertisement.controlsReleaseName') }}</el-button>
                <el-button
                  link
                  type="info"
                  @click="handleDel(row)"
                >{{ $t('advertisement.controlsDelName') }}</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-main>
      </el-container>
      <el-dialog :title="showForm.title" v-model="showForm.open" width="500px" append-to-body @close="closeDialog">
        <el-form ref="formRefs" :model="showForm.form" :rules="rules">
          <el-form-item :label="$t('advertisement.tableAdName')" label-width="80">
            <el-input v-model="showForm.form.name" placeholder="请输入参数键名" />
          </el-form-item>
          <el-form-item :label="$t('advertisement.tablePhotoName')" label-width="80">
            <imageUpload v-model="showForm.form.ossId" :limit='1' :fileType='["png", "jpg", "jpeg", "ico"]'/>
          </el-form-item>
          <el-form-item :label="$t('advertisement.tableSortName')" label-width="80">
            <el-input v-model="showForm.form.sort" placeholder="请输入参数键名" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <template v-if="!showForm.form?.id">
              <el-button type="info" @click="submitForm(1)">{{ $t('advertisement.dialogSubmit') }}</el-button>
              <el-button type="info" @click="submitForm(0)">{{ $t('advertisement.dialogSave') }}</el-button>
            </template>
            <el-button v-else type="info" @click="submitForm(null)">{{ $t('advertisement.dialogSave') }}</el-button>
            <el-button @click="showForm.open = false">{{ $t('advertisement.dialogExit') }}</el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>

</template>
<script setup name="ruleManager">
import { useTableHeight } from '@/hooks/useTableHeight'
// 导入api接口
import { getAdvertList, addAdvert, editAdvert, editAdvertStatus, delAdvert } from '@/api/legao/advertisement'
import { reactive, toRefs } from 'vue';
const { windowSize } = useTableHeight('max-height')

// 导入字典
const { proxy } = getCurrentInstance();
const { sys_yes_no, advert_state } = proxy.useDict("sys_yes_no", "advert_state");

const data = reactive({
  status: {
    loading: false,
    unselected: true,
    showForm: false,
    showCodePart: false
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  total: 0,
  list: [],
  showForm: {
    title: '',
    open: false,
    form: {

    }
  },
  rules: [
    {}
  ]
})

const { status, queryParams, total, list, showForm, rules } = toRefs(data)


//#region 初始化
btn_search()
//#endregion


// 获取列表数据
function getList () {
  status.loading = true

  getAdvertList(queryParams.value)
    .then((res) => {
      list.value = res.rows
      total.value = res.total
    })
    .finally(() => {
      status.loading = false
    })
}

// 搜索
function btn_search () {
  queryParams.value.pageNum = 1
  getList()
}

// 单条编辑
function handleEdit (row) {
  showForm.value.title = proxy.$t('advertisement.dialogTitleEdit')
  showForm.value = {
    ...showForm.value,
    open: true,
    form: {...row}
  }
}

function btn_add () {
  showForm.value.title = proxy.$t('advertisement.dialogTitleAdd')
  showForm.value = {
    ...showForm.value,
    open: true
  }
}

function handleDel(row) {
  proxy.$modal.confirm(`是否删除广告名称:${row.name}的数据`).then(() => {
    return delAdvert(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess(res.msg);
  }).finally(() => {

  });
}

function handleChangeStatus(row, status) {
  editAdvertStatus({
    ...row,
    status
  }).then(res => {
    proxy.$modal.msgSuccess(res.msg);
    getList()
    showForm.value.open = false
  })
}

function submitForm(type) {
  const data = {
    ...showForm.value.form,
  }
  type && (data['status'] = type);
  const fun = showForm.value.form?.id ? editAdvert : addAdvert
  fun(data).then(res => {
    // console.log(res);
    proxy.$modal.msgSuccess(res.msg);
    getList()
    showForm.value.open = false
  })
}

function closeDialog() {
  showForm.value = {
    ...showForm.value,
    form: {}
  }
}
</script>