<script setup> 
 | 
import { getCurrentInstance, ref, computed, reactive } from 'vue'; 
 | 
import { useRouter } from "vue-router"; 
 | 
import { get, post, postBlob } from '@/utils/request.js' 
 | 
import { message, Modal } from 'ant-design-vue'; 
 | 
import { logout } from '@/config.js'; 
 | 
import EditView from './edit.vue' 
 | 
import { formatMessage } from '@/utils/localeUtils.js'; 
 | 
import useTableSearch from '@/utils/tableUtils.jsx'; 
 | 
const context = getCurrentInstance()?.appContext.config.globalProperties; 
 | 
  
 | 
const router = useRouter(); 
 | 
  
 | 
const TABLE_KEY = 'table-host'; 
 | 
let currentPage = 1; 
 | 
let pageSize = 10; 
 | 
const searchInput = ref("") 
 | 
const editChild = ref(null) 
 | 
  
 | 
let tableData = ref([]); 
 | 
getPage(); 
 | 
  
 | 
const { 
 | 
  getColumnSearchProps, 
 | 
} = useTableSearch(); 
 | 
  
 | 
const columns = [ 
 | 
  { 
 | 
    title: formatMessage('db.sys_host.name', '名称'), 
 | 
    dataIndex: 'name', 
 | 
    width: 140, 
 | 
    ...getColumnSearchProps('name'), 
 | 
  }, 
 | 
  { 
 | 
    title: formatMessage('db.sys_host.status', '状态'), 
 | 
    dataIndex: 'status$', 
 | 
    width: 140, 
 | 
    ...getColumnSearchProps('status$'), 
 | 
  }, 
 | 
  { 
 | 
    title: formatMessage('db.sys_host.create_time', '添加时间'), 
 | 
    dataIndex: 'createTime$', 
 | 
    width: 140, 
 | 
    ...getColumnSearchProps('createTime$'), 
 | 
  }, 
 | 
  { 
 | 
    title: formatMessage('db.sys_host.update_time', '修改时间'), 
 | 
    dataIndex: 'updateTime$', 
 | 
    width: 140, 
 | 
    ...getColumnSearchProps('updateTime$'), 
 | 
  }, 
 | 
  { 
 | 
    title: formatMessage('db.sys_host.memo', '备注'), 
 | 
    dataIndex: 'memo', 
 | 
    width: 140, 
 | 
    ...getColumnSearchProps('memo'), 
 | 
  }, 
 | 
  
 | 
  { 
 | 
    title: formatMessage('common.operation', '操作'), 
 | 
    name: 'oper', 
 | 
    dataIndex: 'oper', 
 | 
    key: 'oper', 
 | 
    width: 140, 
 | 
  }, 
 | 
]; 
 | 
  
 | 
const state = reactive({ 
 | 
  selectedRowKeys: [], 
 | 
  loading: false, 
 | 
}); 
 | 
const hasSelected = computed(() => state.selectedRowKeys.length > 0); 
 | 
const start = () => { 
 | 
  state.loading = true; 
 | 
  // ajax request after empty completing 
 | 
  setTimeout(() => { 
 | 
    state.loading = false; 
 | 
    state.selectedRowKeys = []; 
 | 
  }, 1000); 
 | 
}; 
 | 
const onSelectChange = selectedRowKeys => { 
 | 
  // console.log('selectedRowKeys changed: ', selectedRowKeys); 
 | 
  state.selectedRowKeys = selectedRowKeys; 
 | 
}; 
 | 
  
 | 
function getPage() { 
 | 
  post('/api/host/page', { 
 | 
    current: currentPage, 
 | 
    pageSize: pageSize, 
 | 
    condition: searchInput.value 
 | 
  }).then((resp) => { 
 | 
    let result = resp.data; 
 | 
    if (result.code == 200) { 
 | 
      let data = result.data; 
 | 
      tableData.value = data; 
 | 
    } else if (result.code === 401) { 
 | 
      message.error(result.msg); 
 | 
      logout() 
 | 
    } else { 
 | 
      message.error(result.msg); 
 | 
    } 
 | 
  }) 
 | 
} 
 | 
  
 | 
const handleEdit = (item) => { 
 | 
  editChild.value.open = true; 
 | 
  editChild.value.formData = item == null ? editChild.value.initFormData : JSON.parse(JSON.stringify(item)); 
 | 
  editChild.value.isSave = item == null; 
 | 
} 
 | 
  
 | 
const handleDel = (rows) => { 
 | 
  Modal.confirm({ 
 | 
    title: formatMessage('page.delete', '删除'), 
 | 
    content: formatMessage('page.delete.confirm', '确定删除该项吗?'), 
 | 
    maskClosable: true, 
 | 
    onOk: async () => { 
 | 
      const hide = message.loading(formatMessage('common.loading', '请求中')); 
 | 
      try { 
 | 
        post('/api/host/remove/' + rows.map((row) => row.id).join(','), {}).then(resp => { 
 | 
          let result = resp.data; 
 | 
          if (result.code === 200) { 
 | 
            message.success(result.msg); 
 | 
          } else { 
 | 
            message.error(result.msg); 
 | 
          } 
 | 
          getPage() 
 | 
          hide() 
 | 
        }) 
 | 
      } catch (error) { 
 | 
        message.error(formatMessage('common.fail', '请求失败')); 
 | 
      } 
 | 
    }, 
 | 
  }); 
 | 
} 
 | 
  
 | 
const handleExport = async (intl) => { 
 | 
  postBlob('/api/host/export', {}).then(result => { 
 | 
    const blob = new Blob([result.data], { type: 'application/vnd.ms-excel' }); 
 | 
    window.location.href = window.URL.createObjectURL(blob); 
 | 
    return true; 
 | 
  }) 
 | 
}; 
 | 
  
 | 
const onSearch = () => { 
 | 
  // console.log('search'); 
 | 
  getPage() 
 | 
} 
 | 
  
 | 
const onPageChange = (page, size) => { 
 | 
  currentPage = page; 
 | 
  pageSize = size; 
 | 
  getPage(); 
 | 
} 
 | 
  
 | 
function handleTableReload(value) { 
 | 
  getPage() 
 | 
  window.location.reload(); 
 | 
} 
 | 
  
 | 
</script> 
 | 
  
 | 
<script> 
 | 
export default { 
 | 
  name: '机构管理' 
 | 
} 
 | 
</script> 
 | 
  
 | 
<template> 
 | 
  <div> 
 | 
    <EditView ref="editChild" @tableReload="handleTableReload" /> 
 | 
    <div class="table-header"> 
 | 
      <a-input-search v-model:value="searchInput" :placeholder="formatMessage('page.input', '请输入')" 
 | 
        style="width: 200px;" @search="onSearch" /> 
 | 
      <div class="table-header-right"> 
 | 
        <a-button @click="handleEdit(null)" type="primary">{{ formatMessage('page.add', '添加') }}</a-button> 
 | 
        <a-button @click="handleExport">{{ formatMessage('page.export', '导出') }}</a-button> 
 | 
      </div> 
 | 
    </div> 
 | 
    <a-table :row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange: onSelectChange }" 
 | 
      :data-source="tableData.records" :defaultExpandAllRows="false" :key="TABLE_KEY" rowKey="id" 
 | 
      :pagination="{ total: tableData.total, onChange: onPageChange }" 
 | 
      :scroll="{ y: 768, scrollToFirstRowOnChange: true }" :columns="columns"> 
 | 
      <template #bodyCell="{ column, text, record }"> 
 | 
        <template v-if="column.dataIndex === 'oper'"> 
 | 
          <div style="display: flex;justify-content: space-evenly;"> 
 | 
            <a-button type="link" primary @click="handleEdit(record)">{{ formatMessage('page.edit', '编辑') }}</a-button> 
 | 
            <a-button type="link" danger @click="handleDel([record])">{{ formatMessage('page.delete', '删除') 
 | 
              }}</a-button> 
 | 
          </div> 
 | 
        </template> 
 | 
      </template> 
 | 
    </a-table> 
 | 
  </div> 
 | 
</template> 
 | 
  
 | 
<style></style> 
 |