Newer
Older
smartwell_front / src / views / home / device / product / index.vue
<!--
  Description: 设备管理-产品管理
  Author: 李亚光
  Date: 2024-07-15
 -->
<script lang="ts" setup name="ProductManage">
import { ElMessage, ElMessageBox } from 'element-plus'
import editDialog from './components/editDialog.vue'
import { getManufacturerListPage, getProductListPage, removeProduct } from '@/api/home/device/product'
import { getDeviceTypeListPage } from '@/api/home/device/type'
import { getDictByCode } from '@/api/system/dict'

// 表格数据
const list = ref([
])
const total = ref(0)
// 初始展示列
const columns = ref<any>([
  { text: '产品名称', value: 'productName', align: 'center' },
  { text: '设备类型', value: 'deviceTypeName', align: 'center' },
  { text: '设备型号', value: 'deviceModel', align: 'center' },
  { text: '厂商', value: 'manufacturerName', align: 'center' },
  { text: '通讯协议', value: 'protocolName', align: 'center' },
  { text: '接入方式', value: 'accessMethodName', align: 'center' },
  { text: '创建时间', value: 'ts', align: 'center' },
])
// 最终展示列
const columnsConfig = ref([])
// 修改列
const editColumns = (data: any) => {
  columnsConfig.value = data
}
const loadingTable = ref(true)
//  查询条件
const listQuery = ref({
  limit: 20,
  offset: 1,
  accessMethod: '', // 接入方式
  devTypeId: '', // 设备类型id
  manufacturerId: '', // 厂商id
  productName: '', // 产品名称
})
// 查询数据
const fetchData = () => {
  loadingTable.value = true
  getProductListPage(listQuery.value).then((res) => {
    list.value = res.data.rows
    total.value = res.data.total
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}
// 重置查询条件f
const reset = () => {
  listQuery.value = {
    limit: 20,
    offset: 1,
    accessMethod: '', // 接入方式
    devTypeId: '', // 设备类型id
    manufacturerId: '', // 厂商id
    productName: '', // 产品名称
  }
  fetchData()
}
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  fetchData()
}
// 新建编辑操作
const editRef = ref()
const editRow = (type: string, row: any) => {
  editRef.value.initDialog(type, row)
}
// 删除
const removeRow = (row: any) => {
  ElMessageBox.confirm(
    `确定要删除${row.productName}吗?`,
    '确认操作',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    removeProduct([row.id]).then((response) => {
      if (response.code === 200) {
        ElMessage({
          message: '删除成功',
          type: 'success',
        })
        fetchData()
      }
    })
  })
}
const deviceTypeList = ref<{ id: string; name: string; value: string }[]>([]) // 设备类型
const accessMethodList = ref<{ id: string; name: string; value: string }[]>([]) // 接入方式
const manufacturerList = ref<{ id: string; name: string; value: string }[]>([]) // 厂商列表

// 获取字典
const fetchDict = async () => {
  // 设备类型
  const res = await getDeviceTypeListPage({ offset: 1, limit: 99999 })
  deviceTypeList.value = res.data.rows.map((item: any) => ({
    name: item.typeName || '',
    id: item.id,
    value: item.id,
  }))
  // 接入方式
  const res1 = await getDictByCode('accessMethod')
  accessMethodList.value = res1.data
  // 厂商列表
  const res2 = await getManufacturerListPage()
  manufacturerList.value = res2.data.map((item: any) => ({
    name: item.name || '',
    id: item.id,
    value: item.id,
  }))
}
onMounted(async () => {
  await fetchDict()
  fetchData()
})
</script>

<template>
  <!-- 布局 -->
  <app-container>
    <!-- 新建编辑弹窗 -->
    <edit-dialog ref="editRef" @refresh="fetchData" />
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="fetchData" @clear="reset">
      <search-item>
        <el-input v-model="listQuery.productName" placeholder="产品名称" clearable />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.devTypeId" placeholder="设备类型" clearable filterable>
          <el-option v-for="item in deviceTypeList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.manufacturerId" placeholder="厂商" clearable filterable>
          <el-option v-for="item in manufacturerList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.accessMethod" placeholder="接入方式" clearable filterable>
          <el-option v-for="item in accessMethodList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
    </search-area>
    <!-- 表头标题 -->
    <table-container
      :is-config="true" config-title="product-manage" :columns="columns" :config-columns="columnsConfig"
      :edit="editColumns"
    >
      <template #btns-right>
        <!-- 操作 -->
        <div>
          <el-button type="primary" @click="editRow('add', {})">
            新建
          </el-button>
        </div>
      </template>
      <!-- 查询结果Table显示 -->
      <normal-table
        :data="list" :total="total" :columns="columnsConfig" :query="listQuery" :list-loading="loadingTable"
        @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="操作" align="center" width="120">
            <template #default="scope">
              <el-button type="primary" link size="small" @click="editRow('edit', scope.row)">
                编辑
              </el-button>
              <el-button type="danger" link size="small" @click="removeRow(scope.row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>