index.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. <template>
  2. <basic-container>
  3. <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" ref="crud" @row-del="rowDel"
  4. v-model="form" :permission="permissionList" @row-update="rowUpdate" @row-save="rowSave"
  5. :before-open="beforeOpen" @search-change="searchChange" @search-reset="searchReset"
  6. @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
  7. @refresh-change="refreshChange" @on-load="onLoad">
  8. <template slot="menuLeft">
  9. <el-button type="danger" size="small" icon="el-icon-delete" plain v-if="permission.announcement_delete"
  10. @click="handleDelete">删 除
  11. </el-button>
  12. </template>
  13. <template slot-scope="{row}" slot="dealer">
  14. <el-tag>{{ row.dealerName }}</el-tag>
  15. </template>
  16. <template slot-scope="{row}" slot="brand">
  17. <el-tag>{{ row.brandName }}</el-tag>
  18. </template>
  19. <template slot-scope="{row}" slot="detail">
  20. <el-button type="text" @click="viewDetail(row)">查看详情</el-button>
  21. </template>
  22. </avue-crud>
  23. <!-- 详情查看对话框 -->
  24. <el-dialog title="公告详情" :visible.sync="detailVisible" width="60%" append-to-body>
  25. <div class="detail-content">
  26. <h3>{{ currentDetail.title }}</h3>
  27. <div class="detail-info">
  28. <p><strong>发布时间:</strong>{{ currentDetail.publishTime }}</p>
  29. <p><strong>经销商:</strong>{{ currentDetail.dealerName }}</p>
  30. <p><strong>品牌:</strong>{{ currentDetail.brandName }}</p>
  31. </div>
  32. <div class="detail-body" v-html="currentDetail.content"></div>
  33. </div>
  34. <span slot="footer" class="dialog-footer">
  35. <el-button @click="detailVisible = false">关 闭</el-button>
  36. </span>
  37. </el-dialog>
  38. </basic-container>
  39. </template>
  40. <script>
  41. import { getList, remove, update, add, getAnnouncement, getDealerList, getBrandList, getCategoryList } from "@/api/announcement";
  42. import { mapGetters } from "vuex";
  43. /**
  44. * @typedef {Object} AnnouncementItem
  45. * @property {number} id - 公告ID
  46. * @property {string} title - 公告标题
  47. * @property {string} customerCode - 客户编号
  48. * @property {string} publishTime - 发布时间
  49. * @property {number} dealerId - 经销商ID
  50. * @property {string} dealerName - 经销商名称
  51. * @property {number} brandId - 品牌ID
  52. * @property {string} brandName - 品牌名称
  53. * @property {number} categoryId - 分类ID
  54. * @property {string} categoryName - 分类名称
  55. * @property {string} roleType - 角色类型
  56. * @property {string} content - 公告内容
  57. */
  58. /**
  59. * @typedef {Object} OptionItem
  60. * @property {number} id - 选项ID
  61. * @property {string} dealerName - 经销商名称
  62. * @property {string} brandName - 品牌名称
  63. * @property {string} categoryName - 分类名称
  64. * @property {number} value - 选项值
  65. * @property {string} label - 选项标签
  66. */
  67. /**
  68. * @typedef {Object} PageInfo
  69. * @property {number} pageSize - 每页大小
  70. * @property {number} currentPage - 当前页码
  71. * @property {number} total - 总记录数
  72. */
  73. /**
  74. * @typedef {Object} QueryParams
  75. * @property {string} [title] - 公告标题
  76. * @property {string} [customerCode] - 客户编号
  77. * @property {Array<string>} [publishTime] - 发布时间范围
  78. * @property {number} [dealerId] - 经销商ID
  79. * @property {number} [brandId] - 品牌ID
  80. * @property {number} [categoryId] - 分类ID
  81. * @property {string} [roleType] - 角色类型
  82. */
  83. /**
  84. * 公告管理组件
  85. * @component AnnouncementIndex
  86. */
  87. export default {
  88. name: 'AnnouncementIndex',
  89. data() {
  90. return {
  91. /** @type {AnnouncementItem} 表单数据 */
  92. form: {},
  93. /** @type {QueryParams} 查询参数 */
  94. query: {},
  95. /** @type {boolean} 加载状态 */
  96. loading: true,
  97. /** @type {boolean} 详情对话框显示状态 */
  98. detailVisible: false,
  99. /** @type {AnnouncementItem} 当前查看的详情数据 */
  100. currentDetail: {},
  101. /** @type {PageInfo} 分页信息 */
  102. page: {
  103. pageSize: 10,
  104. currentPage: 1,
  105. total: 0
  106. },
  107. /** @type {Array<AnnouncementItem>} 选中的数据列表 */
  108. selectionList: [],
  109. /** @type {Array<OptionItem>} 经销商选项列表 */
  110. dealerOptions: [],
  111. /** @type {Array<OptionItem>} 品牌选项列表 */
  112. brandOptions: [],
  113. /** @type {Array<OptionItem>} 分类选项列表 */
  114. categoryOptions: [],
  115. /** @type {Object} 表格配置选项 */
  116. option: {
  117. height: 'auto',
  118. calcHeight: 30,
  119. dialogWidth: 950,
  120. tip: false,
  121. searchShow: true,
  122. searchMenuSpan: 6,
  123. border: true,
  124. index: true,
  125. viewBtn: true,
  126. selection: true,
  127. excelBtn: false, // 隐藏下载按钮
  128. columnBtn: false, // 隐藏列设置按钮
  129. dialogClickModal: false,
  130. column: [
  131. {
  132. label: "公告标题",
  133. prop: "title",
  134. span: 12,
  135. search: true,
  136. overHidden: true,
  137. rules: [{
  138. required: true,
  139. message: "请输入公告标题",
  140. trigger: "blur"
  141. }]
  142. },
  143. {
  144. label: "客户编号",
  145. prop: "customerCode",
  146. span: 12,
  147. search: true,
  148. overHidden: true
  149. },
  150. {
  151. label: "发布时间",
  152. prop: "publishTime",
  153. type: "daterange",
  154. format: "yyyy-MM-dd",
  155. valueFormat: "yyyy-MM-dd",
  156. rangeSeparator: "至",
  157. searchRange: true,
  158. startPlaceholder: "开始时间",
  159. endPlaceholder: "结束时间",
  160. overHidden: true,
  161. search: true,
  162. hide: true, // 在表格中隐藏,只用于搜索
  163. addDisplay: false, // 新增时不显示
  164. editDisplay: false, // 编辑时不显示
  165. viewDisplay: false // 查看时不显示
  166. },
  167. {
  168. label: "经销商",
  169. prop: "dealerId",
  170. type: "select",
  171. dicData: [],
  172. props: {
  173. label: "dealerName",
  174. value: "id"
  175. },
  176. slot: true,
  177. overHidden: true,
  178. search: true,
  179. span: 12,
  180. rules: [{
  181. required: true,
  182. message: "请选择经销商",
  183. trigger: "change"
  184. }]
  185. },
  186. {
  187. label: "品牌",
  188. prop: "brandId",
  189. type: "select",
  190. dicData: [],
  191. props: {
  192. label: "brandName",
  193. value: "id"
  194. },
  195. slot: true,
  196. overHidden: true,
  197. search: true,
  198. span: 12,
  199. rules: [{
  200. required: true,
  201. message: "请选择品牌",
  202. trigger: "change"
  203. }]
  204. },
  205. {
  206. label: "分类",
  207. prop: "categoryId",
  208. type: "select",
  209. dicData: [], // 初始为空,通过loadCategoryOptions方法动态加载
  210. props: {
  211. label: "categoryName", // 根据后端返回的字段名调整
  212. value: "id"
  213. },
  214. search: true,
  215. span: 12,
  216. rules: [{
  217. required: true,
  218. message: "请选择分类",
  219. trigger: "change"
  220. }]
  221. },
  222. {
  223. label: "角色",
  224. prop: "roleType",
  225. type: "select",
  226. dicData: [
  227. { label: "工厂", value: "factory" },
  228. { label: "经销商", value: "dealer" },
  229. { label: "零售商", value: "retailer" }
  230. ],
  231. search: true,
  232. span: 12,
  233. rules: [{
  234. required: true,
  235. message: "请选择角色",
  236. trigger: "change"
  237. }]
  238. },
  239. {
  240. label: "公告内容",
  241. prop: "content",
  242. component: 'AvueUeditor',
  243. options: {
  244. action: '/api/blade-resource/oss/endpoint/put-file',
  245. props: {
  246. res: "data",
  247. url: "link",
  248. }
  249. },
  250. showColumn: false,
  251. hide: true,
  252. minRows: 6,
  253. span: 24,
  254. rules: [{
  255. required: true,
  256. message: "请输入公告内容",
  257. trigger: "blur"
  258. }]
  259. },
  260. ]
  261. },
  262. /** @type {Array<AnnouncementItem>} 表格数据 */
  263. data: []
  264. };
  265. },
  266. computed: {
  267. ...mapGetters(["permission"]),
  268. /**
  269. * 权限列表
  270. * @returns {Object} 权限配置对象
  271. */
  272. permissionList() {
  273. return {
  274. addBtn: this.vaildData(this.permission.announcement_add, false),
  275. viewBtn: this.vaildData(this.permission.announcement_view, false),
  276. delBtn: this.vaildData(this.permission.announcement_delete, false),
  277. editBtn: this.vaildData(this.permission.announcement_edit, false)
  278. };
  279. },
  280. /**
  281. * 选中的ID字符串
  282. * @returns {string} 逗号分隔的ID字符串
  283. */
  284. ids() {
  285. let ids = [];
  286. this.selectionList.forEach(ele => {
  287. ids.push(ele.id);
  288. });
  289. return ids.join(",");
  290. }
  291. },
  292. created() {
  293. this.loadDealerOptions();
  294. this.loadBrandOptions();
  295. this.loadCategoryOptions(); // 添加分类加载
  296. },
  297. methods: {
  298. /**
  299. * 加载分类选项
  300. * @async
  301. * @returns {Promise<void>}
  302. */
  303. async loadCategoryOptions() {
  304. try {
  305. const res = await getCategoryList();
  306. // 根据新接口返回的数据结构进行转换
  307. const categoryData = res.data.data || [];
  308. // 将接口返回的数据转换为页面所需的格式
  309. this.categoryOptions = categoryData
  310. .filter(item => item.status === 1 && item.isDeleted === 0) // 只显示启用且未删除的分类
  311. .map(item => ({
  312. id: item.id,
  313. name: item.name,
  314. categoryName: item.name, // 兼容原有字段名
  315. value: item.id,
  316. label: item.name,
  317. orgId: item.orgId,
  318. orgName: item.orgName,
  319. isSystem: item.isSystem,
  320. sortOrder: item.sortOrder
  321. }))
  322. .sort((a, b) => a.sortOrder - b.sortOrder); // 按排序字段排序
  323. const categoryColumn = this.option.column.find(col => col.prop === 'categoryId');
  324. if (categoryColumn) {
  325. categoryColumn.dicData = this.categoryOptions;
  326. }
  327. } catch (error) {
  328. console.error('加载分类选项失败:', error);
  329. // 如果接口不存在,使用模拟数据
  330. this.categoryOptions = [
  331. { id: 1, name: '系统公告', categoryName: '系统公告', value: 1, label: '系统公告', isSystem: 1, sortOrder: 0 },
  332. { id: 2, name: '产品公告', categoryName: '产品公告', value: 2, label: '产品公告', isSystem: 0, sortOrder: 1 },
  333. { id: 3, name: '活动公告', categoryName: '活动公告', value: 3, label: '活动公告', isSystem: 0, sortOrder: 2 },
  334. { id: 4, name: '维护公告', categoryName: '维护公告', value: 4, label: '维护公告', isSystem: 0, sortOrder: 3 }
  335. ];
  336. const categoryColumn = this.option.column.find(col => col.prop === 'categoryId');
  337. if (categoryColumn) {
  338. categoryColumn.dicData = this.categoryOptions;
  339. }
  340. }
  341. },
  342. /**
  343. * 查看详情
  344. * @param {AnnouncementItem} row - 行数据
  345. * @returns {void}
  346. */
  347. viewDetail(row) {
  348. this.currentDetail = row;
  349. this.detailVisible = true;
  350. },
  351. /**
  352. * 加载经销商选项
  353. * @async
  354. * @returns {Promise<void>}
  355. */
  356. async loadDealerOptions() {
  357. try {
  358. const res = await getDealerList();
  359. this.dealerOptions = res.data.data || [];
  360. const dealerColumn = this.option.column.find(col => col.prop === 'dealerId');
  361. if (dealerColumn) {
  362. dealerColumn.dicData = this.dealerOptions;
  363. }
  364. } catch (error) {
  365. // 如果接口不存在,使用模拟数据
  366. this.dealerOptions = [
  367. { id: 1, dealerName: '经销商A' },
  368. { id: 2, dealerName: '经销商B' },
  369. { id: 3, dealerName: '经销商C' }
  370. ];
  371. const dealerColumn = this.option.column.find(col => col.prop === 'dealerId');
  372. if (dealerColumn) {
  373. dealerColumn.dicData = this.dealerOptions;
  374. }
  375. }
  376. },
  377. /**
  378. * 加载品牌选项
  379. * @async
  380. * @returns {Promise<void>}
  381. */
  382. async loadBrandOptions() {
  383. try {
  384. const res = await getBrandList();
  385. this.brandOptions = res.data.data || [];
  386. const brandColumn = this.option.column.find(col => col.prop === 'brandId');
  387. if (brandColumn) {
  388. brandColumn.dicData = this.brandOptions;
  389. }
  390. } catch (error) {
  391. // 如果接口不存在,使用模拟数据
  392. this.brandOptions = [
  393. { id: 1, brandName: '品牌A' },
  394. { id: 2, brandName: '品牌B' },
  395. { id: 3, brandName: '品牌C' }
  396. ];
  397. const brandColumn = this.option.column.find(col => col.prop === 'brandId');
  398. if (brandColumn) {
  399. brandColumn.dicData = this.brandOptions;
  400. }
  401. }
  402. },
  403. /**
  404. * 保存行数据
  405. * @async
  406. * @param {AnnouncementItem} row - 行数据
  407. * @param {Function} done - 完成回调
  408. * @param {Function} loading - 加载回调
  409. * @returns {Promise<void>}
  410. */
  411. async rowSave(row, done, loading) {
  412. try {
  413. await add(row);
  414. this.onLoad(this.page);
  415. this.$message({
  416. type: "success",
  417. message: "操作成功!"
  418. });
  419. done();
  420. } catch (error) {
  421. console.log(error);
  422. loading();
  423. }
  424. },
  425. /**
  426. * 更新行数据
  427. * @async
  428. * @param {AnnouncementItem} row - 行数据
  429. * @param {number} index - 行索引
  430. * @param {Function} done - 完成回调
  431. * @param {Function} loading - 加载回调
  432. * @returns {Promise<void>}
  433. */
  434. async rowUpdate(row, index, done, loading) {
  435. try {
  436. await update(row);
  437. this.onLoad(this.page);
  438. this.$message({
  439. type: "success",
  440. message: "操作成功!"
  441. });
  442. done();
  443. } catch (error) {
  444. console.log(error);
  445. loading();
  446. }
  447. },
  448. /**
  449. * 删除行数据
  450. * @async
  451. * @param {AnnouncementItem} row - 行数据
  452. * @returns {Promise<void>}
  453. */
  454. async rowDel(row) {
  455. try {
  456. await this.$confirm("确定将选择数据删除?", {
  457. confirmButtonText: "确定",
  458. cancelButtonText: "取消",
  459. type: "warning"
  460. });
  461. await remove(row.id);
  462. this.onLoad(this.page);
  463. this.$message({
  464. type: "success",
  465. message: "操作成功!"
  466. });
  467. } catch (error) {
  468. // 用户取消删除或删除失败
  469. console.log(error);
  470. }
  471. },
  472. /**
  473. * 重置搜索
  474. * @returns {void}
  475. */
  476. searchReset() {
  477. this.query = {};
  478. this.onLoad(this.page);
  479. },
  480. /**
  481. * 搜索变化
  482. * @param {QueryParams} params - 搜索参数
  483. * @param {Function} done - 完成回调
  484. * @returns {void}
  485. */
  486. searchChange(params, done) {
  487. this.query = params;
  488. this.page.currentPage = 1;
  489. this.onLoad(this.page, params);
  490. done();
  491. },
  492. /**
  493. * 选择变化
  494. * @param {Array<AnnouncementItem>} list - 选中的数据列表
  495. * @returns {void}
  496. */
  497. selectionChange(list) {
  498. this.selectionList = list;
  499. },
  500. /**
  501. * 清空选择
  502. * @returns {void}
  503. */
  504. selectionClear() {
  505. this.selectionList = [];
  506. this.$refs.crud.toggleSelection();
  507. },
  508. /**
  509. * 批量删除
  510. * @async
  511. * @returns {Promise<void>}
  512. */
  513. async handleDelete() {
  514. if (this.selectionList.length === 0) {
  515. this.$message.warning("请选择至少一条数据");
  516. return;
  517. }
  518. try {
  519. await this.$confirm("确定将选择数据删除?", {
  520. confirmButtonText: "确定",
  521. cancelButtonText: "取消",
  522. type: "warning"
  523. });
  524. await remove(this.ids);
  525. this.onLoad(this.page);
  526. this.$message({
  527. type: "success",
  528. message: "操作成功!"
  529. });
  530. this.$refs.crud.toggleSelection();
  531. } catch (error) {
  532. // 用户取消删除或删除失败
  533. console.log(error);
  534. }
  535. },
  536. /**
  537. * 打开前回调
  538. * @async
  539. * @param {Function} done - 完成回调
  540. * @param {string} type - 操作类型
  541. * @returns {Promise<void>}
  542. */
  543. async beforeOpen(done, type) {
  544. if (["edit", "view"].includes(type)) {
  545. try {
  546. const res = await getAnnouncement(this.form.id);
  547. this.form = res.data.data;
  548. } catch (error) {
  549. console.log(error);
  550. }
  551. }
  552. done();
  553. },
  554. /**
  555. * 当前页变化
  556. * @param {number} currentPage - 当前页码
  557. * @returns {void}
  558. */
  559. currentChange(currentPage) {
  560. this.page.currentPage = currentPage;
  561. },
  562. /**
  563. * 页大小变化
  564. * @param {number} pageSize - 页大小
  565. * @returns {void}
  566. */
  567. sizeChange(pageSize) {
  568. this.page.pageSize = pageSize;
  569. },
  570. /**
  571. * 刷新变化
  572. * @returns {void}
  573. */
  574. refreshChange() {
  575. this.onLoad(this.page, this.query);
  576. },
  577. /**
  578. * 加载数据
  579. * @async
  580. * @param {PageInfo} page - 分页信息
  581. * @param {QueryParams} [params={}] - 查询参数
  582. * @returns {Promise<void>}
  583. */
  584. async onLoad(page, params = {}) {
  585. const { publishTime } = this.query;
  586. let values = {
  587. ...params,
  588. };
  589. if (publishTime) {
  590. values = {
  591. ...params,
  592. publishTimeStart: publishTime[0],
  593. publishTimeEnd: publishTime[1],
  594. ...this.query
  595. };
  596. values.publishTime = null;
  597. }
  598. this.loading = true;
  599. try {
  600. const res = await getList(page.currentPage, page.pageSize, values);
  601. const data = res.data.data;
  602. this.page.total = data.total;
  603. this.data = data.records;
  604. this.loading = false;
  605. this.selectionClear();
  606. } catch (error) {
  607. console.log(error);
  608. this.loading = false;
  609. }
  610. }
  611. }
  612. };
  613. </script>
  614. <style scoped>
  615. .detail-content {
  616. padding: 20px;
  617. }
  618. .detail-info {
  619. margin: 20px 0;
  620. padding: 15px;
  621. background-color: #f5f5f5;
  622. border-radius: 4px;
  623. }
  624. .detail-info p {
  625. margin: 8px 0;
  626. }
  627. .detail-body {
  628. margin-top: 20px;
  629. padding: 15px;
  630. border: 1px solid #e4e7ed;
  631. border-radius: 4px;
  632. min-height: 200px;
  633. }
  634. </style>