diff --git a/User/views.py b/User/views.py index e2303a8..aac0d2d 100644 --- a/User/views.py +++ b/User/views.py @@ -947,35 +947,55 @@ class roxyExhibition(APIView): content = info.content # 处理title字段中的JSON格式数据,将委托人和相对方的JSON格式转换为只显示name - # 处理委托人:委托人:[{\"index\":1,\"name\":\"测试111\",\"idNumber\":\"111\"}] - title_client_pattern = r'委托人:(\[.*?\](?= vs |)|$))' + # 处理委托人:委托人:[{\"index\":1,\"name\":\"测试111\",\"idNumber\":\"111\"}] 或 委托人:[{\"index\":1 + title_client_pattern = r'委托人:(\[.*?)(?= vs |)|$)' def replace_title_client(match): json_str = match.group(1) try: # 处理转义的引号,将 \" 转换为 " json_str = json_str.replace('\\"', '"') - client_list = json.loads(json_str) + # 尝试补全不完整的 JSON(如果以 [ 开头但没有 ] 结尾) + if json_str.startswith('[') and not json_str.endswith(']'): + # 尝试找到最后一个完整的对象 + # 查找最后一个 "name" 字段 + name_match = re.search(r'"name"\s*:\s*"([^"]+)"', json_str) + if name_match: + return f"委托人:{name_match.group(1)}" + # 尝试解析完整的 JSON + client_list = json.loads(json_str + ']' if json_str.startswith('[') and not json_str.endswith(']') else json_str) if isinstance(client_list, list): names = [item.get('name', '') for item in client_list if isinstance(item, dict) and item.get('name')] return f"委托人:{'、'.join(names)}" if names else match.group(0) except (json.JSONDecodeError, TypeError, ValueError): - pass + # 如果解析失败,尝试直接提取 name 字段 + name_match = re.search(r'"name"\s*:\s*"([^"]+)"', json_str) + if name_match: + return f"委托人:{name_match.group(1)}" return match.group(0) title = re.sub(title_client_pattern, replace_title_client, title) - # 处理相对方:相对方:[{\"index\":1,\"name\":\"测试222\",\"idNumber\":\"222\"}] - title_party_pattern = r'相对方:(\[.*?\](?=)|$))' + # 处理相对方:相对方:[{\"index\":1,\"name\":\"测试222\",\"idNumber\":\"222\"}] 或 相对方:[{\"index\":1 + title_party_pattern = r'相对方:(\[.*?)(?=)|$)' def replace_title_party(match): json_str = match.group(1) try: # 处理转义的引号,将 \" 转换为 " json_str = json_str.replace('\\"', '"') - party_list = json.loads(json_str) + # 尝试补全不完整的 JSON + if json_str.startswith('[') and not json_str.endswith(']'): + name_match = re.search(r'"name"\s*:\s*"([^"]+)"', json_str) + if name_match: + return f"相对方:{name_match.group(1)}" + # 尝试解析完整的 JSON + party_list = json.loads(json_str + ']' if json_str.startswith('[') and not json_str.endswith(']') else json_str) if isinstance(party_list, list): names = [item.get('name', '') for item in party_list if isinstance(item, dict) and item.get('name')] return f"相对方:{'、'.join(names)}" if names else match.group(0) except (json.JSONDecodeError, TypeError, ValueError): - pass + # 如果解析失败,尝试直接提取 name 字段 + name_match = re.search(r'"name"\s*:\s*"([^"]+)"', json_str) + if name_match: + return f"相对方:{name_match.group(1)}" return match.group(0) title = re.sub(title_party_pattern, replace_title_party, title) @@ -1013,37 +1033,53 @@ class roxyExhibition(APIView): return match.group(0) content = re.sub(party_name_pattern, replace_party_name, content) - # 处理委托人:委托人:[{\"index\":1,\"name\":\"测试111\",\"idNumber\":\"111\"}] - # 匹配从"委托人:"开始到下一个","或")"或" - "或"相对方"或字符串结尾的JSON数组 - client_pattern = r'委托人:(\[.*?\](?=,|)|$| - |相对方))' + # 处理委托人:委托人:[{\"index\":1,\"name\":\"测试111\",\"idNumber\":\"111\"}] 或 委托人:[{\"index\":1 + client_pattern = r'委托人:(\[.*?)(?=,|)|$| - |相对方)' def replace_client(match): json_str = match.group(1) try: # 处理转义的引号,将 \" 转换为 " json_str = json_str.replace('\\"', '"') - client_list = json.loads(json_str) + # 尝试补全不完整的 JSON + if json_str.startswith('[') and not json_str.endswith(']'): + name_match = re.search(r'"name"\s*:\s*"([^"]+)"', json_str) + if name_match: + return f"委托人:{name_match.group(1)}" + # 尝试解析完整的 JSON + client_list = json.loads(json_str + ']' if json_str.startswith('[') and not json_str.endswith(']') else json_str) if isinstance(client_list, list): names = [item.get('name', '') for item in client_list if isinstance(item, dict) and item.get('name')] return f"委托人:{'、'.join(names)}" if names else match.group(0) except (json.JSONDecodeError, TypeError, ValueError): - pass + # 如果解析失败,尝试直接提取 name 字段 + name_match = re.search(r'"name"\s*:\s*"([^"]+)"', json_str) + if name_match: + return f"委托人:{name_match.group(1)}" return match.group(0) content = re.sub(client_pattern, replace_client, content) - # 处理相对方:相对方:[{\"index\":1,\"name\":\"测试222\",\"idNumber\":\"222\"}] - # 匹配从"相对方:"开始到下一个","或")"或" - "或字符串结尾的JSON数组 - party_pattern = r'相对方:(\[.*?\](?=,|)|$| - |\s))' + # 处理相对方:相对方:[{\"index\":1,\"name\":\"测试222\",\"idNumber\":\"222\"}] 或 相对方:[{\"index\":1 + party_pattern = r'相对方:(\[.*?)(?=,|)|$| - |\s)' def replace_party(match): json_str = match.group(1) try: # 处理转义的引号,将 \" 转换为 " json_str = json_str.replace('\\"', '"') - party_list = json.loads(json_str) + # 尝试补全不完整的 JSON + if json_str.startswith('[') and not json_str.endswith(']'): + name_match = re.search(r'"name"\s*:\s*"([^"]+)"', json_str) + if name_match: + return f"相对方:{name_match.group(1)}" + # 尝试解析完整的 JSON + party_list = json.loads(json_str + ']' if json_str.startswith('[') and not json_str.endswith(']') else json_str) if isinstance(party_list, list): names = [item.get('name', '') for item in party_list if isinstance(item, dict) and item.get('name')] return f"相对方:{'、'.join(names)}" if names else match.group(0) except (json.JSONDecodeError, TypeError, ValueError): - pass + # 如果解析失败,尝试直接提取 name 字段 + name_match = re.search(r'"name"\s*:\s*"([^"]+)"', json_str) + if name_match: + return f"相对方:{name_match.group(1)}" return match.group(0) content = re.sub(party_pattern, replace_party, content) @@ -1212,10 +1248,46 @@ class roxyExhibition(APIView): exclude_project_id=project_id ) - # 添加三个冲突字段 - itme["prefiling_conflicts"] = conflict_records.get('prefiling_conflicts', []) - itme["project_conflicts"] = conflict_records.get('project_conflicts', []) - itme["bid_conflicts"] = conflict_records.get('bid_conflicts', []) + # 处理冲突记录中的JSON格式数据,只显示name字段 + def process_conflict_records(records): + """处理冲突记录中的JSON格式字段,只提取name""" + processed = [] + for record in records: + new_record = dict(record) + # 处理 client_info 字段 + if 'client_info' in new_record and new_record['client_info']: + try: + client_data = json.loads(new_record['client_info']) + if isinstance(client_data, list): + names = [item.get('name', '') for item in client_data if isinstance(item, dict) and item.get('name')] + new_record['client_info'] = '、'.join(names) if names else new_record['client_info'] + except (json.JSONDecodeError, TypeError, ValueError): + pass + # 处理 party_info 字段 + if 'party_info' in new_record and new_record['party_info']: + try: + party_data = json.loads(new_record['party_info']) + if isinstance(party_data, list): + names = [item.get('name', '') for item in party_data if isinstance(item, dict) and item.get('name')] + new_record['party_info'] = '、'.join(names) if names else new_record['party_info'] + except (json.JSONDecodeError, TypeError, ValueError): + pass + # 处理 BiddingUnit 字段 + if 'BiddingUnit' in new_record and new_record['BiddingUnit']: + try: + bidding_data = json.loads(new_record['BiddingUnit']) + if isinstance(bidding_data, list): + names = [item.get('name', '') for item in bidding_data if isinstance(item, dict) and item.get('name')] + new_record['BiddingUnit'] = '、'.join(names) if names else new_record['BiddingUnit'] + except (json.JSONDecodeError, TypeError, ValueError): + pass + processed.append(new_record) + return processed + + # 添加三个冲突字段,并处理其中的JSON格式数据 + itme["prefiling_conflicts"] = process_conflict_records(conflict_records.get('prefiling_conflicts', [])) + itme["project_conflicts"] = process_conflict_records(conflict_records.get('project_conflicts', [])) + itme["bid_conflicts"] = process_conflict_records(conflict_records.get('bid_conflicts', [])) else: # 如果没有找到项目或缺少信息,返回空数组 itme["prefiling_conflicts"] = []