index.vue 5.52 KB
<template>
  <div class="app-container client-container">
    <div class="filter-container client-filter-wrapper">
      <el-input v-model="listQuery.name" :placeholder="$t('resource.placeholderName')" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.typeId" :placeholder="$t('resource.placeholderResourceType')" class="filter-item" style="width: 130px" @change="handleFilter">
        <el-option
          v-for="item in resourceTypeOptions"
          :key="item.unid"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select> 
      <el-select v-model="listQuery.status" :placeholder="$t('resource.placeholderStatus')" class="filter-item" style="width: 130px" @change="handleFilter">
        <el-option
          v-for="item in statusOptions"
          :key="item.key"
          :label="$t('resource.' + item.label)"
          :value="item.key">
        </el-option>
      </el-select> 
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('resource.search') }}
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        {{ $t('resource.add') }}
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="名称" prop="name" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资源类型" prop="typeId" align="center">
        <template slot-scope="{row}">
          <span>{{ resourceTypeFormatter(row.typeId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" prop="description" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status ? $t('resource.online') : $t('resource.offline') }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            {{ $t('resource.edit') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table> 
    <Pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList"></Pagination>
    <actions-dialog :resourceTypeOptions="resourceTypeOptions" ref="actionsDialog"></actions-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { fetchResourceList } from '@/api/resource'
import { fetchResourcesTypeList } from '@/api/resources-type'
import actionsDialog from './actionsDialog'

export default {
  name: 'Client',
  components: { Pagination, actionsDialog },
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'danger',
        1: 'success'
      }
      return statusMap[status]
    },
  },
  data () {
    return {
      statusOptions: [{ label: 'all', key: '' }, { label: 'online', key: 1 }, { label: 'offline', key: 0 }],
      queryResourceTypeOptions: [],
      resourceTypeOptions: [],
      resourceTypeTemp: {},
      listQuery: {
        name: '',
        typeId: '',
        status: '',
        page: 1,
        pageSize: 20
      },
      tableKey: 0,
      list: null,
      listLoading: true,
      total: 0,
      // dialogStatus: '',
      textMap: {
        update: 'edit',
        create: 'add'
      },
    }
  },
  created() {
    this.getResourceType()
    // this.getList()
  },
  methods: {
    async getResourceType() {
      this.resourceTypeTemp = {}
      await fetchResourcesTypeList().then(response => {
        // 未带查询分页条件是应该为 response.data
        this.resourceTypeOptions = response.data
        let result = JSON.parse(JSON.stringify(response.data))
        result.unshift({
          id: '',
          name: this.$t('resource.all')
        })
        this.queryResourceTypeOptions = result
        this.resourceTypeOptions.forEach(item => {
          this.resourceTypeTemp[item.id] = item.name
        })
      })
      await this.getList()
    },
    getList() {
      this.listLoading = true
      let params = {}
      Object.keys(this.listQuery).forEach(key => {
        if(typeof(this.listQuery[key]) === 'number' || this.listQuery[key]) {
          params[key] = this.listQuery[key]
        }
      })
      params['_t'] = Date.parse(new Date()) / 1000
      fetchResourceList(params).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    resourceTypeFormatter(typeId) {
      let typeName = ''
      typeName = this.resourceTypeTemp[typeId]
      return typeName || '--'
    },
    handleCreate() {
      // this.dialogStatus = 'create'
      this.$refs.actionsDialog.dialogInit('create')
    },
    handleUpdate(row) {
      // this.dialogStatus = 'update'
      this.$refs.actionsDialog.dialogInit('update', row)
    },
    handleFilter () {
      this.getList()
    }
  }
}
</script>