index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. export default {
  44. name: 'AnnouncementIndex',
  45. data() {
  46. return {
  47. form: {},
  48. query: {},
  49. loading: true,
  50. detailVisible: false,
  51. currentDetail: {},
  52. page: {
  53. pageSize: 10,
  54. currentPage: 1,
  55. total: 0
  56. },
  57. selectionList: [],
  58. dealerOptions: [],
  59. brandOptions: [],
  60. categoryOptions: [], // 添加分类选项数组
  61. option: {
  62. height: 'auto',
  63. calcHeight: 30,
  64. dialogWidth: 950,
  65. tip: false,
  66. searchShow: true,
  67. searchMenuSpan: 6,
  68. border: true,
  69. index: true,
  70. viewBtn: true,
  71. selection: true,
  72. excelBtn: false, // 隐藏下载按钮
  73. columnBtn: false, // 隐藏列设置按钮
  74. dialogClickModal: false,
  75. column: [
  76. {
  77. label: "公告标题",
  78. prop: "title",
  79. span: 12,
  80. search: true,
  81. overHidden: true,
  82. rules: [{
  83. required: true,
  84. message: "请输入公告标题",
  85. trigger: "blur"
  86. }]
  87. },
  88. {
  89. label: "客户编号",
  90. prop: "customerCode",
  91. span: 12,
  92. search: true,
  93. overHidden: true
  94. },
  95. {
  96. label: "发布时间",
  97. prop: "publishTime",
  98. type: "daterange",
  99. format: "yyyy-MM-dd",
  100. valueFormat: "yyyy-MM-dd",
  101. rangeSeparator: "至",
  102. searchRange: true,
  103. startPlaceholder: "开始时间",
  104. endPlaceholder: "结束时间",
  105. overHidden: true,
  106. search: true,
  107. hide: true, // 在表格中隐藏,只用于搜索
  108. addDisplay: false, // 新增时不显示
  109. editDisplay: false, // 编辑时不显示
  110. viewDisplay: false // 查看时不显示
  111. },
  112. {
  113. label: "经销商",
  114. prop: "dealerId",
  115. type: "select",
  116. dicData: [],
  117. props: {
  118. label: "dealerName",
  119. value: "id"
  120. },
  121. slot: true,
  122. overHidden: true,
  123. search: true,
  124. span: 12,
  125. rules: [{
  126. required: true,
  127. message: "请选择经销商",
  128. trigger: "change"
  129. }]
  130. },
  131. {
  132. label: "品牌",
  133. prop: "brandId",
  134. type: "select",
  135. dicData: [],
  136. props: {
  137. label: "brandName",
  138. value: "id"
  139. },
  140. slot: true,
  141. overHidden: true,
  142. search: true,
  143. span: 12,
  144. rules: [{
  145. required: true,
  146. message: "请选择品牌",
  147. trigger: "change"
  148. }]
  149. },
  150. {
  151. label: "分类",
  152. prop: "categoryId",
  153. type: "select",
  154. dicData: [], // 初始为空,通过loadCategoryOptions方法动态加载
  155. props: {
  156. label: "categoryName", // 根据后端返回的字段名调整
  157. value: "id"
  158. },
  159. search: true,
  160. span: 12,
  161. rules: [{
  162. required: true,
  163. message: "请选择分类",
  164. trigger: "change"
  165. }]
  166. },
  167. {
  168. label: "角色",
  169. prop: "roleType",
  170. type: "select",
  171. dicData: [
  172. { label: "工厂", value: "factory" },
  173. { label: "经销商", value: "dealer" },
  174. { label: "零售商", value: "retailer" }
  175. ],
  176. search: true,
  177. span: 12,
  178. rules: [{
  179. required: true,
  180. message: "请选择角色",
  181. trigger: "change"
  182. }]
  183. },
  184. {
  185. label: "公告内容",
  186. prop: "content",
  187. component: 'AvueUeditor',
  188. options: {
  189. action: '/api/blade-resource/oss/endpoint/put-file',
  190. props: {
  191. res: "data",
  192. url: "link",
  193. }
  194. },
  195. showColumn: false,
  196. hide: true,
  197. minRows: 6,
  198. span: 24,
  199. rules: [{
  200. required: true,
  201. message: "请输入公告内容",
  202. trigger: "blur"
  203. }]
  204. },
  205. ]
  206. },
  207. data: []
  208. };
  209. },
  210. computed: {
  211. ...mapGetters(["permission"]),
  212. permissionList() {
  213. return {
  214. addBtn: this.vaildData(this.permission.announcement_add, false),
  215. viewBtn: this.vaildData(this.permission.announcement_view, false),
  216. delBtn: this.vaildData(this.permission.announcement_delete, false),
  217. editBtn: this.vaildData(this.permission.announcement_edit, false)
  218. };
  219. },
  220. ids() {
  221. let ids = [];
  222. this.selectionList.forEach(ele => {
  223. ids.push(ele.id);
  224. });
  225. return ids.join(",");
  226. }
  227. },
  228. created() {
  229. this.loadDealerOptions();
  230. this.loadBrandOptions();
  231. this.loadCategoryOptions(); // 添加分类加载
  232. },
  233. methods: {
  234. // 加载分类选项
  235. async loadCategoryOptions() {
  236. try {
  237. const res = await getCategoryList();
  238. this.categoryOptions = res.data.data || [];
  239. const categoryColumn = this.option.column.find(col => col.prop === 'categoryId');
  240. if (categoryColumn) {
  241. categoryColumn.dicData = this.categoryOptions;
  242. }
  243. } catch (error) {
  244. // 如果接口不存在,使用模拟数据
  245. this.categoryOptions = [
  246. { id: 1, categoryName: '系统公告', value: 1, label: '系统公告' },
  247. { id: 2, categoryName: '产品公告', value: 2, label: '产品公告' },
  248. { id: 3, categoryName: '活动公告', value: 3, label: '活动公告' },
  249. { id: 4, categoryName: '维护公告', value: 4, label: '维护公告' }
  250. ];
  251. const categoryColumn = this.option.column.find(col => col.prop === 'categoryId');
  252. if (categoryColumn) {
  253. categoryColumn.dicData = this.categoryOptions;
  254. }
  255. }
  256. },
  257. // 查看详情
  258. viewDetail(row) {
  259. this.currentDetail = row;
  260. this.detailVisible = true;
  261. },
  262. // 加载经销商选项
  263. async loadDealerOptions() {
  264. try {
  265. const res = await getDealerList();
  266. this.dealerOptions = res.data.data || [];
  267. const dealerColumn = this.option.column.find(col => col.prop === 'dealerId');
  268. if (dealerColumn) {
  269. dealerColumn.dicData = this.dealerOptions;
  270. }
  271. } catch (error) {
  272. // 如果接口不存在,使用模拟数据
  273. this.dealerOptions = [
  274. { id: 1, dealerName: '经销商A' },
  275. { id: 2, dealerName: '经销商B' },
  276. { id: 3, dealerName: '经销商C' }
  277. ];
  278. const dealerColumn = this.option.column.find(col => col.prop === 'dealerId');
  279. if (dealerColumn) {
  280. dealerColumn.dicData = this.dealerOptions;
  281. }
  282. }
  283. },
  284. // 加载品牌选项
  285. async loadBrandOptions() {
  286. try {
  287. const res = await getBrandList();
  288. this.brandOptions = res.data.data || [];
  289. const brandColumn = this.option.column.find(col => col.prop === 'brandId');
  290. if (brandColumn) {
  291. brandColumn.dicData = this.brandOptions;
  292. }
  293. } catch (error) {
  294. // 如果接口不存在,使用模拟数据
  295. this.brandOptions = [
  296. { id: 1, brandName: '品牌A' },
  297. { id: 2, brandName: '品牌B' },
  298. { id: 3, brandName: '品牌C' }
  299. ];
  300. const brandColumn = this.option.column.find(col => col.prop === 'brandId');
  301. if (brandColumn) {
  302. brandColumn.dicData = this.brandOptions;
  303. }
  304. }
  305. },
  306. async rowSave(row, done, loading) {
  307. try {
  308. await add(row);
  309. this.onLoad(this.page);
  310. this.$message({
  311. type: "success",
  312. message: "操作成功!"
  313. });
  314. done();
  315. } catch (error) {
  316. console.log(error);
  317. loading();
  318. }
  319. },
  320. async rowUpdate(row, index, done, loading) {
  321. try {
  322. await update(row);
  323. this.onLoad(this.page);
  324. this.$message({
  325. type: "success",
  326. message: "操作成功!"
  327. });
  328. done();
  329. } catch (error) {
  330. console.log(error);
  331. loading();
  332. }
  333. },
  334. async rowDel(row) {
  335. try {
  336. await this.$confirm("确定将选择数据删除?", {
  337. confirmButtonText: "确定",
  338. cancelButtonText: "取消",
  339. type: "warning"
  340. });
  341. await remove(row.id);
  342. this.onLoad(this.page);
  343. this.$message({
  344. type: "success",
  345. message: "操作成功!"
  346. });
  347. } catch (error) {
  348. // 用户取消删除或删除失败
  349. console.log(error);
  350. }
  351. },
  352. searchReset() {
  353. this.query = {};
  354. this.onLoad(this.page);
  355. },
  356. searchChange(params, done) {
  357. this.query = params;
  358. this.page.currentPage = 1;
  359. this.onLoad(this.page, params);
  360. done();
  361. },
  362. selectionChange(list) {
  363. this.selectionList = list;
  364. },
  365. selectionClear() {
  366. this.selectionList = [];
  367. this.$refs.crud.toggleSelection();
  368. },
  369. async handleDelete() {
  370. if (this.selectionList.length === 0) {
  371. this.$message.warning("请选择至少一条数据");
  372. return;
  373. }
  374. try {
  375. await this.$confirm("确定将选择数据删除?", {
  376. confirmButtonText: "确定",
  377. cancelButtonText: "取消",
  378. type: "warning"
  379. });
  380. await remove(this.ids);
  381. this.onLoad(this.page);
  382. this.$message({
  383. type: "success",
  384. message: "操作成功!"
  385. });
  386. this.$refs.crud.toggleSelection();
  387. } catch (error) {
  388. // 用户取消删除或删除失败
  389. console.log(error);
  390. }
  391. },
  392. async beforeOpen(done, type) {
  393. if (["edit", "view"].includes(type)) {
  394. try {
  395. const res = await getAnnouncement(this.form.id);
  396. this.form = res.data.data;
  397. } catch (error) {
  398. console.log(error);
  399. }
  400. }
  401. done();
  402. },
  403. currentChange(currentPage) {
  404. this.page.currentPage = currentPage;
  405. },
  406. sizeChange(pageSize) {
  407. this.page.pageSize = pageSize;
  408. },
  409. refreshChange() {
  410. this.onLoad(this.page, this.query);
  411. },
  412. async onLoad(page, params = {}) {
  413. const { publishTime } = this.query;
  414. let values = {
  415. ...params,
  416. };
  417. if (publishTime) {
  418. values = {
  419. ...params,
  420. publishTimeStart: publishTime[0],
  421. publishTimeEnd: publishTime[1],
  422. ...this.query
  423. };
  424. values.publishTime = null;
  425. }
  426. this.loading = true;
  427. try {
  428. const res = await getList(page.currentPage, page.pageSize, values);
  429. const data = res.data.data;
  430. this.page.total = data.total;
  431. this.data = data.records;
  432. this.loading = false;
  433. this.selectionClear();
  434. } catch (error) {
  435. console.log(error);
  436. this.loading = false;
  437. }
  438. }
  439. }
  440. };
  441. </script>
  442. <style scoped>
  443. .detail-content {
  444. padding: 20px;
  445. }
  446. .detail-info {
  447. margin: 20px 0;
  448. padding: 15px;
  449. background-color: #f5f5f5;
  450. border-radius: 4px;
  451. }
  452. .detail-info p {
  453. margin: 8px 0;
  454. }
  455. .detail-body {
  456. margin-top: 20px;
  457. padding: 15px;
  458. border: 1px solid #e4e7ed;
  459. border-radius: 4px;
  460. min-height: 200px;
  461. }
  462. </style>