import json import urllib.parse # ---------- 常量 ---------- # 自定义 Base64 字符表(索引0~64,64为填充字符) CUSTOM_CHARS = "y!89(*z02BCcdD_bEeGHIJKLmnNOPQopRMSTqrUklVstWXYZafgh3ij1uvwx456A7" PAD_CHAR = '7' # 填充字符(索引64) SALT = "yyryrtweasdasd" # 固定盐值 # 反向映射表(解码用) CUSTOM_REV = {ch: idx for idx, ch in enumerate(CUSTOM_CHARS)} # ---------- 自定义 Base64 编码 ---------- def custom_base64_encode(data: bytes) -> str: """ 使用自定义字符表进行 Base64 编码。 data: 已 UTF-8 编码的字节序列 返回: 编码后的字符串(填充使用 '7') """ result = [] for i in range(0, len(data), 3): chunk = data[i:i+3] b1 = chunk[0] if len(chunk) > 0 else 0 b2 = chunk[1] if len(chunk) > 1 else 0 b3 = chunk[2] if len(chunk) > 2 else 0 enc1 = (b1 >> 2) & 0x3F enc2 = ((b1 << 4) | (b2 >> 4)) & 0x3F enc3 = ((b2 << 2) | (b3 >> 6)) & 0x3F enc4 = b3 & 0x3F if len(chunk) == 1: enc3 = 64 enc4 = 64 elif len(chunk) == 2: enc4 = 64 result.append(CUSTOM_CHARS[enc1]) result.append(CUSTOM_CHARS[enc2]) result.append(CUSTOM_CHARS[enc3]) result.append(CUSTOM_CHARS[enc4]) return ''.join(result) # ---------- 自定义 Base64 解码 ---------- def custom_base64_decode(s: str) -> bytes: """ 使用自定义字符表解码 Base64 字符串。 返回原始 UTF-8 字节序列。 """ # 过滤掉不属于字符表的字符(与前端一致) filtered = ''.join(ch for ch in s if ch in CUSTOM_REV) result = [] for i in range(0, len(filtered), 4): chunk = filtered[i:i+4] # 补足4个字符(使用填充字符'7') while len(chunk) < 4: chunk += PAD_CHAR enc1 = CUSTOM_REV[chunk[0]] enc2 = CUSTOM_REV[chunk[1]] enc3 = CUSTOM_REV[chunk[2]] enc4 = CUSTOM_REV[chunk[3]] b1 = ((enc1 << 2) | (enc2 >> 4)) & 0xFF b2 = ((enc2 << 4) | (enc3 >> 2)) & 0xFF b3 = ((enc3 << 6) | enc4) & 0xFF result.append(b1) if enc3 != 64: result.append(b2) if enc4 != 64: result.append(b3) return bytes(result) # ---------- 加密(编码)---------- def encode_common_query(query_list: list) -> str: """ 将查询条件列表(Python 对象)加密为 commonQuery 字符串。 参数 query_list: 与前端格式一致的列表,例如: [ {"queryField": "`hr_staff_move_info.MOVE_DATE`", ...}, ... ] 返回: 加密后的字符串(可直接用于表单提交) """ # 1. 转为 JSON 字符串(无空格,与前端 JSON.stringify 一致) json_str = json.dumps(query_list, ensure_ascii=False, separators=(',', ':')) # 2. URI 编码(模拟 encodeURIComponent:不编码 ! ' ( ) * - . _ ~) uri_encoded = urllib.parse.quote(json_str, safe="-_.~!*'()") # 3. 转为 UTF-8 字节 utf8_bytes = uri_encoded.encode('utf-8') # 4. 自定义 Base64 编码 base64_str = custom_base64_encode(utf8_bytes) # 5. 追加盐值 en_str = base64_str + SALT # 6. 重排:前2位 + 后3位 + 中间部分 first = en_str[:2] second = en_str[2:-3] third = en_str[-3:] return first + third + second # ---------- 解密(解码)---------- def decode_common_query(encrypted: str) -> list: """ 将加密的 commonQuery 字符串还原为原始查询条件列表。 参数 encrypted: 加密后的字符串 返回: 原始的查询条件列表(Python 对象) """ # 1. 逆向重排:前2位 + 中间部分 + 后3位 first = encrypted[:2] third = encrypted[2:5] second = encrypted[5:] orgin = first + second + third # 2. 去除盐值 if orgin.endswith(SALT): orgin = orgin[:-len(SALT)] else: # 容错:如果盐值不完整,尝试匹配前缀(有时末尾会缺失几个字符) if orgin.endswith(SALT[:-3]): orgin = orgin[:-(len(SALT)-3)] # 3. 自定义 Base64 解码 utf8_bytes = custom_base64_decode(orgin) uri_encoded = utf8_bytes.decode('utf-8') # 4. URI 解码 json_str = urllib.parse.unquote(uri_encoded) # 5. 解析 JSON return json.loads(json_str) # ---------- 辅助函数(可选)---------- def build_date_range_query(field: str, start_date: str, end_date: str) -> list: """ 构造一个标准的日期范围查询列表。 参数: field: 字段名(含反引号),如 "`hr_staff_move_info.MOVE_DATE`" start_date: 开始日期,如 "2026-08-01" end_date: 结束日期,如 "2026-08-30" 返回: 符合前端格式的查询条件列表。 """ return [ { "queryField": field, "leftBracket": "(", "queryType": ">=", "queryValue": start_date, "queryText": "", "rightBracket": "", "logic": "AND", "inputType": "date" }, { "queryField": field, "leftBracket": "", "queryType": "<=", "queryValue": end_date, "queryText": "", "rightBracket": ")", "logic": "AND", "inputType": "date" } ] # ---------- 示例 ---------- if __name__ == "__main__": # 示例:查询 2026-08-01 至 2026-08-30 的调动记录 field = "`hr_staff_move_info.MOVE_DATE`" query = build_date_range_query(field, "2026-08-01", "2026-08-30") # 加密 encrypted = encode_common_query(query) print("加密后的 commonQuery:") print(encrypted) print() # 解密验证 decoded = decode_common_query(encrypted) print("解密后的查询条件:") print(json.dumps(decoded, ensure_ascii=False, indent=2)) print() # 验证是否与原始一致 print("解密后与原始一致:", decoded == query)