category.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <template>
  2. <div>
  3. <basic-container>
  4. <avue-crud :option="option" :table-loading="loading" :data="data" ref="crud" v-model="form"
  5. :permission="permissionList" :before-open="beforeOpen" :before-close="beforeClose" @row-del="rowDel"
  6. @row-update="rowUpdate" @row-save="rowSave" @search-change="searchChange" @search-reset="searchReset"
  7. @selection-change="selectionChange" @refresh-change="refreshChange" @on-load="onLoad">
  8. <template slot="menuLeft">
  9. <el-button type="danger" size="small" icon="el-icon-delete" v-if="permission.category_delete" plain
  10. @click="handleDelete">
  11. 删除
  12. </el-button>
  13. </template>
  14. <template slot-scope="{ row }" slot="status">
  15. <el-switch v-model="row.status" :active-value="1" :inactive-value="0" active-color="#13ce66"
  16. inactive-color="#ff4949" @change="handleStatusChange(row)"
  17. >
  18. </el-switch>
  19. </template>
  20. <template slot-scope="{ row }" slot="isSystem">
  21. <el-tag :type="row.isSystem ? 'warning' : 'success'">
  22. {{ row.isSystem ? '系统分类' : '自定义分类' }}
  23. </el-tag>
  24. </template>
  25. </avue-crud>
  26. </basic-container>
  27. </div>
  28. </template>
  29. <script>
  30. /**
  31. * 公告分类管理页面
  32. * @description 提供公告分类的完整CRUD功能,包括状态管理
  33. * @version 1.0.0
  34. */
  35. import {
  36. getCategoryList,
  37. addCategory,
  38. updateCategory,
  39. removeCategory,
  40. getCategoryDetail,
  41. updateCategoryStatus
  42. } from "@/api/announcement/category";
  43. import { mapGetters } from "vuex";
  44. /**
  45. * 分类数据类型定义
  46. * @typedef {Object} CategoryItem
  47. * @property {number} id - 分类ID
  48. * @property {string} name - 分类名称
  49. * @property {number} sortOrder - 排序
  50. * @property {number} orgId - 组织ID
  51. * @property {string} orgCode - 组织编码
  52. * @property {string} orgName - 组织名称
  53. * @property {number} isSystem - 是否系统分类 (0-否, 1-是)
  54. * @property {string} remark - 备注
  55. * @property {number} createUser - 创建用户ID
  56. * @property {number} createDept - 创建部门ID
  57. * @property {string|null} createTime - 创建时间
  58. * @property {number|null} updateUser - 更新用户ID
  59. * @property {string|null} updateTime - 更新时间
  60. * @property {number} status - 状态 (0-禁用, 1-启用)
  61. * @property {number} isDeleted - 是否删除 (0-否, 1-是)
  62. */
  63. /**
  64. * 分类表单数据类型
  65. * @typedef {Object} CategoryForm
  66. * @property {number} [id] - 分类ID(编辑时存在)
  67. * @property {string} name - 分类名称
  68. * @property {number} sortOrder - 排序
  69. * @property {number} orgId - 组织ID
  70. * @property {string} orgCode - 组织编码
  71. * @property {string} orgName - 组织名称
  72. * @property {string} remark - 备注
  73. * @property {number} status - 状态
  74. */
  75. /**
  76. * 查询参数类型
  77. * @typedef {Object} QueryParams
  78. * @property {string} [name] - 分类名称
  79. * @property {string} [orgName] - 组织名称
  80. * @property {number} [status] - 状态
  81. */
  82. export default {
  83. name: "CategoryManagement",
  84. data() {
  85. return {
  86. /** @type {CategoryForm} 表单数据 */
  87. form: {},
  88. /** @type {Array<CategoryItem>} 选中的行数据 */
  89. selectionList: [],
  90. /** @type {QueryParams} 查询条件 */
  91. query: {},
  92. /** @type {boolean} 表格加载状态 */
  93. loading: true,
  94. /** @type {Array<CategoryItem>} 表格数据 */
  95. data: [],
  96. /** @type {Object} 表格配置选项 */
  97. option: {
  98. height: 'auto',
  99. calcHeight: 30,
  100. tip: false,
  101. searchShow: true,
  102. searchMenuSpan: 6,
  103. border: true,
  104. index: true,
  105. selection: true,
  106. viewBtn: true,
  107. dialogClickModal: false,
  108. column: [
  109. {
  110. label: "分类名称",
  111. prop: "name",
  112. search: true,
  113. rules: [
  114. {
  115. required: true,
  116. message: "请输入分类名称",
  117. trigger: "blur"
  118. },
  119. {
  120. min: 2,
  121. max: 50,
  122. message: "分类名称长度在2到50个字符",
  123. trigger: "blur"
  124. }
  125. ]
  126. },
  127. {
  128. label: "组织名称",
  129. prop: "orgName",
  130. search: true,
  131. rules: [
  132. {
  133. required: true,
  134. message: "请输入组织名称",
  135. trigger: "blur"
  136. }
  137. ]
  138. },
  139. {
  140. label: "组织编码",
  141. prop: "orgCode",
  142. rules: [
  143. {
  144. required: true,
  145. message: "请输入组织编码",
  146. trigger: "blur"
  147. },
  148. {
  149. pattern: /^[A-Z0-9_-]+$/,
  150. message: "组织编码只能包含大写字母、数字、下划线和中横线",
  151. trigger: "blur"
  152. }
  153. ]
  154. },
  155. {
  156. label: "组织ID",
  157. prop: "orgId",
  158. type: "number",
  159. rules: [
  160. {
  161. required: true,
  162. message: "请输入组织ID",
  163. trigger: "blur"
  164. }
  165. ]
  166. },
  167. {
  168. label: "排序",
  169. prop: "sortOrder",
  170. type: "number",
  171. value: 0,
  172. rules: [
  173. {
  174. type: "number",
  175. min: 0,
  176. max: 9999,
  177. message: "排序值范围为0-9999",
  178. trigger: "blur"
  179. }
  180. ]
  181. },
  182. {
  183. label: "状态",
  184. prop: "status",
  185. type: "select",
  186. slot: true,
  187. dicData: [
  188. {
  189. label: "启用",
  190. value: 1
  191. },
  192. {
  193. label: "禁用",
  194. value: 0
  195. }
  196. ],
  197. search: true,
  198. value: 1
  199. },
  200. {
  201. label: "分类类型",
  202. prop: "isSystem",
  203. slot: true,
  204. addDisplay: false,
  205. editDisplay: false
  206. },
  207. {
  208. label: "备注",
  209. prop: "remark",
  210. type: "textarea",
  211. span: 24,
  212. hide: true,
  213. rules: [
  214. {
  215. max: 100,
  216. message: "备注不能超过100个字符",
  217. trigger: "blur"
  218. }
  219. ]
  220. },
  221. {
  222. label: "创建时间",
  223. prop: "createTime",
  224. type: "datetime",
  225. format: "yyyy-MM-dd HH:mm:ss",
  226. valueFormat: "yyyy-MM-dd HH:mm:ss",
  227. addDisplay: false,
  228. editDisplay: false,
  229. width: 180
  230. }
  231. ]
  232. }
  233. };
  234. },
  235. computed: {
  236. ...mapGetters(["permission", "userInfo"]),
  237. /**
  238. * 权限列表配置
  239. * @returns {Object} 权限配置对象
  240. */
  241. permissionList() {
  242. return {
  243. // addBtn: this.vaildData(this.permission.category_add, false),
  244. // viewBtn: this.vaildData(this.permission.category_view, false),
  245. // delBtn: this.vaildData(this.permission.category_delete, false),
  246. // editBtn: this.vaildData(this.permission.category_edit, false)
  247. addBtn: true,
  248. viewBtn: false,
  249. delBtn: false,
  250. editBtn: true,
  251. };
  252. },
  253. /**
  254. * 表格行主键
  255. * @returns {string} 主键字段名
  256. */
  257. ids() {
  258. const ids = [];
  259. this.selectionList.forEach(ele => {
  260. ids.push(ele.id);
  261. });
  262. return ids.join(",");
  263. }
  264. },
  265. methods: {
  266. /**
  267. * 删除选中的分类
  268. * @description 批量删除选中的分类
  269. */
  270. async handleDelete() {
  271. if (this.selectionList.length === 0) {
  272. this.$message.warning("请选择至少一条数据");
  273. return;
  274. }
  275. // 检查是否包含系统分类
  276. const hasSystemCategory = this.selectionList.some(item => item.isSystem === 1);
  277. if (hasSystemCategory) {
  278. this.$message.error("系统分类不能删除");
  279. return;
  280. }
  281. try {
  282. await this.$confirm("确定将选择数据删除?", {
  283. confirmButtonText: "确定",
  284. cancelButtonText: "取消",
  285. type: "warning"
  286. });
  287. await removeCategory(this.ids);
  288. this.onLoad();
  289. this.$message({
  290. type: "success",
  291. message: "操作成功!"
  292. });
  293. this.$refs.crud.toggleSelection();
  294. } catch (error) {
  295. this.$message({
  296. type: "info",
  297. message: "已取消删除"
  298. });
  299. }
  300. },
  301. /**
  302. * 处理状态变更
  303. * @param {CategoryItem} row - 行数据
  304. */
  305. async handleStatusChange(row) {
  306. const statusText = row.status === 1 ? '启用' : '禁用';
  307. try {
  308. await this.$confirm(`确定${statusText}该分类吗?`, {
  309. confirmButtonText: "确定",
  310. cancelButtonText: "取消",
  311. type: "warning"
  312. });
  313. // 复用现有的更新接口,传递完整的行数据
  314. await updateCategory({
  315. id: row.id,
  316. name: row.name,
  317. orgId: row.orgId,
  318. orgCode: row.orgCode,
  319. orgName: row.orgName,
  320. sortOrder: row.sortOrder,
  321. status: row.status, // 新的状态值
  322. remark: row.remark || ''
  323. });
  324. // 刷新页面数据
  325. this.onLoad();
  326. this.$message({
  327. type: "success",
  328. message: `${statusText}成功!`
  329. });
  330. } catch (error) {
  331. // 恢复原状态
  332. row.status = row.status === 1 ? 0 : 1;
  333. if (error !== 'cancel') {
  334. console.error('状态更新失败:', error);
  335. this.$message({
  336. type: "error",
  337. message: "状态更新失败"
  338. });
  339. } else {
  340. this.$message({
  341. type: "info",
  342. message: "已取消操作"
  343. });
  344. }
  345. }
  346. },
  347. /**
  348. * 表单打开前的回调
  349. * @param {Function} done - 完成回调
  350. * @param {string} type - 操作类型 (add/edit/view)
  351. */
  352. async beforeOpen(done, type) {
  353. if (["edit", "view"].includes(type)) {
  354. // try {
  355. // const res = await getCategoryDetail(this.form.id);
  356. // this.form = res.data.data;
  357. // } catch (error) {
  358. // console.error('获取分类详情失败:', error);
  359. // }
  360. } else if (type === "add") {
  361. // 新增时设置默认值
  362. this.form = {
  363. createDept: this.userInfo.deptId || 1,
  364. createUser: this.userInfo.userId || 1,
  365. orgId: 1,
  366. orgCode: "ORG_0001",
  367. orgName: "库比森",
  368. sortOrder: 0,
  369. status: 1,
  370. remark: ""
  371. };
  372. }
  373. done();
  374. },
  375. /**
  376. * 表单关闭前的回调
  377. * @param {Function} done - 完成回调
  378. */
  379. beforeClose(done) {
  380. this.form = {};
  381. done();
  382. },
  383. /**
  384. * 行删除回调
  385. * @param {CategoryItem} row - 行数据
  386. * @param {number} index - 行索引
  387. */
  388. async rowDel(row, index) {
  389. if (row.isSystem === 1) {
  390. this.$message.error("系统分类不能删除");
  391. return;
  392. }
  393. try {
  394. await this.$confirm("确定将选择数据删除?", {
  395. confirmButtonText: "确定",
  396. cancelButtonText: "取消",
  397. type: "warning"
  398. });
  399. await removeCategory(row.id);
  400. this.onLoad();
  401. this.$message({
  402. type: "success",
  403. message: "操作成功!"
  404. });
  405. } catch (error) {
  406. console.error('删除分类失败:', error);
  407. }
  408. },
  409. /**
  410. * 行更新回调
  411. * @param {CategoryForm} row - 行数据
  412. * @param {number} index - 行索引
  413. * @param {Function} done - 完成回调
  414. * @param {Function} loading - 加载状态回调
  415. */
  416. async rowUpdate(row, index, done, loading) {
  417. try {
  418. await updateCategory(row);
  419. this.onLoad();
  420. this.$message({
  421. type: "success",
  422. message: "操作成功!"
  423. });
  424. done();
  425. } catch (error) {
  426. console.error('更新分类失败:', error);
  427. loading();
  428. }
  429. },
  430. /**
  431. * 行保存回调
  432. * @param {CategoryForm} row - 行数据
  433. * @param {Function} done - 完成回调
  434. * @param {Function} loading - 加载状态回调
  435. */
  436. async rowSave(row, done, loading) {
  437. try {
  438. await addCategory(row);
  439. this.onLoad();
  440. this.$message({
  441. type: "success",
  442. message: "操作成功!"
  443. });
  444. done();
  445. } catch (error) {
  446. console.error('新增分类失败:', error);
  447. loading();
  448. }
  449. },
  450. /**
  451. * 搜索条件变化回调
  452. * @param {QueryParams} params - 搜索参数
  453. * @param {Function} done - 完成回调
  454. */
  455. searchChange(params, done) {
  456. this.query = params;
  457. this.onLoad(params);
  458. done();
  459. },
  460. /**
  461. * 搜索重置回调
  462. */
  463. searchReset() {
  464. this.query = {};
  465. this.onLoad();
  466. },
  467. /**
  468. * 选择变化回调
  469. * @param {Array<CategoryItem>} list - 选中的行数据列表
  470. */
  471. selectionChange(list) {
  472. this.selectionList = list;
  473. },
  474. /**
  475. * 清空选择
  476. */
  477. selectionClear() {
  478. this.selectionList = [];
  479. this.$refs.crud.toggleSelection();
  480. },
  481. /**
  482. * 刷新回调
  483. */
  484. refreshChange() {
  485. this.onLoad(this.query);
  486. },
  487. /**
  488. * 加载数据
  489. * @param {QueryParams} params - 查询参数
  490. */
  491. async onLoad(params = {}) {
  492. this.loading = true;
  493. try {
  494. const res = await getCategoryList(Object.assign(params, this.query));
  495. const data = res.data.data;
  496. this.data = Array.isArray(data) ? data : [];
  497. this.selectionClear();
  498. } catch (error) {
  499. console.error('加载分类列表失败:', error);
  500. this.data = [];
  501. } finally {
  502. this.loading = false;
  503. }
  504. }
  505. },
  506. /**
  507. * 组件挂载后初始化数据
  508. */
  509. mounted() {
  510. this.onLoad();
  511. }
  512. };
  513. </script>
  514. <style scoped>
  515. /* 组件样式 */
  516. .el-tag {
  517. margin-right: 8px;
  518. }
  519. .el-switch {
  520. margin: 0;
  521. }
  522. </style>