gaoluyang
2026-05-18 da57fbd8e7fa021614fb32502fb1520ea4e34e1e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<template>
  <div class="app-container">
    <div class="search_form" style="margin-bottom: 10px">
      <div>
        <span class="search_title">仓库名称:</span>
        <el-input
          v-model="searchForm.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
        <span class="search_title ml10">仓库位置:</span>
        <el-input
          v-model="searchForm.location"
          placeholder="请输入仓库位置"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
        <span class="search_title ml10">负责人:</span>
        <el-input
          v-model="searchForm.managerName"
          placeholder="请输入负责人"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
        <el-button type="primary" @click="handleQuery" style="margin-left: 10px"
          >搜索</el-button
        >
        <el-button @click="handleReset">重置</el-button>
      </div>
      <div>
        <el-button type="primary" @click="handleAdd">新增</el-button>
        <el-button type="danger" plain @click="handleDelete">删除</el-button>
      </div>
    </div>
    <div class="table_list">
      <el-table
        :data="tableData"
        border
        v-loading="tableLoading"
        @selection-change="handleSelectionChange"
        style="width: 100%"
        height="calc(100vh - 18.5em)"
      >
        <el-table-column align="center" type="selection" width="55" />
        <el-table-column align="center" label="序号" type="index" width="60" />
        <el-table-column
          label="仓库名称"
          prop="warehouseName"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="仓库位置"
          prop="location"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="负责人"
          prop="managerName"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="联系电话"
          prop="contactPhone"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column label="状态" prop="status" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status ? 'success' : 'info'" size="small">
              {{ scope.row.status ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button link type="danger" size="small" @click="handleRowDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        :page="page.current"
        :limit="page.size"
        @pagination="paginationChange"
      />
    </div>
 
    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="600"
      @close="closeDialog"
    >
      <el-form
        ref="formRef"
        :model="formState"
        label-width="100px"
        :rules="formRules"
      >
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input
            v-model="formState.warehouseName"
            placeholder="请输入仓库名称"
            maxlength="50"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="仓库位置" prop="location">
          <el-input
            v-model="formState.location"
            placeholder="请输入仓库位置"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="负责人" prop="managerId">
          <el-select
            v-model="formState.managerId"
            placeholder="请选择负责人"
            clearable
            filterable
            style="width: 100%"
            @change="handleManagerChange"
          >
            <el-option
              v-for="user in userList"
              :key="user.userId"
              :label="user.nickName"
              :value="user.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input
            v-model="formState.contactPhone"
            placeholder="请输入联系电话"
            maxlength="20"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formState.status">
            <el-radio :value="true">启用</el-radio>
            <el-radio :value="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确认</el-button>
          <el-button @click="closeDialog">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script setup>
import pagination from "@/components/PIMTable/Pagination.vue";
import { ref, reactive, computed, getCurrentInstance, onMounted } from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import {
  getWarehousePage,
  addWarehouse,
  updateWarehouse,
  delWarehouse,
  updateWarehouseStatus,
} from "@/api/inventoryManagement/warehouse.js";
import { listUser } from "@/api/system/user.js";
 
const { proxy } = getCurrentInstance();
const tableData = ref([]);
const selectedRows = ref([]);
const tableLoading = ref(false);
 
const page = reactive({
  current: 1,
  size: 10,
});
const total = ref(0);
 
// 搜索表单
const searchForm = reactive({
  warehouseName: "",
  location: "",
  managerName: "",
});
 
// 对话框相关
const dialogVisible = ref(false);
const dialogType = ref("add"); // 'add' 或 'edit'
const dialogTitle = computed(() =>
  dialogType.value === "add" ? "新增仓库" : "编辑仓库"
);
const isEdit = computed(() => dialogType.value === "edit");
const formRef = ref();
 
// 表单数据
const formState = reactive({
  id: undefined,
  warehouseName: "",
  location: "",
  managerId: undefined,
  managerName: "",
  contactPhone: "",
  status: true,
});
 
// 用户列表
const userList = ref([]);
 
// 表单验证规则
const formRules = {
  warehouseName: [
    { required: true, message: "请输入仓库名称", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  location: [
    { required: true, message: "请输入仓库位置", trigger: "blur" },
    { min: 1, max: 200, message: "长度在 1 到 200 个字符", trigger: "blur" },
  ],
  managerId: [
    { required: true, message: "请选择负责人", trigger: "change" },
  ],
  contactPhone: [
    { required: true, message: "请输入联系电话", trigger: "blur" },
    { pattern: /^1[3-9]\d{9}$|^0\d{2,3}-?\d{7,8}$/, message: "请输入正确的电话号码", trigger: "blur" },
  ],
  status: [{ required: true, message: "请选择状态", trigger: "change" }],
};
 
// 获取用户列表
const getUserList = () => {
  listUser({ pageNum: 1, pageSize: 1000 }).then((res) => {
    userList.value = res.rows || [];
  });
};
 
// 负责人选择变化
const handleManagerChange = (userId) => {
  const user = userList.value.find((item) => item.userId === userId);
  if (user) {
    formState.managerName = user.nickName;
  } else {
    formState.managerName = "";
  }
};
 
// 查询列表
const handleQuery = () => {
  page.current = 1;
  getList();
};
 
// 重置搜索
const handleReset = () => {
  searchForm.warehouseName = "";
  searchForm.location = "";
  searchForm.managerName = "";
  handleQuery();
};
 
const paginationChange = (obj) => {
  page.current = obj.page;
  page.size = obj.limit;
  getList();
};
 
const getList = () => {
  tableLoading.value = true;
  getWarehousePage({
    ...searchForm,
    current: page.current,
    size: page.size,
  })
    .then((res) => {
      tableLoading.value = false;
      tableData.value = res.data.records || [];
      total.value = res.data.total || 0;
    })
    .catch(() => {
      tableLoading.value = false;
    });
};
 
// 新增
const handleAdd = () => {
  dialogType.value = "add";
  resetForm();
  dialogVisible.value = true;
};
 
// 编辑
const handleEdit = (row) => {
  dialogType.value = "edit";
  resetForm();
  // 根据managerName查找对应的managerId
  const user = userList.value.find((item) => item.nickName === row.managerName);
  // 填充表单数据
  Object.assign(formState, {
    id: row.id,
    warehouseName: row.warehouseName,
    location: row.location,
    managerId: user ? user.userId : row.managerId,
    managerName: row.managerName,
    contactPhone: row.contactPhone,
    status: row.status,
  });
  dialogVisible.value = true;
};
 
// 重置表单
const resetForm = () => {
  formState.id = undefined;
  formState.warehouseName = "";
  formState.location = "";
  formState.managerId = undefined;
  formState.managerName = "";
  formState.contactPhone = "";
  formState.status = true;
  proxy.$refs["formRef"]?.resetFields();
};
 
// 关闭对话框
const closeDialog = () => {
  dialogVisible.value = false;
  resetForm();
};
 
// 提交表单
const handleSubmit = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (dialogType.value === "add") {
        submitAdd();
      } else {
        submitEdit();
      }
    }
  });
};
 
// 提交新增
const submitAdd = () => {
  const params = { ...formState };
  delete params.id;
  addWarehouse(params)
    .then(() => {
      ElMessage.success("新增成功");
      closeDialog();
      getList();
    })
    .catch(() => {
      ElMessage.error("新增失败");
    });
};
 
// 提交编辑
const submitEdit = () => {
  const params = { ...formState };
  updateWarehouse(params)
    .then(() => {
      ElMessage.success("编辑成功");
      closeDialog();
      getList();
    })
    .catch(() => {
      ElMessage.error("编辑失败");
    });
};
 
// 状态变更
const handleStatusChange = (row) => {
  const statusText = row.status ? "启用" : "停用";
  ElMessageBox.confirm(
    `确认要${statusText}该仓库吗?`,
    "提示",
    {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      updateWarehouseStatus(row.id, row.status)
        .then(() => {
          ElMessage.success(`${statusText}成功`);
          getList();
        })
        .catch(() => {
          // 恢复原状态
          row.status = !row.status;
          ElMessage.error(`${statusText}失败`);
        });
    })
    .catch(() => {
      // 恢复原状态
      row.status = !row.status;
      ElMessage.info("已取消");
    });
};
 
// 表格选择数据
const handleSelectionChange = (selection) => {
  selectedRows.value = selection;
};
 
// 批量删除
const handleDelete = () => {
  if (selectedRows.value.length === 0) {
    proxy.$modal.msgWarning("请选择要删除的数据");
    return;
  }
  const ids = selectedRows.value.map((item) => item.id);
  ElMessageBox.confirm("选中的内容将被删除,是否确认删除?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delWarehouse(ids).then(() => {
        proxy.$modal.msgSuccess("删除成功");
        getList();
      });
    })
    .catch(() => {
      proxy.$modal.msg("已取消");
    });
};
 
// 单行删除
const handleRowDelete = (row) => {
  ElMessageBox.confirm("确认删除该仓库吗?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delWarehouse([row.id]).then(() => {
        proxy.$modal.msgSuccess("删除成功");
        getList();
      });
    })
    .catch(() => {
      proxy.$modal.msg("已取消");
    });
};
 
// 初始化加载数据
onMounted(() => {
  getList();
  getUserList();
});
</script>
 
<style scoped>
.search_form {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
.search_title {
  font-size: 14px;
  color: #606266;
}
.ml10 {
  margin-left: 10px;
}
.table_list {
  margin-top: 10px;
}
</style>