index.vue 7.64 KB
<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getTableList" :init-param="initParam" :data-callback="dataCallback">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button type="primary" :icon="CirclePlus" @click="openDailog('新增设备')">新增设备</el-button>
        <!-- <el-button type="primary" @click="openDailog('新增设备')">导入</el-button>
        <el-button type="primary" @click="openDailog('新增设备')">导出</el-button> -->
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link @click="openReport(scope.row)">通信</el-button>
        <el-button type="primary" link @click="openLog(scope.row)">日志文件</el-button>
        <el-button type="primary" link @click="openConfig('更新配置', scope.row)">更新配置</el-button>
        <!-- <el-button type="primary" link @click="openDailog('查看', scope.row)">查看</el-button> -->
        <el-button type="primary" link @click="openDailog('更新设备信息', scope.row)">更新设备信息</el-button>
        <!-- <el-button type="primary" link @click="handleDelete(scope.row)">删除</el-button> -->
        <!-- <el-button type="primary" link @click="sendCmd(scope.row)">上报信息</el-button> -->
      </template>
    </ProTable>
    <Dialog ref="dialogRef" />
    <Config ref="configRef" />
    <Report ref="reportRef" :show-report="showReport" v-if="showReport" @close="showReport = false" :device-id="deviceId" />
    <LogFile ref="logFileRef" :show-log="showLog" v-if="showLog" @close="showLog = false" :device-id="deviceId" />
  </div>
</template>

<script setup lang="tsx" name="logProTable">
import { ref, reactive } from "vue";
import { Device } from "@/api/interface";
// import { useHandleData } from "@/hooks/useHandleData";
import ProTable from "@/components/ProTable/index.vue";
import Dialog from "./components/Dailog.vue";
import Config from "./components/Config.vue";
import LogFile from "./components/LogFile.vue";
import Report from "./components/Report.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus } from "@element-plus/icons-vue";
import { getDeviceList, editDevice, addDevice } from "@/api/modules/device";
// import { useCmd } from "@/hooks/useCmd";
// import useWebSocket from "@/hooks/useWebSocket";
// const socketUrl = import.meta.env.VITE_API_URL + "/ws/device";
// ProTable 实例
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数,直接定义传给 ProTable (之后每次请求都会自动带上该参数,此参数更改之后也会一直带上,改变此参数会自动刷新表格数据)
const initParam = reactive({});

const showLog = ref<boolean>(false);
let showReport = ref<boolean>(false);
const deviceId = ref<number>(0);
// dataCallback 是对于返回的表格数据做处理,如果你后台返回的数据不是 list && total 这些字段,可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
  return {
    list: data.data,
    total: data.total
  };
};
const formatDate = (date?: string) => {
  return date ? new Date(date).toLocaleString() : "-";
};
// 如果你想在请求之前对当前请求参数做一些操作,可以自定义如下函数:params 为当前所有的请求参数(包括分页),最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getDeviceList"
const getTableList = (params: any) => {
  let newParams = JSON.parse(JSON.stringify(params));
  return getDeviceList(newParams);
};
// 表格配置项
const columns = reactive<ColumnProps<Device.ResDeviceList>[]>([
  {
    prop: "user",
    label: "用户名",
    search: { el: "input", props: { placeholder: "请输入用户名", maxlength: 15 } }
  },
  {
    prop: "mobile",
    label: "手机号",
    search: { el: "input", props: { placeholder: "请输入手机号", maxlength: 11 } }
  },
  {
    prop: "sn",
    label: "设备编号",
    search: { el: "input", props: { placeholder: "请输入设备编号", maxlength: 50 } }
  },
  // {
  //   prop: "sn",
  //   label: "APP版本",
  //   search: { el: "input", props: { placeholder: "APP版本", maxlength: 50 } },
  //   render: () => {
  //     return "V2.0.0";
  //   }
  // },
  {
    prop: "log_saved_days",
    label: "日志存储时间",
    render: scope => {
      return scope.row.log_saved_days;
    },
    width: 120
  },
  {
    prop: "last_log_remove_time",
    label: "删除日志时间",
    render: scope => {
      return formatDate(scope.row.last_log_remove_time);
    }
  },
  {
    prop: "last_log_upload_time",
    label: "上传日志时间",
    render: scope => {
      return formatDate(scope.row.last_log_upload_time);
    }
  },
  {
    prop: "last_offline_time",
    label: "上次上线时间",
    render: scope => {
      return formatDate(scope.row.last_offline_time);
    }
    // search: { el: "input" }
  },
  {
    prop: "last_login_time",
    label: "上次登录时间",
    render: scope => {
      return formatDate(scope.row.last_login_time);
    }
    // search: { el: "input" }
  },
  {
    prop: "status",
    label: "在线状态",
    width: 90,
    render: scope => {
      return <el-tag type={scope.row.status == 1 ? "success" : "danger"}>{scope.row.status == 1 ? "在线" : "离线"}</el-tag>;
    },
    enum: [
      {
        label: "在线",
        value: 1
      },
      {
        label: "离线",
        value: 2
      }
    ],
    search: { el: "select", props: { placeholder: "请选择在线状态" } },
    fieldNames: { label: "label", value: "value" }
  },
  { prop: "operation", label: "操作", fixed: "right", width: 320 }
]);

// 删除
// const handleDelete = async (params: Device.ResDeviceList) => {
//   await useHandleData(deleteDevice, { id: [params.id] }, `是否确定删除该记录?`);
//   proTable.value?.getTableList();
// };

// 打开 dialog(新增、查看、编辑)
const dialogRef = ref<InstanceType<typeof Dialog> | null>(null);
const openDailog = (title: string, row: Partial<Device.ResDeviceList> = {}) => {
  const { sn, user, mobile, id: device_id } = row;
  const params = {
    title,
    isView: title === "查看",
    row: { sn, user, mobile, device_id },
    api: title === "新增设备" ? addDevice : title === "更新设备信息" ? editDevice : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
//打开 dialog(配置)
const configRef = ref<InstanceType<typeof Config> | null>(null);
const openConfig = (title: string, row: Partial<Device.ResDeviceList> = {}) => {
  const params = {
    title,
    row: { device_id: row.id, log_saved_days: row.log_saved_days },
    api: editDevice,
    getTableList: proTable.value?.getTableList
  };
  configRef.value?.acceptParams(params);
};
const reportRef = ref<InstanceType<typeof Report> | null>(null);
const openReport = (row: Partial<Device.ResDeviceList> = {}) => {
  showReport.value = true;
  deviceId.value = row.id!;
};
const openLog = (row: Partial<Device.ResDeviceList> = {}) => {
  showLog.value = true;
  deviceId.value = row.id!;
};
// const sendCmd = async (row: Partial<Device.ResDeviceList> = {}) => {
//   await useCmd({
//     device_id: row.id!,
//     event: "private",
//     data: { command: "getStorage", key: "recordList" }
//   });
// };
// //websocket连接
// const { sendMessage } = useWebSocket(socketUrl, (message: any) => {
//   console.log(message);
// });

// // sendMessage 数据推送方法
// //向成功连接的服务端推送数据
// sendMessage({ filed: "hello" });
</script>