qx
9 天以前 f5033d775292408a15d6479c38ec0518d53bb6a6
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公告标题" prop="noticeTitle">
        <el-input v-model="queryParams.noticeTitle" placeholder="请输入公告标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="操作人员" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入操作人员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="类型" prop="noticeType">
        <el-select v-model="queryParams.noticeType" placeholder="公告类型" clearable>
          <el-option v-for="dict in dict.type.sys_notice_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
 
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:notice:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:notice:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:notice:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
 
    <el-table
      v-loading="loading"
      :data="noticeList"
      @selection-change="handleSelectionChange"
      :row-class-name="tableRowClassName"
      ref="table"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="noticeId" width="100" />
      <el-table-column label="公告标题" align="center" prop="noticeTitle" :show-overflow-tooltip="true" />
      <el-table-column label="公告类型" align="center" prop="noticeType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_notice_type" :value="scope.row.noticeType" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_notice_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="接收科室" align="center" prop="deptId" width="150">
        <template slot-scope="scope">
          <span>{{ getDeptNames(scope.row.deptId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接收用户" align="center" prop="userIds" width="200" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ getUserNames(scope.row.userIds) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:notice:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:notice:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-share" @click="handledetails(scope.row)"
            v-hasPermi="['system:notice:query']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
 
    <div class="pagination-container">
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>
 
    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公告标题" prop="noticeTitle">
              <el-input v-model="form.noticeTitle" placeholder="请输入公告标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公告类型" prop="noticeType">
              <el-select v-model="form.noticeType" placeholder="请选择公告类型">
                <el-option v-for="dict in dict.type.sys_notice_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接收科室" prop="deptId">
              <el-select v-model="form.deptId" filterable placeholder="请选择接收科室" @change="handleDeptChange">
                <el-option v-for="dept in deptList" :key="dept.id" :label="dept.label" :value="dept.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="接收用户" prop="userIds">
              <el-select v-model="form.userIds" multiple filterable placeholder="请选择接收用户">
                <el-option v-for="user in editUserList" :key="user.userId" :label="user.nickName || user.userName"
                  :value="user.userId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in dict.type.sys_notice_status" :key="dict.value"
                  :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容">
              <editor v-model="form.noticeContent" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
 
    <!-- 通知公告详情 -->
    <el-dialog :title="formIn.noticeTitle" :visible.sync="openDetail" width="800px" append-to-body>
      <div class="detail-header">
        <el-tag size="mini" effect="dark" type="warning" v-if="formIn.noticeType === '2'">公告</el-tag>
        <el-tag size="mini" effect="dark" v-else>通知</el-tag>
        <span class="time">{{ parseTime(formIn.createTime, '{y}-{m}-{d} {h}:{i}:{s}') || '无' }}</span>
      </div>
      <div class="detail-item">
        <span class="label">接收科室:</span>
        <span>{{ getDeptNames(formIn.deptId) }}</span>
      </div>
      <div class="detail-item">
        <span class="label">接收用户:</span>
        <span>{{ getUserNames(formIn.userIds) }}</span>
      </div>
      <div class="detail-content">
        <div v-html="formIn.noticeContent" class="ql-editor"></div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
 
<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/system/notice";
import { deptTreeSelect } from "@/api/system/dept";
import { listUser1 } from "@/api/system/user";
 
export default {
  name: "Notice",
  dicts: ["sys_notice_status", "sys_notice_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      openDetail: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 科室列表
      deptList: [],
      // 用户列表(全局,用于表格和详情)
      userList: [],
      // 用户列表(用于编辑对话框)
      editUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否为初次加载
      isInitialLoad: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        noticeType: undefined,
      },
      formIn: {},
      // 表单参数
      form: {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0",
        deptId: undefined,
        userIds: [],
      },
      // 表单校验
      rules: {
        noticeTitle: [{ required: true, message: "公告标题不能为空", trigger: "blur" }],
        noticeType: [{ required: true, message: "公告类型不能为空", trigger: "change" }],
        deptId: [
          {
            required: false,
            message: "请选择接收科室",
            trigger: "change",
            validator: (rule, value, callback) => {
              callback(); // 非必填
            },
          },
        ],
        userIds: [
          {
            required: false,
            message: "请选择接收用户",
            trigger: "change",
            validator: (rule, value, callback) => {
              callback(); // 非必填
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    // 获取科室列表
    deptTreeSelect()
      .then((response) => {
        this.deptList = response.data || [];
        this.deptList = this.flattenDeptList(this.deptList);
        console.log("Flattened deptList:", this.deptList);
      })
      .catch((error) => {
        this.$modal.msgError("获取科室列表失败:" + error.message);
      });
    // 预加载所有用户列表(用于表格和详情)
    listUser1({})
      .then((response) => {
        this.userList = (response.rows || response.data || []).map(user => ({
          ...user,
          userId: String(user.userId),
        }));
        this.editUserList = [...this.userList]; // 初始化编辑用户列表
        console.log("Preloaded userList:", this.userList);
      })
      .catch((error) => {
        this.$modal.msgError("获取用户列表失败:" + error.message);
      });
    // 处理跳转参数
    this.handleRouteParams();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then((response) => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
        // 高亮或打开详情(仅初次加载时触发详情)
        this.$nextTick(() => {
          this.highlightNotice(this.isInitialLoad);
          this.isInitialLoad = false; // 标记为非初次加载
        });
      }).catch((error) => {
        console.error("listNotice error:", error);
        this.$modal.msgError("获取公告列表失败:" + error.message);
        this.loading = false;
      });
    },
    /** 处理路由参数 */
    handleRouteParams() {
      const { noticeId } = this.$route.query;
      if (noticeId) {
        this.queryParams.noticeId = noticeId;
        this.getList();
      }
    },
    /** 高亮或打开公告详情 */
    highlightNotice(isInitialLoad) {
      const { noticeId } = this.$route.query;
      if (noticeId) {
        const notice = this.noticeList.find(item => item.noticeId === noticeId);
        if (notice) {
          this.$refs.table.setCurrentRow(notice);
          if (isInitialLoad) {
            this.handledetails(notice); // 仅初次加载时打开详情
          }
        }
      }
    },
    /** 表格行类名 */
    tableRowClassName({ row }) {
      const { noticeId } = this.$route.query;
      return noticeId && row.noticeId === noticeId ? 'highlight-row' : '';
    },
    /** 将科室ID转换为科室名称 */
    getDeptNames(deptId) {
      if (!deptId) return "无";
      const dept = this.deptList.find((dept) => dept.id === deptId);
      return dept ? dept.label : "未知科室";
    },
    /** 将用户ID转换为用户名 */
    getUserNames(userIds) {
      if (!userIds) return "无";
      const ids = typeof userIds === "string" ? userIds.split(",") : userIds;
      const names = this.userList
        .filter((user) => ids.includes(user.userId))
        .map((user) => user.nickName || user.userName)
        .join(", ");
      return names || ids.join(", ");
    },
    /** 展平部门树形结构 */
    flattenDeptList(depts, result = []) {
      depts.forEach((dept) => {
        result.push({ id: String(dept.id), label: dept.label });
        if (dept.children && dept.children.length) {
          this.flattenDeptList(dept.children, result);
        }
      });
      return result;
    },
    /** 科室选择变化时调用 */
    handleDeptChange(deptId) {
      if (deptId) {
        this.editUserList = [];
        return listUser1({ deptId })
          .then((response) => {
            this.editUserList = (response.rows || response.data || []).map(user => ({
              ...user,
              userId: String(user.userId),
            }));
            console.log("Fetched editUserList for deptId", deptId, ":", this.editUserList);
          })
          .catch((error) => {
            this.$modal.msgError("获取用户列表失败:" + error.message);
            this.editUserList = [...this.userList]; // 回退到全局用户列表
          });
      } else {
        console.log("No deptId, retained form.userIds:", this.form.userIds);
        this.editUserList = [...this.userList]; // 恢复为全局用户列表
        return Promise.resolve();
      }
    },
    // 取消按钮
    cancel() {
      console.log("Cancel triggered, resetting form and closing dialogs");
      this.open = false;
      this.openDetail = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0",
        deptId: undefined,
        userIds: [],
      };
      this.editUserList = [...this.userList]; // 重置为全局用户列表
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.noticeId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      console.log("handleAdd triggered");
      this.reset();
      this.open = true;
      this.title = "添加公告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log("handleUpdate triggered for noticeId:", row.noticeId);
      this.reset();
      const noticeId = row.noticeId || this.ids;
      getNotice(noticeId)
        .then((response) => {
          console.log("getNotice response:", response.data);
          const data = response.data;
          this.form = {
            noticeId: data.noticeId,
            noticeTitle: data.noticeTitle,
            noticeType: data.noticeType,
            noticeContent: data.noticeContent,
            status: data.status,
            deptId: data.deptId ? String(data.deptId) : undefined,
            userIds: data.userIds ? (typeof data.userIds === 'string' ? data.userIds.split(',') : data.userIds) : [],
          };
          if (this.form.deptId) {
            return this.handleDeptChange(this.form.deptId).then(() => {
              this.open = true;
              this.title = "修改公告";
              console.log("Modification dialog opened, form:", this.form, "editUserList:", this.editUserList);
            });
          } else {
            this.editUserList = [...this.userList];
            console.log("No deptId, retained form.userIds:", this.form.userIds, "editUserList:", this.editUserList);
            this.open = true;
            this.title = "修改公告";
            console.log("Modification dialog opened, form:", this.form);
            return Promise.resolve();
          }
        })
        .catch((error) => {
          console.error("getNotice error:", error);
          this.$modal.msgError("获取公告详情失败:" + error.message);
        });
    },
    /** 详情按钮操作 */
    handledetails(row) {
      console.log("handledetails triggered for noticeId:", row.noticeId);
      this.formIn = row;
      this.openDetail = true;
      console.log("Detail dialog opened, formIn:", this.formIn);
    },
    /** 提交按钮 */
    submitForm() {
      console.log("submitForm triggered, form:", this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const formData = {
            ...this.form,
            deptId: this.form.deptId ? String(this.form.deptId) : "",
            userIds: this.form.userIds || [],
          };
          console.log("Submitting formData:", formData);
          if (formData.noticeId != undefined) {
            updateNotice(formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch((error) => {
              console.error("updateNotice error:", error);
              this.$modal.msgError("修改失败:" + error.message);
            });
          } else {
            addNotice(formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch((error) => {
              console.error("addNotice error:", error);
              this.$modal.msgError("新增失败:" + error.message);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log("handleDelete triggered for noticeId:", row.noticeId);
      const noticeIds = row.noticeId || this.ids;
      this.$modal
        .confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项?')
        .then(() => delNotice(noticeIds))
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 时间格式化 */
    parseTime(time, cFormat = '{y}-{m}-{d} {h}:{i}:{s}') {
      if (!time) return '';
      try {
        const date = new Date(time);
        if (isNaN(date.getTime())) return '';
        const formatObj = {
          y: date.getFullYear(),
          m: String(date.getMonth() + 1).padStart(2, '0'),
          d: String(date.getDate()).padStart(2, '0'),
          h: String(date.getHours()).padStart(2, '0'),
          i: String(date.getMinutes()).padStart(2, '0'),
          s: String(date.getSeconds()).padStart(2, '0')
        };
        return cFormat.replace(/{([ymdhis]+)}/g, (result, key) => formatObj[key] || '');
      } catch (error) {
        console.error('parseTime error:', error, 'time:', time);
        return '';
      }
    }
  },
};
</script>
 
<style scoped>
/* 容器样式,确保内容有适当内边距 */
.app-container {
  padding: 20px;
}
 
/* 分页容器,替换原 .pag 和 .pag1,居中显示 */
.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
 
/* 高亮行样式,突出显示路由指定的公告 */
.highlight-row {
  background-color: #e6f7ff;
}
 
/* 对话框底部按钮居中 */
.dialog-footer {
  text-align: center;
  padding: 10px 0;
}
 
/* 详情对话框头部(类型标签和时间) */
.detail-header {
  margin: -20px 0 10px;
  display: flex;
  align-items: center;
}
 
.detail-header .time {
  margin-left: 20px;
  color: #606266;
}
 
/* 详情对话框信息项(接收科室、接收用户) */
.detail-item {
  margin-bottom: 10px;
}
 
.detail-item .label {
  font-weight: bold;
  color: #303133;
}
 
/* 详情对话框内容区域,限制富文本高度 */
.detail-content .ql-editor {
  max-height: 300px;
  overflow-y: auto;
  padding: 10px;
  color: #303133;
}
 
/* 表格操作列,优化按钮间距 */
.small-padding {
  padding: 0 10px;
}
 
/* 搜索表单顶部间距 */
.mb8 {
  margin-bottom: 8px;
}
</style>