import { computed, ref } from 'vue'; import { FormSchema } from '@/components/Form'; import { defRangeShortcuts } from '@/utils/dateUtil'; import { useDictStore } from '@/store/modules/dict'; import { List as TenantList } from '@/api/imTenant'; const dict = useDictStore(); export const tenantOptions = ref([]); export class State { public id = ''; public tenantId = ''; public tenantName = ''; public batchId = ''; public code = ''; public status = ''; public expiresAt = ''; public maxUses = 0; public usageCount = 0; public boundUserId = ''; public boundUserLabel = ''; public lastUsedAt = ''; public createdBy = ''; public remark = ''; public createdAt = ''; } export const statusOptions = [ { label: '有效', value: 'active', type: 'success' }, { label: '已过期', value: 'expired', type: 'warning' }, { label: '已撤销', value: 'revoked', type: 'error' }, ]; export const schemas = ref([ { field: 'tenantId', component: 'NSelect', label: '商户', componentProps: { clearable: true, options: computed(() => tenantOptions.value), placeholder: '请选择商户', }, }, { field: 'inviteCode', component: 'NInput', label: '邀请码', componentProps: { placeholder: '完整5位,区分大小写', maxlength: 5, }, }, { field: 'status', component: 'NSelect', label: '状态', componentProps: { clearable: true, options: statusOptions, placeholder: '请选择状态', }, }, { field: 'createdBy', component: 'NInput', label: '创建人', componentProps: { placeholder: '请输入创建人ID', }, }, { field: 'createdAt', component: 'NDatePicker', label: '创建时间', componentProps: { type: 'datetimerange', clearable: true, shortcuts: defRangeShortcuts(), }, }, { field: 'expiresAt', component: 'NDatePicker', label: '到期时间', componentProps: { type: 'datetimerange', clearable: true, shortcuts: defRangeShortcuts(), }, }, ]); export const columns = [ { title: 'ID', key: 'id', align: 'left', width: 150 }, { title: '商户', key: 'tenantName', align: 'left', width: 160 }, { title: '邀请码', key: 'code', align: 'left', width: 120 }, { title: '状态', key: 'status', align: 'left', width: 110 }, { title: '到期时间', key: 'expiresAt', align: 'left', width: 190 }, { title: '累计使用次数', key: 'usageCount', align: 'left', width: 130 }, { title: '可用次数', key: 'availableUses', align: 'left', width: 110, render(row: State) { return inviteAvailableUsesLabel(row); }, }, { title: '绑定用户', key: 'boundUserLabel', align: 'left', width: 180, render(row: State) { return row.boundUserLabel || row.boundUserId || '--'; }, }, { title: '最后使用时间', key: 'lastUsedAt', align: 'left', width: 190 }, { title: '创建人', key: 'createdBy', align: 'left', width: 110 }, { title: '创建时间', key: 'createdAt', align: 'left', width: 190 }, { title: '备注', key: 'remark', align: 'left', width: 220 }, ]; export function loadOptions() { dict.loadOptions(['sys_switch']); } export async function loadTenantOptions() { const res = await TenantList({ page: 1, perPage: 20, state: 1 }); tenantOptions.value = (res?.list || []).map((item) => ({ label: `${item.name}(${item.code})`, value: item.id, })); return tenantOptions.value; } export function statusLabel(status: string) { return statusOptions.find((item) => item.value === status)?.label || status; } export function inviteAvailableUsesLabel(row: State) { if (!row.maxUses || Number(row.maxUses) <= 0) { return '不限制'; } return `${Math.max(Number(row.maxUses) - Number(row.usageCount || 0), 0)}`; } export function localRfc3339(value: number | null) { if (!value) { return ''; } const date = new Date(value); const pad = (n: number) => `${n}`.padStart(2, '0'); const offset = -date.getTimezoneOffset(); const sign = offset >= 0 ? '+' : '-'; const hours = pad(Math.floor(Math.abs(offset) / 60)); const minutes = pad(Math.abs(offset) % 60); return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad( date.getHours() )}:${pad(date.getMinutes())}:${pad(date.getSeconds())}${sign}${hours}:${minutes}`; }