File size: 8,835 Bytes
81d8726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<template>
  <div class="host_card">
    <el-table
      ref="tableRef"
      :data="hosts"
      row-key="id"
      :default-sort="defaultSort"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column v-if="props.columnSettings.selection" type="selection" reserve-selection />
      <el-table-column
        v-if="props.columnSettings.index"
        property="index"
        label="序号"
        sortable
        width="100px"
      />
      <el-table-column
        v-if="props.columnSettings.name"
        label="名称"
        property="name"
        sortable
        :sort-method="(a, b) => a.name - b.name"
      >
        <template #default="scope">
          <span v-if="scope.row.connectType !== 'rdp'">
            <svg-icon name="icon-linux" class="icon" />
            {{ scope.row.name }}
          </span>
          <span v-else>
            <svg-icon name="icon-Windows" class="icon" />
            {{ scope.row.name }}
          </span>
        </template>
      </el-table-column>
      <el-table-column v-if="props.columnSettings.username" property="username" label="用户名" />
      <el-table-column v-if="props.columnSettings.host" property="host" label="IP">
        <template #default="scope">
          <span @click="handleCopy(scope.row.host)">{{ scope.row.host }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="props.columnSettings.port" property="port" label="端口" />
      <el-table-column v-if="props.columnSettings.authType" property="port" label="认证类型">
        <template #default="scope">{{ scope.row.authType === 'password' ? '密码' : '密钥' }}</template>
      </el-table-column>
      <el-table-column
        v-if="props.columnSettings.proxyType"
        property="port"
        show-overflow-tooltip
        label="代理类型"
      >
        <template #default="scope">{{ formatProxyType(scope.row) }}</template>
      </el-table-column>
      <el-table-column v-if="props.columnSettings.expired" property="expired" label="到期时间" sortable />
      <el-table-column
        v-if="props.columnSettings.consoleUrl"
        property="consoleUrl"
        show-overflow-tooltip
        label="控制台URL"
      >
        <template #default="scope">
          <span v-if="scope.row.consoleUrl" class="link" @click="handleToConsole(scope.row)">{{ scope.row.consoleUrl }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="props.columnSettings.tag"
        show-overflow-tooltip
        property="tag"
        label="标签"
      >
        <template #default="scope">
          <span v-if="scope.row.tag?.length">
            <el-tag
              v-for="tag in scope.row.tag"
              :key="tag"
              type="success"
              effect="plain"
              size="small"
            >
              {{ tag }}
            </el-tag>
          </span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" :width="isMobileScreen ? 'auto' : '260px'">
        <template #default="{ row }">
          <el-dropdown v-if="isMobileScreen" trigger="click">
            <span class="link">
              操作
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item>
                  <el-tooltip
                    :disabled="row.isConfig"
                    effect="dark"
                    content="请先配置ssh连接信息"
                    placement="left"
                  >
                    <el-button type="success" :disabled="!row.isConfig" @click="handleSSH(row)">连接</el-button>
                  </el-tooltip>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button type="primary" @click="handleUpdate(row)">配置</el-button>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-button type="danger" @click="handleRemoveHost(row)">删除</el-button>
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
          <template v-else>
            <el-tooltip
              :disabled="row.isConfig"
              effect="dark"
              content="请先配置ssh连接信息"
              placement="left"
            >
              <el-button type="success" :disabled="!row.isConfig" @click="handleSSH(row)">连接</el-button>
            </el-tooltip>
            <el-button type="primary" @click="handleUpdate(row)">配置</el-button>
            <el-button type="danger" @click="handleRemoveHost(row)">删除</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, computed, getCurrentInstance, nextTick } from 'vue'
import { ArrowDown } from '@element-plus/icons-vue'
import useMobileWidth from '@/composables/useMobileWidth'
import clipboard from '@/utils/clipboard'

const { proxy: { $message, $messageBox, $api, $router, $store } } = getCurrentInstance()

const props = defineProps({
  hosts: {
    required: true,
    type: Array
  },
  columnSettings: {
    type: Object,
    default: () => ({
      selection: true,
      index: true,
      name: true,
      username: true,
      host: true,
      port: true,
      authType: true,
      proxyType: true,
      expired: true,
      consoleUrl: true,
      tag: true
    })
  }
})

const emit = defineEmits(['update-list', 'update-host', 'select-change',])

const { isMobileScreen } = useMobileWidth()
const tableRef = ref(null)

const hosts = computed(() => props.hosts)
const hostList = computed(() => $store.hostList)
const proxyList = computed(() => $store.proxyList)

const handleUpdate = (hostInfo) => {
  emit('update-host', hostInfo)
}

const handleToConsole = ({ consoleUrl }) => {
  if (!consoleUrl) return $message({ message: '未配置服务商控制台地址', type: 'warning', center: true })
  window.open(consoleUrl)
}

const handleSSH = async (row) => {
  let { id, connectType } = row
  $router.push({ path: connectType === 'rdp' ? '/rdp' : '/terminal', query: { hostIds: id } })
}

const defaultSortLocal = localStorage.getItem('host_table_sort')
const defaultSort = ref(defaultSortLocal ? JSON.parse(defaultSortLocal) : { prop: 'index', order: null }) // 'ascending' or 'descending'

const handleSortChange = (sortObj) => {
  defaultSort.value = sortObj
  localStorage.setItem('host_table_sort', JSON.stringify(sortObj))
}

const selectHosts = ref([])
const handleSelectionChange = (val) => {
  // console.log('select: ', val)
  selectHosts.value = val
  emit('select-change', val)
}

const getSelectHosts = () => {
  return selectHosts.value
}

const clearSelection = () => {
  nextTick(() => tableRef.value.clearSelection())
}

const selectAll = () => {
  nextTick(() => tableRef.value.toggleAllSelection())
}

// 反选:已选的变成不选,未选的变成已选
const toggleSelection = () => {
  nextTick(() => {
    hosts.value.forEach(row => {
      tableRef.value.toggleRowSelection(row)
    })
  })
}

defineExpose({
  getSelectHosts,
  clearSelection,
  selectAll,
  toggleSelection
})

const handleRemoveHost = async ({ id }) => {
  $messageBox.confirm('确认删除实例', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    let { data } = await $api.removeHost({ ids: [id,] })
    $message({
      message: data,
      type: 'success',
      center: true
    })
    emit('update-list')
    clearSelection()
  })
}

const handleCopy = (host) => {
  clipboard.copy(host)
}

const formatProxyType = ({ proxyType, jumpHosts, proxyServer }) => {
  if (!proxyType) return '--'
  if (proxyType === 'jumpHosts' && jumpHosts?.length > 0) {
    const jumpHostsName = jumpHosts.map(item => {
      const hostInfo = hostList.value.find(host => host.id === item)
      return hostInfo?.name || 'Error'
    }).join('>>>')
    return `[跳板机]${ jumpHostsName }`
  }
  if (proxyType === 'proxyServer' && proxyList.value.some(item => item.id === proxyServer)) {
    const proxyServerInfo = proxyList.value.find(item => item.id === proxyServer)
    return `[${ proxyServerInfo.type }]${ proxyServerInfo.name }`
  }
  return '--'
}
</script>

<style lang="scss" scoped>
.host_card {
  margin: 0px 10px;
  // transition: all 0.5s;
  .no_client_data {
    font-size: 14px;
    font-weight: normal;
    line-height: 23px;
    text-align: center;
    color: var(--el-color-warning);;
  }
  .host_info {
    padding: 0 20px;
  }
}
</style>