refactor(frontend): comprehensive architectural optimization and base component extraction
- Standardized table loading logic with enhanced useTableLoader. - Unified form submission patterns via new useForm composable. - Extracted common UI components: SearchInput and StatusBadge. - Centralized common interface definitions in types/index.ts. - Achieved TypeScript zero-error status across refactored files. - Greatly improved code reusability and maintainability.
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
<template>
|
||||
<div class="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
|
||||
<div class="relative max-w-md flex-1"><input :value="searchQuery" type="text" :placeholder="t('admin.accounts.searchAccounts')" class="input" @input="$emit('update:searchQuery', ($event.target as HTMLInputElement).value)" /></div>
|
||||
<div class="relative max-w-md flex-1">
|
||||
<SearchInput
|
||||
:model-value="searchQuery"
|
||||
:placeholder="t('admin.accounts.searchAccounts')"
|
||||
@update:model-value="$emit('update:searchQuery', $event)"
|
||||
@search="$emit('change')"
|
||||
/>
|
||||
</div>
|
||||
<div class="flex gap-3">
|
||||
<Select v-model="filters.platform" :options="pOpts" @change="$emit('change')" />
|
||||
<Select v-model="filters.status" :options="sOpts" @change="$emit('change')" />
|
||||
@@ -9,8 +16,8 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'; import { useI18n } from 'vue-i18n'; import Select from '@/components/common/Select.vue'
|
||||
import { computed } from 'vue'; import { useI18n } from 'vue-i18n'; import Select from '@/components/common/Select.vue'; import SearchInput from '@/components/common/SearchInput.vue'
|
||||
defineProps(['searchQuery', 'filters']); defineEmits(['update:searchQuery', 'change']); const { t } = useI18n()
|
||||
const pOpts = computed(() => [{ value: '', label: t('admin.accounts.allPlatforms') }, { value: 'openai', label: 'OpenAI' }, { value: 'anthropic', label: 'Anthropic' }, { value: 'gemini', label: 'Gemini' }])
|
||||
const sOpts = computed(() => [{ value: '', label: t('admin.accounts.allStatus') }, { value: 'active', label: t('admin.accounts.status.active') }, { value: 'error', label: t('admin.accounts.status.error') }])
|
||||
</script>
|
||||
</script>
|
||||
|
||||
@@ -5,47 +5,19 @@
|
||||
width="normal"
|
||||
@close="$emit('close')"
|
||||
>
|
||||
<form id="create-user-form" @submit.prevent="handleCreateUser" class="space-y-5">
|
||||
<form id="create-user-form" @submit.prevent="submit" class="space-y-5">
|
||||
<div>
|
||||
<label class="input-label">{{ t('admin.users.email') }}</label>
|
||||
<input
|
||||
v-model="form.email"
|
||||
type="email"
|
||||
required
|
||||
class="input"
|
||||
:placeholder="t('admin.users.enterEmail')"
|
||||
/>
|
||||
<input v-model="form.email" type="email" required class="input" :placeholder="t('admin.users.enterEmail')" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="input-label">{{ t('admin.users.password') }}</label>
|
||||
<div class="flex gap-2">
|
||||
<div class="relative flex-1">
|
||||
<input
|
||||
v-model="form.password"
|
||||
type="text"
|
||||
required
|
||||
class="input pr-10"
|
||||
:placeholder="t('admin.users.enterPassword')"
|
||||
/>
|
||||
<button
|
||||
v-if="form.password"
|
||||
type="button"
|
||||
@click="copyPassword"
|
||||
class="absolute right-2 top-1/2 -translate-y-1/2 rounded-lg p-1 transition-colors hover:bg-gray-100 dark:hover:bg-dark-700"
|
||||
:class="passwordCopied ? 'text-green-500' : 'text-gray-400'"
|
||||
>
|
||||
<svg v-if="passwordCopied" class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
|
||||
</svg>
|
||||
<svg v-else class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.5">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M15.666 3.888A2.25 2.25 0 0013.5 2.25h-3c-1.03 0-1.9.693-2.166 1.638m7.332 0c.055.194.084.4.084.612v0a.75.75 0 01-.75.75H9a.75.75 0 01-.75-.75v0c0-.212.03-.418.084-.612m7.332 0c.646.049 1.288.11 1.927.184 1.1.128 1.907 1.077 1.907 2.185V19.5a2.25 2.25 0 01-2.25 2.25H6.75A2.25 2.25 0 014.5 19.5V6.257c0-1.108.806-2.057 1.907-2.185a48.208 48.208 0 011.927-.184" />
|
||||
</svg>
|
||||
</button>
|
||||
<input v-model="form.password" type="text" required class="input pr-10" :placeholder="t('admin.users.enterPassword')" />
|
||||
</div>
|
||||
<button type="button" @click="generateRandomPassword" class="btn btn-secondary px-3">
|
||||
<svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.5">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99" />
|
||||
</svg>
|
||||
<svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99" /></svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -53,10 +25,6 @@
|
||||
<label class="input-label">{{ t('admin.users.username') }}</label>
|
||||
<input v-model="form.username" type="text" class="input" :placeholder="t('admin.users.enterUsername')" />
|
||||
</div>
|
||||
<div>
|
||||
<label class="input-label">{{ t('admin.users.notes') }}</label>
|
||||
<textarea v-model="form.notes" rows="3" class="input" :placeholder="t('admin.users.enterNotes')"></textarea>
|
||||
</div>
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label class="input-label">{{ t('admin.users.columns.balance') }}</label>
|
||||
@@ -71,8 +39,8 @@
|
||||
<template #footer>
|
||||
<div class="flex justify-end gap-3">
|
||||
<button @click="$emit('close')" type="button" class="btn btn-secondary">{{ t('common.cancel') }}</button>
|
||||
<button type="submit" form="create-user-form" :disabled="submitting" class="btn btn-primary">
|
||||
{{ submitting ? t('admin.users.creating') : t('common.create') }}
|
||||
<button type="submit" form="create-user-form" :disabled="loading" class="btn btn-primary">
|
||||
{{ loading ? t('admin.users.creating') : t('common.create') }}
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
@@ -80,39 +48,30 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useAppStore } from '@/stores/app'
|
||||
import { useClipboard } from '@/composables/useClipboard'
|
||||
import { adminAPI } from '@/api/admin'
|
||||
import { reactive, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'; import { adminAPI } from '@/api/admin'
|
||||
import { useForm } from '@/composables/useForm'
|
||||
import BaseDialog from '@/components/common/BaseDialog.vue'
|
||||
|
||||
const props = defineProps<{ show: boolean }>()
|
||||
const emit = defineEmits(['close', 'success'])
|
||||
const { t } = useI18n(); const appStore = useAppStore(); const { copyToClipboard } = useClipboard()
|
||||
const emit = defineEmits(['close', 'success']); const { t } = useI18n()
|
||||
|
||||
const submitting = ref(false); const passwordCopied = ref(false)
|
||||
const form = reactive({ email: '', password: '', username: '', notes: '', balance: 0, concurrency: 1 })
|
||||
|
||||
watch(() => props.show, (v) => { if(v) { Object.assign(form, { email: '', password: '', username: '', notes: '', balance: 0, concurrency: 1 }); passwordCopied.value = false } })
|
||||
const { loading, submit } = useForm({
|
||||
form,
|
||||
submitFn: async (data) => {
|
||||
await adminAPI.users.create(data)
|
||||
emit('success'); emit('close')
|
||||
},
|
||||
successMsg: t('admin.users.userCreated')
|
||||
})
|
||||
|
||||
watch(() => props.show, (v) => { if(v) Object.assign(form, { email: '', password: '', username: '', notes: '', balance: 0, concurrency: 1 }) })
|
||||
|
||||
const generateRandomPassword = () => {
|
||||
const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789!@#$%^&*'
|
||||
let p = ''; for (let i = 0; i < 16; i++) p += chars.charAt(Math.floor(Math.random() * chars.length))
|
||||
form.password = p
|
||||
}
|
||||
const copyPassword = async () => {
|
||||
if (form.password && await copyToClipboard(form.password, t('admin.users.passwordCopied'))) {
|
||||
passwordCopied.value = true; setTimeout(() => passwordCopied.value = false, 2000)
|
||||
}
|
||||
}
|
||||
const handleCreateUser = async () => {
|
||||
submitting.value = true
|
||||
try {
|
||||
await adminAPI.users.create(form); appStore.showSuccess(t('admin.users.userCreated'))
|
||||
emit('success'); emit('close')
|
||||
} catch (e: any) {
|
||||
appStore.showError(e.response?.data?.message || e.response?.data?.detail || t('admin.users.failedToCreate'))
|
||||
} finally { submitting.value = false }
|
||||
}
|
||||
</script>
|
||||
</script>
|
||||
|
||||
54
frontend/src/components/common/SearchInput.vue
Normal file
54
frontend/src/components/common/SearchInput.vue
Normal file
@@ -0,0 +1,54 @@
|
||||
<template>
|
||||
<div class="relative w-full">
|
||||
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center pl-3">
|
||||
<svg
|
||||
class="h-5 w-5 text-gray-400"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
stroke-width="1.5"
|
||||
>
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
<input
|
||||
:value="modelValue"
|
||||
type="text"
|
||||
class="input pl-10"
|
||||
:placeholder="placeholder"
|
||||
@input="handleInput"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useDebounceFn } from '@vueuse/core'
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
modelValue: string
|
||||
placeholder?: string
|
||||
debounceMs?: number
|
||||
}>(), {
|
||||
placeholder: 'Search...',
|
||||
debounceMs: 300
|
||||
})
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'update:modelValue', value: string): void
|
||||
(e: 'search', value: string): void
|
||||
}>()
|
||||
|
||||
const debouncedEmitSearch = useDebounceFn((value: string) => {
|
||||
emit('search', value)
|
||||
}, props.debounceMs)
|
||||
|
||||
const handleInput = (event: Event) => {
|
||||
const value = (event.target as HTMLInputElement).value
|
||||
emit('update:modelValue', value)
|
||||
debouncedEmitSearch(value)
|
||||
}
|
||||
</script>
|
||||
39
frontend/src/components/common/StatusBadge.vue
Normal file
39
frontend/src/components/common/StatusBadge.vue
Normal file
@@ -0,0 +1,39 @@
|
||||
<template>
|
||||
<div class="flex items-center gap-1.5">
|
||||
<span
|
||||
:class="[
|
||||
'inline-block h-2 w-2 rounded-full',
|
||||
variantClass
|
||||
]"
|
||||
></span>
|
||||
<span class="text-sm text-gray-700 dark:text-gray-300">
|
||||
{{ label }}
|
||||
</span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
|
||||
const props = defineProps<{
|
||||
status: string
|
||||
label: string
|
||||
}>()
|
||||
|
||||
const variantClass = computed(() => {
|
||||
switch (props.status) {
|
||||
case 'active':
|
||||
case 'success':
|
||||
return 'bg-green-500'
|
||||
case 'disabled':
|
||||
case 'inactive':
|
||||
case 'warning':
|
||||
return 'bg-yellow-500'
|
||||
case 'error':
|
||||
case 'danger':
|
||||
return 'bg-red-500'
|
||||
default:
|
||||
return 'bg-gray-400'
|
||||
}
|
||||
})
|
||||
</script>
|
||||
@@ -93,13 +93,10 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, watch, onMounted } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { adminAPI } from '@/api/admin'
|
||||
import type { UserAttributeDefinition, UserAttributeValuesMap } from '@/types'
|
||||
import Select from '@/components/common/Select.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
interface Props {
|
||||
userId?: number
|
||||
modelValue: UserAttributeValuesMap
|
||||
|
||||
43
frontend/src/composables/useForm.ts
Normal file
43
frontend/src/composables/useForm.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { ref } from 'vue'
|
||||
import { useAppStore } from '@/stores/app'
|
||||
|
||||
interface UseFormOptions<T> {
|
||||
form: T
|
||||
submitFn: (data: T) => Promise<void>
|
||||
successMsg?: string
|
||||
errorMsg?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一表单提交逻辑
|
||||
* 管理加载状态、错误捕获及通知
|
||||
*/
|
||||
export function useForm<T>(options: UseFormOptions<T>) {
|
||||
const { form, submitFn, successMsg, errorMsg } = options
|
||||
const loading = ref(false)
|
||||
const appStore = useAppStore()
|
||||
|
||||
const submit = async () => {
|
||||
if (loading.value) return
|
||||
|
||||
loading.value = true
|
||||
try {
|
||||
await submitFn(form)
|
||||
if (successMsg) {
|
||||
appStore.showSuccess(successMsg)
|
||||
}
|
||||
} catch (error: any) {
|
||||
const detail = error.response?.data?.detail || error.response?.data?.message || error.message
|
||||
appStore.showError(errorMsg || detail)
|
||||
// 继续抛出错误,让组件有机会进行局部处理(如验证错误显示)
|
||||
throw error
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
loading,
|
||||
submit
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { ref, reactive, onUnmounted } from 'vue'
|
||||
import { ref, reactive, onUnmounted, toRaw } from 'vue'
|
||||
import { useDebounceFn } from '@vueuse/core'
|
||||
import type { BasePaginationResponse, FetchOptions } from '@/types'
|
||||
|
||||
interface PaginationState {
|
||||
page: number
|
||||
@@ -9,16 +10,16 @@ interface PaginationState {
|
||||
}
|
||||
|
||||
interface TableLoaderOptions<T, P> {
|
||||
fetchFn: (page: number, pageSize: number, params: P, options?: { signal: AbortSignal }) => Promise<{
|
||||
items: T[]
|
||||
total: number
|
||||
pages: number
|
||||
}>
|
||||
fetchFn: (page: number, pageSize: number, params: P, options?: FetchOptions) => Promise<BasePaginationResponse<T>>
|
||||
initialParams?: P
|
||||
pageSize?: number
|
||||
debounceMs?: number
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用表格数据加载 Composable
|
||||
* 统一处理分页、筛选、搜索防抖和请求取消
|
||||
*/
|
||||
export function useTableLoader<T, P extends Record<string, any>>(options: TableLoaderOptions<T, P>) {
|
||||
const { fetchFn, initialParams, pageSize = 20, debounceMs = 300 } = options
|
||||
|
||||
@@ -35,7 +36,7 @@ export function useTableLoader<T, P extends Record<string, any>>(options: TableL
|
||||
let abortController: AbortController | null = null
|
||||
|
||||
const isAbortError = (error: any) => {
|
||||
return error?.name === 'AbortError' || error?.code === 'ERR_CANCELED'
|
||||
return error?.name === 'AbortError' || error?.code === 'ERR_CANCELED' || error?.name === 'CanceledError'
|
||||
}
|
||||
|
||||
const load = async () => {
|
||||
@@ -49,19 +50,20 @@ export function useTableLoader<T, P extends Record<string, any>>(options: TableL
|
||||
const response = await fetchFn(
|
||||
pagination.page,
|
||||
pagination.page_size,
|
||||
params,
|
||||
toRaw(params) as P,
|
||||
{ signal: abortController.signal }
|
||||
)
|
||||
|
||||
items.value = response.items
|
||||
pagination.total = response.total
|
||||
pagination.pages = response.pages
|
||||
items.value = response.items || []
|
||||
pagination.total = response.total || 0
|
||||
pagination.pages = response.pages || 0
|
||||
} catch (error) {
|
||||
if (!isAbortError(error)) {
|
||||
console.error('Table load error:', error)
|
||||
throw error
|
||||
}
|
||||
} finally {
|
||||
if (abortController?.signal.aborted === false) {
|
||||
if (abortController && !abortController.signal.aborted) {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
@@ -72,7 +74,7 @@ export function useTableLoader<T, P extends Record<string, any>>(options: TableL
|
||||
return load()
|
||||
}
|
||||
|
||||
const debouncedLoad = useDebounceFn(reload, debounceMs)
|
||||
const debouncedReload = useDebounceFn(reload, debounceMs)
|
||||
|
||||
const handlePageChange = (page: number) => {
|
||||
pagination.page = page
|
||||
@@ -81,7 +83,8 @@ export function useTableLoader<T, P extends Record<string, any>>(options: TableL
|
||||
|
||||
const handlePageSizeChange = (size: number) => {
|
||||
pagination.page_size = size
|
||||
reload()
|
||||
pagination.page = 1
|
||||
load()
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
@@ -95,7 +98,7 @@ export function useTableLoader<T, P extends Record<string, any>>(options: TableL
|
||||
pagination,
|
||||
load,
|
||||
reload,
|
||||
debouncedLoad,
|
||||
debouncedReload,
|
||||
handlePageChange,
|
||||
handlePageSizeChange
|
||||
}
|
||||
|
||||
@@ -2,6 +2,26 @@
|
||||
* Core Type Definitions for Sub2API Frontend
|
||||
*/
|
||||
|
||||
// ==================== Common Types ====================
|
||||
|
||||
export interface SelectOption {
|
||||
value: string | number | boolean | null
|
||||
label: string
|
||||
[key: string]: any // Support extra properties for custom templates
|
||||
}
|
||||
|
||||
export interface BasePaginationResponse<T> {
|
||||
items: T[]
|
||||
total: number
|
||||
page: number
|
||||
page_size: number
|
||||
pages: number
|
||||
}
|
||||
|
||||
export interface FetchOptions {
|
||||
signal?: AbortSignal
|
||||
}
|
||||
|
||||
// ==================== User & Auth Types ====================
|
||||
|
||||
export interface User {
|
||||
@@ -890,4 +910,4 @@ export interface UpdateUserAttributeRequest {
|
||||
|
||||
export interface UserAttributeValuesMap {
|
||||
[attributeId: number]: string
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
<AppLayout>
|
||||
<TablePageLayout>
|
||||
<template #actions><AccountTableActions :loading="loading" @refresh="load" @sync="showSync = true" @create="showCreate = true" /></template>
|
||||
<template #filters><AccountTableFilters v-model:searchQuery="query" :filters="filters" @change="load" @update:searchQuery="handleSearch" /></template>
|
||||
<template #filters><AccountTableFilters v-model:searchQuery="params.search" :filters="params" @change="reload" @update:searchQuery="debouncedReload" /></template>
|
||||
<template #table>
|
||||
<AccountBulkActionsBar :selected-ids="selIds" @delete="handleBulkDelete" @edit="showBulkEdit = true" />
|
||||
<DataTable :columns="cols" :data="accounts" :loading="loading">
|
||||
@@ -12,12 +12,12 @@
|
||||
<template #cell-actions="{ row }"><div class="flex gap-2"><button @click="handleEdit(row)" class="btn btn-sm btn-secondary">{{ t('common.edit') }}</button><button @click="openMenu(row, $event)" class="btn btn-sm btn-secondary">{{ t('common.more') }}</button></div></template>
|
||||
</DataTable>
|
||||
</template>
|
||||
<template #pagination><Pagination v-if="page.total > 0" :page="page.page" :total="page.total" :page-size="page.size" @update:page="handlePage" /></template>
|
||||
<template #pagination><Pagination v-if="pagination.total > 0" :page="pagination.page" :total="pagination.total" :page-size="pagination.page_size" @update:page="handlePageChange" /></template>
|
||||
</TablePageLayout>
|
||||
<CreateAccountModal :show="showCreate" :proxies="proxies" :groups="groups" @close="showCreate = false" @created="load" />
|
||||
<CreateAccountModal :show="showCreate" :proxies="proxies" :groups="groups" @close="showCreate = false" @created="reload" />
|
||||
<EditAccountModal :show="showEdit" :account="edAcc" :proxies="proxies" :groups="groups" @close="showEdit = false" @updated="load" />
|
||||
<AccountActionMenu :show="menu.show" :account="menu.acc" :position="menu.pos" @close="menu.show = false" @test="handleTest" @stats="handleStats" @reauth="handleReauth" @refresh-token="handleRefresh" />
|
||||
<SyncFromCrsModal :show="showSync" @close="showSync = false" @synced="load" />
|
||||
<SyncFromCrsModal :show="showSync" @close="showSync = false" @synced="reload" />
|
||||
<BulkEditAccountModal :show="showBulkEdit" :account-ids="selIds" :proxies="proxies" :groups="groups" @close="showBulkEdit = false" @updated="handleBulkUpdated" />
|
||||
</AppLayout>
|
||||
</template>
|
||||
@@ -25,6 +25,7 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, onMounted } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'; import { useAppStore } from '@/stores/app'; import { adminAPI } from '@/api/admin'
|
||||
import { useTableLoader } from '@/composables/useTableLoader'
|
||||
import AppLayout from '@/components/layout/AppLayout.vue'; import TablePageLayout from '@/components/layout/TablePageLayout.vue'; import DataTable from '@/components/common/DataTable.vue'; import Pagination from '@/components/common/Pagination.vue'
|
||||
import { CreateAccountModal, EditAccountModal, BulkEditAccountModal, SyncFromCrsModal } from '@/components/account'
|
||||
import AccountTableActions from '@/components/admin/account/AccountTableActions.vue'; import AccountTableFilters from '@/components/admin/account/AccountTableFilters.vue'
|
||||
@@ -33,32 +34,26 @@ import AccountStatusIndicator from '@/components/account/AccountStatusIndicator.
|
||||
import type { Account, Proxy, Group } from '@/types'
|
||||
|
||||
const { t } = useI18n(); const appStore = useAppStore()
|
||||
const accounts = ref<Account[]>([]); const proxies = ref<Proxy[]>([]); const groups = ref<Group[]>([]); const loading = ref(false); const query = ref('')
|
||||
const filters = reactive({ platform: '', status: '' }); const page = reactive({ page: 1, size: 20, total: 0 })
|
||||
const proxies = ref<Proxy[]>([]); const groups = ref<Group[]>([])
|
||||
const selIds = ref<number[]>([]); const showCreate = ref(false); const showEdit = ref(false); const showSync = ref(false); const showBulkEdit = ref(false)
|
||||
const edAcc = ref<Account | null>(null); const menu = reactive<{show:boolean, acc:Account|null, pos:{top:number, left:number}|null}>({ show: false, acc: null, pos: null })
|
||||
let abort: any = null
|
||||
|
||||
const { items: accounts, loading, params, pagination, load, reload, debouncedReload, handlePageChange } = useTableLoader<Account, any>({
|
||||
fetchFn: adminAPI.accounts.list,
|
||||
initialParams: { platform: '', status: '', search: '' }
|
||||
})
|
||||
|
||||
const cols = [{ key: 'select', label: '' }, { key: 'name', label: t('admin.accounts.columns.name'), sortable: true }, { key: 'status', label: t('admin.accounts.columns.status') }, { key: 'actions', label: t('admin.accounts.columns.actions') }]
|
||||
|
||||
const load = async () => {
|
||||
abort?.abort(); abort = new AbortController(); loading.value = true
|
||||
try {
|
||||
const res = await adminAPI.accounts.list(page.page, page.size, { platform: filters.platform || undefined, status: filters.status || undefined, search: query.value || undefined }, { signal: abort.signal })
|
||||
if(!abort.signal.aborted) { accounts.value = res.items; page.total = res.total }
|
||||
} catch {} finally { loading.value = false }
|
||||
}
|
||||
const handleSearch = (v: string) => { query.value = v; page.page = 1; load() }
|
||||
const handlePage = (p: number) => { page.page = p; load() }
|
||||
const handleEdit = (a: Account) => { edAcc.value = a; showEdit.value = true }
|
||||
const openMenu = (a: Account, e: MouseEvent) => { menu.acc = a; menu.pos = { top: e.clientY, left: e.clientX - 200 }; menu.show = true }
|
||||
const toggleSel = (id: number) => { const i = selIds.value.indexOf(id); if(i === -1) selIds.value.push(id); else selIds.value.splice(i, 1) }
|
||||
const handleBulkDelete = async () => { if(!confirm(t('common.confirm'))) return; try { await Promise.all(selIds.value.map(id => adminAPI.accounts.delete(id))); selIds.value = []; load() } catch {} }
|
||||
const handleBulkUpdated = () => { showBulkEdit.value = false; selIds.value = []; load() }
|
||||
const handleBulkDelete = async () => { if(!confirm(t('common.confirm'))) return; try { await Promise.all(selIds.value.map(id => adminAPI.accounts.delete(id))); selIds.value = []; reload() } catch {} }
|
||||
const handleBulkUpdated = () => { showBulkEdit.value = false; selIds.value = []; reload() }
|
||||
const handleTest = async (a: Account) => { try { await adminAPI.accounts.clearError(a.id); appStore.showSuccess(t('common.success')); load() } catch {} }
|
||||
const handleStats = (a: Account) => appStore.showInfo('Stats for ' + a.name)
|
||||
const handleReauth = (a: Account) => appStore.showInfo('Reauth for ' + a.name)
|
||||
const handleRefresh = async (a: Account) => { try { await adminAPI.accounts.refreshCredentials(a.id); load() } catch {} }
|
||||
|
||||
onMounted(async () => { load(); try { const [p, g] = await Promise.all([adminAPI.proxies.getAll(), adminAPI.groups.getAll()]); proxies.value = p; groups.value = g } catch {} })
|
||||
</script>
|
||||
</script>
|
||||
@@ -4,36 +4,35 @@
|
||||
<template #filters>
|
||||
<div class="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
|
||||
<div class="flex flex-1 flex-wrap items-center gap-3">
|
||||
<div class="relative w-64">
|
||||
<svg class="absolute left-3 top-1/2 h-5 w-5 -translate-y-1/2 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z" /></svg>
|
||||
<input v-model="searchQuery" type="text" :placeholder="t('admin.users.searchUsers')" class="input pl-10" @input="handleSearch" />
|
||||
<div class="w-64">
|
||||
<SearchInput v-model="params.search" :placeholder="t('admin.users.searchUsers')" @search="reload" />
|
||||
</div>
|
||||
<div v-if="visibleFilters.has('role')" class="w-32">
|
||||
<Select v-model="filters.role" :options="[{ value: '', label: t('admin.users.allRoles') }, { value: 'admin', label: t('admin.users.admin') }, { value: 'user', label: t('admin.users.user') }]" @change="applyFilter" />
|
||||
<div class="w-32">
|
||||
<Select v-model="params.role" :options="[{ value: '', label: t('admin.users.allRoles') }, { value: 'admin', label: t('admin.users.admin') }, { value: 'user', label: t('admin.users.user') }]" @change="reload" />
|
||||
</div>
|
||||
<div v-if="visibleFilters.has('status')" class="w-32">
|
||||
<Select v-model="filters.status" :options="[{ value: '', label: t('admin.users.allStatus') }, { value: 'active', label: t('common.active') }, { value: 'disabled', label: t('admin.users.disabled') }]" @change="applyFilter" />
|
||||
<div class="w-32">
|
||||
<Select v-model="params.status" :options="[{ value: '', label: t('admin.users.allStatus') }, { value: 'active', label: t('common.active') }, { value: 'disabled', label: t('admin.users.disabled') }]" @change="reload" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-center gap-3">
|
||||
<button @click="loadUsers" :disabled="loading" class="btn btn-secondary"><svg :class="['h-5 w-5', loading ? 'animate-spin' : '']" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99" /></svg></button>
|
||||
<button @click="load" :disabled="loading" class="btn btn-secondary"><svg :class="['h-5 w-5', loading ? 'animate-spin' : '']" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99" /></svg></button>
|
||||
<button @click="showCreateModal = true" class="btn btn-primary"><svg class="mr-2 h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M12 4.5v15m7.5-7.5h-15" /></svg>{{ t('admin.users.createUser') }}</button>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template #table>
|
||||
<DataTable :columns="columns" :data="users" :loading="loading" :actions-count="7">
|
||||
<DataTable :columns="columns" :data="users" :loading="loading">
|
||||
<template #cell-email="{ value }"><div class="flex items-center gap-2"><div class="flex h-8 w-8 items-center justify-center rounded-full bg-primary-100 font-medium text-primary-700"><span>{{ value.charAt(0).toUpperCase() }}</span></div><span class="font-medium text-gray-900 dark:text-white">{{ value }}</span></div></template>
|
||||
<template #cell-role="{ value }"><span :class="['badge', value === 'admin' ? 'badge-purple' : 'badge-gray']">{{ t('admin.users.roles.' + value) }}</span></template>
|
||||
<template #cell-balance="{ value }"><span class="font-medium">${{ value.toFixed(2) }}</span></template>
|
||||
<template #cell-status="{ value }"><div class="flex items-center gap-1.5"><span :class="['h-2 w-2 rounded-full', value === 'active' ? 'bg-green-500' : 'bg-red-500']"></span><span class="text-sm">{{ t('admin.accounts.status.' + (value === 'disabled' ? 'inactive' : value)) }}</span></div></template>
|
||||
<template #cell-actions="{ row }"><div class="flex items-center gap-1"><button @click="handleEdit(row)" class="btn btn-sm btn-secondary">{{ t('common.edit') }}</button><button @click="openActionMenu(row, $event)" class="btn btn-sm btn-secondary">{{ t('common.more') }}</button></div></template>
|
||||
<template #cell-status="{ value }"><StatusBadge :status="value === 'disabled' ? 'inactive' : value" :label="t('admin.accounts.status.' + (value === 'disabled' ? 'inactive' : value))" /></template>
|
||||
<template #cell-actions="{ row }"><div class="flex gap-1"><button @click="handleEdit(row)" class="btn btn-sm btn-secondary">{{ t('common.edit') }}</button><button @click="openActionMenu(row, $event)" class="btn btn-sm btn-secondary">{{ t('common.more') }}</button></div></template>
|
||||
</DataTable>
|
||||
</template>
|
||||
|
||||
<template #pagination>
|
||||
<Pagination v-if="pagination.total > 0" :page="pagination.page" :total="pagination.total" :page-size="pagination.page_size" @update:page="handlePageChange" @update:pageSize="handlePageSizeChange" />
|
||||
<Pagination v-if="pagination.total > 0" :page="pagination.page" :total="pagination.total" :page-size="pagination.page_size" @update:page="handlePageChange" />
|
||||
</template>
|
||||
</TablePageLayout>
|
||||
|
||||
@@ -46,6 +45,7 @@
|
||||
<button @click="handleAllowedGroups(user); closeActionMenu()" class="flex w-full items-center gap-2 px-4 py-2 text-sm hover:bg-gray-100">{{ t('admin.users.groups') }}</button>
|
||||
<button @click="handleDeposit(user); closeActionMenu()" class="flex w-full items-center gap-2 px-4 py-2 text-sm hover:bg-gray-100 text-emerald-600">{{ t('admin.users.deposit') }}</button>
|
||||
<button @click="handleWithdraw(user); closeActionMenu()" class="flex w-full items-center gap-2 px-4 py-2 text-sm hover:bg-gray-100 text-amber-600">{{ t('admin.users.withdraw') }}</button>
|
||||
<button v-if="user.role !== 'admin'" @click="handleToggleStatus(user); closeActionMenu()" class="flex w-full items-center gap-2 px-4 py-2 text-sm hover:bg-gray-100">{{ user.status === 'active' ? t('admin.users.disable') : t('admin.users.enable') }}</button>
|
||||
<button v-if="user.role !== 'admin'" @click="handleDelete(user); closeActionMenu()" class="flex w-full items-center gap-2 px-4 py-2 text-sm text-red-600 hover:bg-red-50">{{ t('common.delete') }}</button>
|
||||
</template>
|
||||
</template>
|
||||
@@ -54,23 +54,23 @@
|
||||
</Teleport>
|
||||
|
||||
<ConfirmDialog :show="showDeleteDialog" :title="t('admin.users.deleteUser')" :message="t('admin.users.deleteConfirm', { email: deletingUser?.email })" :danger="true" @confirm="confirmDelete" @cancel="showDeleteDialog = false" />
|
||||
<UserCreateModal :show="showCreateModal" @close="showCreateModal = false" @success="loadUsers" />
|
||||
<UserEditModal :show="showEditModal" :user="editingUser" @close="closeEditModal" @success="loadUsers" />
|
||||
<UserCreateModal :show="showCreateModal" @close="showCreateModal = false" @success="reload" />
|
||||
<UserEditModal :show="showEditModal" :user="editingUser" @close="closeEditModal" @success="load" />
|
||||
<UserApiKeysModal :show="showApiKeysModal" :user="viewingUser" @close="closeApiKeysModal" />
|
||||
<UserAllowedGroupsModal :show="showAllowedGroupsModal" :user="allowedGroupsUser" @close="closeAllowedGroupsModal" @success="loadUsers" />
|
||||
<UserBalanceModal :show="showBalanceModal" :user="balanceUser" :operation="balanceOperation" @close="closeBalanceModal" @success="loadUsers" />
|
||||
<UserAttributesConfigModal :show="showAttributesModal" @close="handleAttributesModalClose" />
|
||||
<UserAllowedGroupsModal :show="showAllowedGroupsModal" :user="allowedGroupsUser" @close="closeAllowedGroupsModal" @success="load" />
|
||||
<UserBalanceModal :show="showBalanceModal" :user="balanceUser" :operation="balanceOperation" @close="closeBalanceModal" @success="load" />
|
||||
</AppLayout>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, computed, onMounted } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'; import { useAppStore } from '@/stores/app'; import { formatDateTime } from '@/utils/format'
|
||||
import { adminAPI } from '@/api/admin'; import type { User } from '@/types'
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'; import { useAppStore } from '@/stores/app'
|
||||
import { adminAPI } from '@/api/admin'; import { useTableLoader } from '@/composables/useTableLoader'
|
||||
import type { User } from '@/types'
|
||||
import AppLayout from '@/components/layout/AppLayout.vue'; import TablePageLayout from '@/components/layout/TablePageLayout.vue'
|
||||
import DataTable from '@/components/common/DataTable.vue'; import Pagination from '@/components/common/Pagination.vue'
|
||||
import ConfirmDialog from '@/components/common/ConfirmDialog.vue'; import Select from '@/components/common/Select.vue'
|
||||
import UserAttributesConfigModal from '@/components/user/UserAttributesConfigModal.vue'
|
||||
import SearchInput from '@/components/common/SearchInput.vue'; import StatusBadge from '@/components/common/StatusBadge.vue'
|
||||
import UserCreateModal from '@/components/admin/user/UserCreateModal.vue'
|
||||
import UserEditModal from '@/components/admin/user/UserEditModal.vue'
|
||||
import UserApiKeysModal from '@/components/admin/user/UserApiKeysModal.vue'
|
||||
@@ -78,27 +78,14 @@ import UserAllowedGroupsModal from '@/components/admin/user/UserAllowedGroupsMod
|
||||
import UserBalanceModal from '@/components/admin/user/UserBalanceModal.vue'
|
||||
|
||||
const { t } = useI18n(); const appStore = useAppStore()
|
||||
const users = ref<User[]>([]); const loading = ref(false); const searchQuery = ref('')
|
||||
const filters = reactive({ role: '', status: '' }); const visibleFilters = reactive<Set<string>>(new Set(['role', 'status']))
|
||||
const pagination = reactive({ page: 1, page_size: 20, total: 0 })
|
||||
const showCreateModal = ref(false); const showEditModal = ref(false); const showDeleteDialog = ref(false); const showApiKeysModal = ref(false); const showAttributesModal = ref(false)
|
||||
const { items: users, loading, params, pagination, load, reload, handlePageChange } = useTableLoader<User, any>({ fetchFn: adminAPI.users.list, initialParams: { role: '', status: '', search: '' } })
|
||||
|
||||
const showCreateModal = ref(false); const showEditModal = ref(false); const showDeleteDialog = ref(false); const showApiKeysModal = ref(false)
|
||||
const editingUser = ref<User | null>(null); const deletingUser = ref<User | null>(null); const viewingUser = ref<User | null>(null)
|
||||
const activeMenuId = ref<number | null>(null); const menuPosition = ref<{ top: number; left: number } | null>(null)
|
||||
const showAllowedGroupsModal = ref(false); const allowedGroupsUser = ref<User | null>(null); const showBalanceModal = ref(false); const balanceUser = ref<User | null>(null); const balanceOperation = ref<'add' | 'subtract'>('add')
|
||||
|
||||
const columns = computed(() => [{ key: 'email', label: t('admin.users.columns.user'), sortable: true }, { key: 'role', label: t('admin.users.columns.role'), sortable: true }, { key: 'balance', label: t('admin.users.columns.balance'), sortable: true }, { key: 'status', label: t('admin.users.columns.status'), sortable: true }, { key: 'actions', label: t('admin.users.columns.actions') }])
|
||||
|
||||
const loadUsers = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await adminAPI.users.list(pagination.page, pagination.page_size, { role: filters.role as any, status: filters.status as any, search: searchQuery.value || undefined })
|
||||
users.value = res.items; pagination.total = res.total
|
||||
} catch {} finally { loading.value = false }
|
||||
}
|
||||
const handleSearch = () => { pagination.page = 1; loadUsers() }
|
||||
const handlePageChange = (p: number) => { pagination.page = p; loadUsers() }
|
||||
const handlePageSizeChange = (s: number) => { pagination.page_size = s; pagination.page = 1; loadUsers() }
|
||||
const applyFilter = () => { pagination.page = 1; loadUsers() }
|
||||
const handleEdit = (u: User) => { editingUser.value = u; showEditModal.value = true }
|
||||
const closeEditModal = () => { showEditModal.value = false; editingUser.value = null }
|
||||
const handleViewApiKeys = (u: User) => { viewingUser.value = u; showApiKeysModal.value = true }
|
||||
@@ -106,19 +93,12 @@ const closeApiKeysModal = () => { showApiKeysModal.value = false; viewingUser.va
|
||||
const handleAllowedGroups = (u: User) => { allowedGroupsUser.value = u; showAllowedGroupsModal.value = true }
|
||||
const closeAllowedGroupsModal = () => { showAllowedGroupsModal.value = false; allowedGroupsUser.value = null }
|
||||
const handleDelete = (u: User) => { deletingUser.value = u; showDeleteDialog.value = true }
|
||||
const confirmDelete = async () => { if (!deletingUser.value) return; try { await adminAPI.users.delete(deletingUser.value.id); appStore.showSuccess(t('common.success')); showDeleteDialog.value = false; loadUsers() } catch {} }
|
||||
const confirmDelete = async () => { if (!deletingUser.value) return; try { await adminAPI.users.delete(deletingUser.value.id); appStore.showSuccess(t('common.success')); showDeleteDialog.value = false; reload() } catch {} }
|
||||
const handleDeposit = (u: User) => { balanceUser.value = u; balanceOperation.value = 'add'; showBalanceModal.value = true }
|
||||
const handleWithdraw = (u: User) => { balanceUser.value = u; balanceOperation.value = 'subtract'; showBalanceModal.value = true }
|
||||
const closeBalanceModal = () => { showBalanceModal.value = false; balanceUser.value = null }
|
||||
const handleAttributesModalClose = () => { showAttributesModal.value = false; loadUsers() }
|
||||
const getAttributeDefinitionName = (id: number) => String(id)
|
||||
const getAttributeDefinition = (id: number) => ({} as any)
|
||||
|
||||
const openActionMenu = (u: User, e: MouseEvent) => {
|
||||
if (activeMenuId.value === u.id) { activeMenuId.value = null; menuPosition.value = null }
|
||||
else { activeMenuId.value = u.id; menuPosition.value = { top: e.clientY, left: e.clientX - 150 } }
|
||||
}
|
||||
const handleToggleStatus = async (user: User) => { const next = user.status === 'active' ? 'disabled' : 'active'; try { await adminAPI.users.toggleStatus(user.id, next as any); appStore.showSuccess(t('common.success')); load() } catch {} }
|
||||
const openActionMenu = (u: User, e: MouseEvent) => { if (activeMenuId.value === u.id) { activeMenuId.value = null; menuPosition.value = null } else { activeMenuId.value = u.id; menuPosition.value = { top: e.clientY, left: e.clientX - 150 } } }
|
||||
const closeActionMenu = () => { activeMenuId.value = null; menuPosition.value = null }
|
||||
|
||||
onMounted(loadUsers)
|
||||
onMounted(load)
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user