feat: add employee workspace operations admin
This commit is contained in:
@@ -0,0 +1,603 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive, ref } from "vue";
|
||||
import {
|
||||
ElMessage,
|
||||
ElMessageBox,
|
||||
type FormInstance,
|
||||
type FormRules
|
||||
} from "element-plus";
|
||||
import {
|
||||
cancelShift,
|
||||
createShift,
|
||||
listAllStores,
|
||||
listEmployeeOptions,
|
||||
listShifts,
|
||||
updateShift,
|
||||
type Employee,
|
||||
type Shift,
|
||||
type ShiftPayload,
|
||||
type ShiftStatus,
|
||||
type Store
|
||||
} from "@/api/access";
|
||||
import { hasMenuAction, hasPerms } from "@/utils/auth";
|
||||
|
||||
import Plus from "~icons/ep/plus";
|
||||
import Search from "~icons/ep/search";
|
||||
import Refresh from "~icons/ep/refresh";
|
||||
import EditPen from "~icons/ep/edit-pen";
|
||||
import CircleClose from "~icons/ep/circle-close";
|
||||
|
||||
defineOptions({
|
||||
name: "ShiftManagement"
|
||||
});
|
||||
|
||||
type ShiftFormState = ShiftPayload & {
|
||||
id?: number;
|
||||
};
|
||||
|
||||
const tableLoading = ref(false);
|
||||
const optionLoading = ref(false);
|
||||
const submitLoading = ref(false);
|
||||
const dialogVisible = ref(false);
|
||||
const formRef = ref<FormInstance>();
|
||||
const shifts = ref<Shift[]>([]);
|
||||
const stores = ref<Store[]>([]);
|
||||
const employees = ref<Employee[]>([]);
|
||||
|
||||
const query = reactive({
|
||||
storeId: undefined as number | undefined,
|
||||
employeeId: undefined as number | undefined,
|
||||
dateRange: [] as string[],
|
||||
page: 1,
|
||||
pageSize: 20
|
||||
});
|
||||
|
||||
const pagination = reactive({
|
||||
total: 0,
|
||||
totalPages: 0
|
||||
});
|
||||
|
||||
const form = reactive<ShiftFormState>({
|
||||
storeId: undefined as unknown as number,
|
||||
employeeId: undefined as unknown as number,
|
||||
position: "",
|
||||
startAt: "",
|
||||
endAt: ""
|
||||
});
|
||||
|
||||
const rules: FormRules<ShiftFormState> = {
|
||||
storeId: [{ required: true, message: "请选择门店", trigger: "change" }],
|
||||
employeeId: [{ required: true, message: "请选择员工", trigger: "change" }],
|
||||
position: [
|
||||
{ required: true, message: "请输入岗位", trigger: "blur" },
|
||||
{ max: 50, message: "岗位不能超过 50 个字符", trigger: "blur" }
|
||||
],
|
||||
startAt: [{ required: true, message: "请选择开始时间", trigger: "change" }],
|
||||
endAt: [{ required: true, message: "请选择结束时间", trigger: "change" }]
|
||||
};
|
||||
|
||||
const dialogTitle = computed(() => (form.id ? "编辑排班" : "新增排班"));
|
||||
const canManage = computed(
|
||||
() => hasMenuAction("shifts", "manage") || hasPerms("shift:manage")
|
||||
);
|
||||
|
||||
const statusMap: Record<ShiftStatus, { label: string; type: string }> = {
|
||||
SCHEDULED: { label: "已排班", type: "success" },
|
||||
CANCELLED: { label: "已取消", type: "danger" },
|
||||
COMPLETED: { label: "已完成", type: "info" }
|
||||
};
|
||||
|
||||
function getErrorMessage(error: unknown, fallback: string) {
|
||||
const message = (
|
||||
error as { response?: { data?: { error?: { message?: string } } } }
|
||||
)?.response?.data?.error?.message;
|
||||
|
||||
if (message) return message;
|
||||
if (error instanceof Error && error.message) return error.message;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function formatTime(value: string) {
|
||||
return new Date(value).toLocaleString("zh-CN", {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit"
|
||||
});
|
||||
}
|
||||
|
||||
function resetFormState() {
|
||||
Object.assign(form, {
|
||||
id: undefined,
|
||||
storeId: undefined,
|
||||
employeeId: undefined,
|
||||
position: "",
|
||||
startAt: "",
|
||||
endAt: ""
|
||||
});
|
||||
formRef.value?.clearValidate();
|
||||
}
|
||||
|
||||
function buildPayload(): ShiftPayload {
|
||||
return {
|
||||
storeId: form.storeId,
|
||||
employeeId: form.employeeId,
|
||||
position: form.position.trim(),
|
||||
startAt: form.startAt,
|
||||
endAt: form.endAt
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchOptions(storeId?: number) {
|
||||
optionLoading.value = true;
|
||||
try {
|
||||
const [storeResult, employeeResult] = await Promise.all([
|
||||
listAllStores({ includeInactive: true }),
|
||||
listEmployeeOptions({ storeId })
|
||||
]);
|
||||
stores.value = storeResult.data;
|
||||
employees.value = employeeResult.data;
|
||||
} catch (error) {
|
||||
ElMessage.error(getErrorMessage(error, "加载门店和员工选项失败"));
|
||||
} finally {
|
||||
optionLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchShifts() {
|
||||
tableLoading.value = true;
|
||||
try {
|
||||
const result = await listShifts({
|
||||
storeId: query.storeId,
|
||||
employeeId: query.employeeId,
|
||||
startDate: query.dateRange?.[0],
|
||||
endDate: query.dateRange?.[1],
|
||||
page: query.page,
|
||||
pageSize: query.pageSize
|
||||
});
|
||||
shifts.value = result.data.items;
|
||||
pagination.total = result.data.pagination.total;
|
||||
pagination.totalPages = result.data.pagination.totalPages;
|
||||
} catch (error) {
|
||||
ElMessage.error(getErrorMessage(error, "加载排班列表失败"));
|
||||
} finally {
|
||||
tableLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function handleSearch() {
|
||||
query.page = 1;
|
||||
fetchShifts();
|
||||
}
|
||||
|
||||
function handleReset() {
|
||||
query.storeId = undefined;
|
||||
query.employeeId = undefined;
|
||||
query.dateRange = [];
|
||||
query.page = 1;
|
||||
query.pageSize = 20;
|
||||
fetchShifts();
|
||||
}
|
||||
|
||||
function handlePageChange(page: number) {
|
||||
query.page = page;
|
||||
fetchShifts();
|
||||
}
|
||||
|
||||
function handleSizeChange(pageSize: number) {
|
||||
query.page = 1;
|
||||
query.pageSize = pageSize;
|
||||
fetchShifts();
|
||||
}
|
||||
|
||||
function openCreateDialog() {
|
||||
if (!canManage.value) return;
|
||||
resetFormState();
|
||||
dialogVisible.value = true;
|
||||
fetchOptions();
|
||||
}
|
||||
|
||||
function openEditDialog(row: Shift) {
|
||||
if (!canManage.value) return;
|
||||
Object.assign(form, {
|
||||
id: row.id,
|
||||
storeId: row.storeId,
|
||||
employeeId: row.employeeId,
|
||||
position: row.position,
|
||||
startAt: row.startAt,
|
||||
endAt: row.endAt
|
||||
});
|
||||
dialogVisible.value = true;
|
||||
fetchOptions(row.storeId);
|
||||
formRef.value?.clearValidate();
|
||||
}
|
||||
|
||||
async function handleFormStoreChange(storeId: number) {
|
||||
form.employeeId = undefined as unknown as number;
|
||||
await fetchOptions(storeId);
|
||||
}
|
||||
|
||||
async function submitForm() {
|
||||
if (!canManage.value) return;
|
||||
await formRef.value?.validate();
|
||||
|
||||
if (new Date(form.startAt).getTime() >= new Date(form.endAt).getTime()) {
|
||||
ElMessage.warning("结束时间必须晚于开始时间");
|
||||
return;
|
||||
}
|
||||
|
||||
submitLoading.value = true;
|
||||
|
||||
try {
|
||||
const payload = buildPayload();
|
||||
|
||||
if (form.id) {
|
||||
await updateShift(form.id, payload);
|
||||
ElMessage.success("排班已更新");
|
||||
} else {
|
||||
await createShift(payload);
|
||||
ElMessage.success("排班已创建");
|
||||
}
|
||||
|
||||
dialogVisible.value = false;
|
||||
fetchShifts();
|
||||
} catch (error) {
|
||||
ElMessage.error(getErrorMessage(error, "保存排班失败"));
|
||||
} finally {
|
||||
submitLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function handleCancel(row: Shift) {
|
||||
if (!canManage.value) return;
|
||||
try {
|
||||
await ElMessageBox.confirm(
|
||||
`确认取消「${row.employeeName}」这条排班?`,
|
||||
"取消排班",
|
||||
{
|
||||
type: "warning",
|
||||
confirmButtonText: "取消排班",
|
||||
cancelButtonText: "关闭"
|
||||
}
|
||||
);
|
||||
await cancelShift(row.id);
|
||||
ElMessage.success("排班已取消");
|
||||
fetchShifts();
|
||||
} catch (error) {
|
||||
if (error !== "cancel") {
|
||||
ElMessage.error(getErrorMessage(error, "取消排班失败"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
await Promise.all([fetchOptions(), fetchShifts()]);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="operation-page">
|
||||
<div class="page-heading">
|
||||
<div>
|
||||
<p class="eyebrow">员工工作台运营</p>
|
||||
<h1>排班管理</h1>
|
||||
</div>
|
||||
<el-button
|
||||
v-if="canManage"
|
||||
type="primary"
|
||||
:icon="Plus"
|
||||
@click="openCreateDialog"
|
||||
>
|
||||
新增排班
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<div class="toolbar">
|
||||
<el-select
|
||||
v-model="query.storeId"
|
||||
clearable
|
||||
filterable
|
||||
placeholder="全部门店"
|
||||
class="toolbar-control"
|
||||
:loading="optionLoading"
|
||||
@change="handleSearch"
|
||||
>
|
||||
<el-option
|
||||
v-for="store in stores"
|
||||
:key="store.id"
|
||||
:label="store.name"
|
||||
:value="store.id"
|
||||
/>
|
||||
</el-select>
|
||||
<el-select
|
||||
v-model="query.employeeId"
|
||||
clearable
|
||||
filterable
|
||||
placeholder="全部员工"
|
||||
class="toolbar-control"
|
||||
:loading="optionLoading"
|
||||
@change="handleSearch"
|
||||
>
|
||||
<el-option
|
||||
v-for="employee in employees"
|
||||
:key="employee.id"
|
||||
:label="`${employee.name}(${employee.phone})`"
|
||||
:value="employee.id"
|
||||
/>
|
||||
</el-select>
|
||||
<el-date-picker
|
||||
v-model="query.dateRange"
|
||||
type="daterange"
|
||||
value-format="YYYY-MM-DD"
|
||||
range-separator="至"
|
||||
start-placeholder="开始日期"
|
||||
end-placeholder="结束日期"
|
||||
class="date-range"
|
||||
@change="handleSearch"
|
||||
/>
|
||||
<div class="toolbar-actions">
|
||||
<el-button type="primary" :icon="Search" @click="handleSearch">
|
||||
查询
|
||||
</el-button>
|
||||
<el-button :icon="Refresh" @click="handleReset">重置</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="table-shell">
|
||||
<el-table v-loading="tableLoading" :data="shifts" row-key="id" stripe>
|
||||
<el-table-column prop="storeName" label="门店" min-width="160" />
|
||||
<el-table-column prop="employeeName" label="员工" min-width="140" />
|
||||
<el-table-column prop="position" label="岗位" min-width="120" />
|
||||
<el-table-column label="开始时间" width="180">
|
||||
<template #default="{ row }">{{ formatTime(row.startAt) }}</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="结束时间" width="180">
|
||||
<template #default="{ row }">{{ formatTime(row.endAt) }}</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="状态" width="110">
|
||||
<template #default="{ row }">
|
||||
<el-tag :type="statusMap[row.status].type" effect="light">
|
||||
{{ statusMap[row.status].label }}
|
||||
</el-tag>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column
|
||||
v-if="canManage"
|
||||
label="操作"
|
||||
width="170"
|
||||
fixed="right"
|
||||
>
|
||||
<template #default="{ row }">
|
||||
<el-button
|
||||
link
|
||||
type="primary"
|
||||
:icon="EditPen"
|
||||
@click="openEditDialog(row)"
|
||||
>
|
||||
编辑
|
||||
</el-button>
|
||||
<el-button
|
||||
v-if="row.status === 'SCHEDULED'"
|
||||
link
|
||||
type="warning"
|
||||
:icon="CircleClose"
|
||||
@click="handleCancel(row)"
|
||||
>
|
||||
取消
|
||||
</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
||||
<div class="pagination-row">
|
||||
<span
|
||||
>共 {{ pagination.total }} 条,{{ pagination.totalPages }} 页</span
|
||||
>
|
||||
<el-pagination
|
||||
v-model:current-page="query.page"
|
||||
v-model:page-size="query.pageSize"
|
||||
background
|
||||
layout="sizes, prev, pager, next"
|
||||
:total="pagination.total"
|
||||
:page-sizes="[10, 20, 50, 100]"
|
||||
@current-change="handlePageChange"
|
||||
@size-change="handleSizeChange"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<el-dialog
|
||||
v-model="dialogVisible"
|
||||
:title="dialogTitle"
|
||||
width="560px"
|
||||
destroy-on-close
|
||||
>
|
||||
<el-form ref="formRef" :model="form" :rules="rules" label-position="top">
|
||||
<div class="form-grid">
|
||||
<el-form-item label="门店" prop="storeId">
|
||||
<el-select
|
||||
v-model="form.storeId"
|
||||
filterable
|
||||
class="full-width"
|
||||
:loading="optionLoading"
|
||||
@change="handleFormStoreChange"
|
||||
>
|
||||
<el-option
|
||||
v-for="store in stores"
|
||||
:key="store.id"
|
||||
:label="store.name"
|
||||
:value="store.id"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="员工" prop="employeeId">
|
||||
<el-select
|
||||
v-model="form.employeeId"
|
||||
filterable
|
||||
class="full-width"
|
||||
:loading="optionLoading"
|
||||
>
|
||||
<el-option
|
||||
v-for="employee in employees"
|
||||
:key="employee.id"
|
||||
:label="`${employee.name}(${employee.phone})`"
|
||||
:value="employee.id"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
</div>
|
||||
<el-form-item label="岗位" prop="position">
|
||||
<el-input
|
||||
v-model="form.position"
|
||||
maxlength="50"
|
||||
placeholder="请输入"
|
||||
/>
|
||||
</el-form-item>
|
||||
<div class="form-grid">
|
||||
<el-form-item label="开始时间" prop="startAt">
|
||||
<el-date-picker
|
||||
v-model="form.startAt"
|
||||
type="datetime"
|
||||
value-format="YYYY-MM-DD HH:mm:ss"
|
||||
class="full-width"
|
||||
placeholder="请选择开始时间"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="结束时间" prop="endAt">
|
||||
<el-date-picker
|
||||
v-model="form.endAt"
|
||||
type="datetime"
|
||||
value-format="YYYY-MM-DD HH:mm:ss"
|
||||
class="full-width"
|
||||
placeholder="请选择结束时间"
|
||||
/>
|
||||
</el-form-item>
|
||||
</div>
|
||||
</el-form>
|
||||
|
||||
<template #footer>
|
||||
<el-button @click="dialogVisible = false">取消</el-button>
|
||||
<el-button type="primary" :loading="submitLoading" @click="submitForm">
|
||||
保存
|
||||
</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.operation-page {
|
||||
min-height: 100%;
|
||||
padding: 20px;
|
||||
background: #f6f8fb;
|
||||
}
|
||||
|
||||
.page-heading {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 16px;
|
||||
|
||||
h1 {
|
||||
margin: 4px 0 0;
|
||||
font-size: 24px;
|
||||
font-weight: 650;
|
||||
color: #111827;
|
||||
letter-spacing: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.eyebrow {
|
||||
margin: 0;
|
||||
font-size: 13px;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.toolbar,
|
||||
.table-shell {
|
||||
background: #fff;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.toolbar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
padding: 14px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.toolbar-control {
|
||||
width: 180px;
|
||||
}
|
||||
|
||||
.date-range {
|
||||
width: 300px;
|
||||
}
|
||||
|
||||
.toolbar-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
margin-left: auto;
|
||||
}
|
||||
|
||||
.table-shell {
|
||||
padding: 8px 0 14px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.pagination-row {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 14px 16px 0;
|
||||
font-size: 13px;
|
||||
color: #64748b;
|
||||
}
|
||||
|
||||
.form-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.full-width {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
@media (width <= 760px) {
|
||||
.operation-page {
|
||||
padding: 12px;
|
||||
}
|
||||
|
||||
.page-heading {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.toolbar-control,
|
||||
.date-range,
|
||||
.toolbar-actions {
|
||||
width: 100%;
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
.toolbar-actions {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.pagination-row {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.form-grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user