From cbd757eeb57218668df5b1b9e61772b32fd0e21a Mon Sep 17 00:00:00 2001
From: rain <1679827795@qq.com>
Date: Thu, 27 Mar 2025 20:10:58 +0800
Subject: [PATCH] 机构管理处理行政区划代码绑定
---
src/views/system/dept.vue | 946 +++++++++++++++++++++++++++++++++++-----------------------
1 files changed, 564 insertions(+), 382 deletions(-)
diff --git a/src/views/system/dept.vue b/src/views/system/dept.vue
index 66a9305..6d95bfb 100644
--- a/src/views/system/dept.vue
+++ b/src/views/system/dept.vue
@@ -1,390 +1,572 @@
<template>
<basic-container>
- <avue-crud :option="option" :table-loading="loading" :data="data" ref="crud" v-model="form"
- :permission="permissionList" :before-open="beforeOpen" :before-close="beforeClose" @row-del="rowDel"
- @row-update="rowUpdate" @row-save="rowSave" @search-change="searchChange" @search-reset="searchReset"
- @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
- @refresh-change="refreshChange" @on-load="onLoad" @tree-load="treeLoad">
- <template #menu-left>
- <el-button type="danger" icon="el-icon-delete" v-if="permission.dept_delete" plain
- @click="handleDelete">删 除
- </el-button>
- </template>
- <template #menu="scope">
- <el-button type="primary" text icon="el-icon-plus" @click.stop="handleAdd(scope.row, scope.index)"
- v-if="userInfo.role_name.includes('admin')">新增子项
- </el-button>
- </template>
- <template #deptCategory="{ row }">
- <el-tag>{{ row.deptCategoryName }}</el-tag>
- </template>
- </avue-crud>
+ <avue-crud
+ :option="option"
+ :table-loading="loading"
+ :data="data"
+ ref="crud"
+ v-model="form"
+ :permission="permissionList"
+ :before-open="beforeOpen"
+ :before-close="beforeClose"
+ @row-del="rowDel"
+ @row-update="rowUpdate"
+ @row-save="rowSave"
+ @search-change="searchChange"
+ @search-reset="searchReset"
+ @selection-change="selectionChange"
+ @current-change="currentChange"
+ @size-change="sizeChange"
+ @refresh-change="refreshChange"
+ @on-load="onLoad"
+ @tree-load="treeLoad"
+ >
+ <template #menu-left>
+ <el-button
+ type="danger"
+ icon="el-icon-delete"
+ v-if="permission.dept_delete"
+ plain
+ @click="handleDelete"
+ >
+ 删除
+ </el-button>
+ </template>
+ <template #menu="scope">
+ <el-button
+ type="primary"
+ text
+ icon="el-icon-plus"
+ @click.stop="handleAdd(scope.row, scope.index)"
+ v-if="userInfo.role_name.includes('admin')"
+ >
+ 新增子项
+ </el-button>
+ </template>
+ <template #deptCategory="{ row }">
+ <el-tag>{{ row.deptCategoryName }}</el-tag>
+ </template>
+ <!-- 自定义 regionCode 的显示模板,确保显示单一值 -->
+ <template #regionCode="{ row }">
+ {{ row.regionCode }}
+ </template>
+ </avue-crud>
</basic-container>
-</template>
-
-<script>
- import {
- getLazyList,
- getChildLazyTree,
- remove,
- update,
- add,
- getDept,
- getDeptTree
- } from '@/api/system/dept';
- import {
- mapGetters
- } from 'vuex';
- import website from '@/config/website';
-
- export default {
- data() {
- return {
- form: {},
- selectionList: [],
- query: {},
- loading: true,
- parentId: 0,
- page: {
- pageSize: 10,
- currentPage: 1,
- total: 0,
- },
- option: {
- lazy: true,
- tip: false,
- simplePage: true,
- searchShow: true,
- searchMenuSpan: 6,
- tree: true,
- border: true,
- index: true,
- selection: true,
- viewBtn: true,
- menuWidth: 320,
- dialogClickModal: false,
- column: [{
- label: '机构名称',
- prop: 'deptName',
- search: true,
- labelWidth: 120,
- rules: [{
- required: true,
- message: '请输入机构名称',
- trigger: 'blur',
- }, ],
- },
- // {
- // label: '所属租户',
- // prop: 'tenantId',
- // type: 'tree',
- // dicUrl: '/blade-system/tenant/select',
- // addDisplay: false,
- // editDisplay: false,
- // viewDisplay: website.tenantMode,
- // span: 24,
- // props: {
- // label: 'tenantName',
- // value: 'tenantId',
- // },
- // hide: !website.tenantMode,
- // search: website.tenantMode,
- // rules: [
- // {
- // required: true,
- // message: '请输入所属租户',
- // trigger: 'click',
- // },
- // ],
- // },
- {
- label: '机构全称',
- prop: 'fullName',
- search: true,
- labelWidth: 120,
- rules: [{
- required: true,
- message: '请输入机构全称',
- trigger: 'blur',
- }, ],
- },
- {
- label: '上级机构',
- prop: 'parentId',
- dicData: [],
- type: 'tree',
- hide: true,
- addDisabled: false,
- props: {
- label: 'name',
- value:'id'
- },
- labelWidth: 120,
- rules: [{
- required: false,
- message: '请选择上级机构',
- trigger: 'click',
- }, ],
- },
- {
- label: '机构类型',
- type: 'select',
- dicUrl: '/blade-system/dict/dictionary?code=org_category',
- props: {
- label: 'dictValue',
- value: 'dictKey',
- },
- dataType: 'number',
- width: 120,
- labelWidth: 120,
- prop: 'deptCategory',
- slot: true,
- rules: [{
- required: true,
- message: '请输入机构类型',
- trigger: 'blur',
- }, ],
- },
- {
- label: '机构性质',
- type: 'select',
- dicUrl: '/blade-system/dict/dictionary?code=org_nature',
- props: {
- label: 'dictValue',
- value: 'dictKey',
- },
- dataType: 'number',
- width: 120,
- labelWidth: 120,
- prop: 'deptNature',
- slot: true,
- rules: [{
- required: true,
- message: '请输入机构性质',
- trigger: 'blur',
- }, ],
- },
- {
- label: '排序',
- prop: 'sort',
- type: 'number',
- align: 'right',
- labelWidth: 120,
- width: 80,
- rules: [{
- required: true,
- message: '请输入排序',
- trigger: 'blur',
- }, ],
- },
- {
- label: '组织id',
- prop: 'bingId',
- type: 'number',
- align: 'right',
- labelWidth: 120,
- addDisplay: false,
- editDisplay: false,
- width: 80,
- rules: [{
- required: true,
- message: '请输入组织id',
- trigger: 'blur',
- }, ],
- },
- {
- label: '备注',
- prop: 'remark',
- labelWidth: 120,
- rules: [{
- required: false,
- message: '请输入备注',
- trigger: 'blur',
- }, ],
- hide: true,
- },
- ],
- },
- data: [],
- };
+ </template>
+
+ <script>
+ import {
+ getLazyList,
+ getChildLazyTree,
+ remove,
+ update,
+ add,
+ getDept,
+ getDeptTree,
+ } from '@/api/system/dept';
+ import { getLazyTree } from '@/api/base/region';
+ import { mapGetters } from 'vuex';
+ import website from '@/config/website';
+
+ export default {
+ data() {
+ return {
+ form: {},
+ selectionList: [],
+ query: {},
+ loading: true,
+ parentId: 0,
+ page: {
+ pageSize: 10,
+ currentPage: 1,
+ total: 0,
},
- computed: {
- ...mapGetters(['userInfo', 'permission']),
- permissionList() {
- return {
- addBtn: this.validData(this.permission.dept_add, false),
- viewBtn: this.validData(this.permission.dept_view, false),
- delBtn: this.validData(this.permission.dept_delete, false),
- editBtn: this.validData(this.permission.dept_edit, false),
+ option: {
+ lazy: true,
+ tip: false,
+ simplePage: true,
+ searchShow: true,
+ searchMenuSpan: 6,
+ tree: true,
+ border: true,
+ index: true,
+ selection: true,
+ viewBtn: true,
+ menuWidth: 320,
+ dialogClickModal: false,
+ column: [
+ {
+ label: '机构名称',
+ prop: 'deptName',
+ search: true,
+ labelWidth: 120,
+ rules: [
+ {
+ required: true,
+ message: '请输入机构名称',
+ trigger: 'blur',
+ },
+ ],
+ },
+ {
+ label: '机构全称',
+ prop: 'fullName',
+ search: true,
+ labelWidth: 120,
+ rules: [
+ {
+ required: true,
+ message: '请输入机构全称',
+ trigger: 'blur',
+ },
+ ],
+ },
+ {
+ label: '上级机构',
+ prop: 'parentId',
+ dicData: [],
+ type: 'tree',
+ hide: true,
+ addDisabled: false,
+ props: {
+ label: 'name',
+ value: 'id',
+ },
+ labelWidth: 120,
+ rules: [
+ {
+ required: false,
+ message: '请选择上级机构',
+ trigger: 'click',
+ },
+ ],
+ },
+ {
+ label: '机构类型',
+ type: 'select',
+ dicUrl: '/blade-system/dict/dictionary?code=org_category',
+ props: {
+ label: 'dictValue',
+ value: 'dictKey',
+ },
+ dataType: 'number',
+ width: 120,
+ labelWidth: 120,
+ prop: 'deptCategory',
+ slot: true,
+ rules: [
+ {
+ required: true,
+ message: '请输入机构类型',
+ trigger: 'blur',
+ },
+ ],
+ },
+ {
+ label: '机构性质',
+ type: 'select',
+ dicUrl: '/blade-system/dict/dictionary?code=org_nature',
+ props: {
+ label: 'dictValue',
+ value: 'dictKey',
+ },
+ dataType: 'number',
+ width: 120,
+ labelWidth: 120,
+ prop: 'deptNature',
+ slot: true,
+ rules: [
+ {
+ required: true,
+ message: '请输入机构性质',
+ trigger: 'blur',
+ },
+ ],
+ },
+ {
+ label: '排序',
+ prop: 'sort',
+ type: 'number',
+ align: 'right',
+ labelWidth: 120,
+ width: 80,
+ rules: [
+ {
+ required: true,
+ message: '请输入排序',
+ trigger: 'blur',
+ },
+ ],
+ },
+ {
+ label: '组织id',
+ prop: 'bingId',
+ type: 'number',
+ align: 'right',
+ labelWidth: 120,
+ addDisplay: false,
+ editDisplay: false,
+ width: 80,
+ rules: [
+ {
+ required: true,
+ message: '请输入组织id',
+ trigger: 'blur',
+ },
+ ],
+ },
+ {
+ label: '行政区划',
+ prop: 'regionCode',
+ type: 'cascader',
+ labelWidth: 120,
+ props: {
+ label: 'title',
+ value: 'value',
+ emitPath: false, // 只返回最后选中的值
+ checkStrictly: true, // 可以选择任意一级
+ multiple: false, // 确保只能单选
+ expandTrigger: 'hover' // 鼠标悬停时展开子菜单
+ },
+ dataType: 'string', // 使用字符串类型
+ rules: [
+ {
+ required: true,
+ message: '请选择行政区划',
+ trigger: 'change'
+ }
+ ],
+ change: ({ value }) => {
+ if (!value) {
+ this.form.regionCode = null; // 未选择时置为空
+ } else {
+ // 如果 value 是逗号分隔的字符串,拆分并取最后一个值
+ if (typeof value === 'string' && value.includes(',')) {
+ const codes = value.split(',');
+ this.form.regionCode = codes[codes.length - 1];
+ } else {
+ this.form.regionCode = value; // 直接使用单一值
+ }
+ }
+ console.log('cascader change value:', value);
+ console.log('处理后的区县编码:', this.form.regionCode);
+ },
+ lazy: true,
+ lazyLoad(node, resolve) {
+ let level = node.level;
+ let list = [];
+ let callback = () => {
+ resolve(
+ (list || []).map((ele) => ({
+ ...ele,
+ value: ele.value, // 保持原始值类型(字符串)
+ leaf: level >= 2 // 县级为最后一级
+ }))
+ );
};
+
+ if (level === 0) {
+ getLazyTree('000000000000').then((res) => {
+ list = res.data.data;
+ callback();
+ });
+ } else if (level === 1) {
+ getLazyTree(node.value).then((res) => {
+ list = res.data.data;
+ callback();
+ });
+ } else if (level === 2) {
+ getLazyTree(node.value).then((res) => {
+ list = res.data.data;
+ callback();
+ });
+ } else {
+ callback();
+ }
+ }
},
- ids() {
- let ids = [];
- this.selectionList.forEach(ele => {
- ids.push(ele.id);
- });
- return ids.join(',');
+ {
+ label: '备注',
+ prop: 'remark',
+ labelWidth: 120,
+ rules: [
+ {
+ required: false,
+ message: '请输入备注',
+ trigger: 'blur',
+ },
+ ],
+ hide: true,
},
+ ],
},
- methods: {
- initData() {
- getDeptTree().then(res => {
- const column = this.findObject(this.option.column, 'parentId');
- column.dicData = res.data.data;
- });
- },
- handleAdd(row) {
- this.parentId = row.id;
- const column = this.findObject(this.option.column, 'parentId');
- column.value = row.id;
- column.addDisabled = true;
- this.$refs.crud.rowAdd();
- },
- rowSave(row, done, loading) {
- add(row).then(
- res => {
- // 获取新增数据的相关字段
- const data = res.data.data;
- row.id = data.id;
- row.deptCategoryName = data.deptCategoryName;
- row.tenantId = data.tenantId;
- this.$message({
- type: 'success',
- message: '操作成功!',
- });
- // 数据回调进行刷新
- done(row);
- },
- error => {
- window.console.log(error);
- loading();
- }
- );
- },
- rowUpdate(row, index, done, loading) {
- update(row).then(
- () => {
- this.$message({
- type: 'success',
- message: '操作成功!',
- });
- // 数据回调进行刷新
- done(row);
- },
- error => {
- window.console.log(error);
- loading();
- }
- );
- },
- rowDel(row, index, done) {
- this.$confirm('确定将选择数据删除?', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- type: 'warning',
- })
- .then(() => {
- return remove(row.id);
- })
- .then(() => {
- this.$message({
- type: 'success',
- message: '操作成功!',
- });
- // 数据回调进行刷新
- done(row);
- });
- },
- handleDelete() {
- if (this.selectionList.length === 0) {
- this.$message.warning('请选择至少一条数据');
- return;
- }
- this.$confirm('确定将选择数据删除?', {
- confirmButtonText: '确定',
- cancelButtonText: '取消',
- type: 'warning',
- })
- .then(() => {
- return remove(this.ids);
- })
- .then(() => {
- // 刷新表格数据并重载
- this.data = [];
- this.parentId = 0;
- this.$refs.crud.refreshTable();
- this.$refs.crud.toggleSelection();
- // 表格数据重载
- this.onLoad(this.page);
- this.$message({
- type: 'success',
- message: '操作成功!',
- });
- });
- },
- searchReset() {
- this.query = {};
- this.parentId = 0;
- this.onLoad(this.page);
- },
- searchChange(params, done) {
- this.query = params;
- this.parentId = '';
- this.page.currentPage = 1;
- this.onLoad(this.page, params);
- done();
- },
- selectionChange(list) {
- this.selectionList = list;
- },
- selectionClear() {
- this.selectionList = [];
- this.$refs.crud.toggleSelection();
- },
- beforeOpen(done, type) {
- if (['add', 'edit'].includes(type)) {
- this.initData();
- }
- if (['edit', 'view'].includes(type)) {
- getDept(this.form.id).then(res => {
- this.form = Object.assign(res.data.data, {
- hasChildren: this.form.hasChildren,
- });
- });
- }
- done();
- },
- beforeClose(done) {
- this.parentId = '';
- const column = this.findObject(this.option.column, 'parentId');
- column.value = '';
- column.addDisabled = false;
- done();
- },
- currentChange(currentPage) {
- this.page.currentPage = currentPage;
- },
- sizeChange(pageSize) {
- this.page.pageSize = pageSize;
- },
- refreshChange() {
- this.onLoad(this.page, this.query);
- },
- onLoad(page, params = {}) {
- this.loading = true;
- getLazyList(this.parentId, Object.assign(params, this.query)).then(res => {
- this.data = res.data.data;
- this.loading = false;
- this.selectionClear();
- });
- },
- treeLoad(tree, treeNode, resolve) {
- getChildLazyTree({parentId:tree.id}).then(res => {
- resolve(res.data.data);
- });
- },
- },
- };
-</script>
-
-<style></style>
\ No newline at end of file
+ data: [],
+ };
+ },
+ computed: {
+ ...mapGetters(['userInfo', 'permission']),
+ permissionList() {
+ return {
+ addBtn: this.validData(this.permission.dept_add, false),
+ viewBtn: this.validData(this.permission.dept_view, false),
+ delBtn: this.validData(this.permission.dept_delete, false),
+ editBtn: this.validData(this.permission.dept_edit, false),
+ };
+ },
+ ids() {
+ let ids = [];
+ this.selectionList.forEach((ele) => {
+ ids.push(ele.id);
+ });
+ return ids.join(',');
+ },
+ },
+ methods: {
+ initData() {
+ getDeptTree().then((res) => {
+ const column = this.findObject(this.option.column, 'parentId');
+ column.dicData = res.data.data;
+ });
+ },
+ handleAdd(row) {
+ this.parentId = row.id;
+ const column = this.findObject(this.option.column, 'parentId');
+ column.value = row.id;
+ column.addDisabled = true;
+ this.form.regionCode = null; // 初始化为空
+ this.$refs.crud.rowAdd();
+ },
+ validateSubmitData(row) {
+ if (!row.regionCode) {
+ this.$message.warning('请选择行政区划');
+ return false;
+ }
+ return true;
+ },
+ rowSave(row, done, loading) {
+ if (!this.validateSubmitData(row)) {
+ loading();
+ return;
+ }
+ console.log('--- Start rowSave ---');
+ console.log('Form data before save:', this.form);
+
+ // 处理 regionCode,确保只取最后一个值
+ let regionCode = this.form.regionCode;
+ if (Array.isArray(regionCode)) {
+ regionCode = regionCode[regionCode.length - 1]; // 数组取最后一个
+ } else if (typeof regionCode === 'string' && regionCode.includes(',')) {
+ const codes = regionCode.split(',');
+ regionCode = codes[codes.length - 1]; // 字符串拆分取最后一个
+ }
+ row.regionCode = regionCode || null; // 确保非空
+
+ console.log('Row data before submit:', row);
+
+ add(row).then(
+ (res) => {
+ console.log('Save response:', res.data);
+ const data = res.data.data;
+ row.id = data.id;
+ row.deptCategoryName = data.deptCategoryName;
+ row.tenantId = data.tenantId;
+ this.$message({
+ type: 'success',
+ message: '操作成功!'
+ });
+ this.onLoad(this.page);
+ done(row);
+ },
+ (error) => {
+ console.log('Save error:', error);
+ this.loading = false;
+ loading();
+ }
+ );
+ },
+ rowUpdate(row, index, done, loading) {
+ if (!this.validateSubmitData(row)) {
+ loading();
+ return;
+ }
+ console.log('--- Start rowUpdate ---');
+
+ // 处理 regionCode,确保只取最后一个值
+ let regionCode = this.form.regionCode;
+ if (Array.isArray(regionCode)) {
+ regionCode = regionCode[regionCode.length - 1]; // 数组取最后一个
+ } else if (typeof regionCode === 'string' && regionCode.includes(',')) {
+ const codes = regionCode.split(',');
+ regionCode = codes[codes.length - 1]; // 字符串拆分取最后一个
+ }
+ row.regionCode = regionCode || null; // 确保非空
+
+ console.log('Row data before submit:', row);
+
+ update(row).then(
+ (res) => {
+ console.log('Update response:', res.data);
+ this.$message({
+ type: 'success',
+ message: '操作成功!',
+ });
+ this.onLoad(this.page);
+ done(row);
+ },
+ (error) => {
+ console.log('Update error:', error);
+ this.loading = false;
+ loading();
+ }
+ ).catch((error) => {
+ console.log('Update promise rejected:', error);
+ this.loading = false;
+ });
+ },
+ rowDel(row, index, done) {
+ this.$confirm('确定将选择数据删除?', {
+ confirmButtonText: '确定',
+ cancelButtonText: '取消',
+ type: 'warning',
+ })
+ .then(() => {
+ return remove(row.id);
+ })
+ .then(() => {
+ this.$message({
+ type: 'success',
+ message: '操作成功!',
+ });
+ this.onLoad(this.page);
+ done(row);
+ });
+ },
+ handleDelete() {
+ if (this.selectionList.length === 0) {
+ this.$message.warning('请选择至少一条数据');
+ return;
+ }
+ this.$confirm('确定将选择数据删除?', {
+ confirmButtonText: '确定',
+ cancelButtonText: '取消',
+ type: 'warning',
+ })
+ .then(() => {
+ return remove(this.ids);
+ })
+ .then(() => {
+ this.data = [];
+ this.parentId = 0;
+ this.$refs.crud.refreshTable();
+ this.$refs.crud.toggleSelection();
+ this.onLoad(this.page);
+ this.$message({
+ type: 'success',
+ message: '操作成功!',
+ });
+ });
+ },
+ searchReset() {
+ this.query = {};
+ this.parentId = 0;
+ this.onLoad(this.page);
+ },
+ searchChange(params, done) {
+ this.query = params;
+ this.parentId = '';
+ this.page.currentPage = 1;
+ this.onLoad(this.page, params);
+ done();
+ },
+ selectionChange(list) {
+ this.selectionList = list;
+ },
+ selectionClear() {
+ this.selectionList = [];
+ this.$refs.crud.toggleSelection();
+ },
+ beforeOpen(done, type) {
+ if (['add', 'edit'].includes(type)) {
+ this.initData();
+ }
+ if (['edit', 'view'].includes(type)) {
+ getDept(this.form.id).then((res) => {
+ const regionCode = res.data.data.regionCode;
+ let finalRegionCode = regionCode;
+ if (typeof regionCode === 'string' && regionCode.includes(',')) {
+ const codes = regionCode.split(',');
+ finalRegionCode = codes[codes.length - 1]; // 取最后一个值
+ }
+ this.form = Object.assign({}, res.data.data, {
+ hasChildren: this.form.hasChildren,
+ regionCode: finalRegionCode ? String(finalRegionCode) : null // 确保是字符串
+ });
+ });
+ } else if (type === 'add') {
+ this.form.regionCode = null;
+ }
+ done();
+ },
+ beforeClose(done) {
+ this.parentId = '';
+ const column = this.findObject(this.option.column, 'parentId');
+ column.value = '';
+ column.addDisabled = false;
+ this.form.regionCode = null;
+ done();
+ },
+ currentChange(currentPage) {
+ this.page.currentPage = currentPage;
+ this.onLoad(this.page);
+ },
+ sizeChange(pageSize) {
+ this.page.pageSize = pageSize;
+ this.onLoad(this.page);
+ },
+ refreshChange() {
+ this.onLoad(this.page, this.query);
+ },
+ onLoad(page, params = {}) {
+ this.loading = true;
+ console.log('onLoad triggered with page:', page, 'params:', params);
+ console.log('Current parentId:', this.parentId, 'query:', this.query);
+ getLazyList(this.parentId, Object.assign(params, this.query)).then((res) => {
+ console.log('onLoad response:', res.data);
+ // 处理 regionCode,确保是单一字符串
+ const data = (res.data.data || []).map(item => {
+ let regionCode = item.regionCode;
+ // 如果是数组,取最后一个值
+ if (Array.isArray(regionCode)) {
+ regionCode = regionCode[regionCode.length - 1];
+ }
+ // 如果是逗号分隔的字符串,取最后一个值
+ else if (typeof regionCode === 'string' && regionCode.includes(',')) {
+ const codes = regionCode.split(',');
+ regionCode = codes[codes.length - 1];
+ }
+ return {
+ ...item,
+ regionCode: regionCode ? String(regionCode) : '' // 确保是字符串
+ };
+ });
+ this.data = data;
+ this.loading = false;
+ this.selectionClear();
+ console.log('Table data after onLoad:', this.data);
+ }).catch((error) => {
+ console.log('onLoad error:', error);
+ this.loading = false;
+ this.data = this.data; // 保留当前数据
+ console.log('Table data after onLoad error:', this.data);
+ });
+ },
+ treeLoad(tree, treeNode, resolve) {
+ getChildLazyTree({ parentId: tree.id }).then((res) => {
+ resolve(res.data.data);
+ });
+ },
+ },
+ };
+ </script>
+
+ <style></style>
\ No newline at end of file
--
Gitblit v1.9.3