Files
37duAutomation/plugins/XQ0001-0100/XQ0033 门店管理及服务岗出勤积分更新/data/ledger_query.py
T

148 lines
6.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""XQ0033 插件 - HR 台账实时查询参数构造。
HR 台账(queryRealTimeData.mc)要求 commonQuery 为前端加密串:
encodeURIComponent(JSON) → 自定义字符表 Base64 → 拼接盐值 → 首尾重排。
本模块只负责参数构造,实际请求由 hr_client.HRClient 发起。
"""
import json
import time
import urllib.parse
from datetime import date, timedelta
from .. import config
# 自定义字符表(与 HR 前端一致,逆向自页面脚本)
_CUSTOM_CHARS = "y!89(*z02BCcdD_bEeGHIJKLmnNOPQopRMSTqrUklVstWXYZafgh3ij1uvwx456A7"
_CQ_SALT = "yyryrtweasdasd"
def custom_base64_encode(data):
"""使用自定义字符表进行 Base64 编码(data 为 UTF-8 字节序列)。"""
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)
def build_condition(query_field, query_type, query_value,
left_bracket="", right_bracket="", input_type="text"):
"""构造一条符合前端格式的查询条件。"""
return {
"queryField": query_field,
"leftBracket": left_bracket,
"queryType": query_type,
"queryValue": query_value,
"queryText": "",
"rightBracket": right_bracket,
"logic": "AND",
"inputType": input_type,
}
def encode_common_query(query_list):
"""将查询条件列表加密为 commonQuery(encodeURIComponent + 自定义 Base64 + 盐值 + 首尾重排)。"""
json_str = json.dumps(query_list, ensure_ascii=False, separators=(',', ':'))
uri_encoded = urllib.parse.quote(json_str, safe="-_.~!*'()")
en_str = custom_base64_encode(uri_encoded.encode('utf-8')) + _CQ_SALT
return en_str[:2] + en_str[-3:] + en_str[2:-3]
def build_roster_query_params(roster_id, table_id, menu_name, common_query, today):
"""构造 HR 台账实时查询(queryRealTimeData.mc)完整参数。
roster/table/menu/commonQuery 按调用方传入,其余为通用固定参数(与页面抓包一致)。"""
month_first = date(today.year, today.month, 1)
now_ms = str(int(time.time() * 1000))
return {
"menuName": menu_name,
"useSealTreeData": "0",
"treePermitType": "permit",
"_t": now_ms,
"in_use": "1",
"commonQuery": common_query,
"newCondition": "",
"urlConditions": f"_t:{now_ms}#treePermitType:permit#useSealTreeData:0#"
f"menuName:{menu_name}#_t:{now_ms}#",
"exportConfig": "",
"tableId": table_id,
"rosterId": roster_id,
"includeChildrenSetting": "",
"struTreeCode": "cq37du",
"djStruTreeCode": "",
"djStruId": "",
"djOrgName": "",
"struId": config.COMPANY_STRU_ID,
"belongUnitStruId": config.COMPANY_STRU_ID,
"rootId": "cq37du",
"isVirtual": "0",
"currentUserRootId": "cq37du",
"currentUserId": "178b0efa-8a65-11f1-a7d9-000c296f9469",
"currentRoleIds": config.REJOIN_ROLE_IDS,
"currentStaffId": "1342173e-36cf-4160-8826-6b17d7d084a4",
"baseHrefPath": "http:hr.37du.co:80RedseaPlatform",
"qrCodeUrl": "jspresumepage-01.jsp",
"orgName": "集团",
"belongUnitOrgName": "集团",
"currentDate": today.strftime("%Y-%m-%d"),
"currentMonth": today.strftime("%Y%m"),
"currentMonthBar": today.strftime("%Y-%m"),
"lastMonth": (month_first - timedelta(days=1)).strftime("%Y%m"),
"lastMonFirstDay": (month_first - timedelta(days=1)).replace(day=1).strftime("%Y-%m-%d"),
"lastMonLastDay": (month_first - timedelta(days=1)).strftime("%Y-%m-%d"),
"curYearFirstMon": f"{today.year}01",
"curYearFirstMonBar": f"{today.year}-01",
"throughParam": "",
"start": "0",
"sortType": "",
"sortName": "",
"pageNo": "1",
"pageSize": "15",
}
def build_rejoin_query_params(today, month_first=None):
"""「离职当月再入职人员算薪台账」实时查询参数。
查询条件 = 离职日期 ∈ [月首, 今天];月首默认运行日所在月 1 号,
月初运行(积分日期 T-2 属上月)时由调用方传入 T-2 所在月 1 号,
否则上月离职再入职的人员整批查不到、不操作段会失效。"""
month_first = month_first or date(today.year, today.month, 1)
common_query = encode_common_query([
build_condition(f"`{config.REJOIN_FIELD_PREFIX}.leave_date`", ">=",
month_first.strftime("%Y-%m-%d"), left_bracket="(",
input_type="date"),
build_condition(f"`{config.REJOIN_FIELD_PREFIX}.leave_date`", "<=",
today.strftime("%Y-%m-%d"), right_bracket=")",
input_type="date"),
])
params = build_roster_query_params(config.REJOIN_ROSTER_ID, config.REJOIN_TABLE_ID,
config.REJOIN_MENU, common_query, today)
params["pageSize"] = str(config.REJOIN_PAGE_SIZE)
return params
def build_leave_query_params(today, idc):
"""「离职台账」按身份证查历史工号的实时查询参数。"""
common_query = encode_common_query([
build_condition("`hr_staff_base.idCard`", "like", idc, input_type="text"),
])
params = build_roster_query_params(config.LEAVE_ROSTER_ID, config.LEAVE_TABLE_ID,
config.LEAVE_MENU, common_query, today)
params["pageSize"] = str(config.LEAVE_PAGE_SIZE)
return params