admin: add create-user dialog and API
Some checks failed
build quyun / Build (push) Failing after 1m23s

This commit is contained in:
2025-12-22 15:57:32 +08:00
parent 4704cf6949
commit 859d628cd6
5 changed files with 139 additions and 0 deletions

View File

@@ -11,6 +11,12 @@ export const userService = {
}
});
},
createUser({ phone, username } = {}) {
return httpClient.post('/users', {
phone,
username
});
},
searchUser(id) {
return httpClient.get(`/users/${id}`);
},

View File

@@ -49,6 +49,11 @@ const phoneSaving = ref(false);
const phoneTargetUser = ref(null);
const phoneInput = ref('');
const createDialogVisible = ref(false);
const createSaving = ref(false);
const createPhoneInput = ref('');
const createUsernameInput = ref('');
const articlesDialogVisible = ref(false);
const articlesLoading = ref(false);
const articlesUser = ref(null);
@@ -130,6 +135,12 @@ const openPhoneDialog = (user) => {
const normalizePhone = (v) => v.toString().replace(/\D/g, '').slice(0, 11);
const openCreateDialog = () => {
createPhoneInput.value = '';
createUsernameInput.value = '';
createDialogVisible.value = true;
};
const formatMoney = (cents) => `¥${(cents / 100).toFixed(2)}`;
const formatBoughtPrice = (priceCents) => {
@@ -193,6 +204,32 @@ const savePhone = async () => {
}
};
const createUser = async () => {
const phone = normalizePhone(createPhoneInput.value);
if (phone.length !== 11) {
toast.add({ severity: 'error', summary: '错误', detail: '手机号必须为 11 位数字', life: 3000 });
return;
}
createSaving.value = true;
try {
const username = createUsernameInput.value.trim();
await userService.createUser({
phone,
...(username ? { username } : {})
});
toast.add({ severity: 'success', summary: '成功', detail: '用户已创建', life: 3000 });
createDialogVisible.value = false;
first.value = 0;
await fetchUsers();
} catch (error) {
console.error('Failed to create user:', error);
toast.add({ severity: 'error', summary: '错误', detail: error?.response?.data?.message || '创建用户失败', life: 3000 });
} finally {
createSaving.value = false;
}
};
onMounted(() => {
fetchUsers();
});
@@ -270,9 +307,31 @@ const onOnlyBoughtChange = () => {
</template>
</Dialog>
<Dialog v-model:visible="createDialogVisible" modal header="添加用户" :style="{ width: '420px' }">
<div class="space-y-3">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">手机号必填</label>
<InputText v-model="createPhoneInput" class="w-full" placeholder="请输入 11 位手机号" inputmode="numeric"
maxlength="11" @input="(e) => createPhoneInput = normalizePhone(e.target.value)" />
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">用户昵称可选</label>
<InputText v-model="createUsernameInput" class="w-full" placeholder="请输入用户昵称" />
</div>
</div>
<template #footer>
<div class="flex justify-end gap-2">
<Button label="取消" text @click="createDialogVisible = false" />
<Button label="创建" severity="success" :loading="createSaving" @click="createUser" />
</div>
</template>
</Dialog>
<div class="w-full">
<div class="flex justify-between items-center mb-6">
<h1 class="text-2xl font-semibold text-gray-800">用户列表</h1>
<Button label="添加用户" icon="pi pi-plus" severity="success" @click="openCreateDialog" />
</div>
<div class="card">