index.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. this.categoryOptions = res.data.data || [];
  307. const categoryColumn = this.option.column.find(col => col.prop === 'categoryId');
  308. if (categoryColumn) {
  309. categoryColumn.dicData = this.categoryOptions;
  310. }
  311. } catch (error) {
  312. // 如果接口不存在,使用模拟数据
  313. this.categoryOptions = [
  314. { id: 1, categoryName: '系统公告', value: 1, label: '系统公告' },
  315. { id: 2, categoryName: '产品公告', value: 2, label: '产品公告' },
  316. { id: 3, categoryName: '活动公告', value: 3, label: '活动公告' },
  317. { id: 4, categoryName: '维护公告', value: 4, label: '维护公告' }
  318. ];
  319. const categoryColumn = this.option.column.find(col => col.prop === 'categoryId');
  320. if (categoryColumn) {
  321. categoryColumn.dicData = this.categoryOptions;
  322. }
  323. }
  324. },
  325. /**
  326. * 查看详情
  327. * @param {AnnouncementItem} row - 行数据
  328. * @returns {void}
  329. */
  330. viewDetail(row) {
  331. this.currentDetail = row;
  332. this.detailVisible = true;
  333. },
  334. /**
  335. * 加载经销商选项
  336. * @async
  337. * @returns {Promise<void>}
  338. */
  339. async loadDealerOptions() {
  340. try {
  341. const res = await getDealerList();
  342. this.dealerOptions = res.data.data || [];
  343. const dealerColumn = this.option.column.find(col => col.prop === 'dealerId');
  344. if (dealerColumn) {
  345. dealerColumn.dicData = this.dealerOptions;
  346. }
  347. } catch (error) {
  348. // 如果接口不存在,使用模拟数据
  349. this.dealerOptions = [
  350. { id: 1, dealerName: '经销商A' },
  351. { id: 2, dealerName: '经销商B' },
  352. { id: 3, dealerName: '经销商C' }
  353. ];
  354. const dealerColumn = this.option.column.find(col => col.prop === 'dealerId');
  355. if (dealerColumn) {
  356. dealerColumn.dicData = this.dealerOptions;
  357. }
  358. }
  359. },
  360. /**
  361. * 加载品牌选项
  362. * @async
  363. * @returns {Promise<void>}
  364. */
  365. async loadBrandOptions() {
  366. try {
  367. const res = await getBrandList();
  368. this.brandOptions = res.data.data || [];
  369. const brandColumn = this.option.column.find(col => col.prop === 'brandId');
  370. if (brandColumn) {
  371. brandColumn.dicData = this.brandOptions;
  372. }
  373. } catch (error) {
  374. // 如果接口不存在,使用模拟数据
  375. this.brandOptions = [
  376. { id: 1, brandName: '品牌A' },
  377. { id: 2, brandName: '品牌B' },
  378. { id: 3, brandName: '品牌C' }
  379. ];
  380. const brandColumn = this.option.column.find(col => col.prop === 'brandId');
  381. if (brandColumn) {
  382. brandColumn.dicData = this.brandOptions;
  383. }
  384. }
  385. },
  386. /**
  387. * 保存行数据
  388. * @async
  389. * @param {AnnouncementItem} row - 行数据
  390. * @param {Function} done - 完成回调
  391. * @param {Function} loading - 加载回调
  392. * @returns {Promise<void>}
  393. */
  394. async rowSave(row, done, loading) {
  395. try {
  396. await add(row);
  397. this.onLoad(this.page);
  398. this.$message({
  399. type: "success",
  400. message: "操作成功!"
  401. });
  402. done();
  403. } catch (error) {
  404. console.log(error);
  405. loading();
  406. }
  407. },
  408. /**
  409. * 更新行数据
  410. * @async
  411. * @param {AnnouncementItem} row - 行数据
  412. * @param {number} index - 行索引
  413. * @param {Function} done - 完成回调
  414. * @param {Function} loading - 加载回调
  415. * @returns {Promise<void>}
  416. */
  417. async rowUpdate(row, index, done, loading) {
  418. try {
  419. await update(row);
  420. this.onLoad(this.page);
  421. this.$message({
  422. type: "success",
  423. message: "操作成功!"
  424. });
  425. done();
  426. } catch (error) {
  427. console.log(error);
  428. loading();
  429. }
  430. },
  431. /**
  432. * 删除行数据
  433. * @async
  434. * @param {AnnouncementItem} row - 行数据
  435. * @returns {Promise<void>}
  436. */
  437. async rowDel(row) {
  438. try {
  439. await this.$confirm("确定将选择数据删除?", {
  440. confirmButtonText: "确定",
  441. cancelButtonText: "取消",
  442. type: "warning"
  443. });
  444. await remove(row.id);
  445. this.onLoad(this.page);
  446. this.$message({
  447. type: "success",
  448. message: "操作成功!"
  449. });
  450. } catch (error) {
  451. // 用户取消删除或删除失败
  452. console.log(error);
  453. }
  454. },
  455. /**
  456. * 重置搜索
  457. * @returns {void}
  458. */
  459. searchReset() {
  460. this.query = {};
  461. this.onLoad(this.page);
  462. },
  463. /**
  464. * 搜索变化
  465. * @param {QueryParams} params - 搜索参数
  466. * @param {Function} done - 完成回调
  467. * @returns {void}
  468. */
  469. searchChange(params, done) {
  470. this.query = params;
  471. this.page.currentPage = 1;
  472. this.onLoad(this.page, params);
  473. done();
  474. },
  475. /**
  476. * 选择变化
  477. * @param {Array<AnnouncementItem>} list - 选中的数据列表
  478. * @returns {void}
  479. */
  480. selectionChange(list) {
  481. this.selectionList = list;
  482. },
  483. /**
  484. * 清空选择
  485. * @returns {void}
  486. */
  487. selectionClear() {
  488. this.selectionList = [];
  489. this.$refs.crud.toggleSelection();
  490. },
  491. /**
  492. * 批量删除
  493. * @async
  494. * @returns {Promise<void>}
  495. */
  496. async handleDelete() {
  497. if (this.selectionList.length === 0) {
  498. this.$message.warning("请选择至少一条数据");
  499. return;
  500. }
  501. try {
  502. await this.$confirm("确定将选择数据删除?", {
  503. confirmButtonText: "确定",
  504. cancelButtonText: "取消",
  505. type: "warning"
  506. });
  507. await remove(this.ids);
  508. this.onLoad(this.page);
  509. this.$message({
  510. type: "success",
  511. message: "操作成功!"
  512. });
  513. this.$refs.crud.toggleSelection();
  514. } catch (error) {
  515. // 用户取消删除或删除失败
  516. console.log(error);
  517. }
  518. },
  519. /**
  520. * 打开前回调
  521. * @async
  522. * @param {Function} done - 完成回调
  523. * @param {string} type - 操作类型
  524. * @returns {Promise<void>}
  525. */
  526. async beforeOpen(done, type) {
  527. if (["edit", "view"].includes(type)) {
  528. try {
  529. const res = await getAnnouncement(this.form.id);
  530. this.form = res.data.data;
  531. } catch (error) {
  532. console.log(error);
  533. }
  534. }
  535. done();
  536. },
  537. /**
  538. * 当前页变化
  539. * @param {number} currentPage - 当前页码
  540. * @returns {void}
  541. */
  542. currentChange(currentPage) {
  543. this.page.currentPage = currentPage;
  544. },
  545. /**
  546. * 页大小变化
  547. * @param {number} pageSize - 页大小
  548. * @returns {void}
  549. */
  550. sizeChange(pageSize) {
  551. this.page.pageSize = pageSize;
  552. },
  553. /**
  554. * 刷新变化
  555. * @returns {void}
  556. */
  557. refreshChange() {
  558. this.onLoad(this.page, this.query);
  559. },
  560. /**
  561. * 加载数据
  562. * @async
  563. * @param {PageInfo} page - 分页信息
  564. * @param {QueryParams} [params={}] - 查询参数
  565. * @returns {Promise<void>}
  566. */
  567. async onLoad(page, params = {}) {
  568. const { publishTime } = this.query;
  569. let values = {
  570. ...params,
  571. };
  572. if (publishTime) {
  573. values = {
  574. ...params,
  575. publishTimeStart: publishTime[0],
  576. publishTimeEnd: publishTime[1],
  577. ...this.query
  578. };
  579. values.publishTime = null;
  580. }
  581. this.loading = true;
  582. try {
  583. const res = await getList(page.currentPage, page.pageSize, values);
  584. const data = res.data.data;
  585. this.page.total = data.total;
  586. this.data = data.records;
  587. this.loading = false;
  588. this.selectionClear();
  589. } catch (error) {
  590. console.log(error);
  591. this.loading = false;
  592. }
  593. }
  594. }
  595. };
  596. </script>
  597. <style scoped>
  598. .detail-content {
  599. padding: 20px;
  600. }
  601. .detail-info {
  602. margin: 20px 0;
  603. padding: 15px;
  604. background-color: #f5f5f5;
  605. border-radius: 4px;
  606. }
  607. .detail-info p {
  608. margin: 8px 0;
  609. }
  610. .detail-body {
  611. margin-top: 20px;
  612. padding: 15px;
  613. border: 1px solid #e4e7ed;
  614. border-radius: 4px;
  615. min-height: 200px;
  616. }
  617. </style>