This commit is contained in:
183
src/composables/useGateway.ts
Normal file
183
src/composables/useGateway.ts
Normal file
@@ -0,0 +1,183 @@
|
||||
import { computed, onBeforeUnmount, onMounted, reactive, ref, shallowRef, watch } from 'vue'
|
||||
import { GatewayService } from '@/api/modules/gateway'
|
||||
import { CarrierService } from '@/api/modules/carrier'
|
||||
import type { GatewayOptions } from '@/types/api/gateway'
|
||||
import type { PaginationParams, PaginationResponse } from '@/types/api/common'
|
||||
import {
|
||||
gatewayData,
|
||||
gatewayError,
|
||||
gatewayLabel,
|
||||
requireGatewayAccess
|
||||
} from '@/utils/business/gateway'
|
||||
|
||||
export function useGatewayOptions() {
|
||||
const options = ref<GatewayOptions>({
|
||||
error_categories: [],
|
||||
exempt_sources: [],
|
||||
gateway_operations: [],
|
||||
match_types: [],
|
||||
rate_limit_modes: [],
|
||||
stop_resume_actions: [],
|
||||
stop_resume_results: []
|
||||
})
|
||||
const error = ref('')
|
||||
const loading = ref(false)
|
||||
const ready = ref(false)
|
||||
let active = true
|
||||
async function load() {
|
||||
if (loading.value) return
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const data = gatewayData(await GatewayService.getOptions())
|
||||
if (!active) return
|
||||
// 不用空数组掩盖异常契约。
|
||||
for (const key of Object.keys(options.value) as (keyof GatewayOptions)[]) {
|
||||
if (!Array.isArray(data[key])) throw new Error(`枚举响应缺少 ${key}`)
|
||||
}
|
||||
options.value = data
|
||||
ready.value = true
|
||||
} catch (cause) {
|
||||
if (active) {
|
||||
error.value = gatewayError(cause)
|
||||
ready.value = false
|
||||
}
|
||||
} finally {
|
||||
if (active) loading.value = false
|
||||
}
|
||||
}
|
||||
onMounted(load)
|
||||
onBeforeUnmount(() => {
|
||||
active = false
|
||||
})
|
||||
const label = (key: keyof GatewayOptions, value?: string | null) =>
|
||||
gatewayLabel(options.value[key], value)
|
||||
return { options, error, loading, ready, load, label }
|
||||
}
|
||||
|
||||
export function useGatewayList<T>(
|
||||
request: (pagination: PaginationParams) => Promise<PaginationResponse<T>>,
|
||||
allowed: () => boolean
|
||||
) {
|
||||
const rows = shallowRef<T[]>([])
|
||||
const total = ref(0)
|
||||
const loading = ref(false)
|
||||
const error = ref('')
|
||||
const pagination = reactive({ page: 1, page_size: 20 })
|
||||
let sequence = 0
|
||||
const canRead = computed(allowed)
|
||||
async function load() {
|
||||
const current = ++sequence
|
||||
error.value = ''
|
||||
loading.value = true
|
||||
try {
|
||||
requireGatewayAccess(canRead.value)
|
||||
const data = gatewayData(await request({ ...pagination }))
|
||||
if (current !== sequence) return
|
||||
rows.value = data.items || []
|
||||
total.value = data.total
|
||||
pagination.page = data.page
|
||||
pagination.page_size = data.size
|
||||
} catch (cause) {
|
||||
if (current !== sequence) return
|
||||
rows.value = []
|
||||
total.value = 0
|
||||
error.value = gatewayError(cause)
|
||||
} finally {
|
||||
if (current === sequence) loading.value = false
|
||||
}
|
||||
}
|
||||
function search() {
|
||||
pagination.page = 1
|
||||
return load()
|
||||
}
|
||||
function resize() {
|
||||
pagination.page = 1
|
||||
return load()
|
||||
}
|
||||
function clear() {
|
||||
++sequence
|
||||
rows.value = []
|
||||
total.value = 0
|
||||
error.value = ''
|
||||
loading.value = false
|
||||
}
|
||||
onBeforeUnmount(clear)
|
||||
watch(canRead, (allowed) => {
|
||||
clear()
|
||||
if (allowed) void load()
|
||||
else error.value = '无权查看此页面'
|
||||
})
|
||||
return { rows, total, pagination, loading, error, load, search, resize, clear, canRead }
|
||||
}
|
||||
|
||||
export function useGatewayColumns<T extends { prop: string; label: string }>(definitions: T[]) {
|
||||
const columnChecks = ref(definitions.map((column) => ({ ...column, checked: true })))
|
||||
const showColumn = (prop: string) =>
|
||||
columnChecks.value.some((column) => column.prop === prop && column.checked)
|
||||
const columns = computed(() =>
|
||||
columnChecks.value
|
||||
.filter((column) => column.checked && column.prop !== 'operation')
|
||||
.map((column) => definitions.find((definition) => definition.prop === column.prop)!)
|
||||
)
|
||||
return { columnChecks, showColumn, columns }
|
||||
}
|
||||
|
||||
/** 所有网关页面共用按运营商名称搜索的账户候选,不把名称提交成 carrier_id。 */
|
||||
export function useGatewayCarriers(loadOnMount = true) {
|
||||
const candidates = ref<{ value: number; label: string }[]>([])
|
||||
const remembered = ref<{ value: number; label: string }[]>([])
|
||||
const loading = ref(false)
|
||||
const error = ref('')
|
||||
let sequence = 0
|
||||
function remember(id: number | null | undefined, name?: string) {
|
||||
if (id == null || !name) return
|
||||
remembered.value = [
|
||||
...remembered.value.filter((option) => option.value !== id),
|
||||
{ value: id, label: name }
|
||||
]
|
||||
}
|
||||
function select(value: unknown) {
|
||||
const id = Number(value)
|
||||
if (value == null || value === '' || !Number.isSafeInteger(id)) return
|
||||
const selected = options.value.find((option) => option.value === id)
|
||||
if (selected) remember(selected.value, selected.label)
|
||||
}
|
||||
const options = computed(() => {
|
||||
const values = new Map(remembered.value.map((option) => [option.value, option]))
|
||||
candidates.value.forEach((option) => values.set(option.value, option))
|
||||
return [...values.values()]
|
||||
})
|
||||
async function search(query = '') {
|
||||
const current = ++sequence
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const data = gatewayData(
|
||||
await CarrierService.getCarriers({ page: 1, page_size: 20, carrier_name: query.trim() })
|
||||
)
|
||||
if (current !== sequence) return
|
||||
if (!Array.isArray(data.items)) throw new Error('运营商账户响应缺少 items')
|
||||
candidates.value = data.items.map((carrier) => ({
|
||||
value: carrier.id,
|
||||
label: carrier.carrier_name
|
||||
}))
|
||||
} catch (cause) {
|
||||
if (current === sequence) {
|
||||
error.value = gatewayError(cause)
|
||||
candidates.value = []
|
||||
}
|
||||
} finally {
|
||||
if (current === sequence) loading.value = false
|
||||
}
|
||||
}
|
||||
const label = (id: number) =>
|
||||
options.value.find((option) => option.value === id)?.label || `运营商账户(${id})`
|
||||
onMounted(() => {
|
||||
if (loadOnMount) void search()
|
||||
})
|
||||
onBeforeUnmount(() => {
|
||||
++sequence
|
||||
})
|
||||
return { options, loading, error, search, remember, select, label }
|
||||
}
|
||||
Reference in New Issue
Block a user