Form.vue 13.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
<template>
	<el-dialog 
		:title="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'" 
		:close-on-click-modal="false" 
		:visible.sync="visible" 
		class="NCC-dialog NCC-dialog_center" 
		lock-scroll 
		width="900px"
	>
		<el-form ref="elForm" :model="dataForm" size="small" label-width="120px" label-position="right" :disabled="!!isDetail" :rules="rules">
			<el-row :gutter="15">
				<!-- <el-col :span="24">
					<el-form-item label="配置编号" prop="id">
						<el-input v-model="dataForm.id" placeholder="自动生成" clearable readonly :style='{"width":"100%"}' />
					</el-form-item>
				</el-col> -->
				<el-col :span="24">
					<el-form-item label="工作流名称" prop="workflowName">
						<el-input v-model="dataForm.workflowName" placeholder="请输入工作流名称" clearable :style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="是否启用" prop="isEnabled">
						<el-switch v-model="dataForm.isEnabled" :active-value="1" :inactive-value="0"  />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="描述" prop="description">
						<el-input v-model="dataForm.description" type="textarea" :rows="3" placeholder="请输入描述" clearable :style='{"width":"100%"}' />
					</el-form-item>
				</el-col>
			</el-row>

			<!-- 审批节点配置 -->
			<el-card class="form-card" shadow="hover">
				<div slot="header" class="card-header">
					<i class="el-icon-s-order"></i>
					<span class="card-title">审批节点配置(1-5个节点)</span>
					<el-button 
						v-if="!isDetail"
						type="text" 
						icon="el-icon-plus" 
						@click="addNode"
						style="margin-left: auto;"
						:disabled="dataForm.nodes.length >= 5"
					>
						添加节点
					</el-button>
				</div>
				<div class="nodes-list">
					<div 
						v-for="(node, index) in dataForm.nodes" 
						:key="index"
						class="node-item"
					>
						<div class="node-header">
							<span class="node-order">节点 {{ node.nodeOrder }}</span>
							<el-button 
								v-if="!isDetail"
								type="text" 
								icon="el-icon-delete" 
								@click="removeNode(index)"
								style="color: #F56C6C"
								:disabled="dataForm.nodes.length <= 1"
							>
								删除
							</el-button>
						</div>
						<el-row :gutter="20">
							<el-col :span="12">
								<el-form-item 
									:label="`节点${node.nodeOrder}名称`" 
									:prop="`nodes.${index}.nodeName`"
									:rules="{ required: true, message: '请输入节点名称', trigger: 'blur' }"
								>
									<el-input 
										v-model="node.nodeName" 
										placeholder="请输入节点名称"
									/>
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item 
									:label="`节点${node.nodeOrder}类型`" 
									:prop="`nodes.${index}.approvalType`"
									:rules="{ required: true, message: '请选择审批类型', trigger: 'change' }"
								>
								<el-select 
									v-model="node.approvalType" 
									placeholder="请选择审批类型"
									style="width: 100%"
								>
									<el-option label="会签" :value="1" />
									<el-option label="或签" :value="0" />
								</el-select>
								</el-form-item>
							</el-col>
							<el-col :span="24">
								<el-form-item 
									:label="`节点${node.nodeOrder}审批人`" 
									:prop="`nodes.${index}.approverIds`"
									:rules="{ required: true, message: '请选择至少一个审批人', trigger: 'change' }"
								>
									<user-select 
										v-model="node.approverIds" 
										placeholder="请选择审批人" 
										:multiple="true"
										@change="(val) => handleApproverChange(index, val)"
										style="width: 100%"
									/>
								</el-form-item>
							</el-col>
							<el-col :span="24" v-if="node.approverNames && node.approverNames.length > 0">
								<el-form-item label="已选审批人">
									<el-tag 
										v-for="(name, nameIndex) in node.approverNames" 
										:key="nameIndex"
										style="margin-right: 8px;"
									>
										{{ name }}
									</el-tag>
								</el-form-item>
							</el-col>
						</el-row>
					</div>
					<div v-if="dataForm.nodes.length === 0" class="empty-nodes">
						<el-empty description="请添加审批节点(至少1个)" :image-size="80" />
					</div>
				</div>
			</el-card>
		</el-form>

		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
	import request from '@/utils/request'
	import { getUserInfoList } from '@/api/permission/user'
	import userSelect from '@/components/NCC-userSelect'
	
	export default {
		components: {
			userSelect
		},
		props: [],
		data() {
			return {
				loading: false,
				visible: false,
				isDetail: false,
				dataForm: {
					id: undefined,
					workflowName: undefined,
					isEnabled: 1,
					description: undefined,
					nodes: []
				},
				rules: {
					workflowName: [
						{ required: true, message: '请输入工作流名称', trigger: 'blur' }
					],
				},
			}
		},
		computed: {},
		watch: {},
		created() {
		},
		mounted() {
		},
		methods: {
			goBack() {
				this.$emit('refresh')
			},
			init(id, isDetail) {
				this.dataForm.id = id || 0;
				this.visible = true;
				this.isDetail = isDetail || false;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						request({
							url: '/api/Extend/LqReimbursementWorkflowConfig/' + this.dataForm.id,
							method: 'get'
						}).then(res => {
							this.dataForm = res.data;
							// 确保 nodes 是数组
							if (!this.dataForm.nodes || !Array.isArray(this.dataForm.nodes)) {
								this.dataForm.nodes = [];
							}
							// 处理节点数据格式转换
							this.dataForm.nodes.forEach(node => {
								// 处理审批人数据:后端返回的是 approvers 数组
								if (Array.isArray(node.approvers) && node.approvers.length > 0) {
									// 从 approvers 数组中提取 userId 和 userName
									const userIds = node.approvers.map(approver => approver.userId).filter(id => id);
									const userNames = node.approvers.map(approver => approver.userName).filter(name => name);
									
									// user-select 组件需要逗号分隔的字符串
									node.approverIds = userIds.join(',');
									node.approverNames = userNames;
								} else {
									// 如果没有 approvers,尝试从 approverIds 和 approverNames 获取
									if (Array.isArray(node.approverIds)) {
										node.approverIds = node.approverIds.filter(id => id && id.trim()).join(',');
									} else if (typeof node.approverIds === 'string') {
										node.approverIds = node.approverIds || '';
									} else {
										node.approverIds = '';
									}
									
									if (!Array.isArray(node.approverNames)) {
										node.approverNames = [];
									}
								}
								
								// 处理 approvalType:可能是字符串 "1" 或 "0"
								if (typeof node.approvalType === 'string') {
									if (node.approvalType === '会签' || node.approvalType === '1') {
										node.approvalType = 1;
									} else if (node.approvalType === '或签' || node.approvalType === '0') {
										node.approvalType = 0;
									} else {
										node.approvalType = 1; // 默认会签
									}
								} else if (typeof node.approvalType === 'number') {
									// 确保是 0 或 1
									node.approvalType = node.approvalType == 0 ? 0 : 1;
								} else {
									node.approvalType = 1; // 默认会签
								}
							});
						})
					} else {
						// 新建时初始化一个节点
						this.dataForm.nodes = [{
							nodeOrder: 1,
							nodeName: '',
							approvalType: 1, // 默认会签
							approverIds: '', // user-select组件需要字符串格式
							approverNames: []
						}];
					}
				})
			},
			addNode() {
				if (this.dataForm.nodes.length >= 5) {
					this.$message.warning('最多只能添加5个节点');
					return;
				}
				const newNode = {
					nodeOrder: this.dataForm.nodes.length + 1,
					nodeName: '',
					approvalType: 1, // 默认会签
					approverIds: '', // user-select组件需要字符串格式
					approverNames: []
				};
				this.dataForm.nodes.push(newNode);
			},
			removeNode(index) {
				if (this.dataForm.nodes.length <= 1) {
					this.$message.warning('至少需要保留一个节点');
					return;
				}
				this.dataForm.nodes.splice(index, 1);
				// 重新排序
				this.dataForm.nodes.forEach((node, idx) => {
					node.nodeOrder = idx + 1;
				});
			},
			async handleApproverChange(index, val) {
				const node = this.dataForm.nodes[index];
				
				// user-select组件返回的是逗号分隔的字符串
				const idsString = val || '';
				// 保存为字符串格式(user-select组件需要字符串)
				node.approverIds = idsString;
				
				// 转换为数组用于获取用户名
				const idsArray = idsString ? idsString.split(',').filter(id => id && id.trim()) : [];
				
				// 获取用户名列表
				if (idsArray.length > 0) {
					try {
						const response = await getUserInfoList(idsArray);
						if (response.code === 200 && response.data && response.data.list) {
							// 创建ID到用户信息的映射,确保顺序正确
							const userMap = new Map();
							response.data.list.forEach(user => {
								userMap.set(user.id, user.fullName || user.realName || user.id);
							});
							
							// 按照原始ID数组的顺序,从映射中取出对应的用户名
							node.approverNames = idsArray.map(id => {
								return userMap.get(id) || id;
							});
						} else {
							node.approverNames = idsArray;
						}
					} catch (error) {
						console.error('获取用户名失败:', error);
						// 如果获取失败,使用ID作为名称
						node.approverNames = idsArray;
					}
				} else {
					node.approverNames = [];
				}
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
					if (valid) {
						// 验证节点配置
						if (!this.dataForm.nodes || this.dataForm.nodes.length === 0) {
							this.$message.error('至少需要配置一个审批节点');
							return;
						}
						
						// 验证每个节点
						for (let i = 0; i < this.dataForm.nodes.length; i++) {
							const node = this.dataForm.nodes[i];
							if (!node.nodeName) {
								this.$message.error(`节点${i + 1}的名称不能为空`);
								return;
							}
							if (node.approvalType != 0 && node.approvalType != 1) {
								this.$message.error(`节点${i + 1}的审批类型不能为空`);
								return;
							}
							// 验证审批人:approverIds 可能是字符串或数组
							const approverIdsArray = typeof node.approverIds === 'string' 
								? (node.approverIds ? node.approverIds.split(',').filter(id => id && id.trim()) : [])
								: (Array.isArray(node.approverIds) ? node.approverIds : []);
							if (approverIdsArray.length === 0) {
								this.$message.error(`节点${i + 1}至少需要选择一个审批人`);
								return;
							}
						}
						
						// 准备提交数据
						const submitData = {
							workflowName: this.dataForm.workflowName,
							isEnabled: this.dataForm.isEnabled,
							description: this.dataForm.description,
							nodes: this.dataForm.nodes.map(node => {
								// 将 approverIds 从字符串转换为数组
								let approverIdsArray = [];
								if (typeof node.approverIds === 'string' && node.approverIds) {
									approverIdsArray = node.approverIds.split(',').filter(id => id && id.trim());
								} else if (Array.isArray(node.approverIds)) {
									approverIdsArray = node.approverIds.filter(id => id && id.trim());
								}
								
								return {
									nodeOrder: node.nodeOrder,
									nodeName: node.nodeName,
									approvalType: node.approvalType,
									isRequired: node.isRequired || 1,
									approverIds: approverIdsArray,
									approverNames: Array.isArray(node.approverNames) ? node.approverNames : []
								};
							})
						};
						
						if (!this.dataForm.id) {
							request({
								url: `/api/Extend/LqReimbursementWorkflowConfig`,
								method: 'post',
								data: submitData,
							}).then((res) => {
								this.$message({
									message: res.msg,
									type: 'success',
									duration: 1000,
									onClose: () => {
										this.visible = false;
										this.$emit('refresh', true)
									}
								})
							})
						} else {
							request({
								url: '/api/Extend/LqReimbursementWorkflowConfig/' + this.dataForm.id,
								method: 'PUT',
								data: submitData
							}).then((res) => {
								this.$message({
									message: res.msg,
									type: 'success',
									duration: 1000,
									onClose: () => {
										this.visible = false
										this.$emit('refresh', true)
									}
								})
							})
						}
					}
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
.form-card {
	margin-top: 20px;
	.card-header {
		display: flex;
		align-items: center;
		.card-title {
			margin-left: 8px;
			font-weight: 500;
		}
	}
}
.nodes-list {
	.node-item {
		padding: 16px;
		margin-bottom: 16px;
		border: 1px solid #EBEEF5;
		border-radius: 4px;
		background-color: #FAFAFA;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.node-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 16px;
			padding-bottom: 12px;
			border-bottom: 1px solid #EBEEF5;
			
			.node-order {
				font-weight: bold;
				font-size: 14px;
				color: #409EFF;
			}
		}
	}
	
	.empty-nodes {
		padding: 40px 0;
		text-align: center;
	}
}
</style>