Files
rustdesk-api-web/src/views/address_book/index.js
T

216 lines
5.0 KiB
JavaScript
Raw Normal View History

2024-10-28 14:25:41 +08:00
import { reactive, ref, watch } from 'vue'
2024-09-13 16:34:15 +08:00
import { create, list, remove, update } from '@/api/address_book'
import { ElMessage, ElMessageBox } from 'element-plus'
2024-09-25 22:24:16 +08:00
import { T } from '@/utils/i18n'
2024-10-28 14:25:41 +08:00
import { useRepositories as useCollectionRepositories } from '@/views/address_book/collection'
import { useRepositories as useTagRepositories } from '@/views/tag/index'
import { loadAllUsers } from '@/global'
export function useRepositories (is_my = 0) {
const { allUsers, getAllUsers } = loadAllUsers()
const {
listRes: collectionListRes,
listQuery: collectionListQuery,
getList: getCollectionList,
} = useCollectionRepositories(is_my)
collectionListQuery.page_size = 9999
const {
listRes: tagListRes,
listQuery: tagListQuery,
getList: getTagList,
} = useTagRepositories(is_my)
tagListQuery.page_size = 9999
2024-09-13 16:34:15 +08:00
const listRes = reactive({
list: [], total: 0, loading: false,
})
const listQuery = reactive({
page: 1,
page_size: 10,
2024-10-28 14:25:41 +08:00
is_my,
id: null,
user_id: null,
username: null,
hostname: null,
2024-10-28 14:25:41 +08:00
collection_id: null,
2024-09-13 16:34:15 +08:00
})
const getList = async () => {
listRes.loading = true
const res = await list(listQuery).catch(_ => false)
listRes.loading = false
if (res) {
listRes.list = res.data.list
listRes.total = res.data.total
}
}
const handlerQuery = () => {
if (listQuery.page === 1) {
getList()
} else {
listQuery.page = 1
}
}
const del = async (row) => {
2024-09-25 22:24:16 +08:00
const cf = await ElMessageBox.confirm(T('Confirm?', { param: T('Delete') }), {
confirmButtonText: T('Confirm'),
cancelButtonText: T('Cancel'),
2024-09-13 16:34:15 +08:00
type: 'warning',
}).catch(_ => false)
if (!cf) {
return false
}
const res = await remove({ row_id: row.row_id }).catch(_ => false)
if (res) {
2024-09-25 22:24:16 +08:00
ElMessage.success(T('OperationSuccess'))
2024-09-13 16:34:15 +08:00
getList()
}
}
const platformList = [
2024-10-28 14:25:41 +08:00
{ label: 'Windows', value: 'Windows', icon: 'windows' },
{ label: 'Linux', value: 'Linux', icon: 'linux' },
{ label: 'Mac OS', value: 'Mac OS', icon: 'mac' },
{ label: 'Android', value: 'Android', icon: 'android' },
2024-09-13 16:34:15 +08:00
]
const formVisible = ref(false)
const formData = reactive({
'row_id': 0,
'alias': '',
2024-09-24 19:39:37 +08:00
'forceAlwaysRelay': false,
2024-09-13 16:34:15 +08:00
'hash': '',
'hostname': '',
'id': '',
2024-09-24 19:39:37 +08:00
'loginName': '',
2024-09-13 16:34:15 +08:00
'online': false,
'password': '',
'platform': '',
2024-09-24 19:39:37 +08:00
'rdpPort': '',
'rdpUsername': '',
'sameServer': false,
2024-09-13 16:34:15 +08:00
'tags': [],
'user_id': null,
user_ids: [],
2024-09-13 16:34:15 +08:00
'username': '',
2024-10-28 14:25:41 +08:00
collection_id: null,
2024-09-13 16:34:15 +08:00
})
const toEdit = (row) => {
formVisible.value = true
//将row中的数据赋值给formData
Object.keys(formData).forEach(key => {
formData[key] = row[key]
})
2024-10-28 14:25:41 +08:00
collectionListQuery.user_id = row.user_id
getCollectionList()
tagListQuery.collection_id = row.collection_id
getTagList()
2024-09-13 16:34:15 +08:00
}
const toAdd = () => {
formVisible.value = true
//重置formData
formData.row_id = 0
formData.alias = ''
2024-09-24 19:39:37 +08:00
formData.forceAlwaysRelay = false
2024-09-13 16:34:15 +08:00
formData.hash = ''
formData.hostname = ''
formData.id = ''
2024-09-24 19:39:37 +08:00
formData.loginName = ''
2024-09-13 16:34:15 +08:00
formData.online = false
formData.password = ''
formData.platform = ''
2024-09-24 19:39:37 +08:00
formData.rdpPort = ''
formData.rdpUsername = ''
formData.sameServer = false
2024-09-13 16:34:15 +08:00
formData.tags = []
formData.user_id = null
formData.username = ''
}
const submit = async () => {
const api = formData.row_id ? update : create
const res = await api(formData).catch(_ => false)
if (res) {
2024-09-25 22:24:16 +08:00
ElMessage.success(T('OperationSuccess'))
2024-09-13 16:34:15 +08:00
formVisible.value = false
getList()
}
}
2024-10-09 15:52:17 +08:00
const shareToWebClientVisible = ref(false)
const shareToWebClientForm = reactive({
id: '',
hash: '',
})
const toShowShare = (row) => {
shareToWebClientForm.id = row.id
shareToWebClientForm.hash = row.hash
shareToWebClientVisible.value = true
}
2024-10-28 14:25:41 +08:00
const changeQueryUser = async (val) => {
tagListRes.list = []
listQuery.collection_id = null
if (!val) {
collectionListRes.list = []
} else {
collectionListQuery.user_id = val
getCollectionList()
}
}
const changeUser = async (val) => {
tagListRes.list = []
formData.tags = []
formData.collection_id = 0
if (!val) {
collectionListRes.list = []
} else {
collectionListQuery.user_id = val
getCollectionList()
}
}
const changeCollection = async (val) => {
tagListRes.list = []
formData.tags = []
tagListQuery.user_id = formData.user_id
tagListQuery.collection_id = val
getTagList()
}
2024-09-13 16:34:15 +08:00
return {
listRes,
listQuery,
getList,
handlerQuery,
del,
platformList,
formVisible,
formData,
toEdit,
toAdd,
submit,
2024-10-09 15:52:17 +08:00
shareToWebClientVisible,
shareToWebClientForm,
toShowShare,
2024-10-28 14:25:41 +08:00
collectionListQuery,
getCollectionList,
collectionListRes,
tagListQuery,
getTagList,
tagListRes,
allUsers,
getAllUsers,
changeQueryUser,
changeUser,
changeCollection,
2024-09-13 16:34:15 +08:00
}
}