Files
37duAutomation/plugins/XQ0001-0100/XQ0038 定性评价推送/data/__init__.py
T

78 lines
3.5 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 -*-
"""XQ0038 定性评价推送 插件 - 数据层:OA 接口请求。
只依赖 Python 标准库 / config(常量与钩子)。会话一律经 config.get_oa_session()
获取底座注入的 requests.Session(已带 OA cookies),不在本层读 cookie 文件。
"""
import re
import time
from .. import config
def org_search(s, key):
"""通讯录查询(orgType=8 人员):key 支持工号精确/姓名模糊,返回 (id, name)。"""
resp = s.post(config.DATAXML_URL,
data={"s_bean": "organizationDialogSearch", "key": key,
"startWith": "", "orgType": "8", "times": "2"}, timeout=20)
m = re.search(r'<data\b[^>]*>', resp.text)
if not m:
return None, None
tag = m.group(0)
i = re.search(r'''\bid="([^"]+)"''', tag)
n = re.search(r'''\bname="([^"]+)"''', tag)
return (i.group(1) if i else None, n.group(1) if n else None)
def get_login_user(s):
"""门户页内嵌登录信息,返回 (姓名 userName, 登录名/工号 loginName)。"""
resp = s.get(config.PORTAL_URL, timeout=30)
name = re.search(r"var\s+userName\s*=\s*'([^']*)'", resp.text)
no = re.search(r"var\s+loginName\s*=\s*'([^']*)'", resp.text)
return (name.group(1) if name else '', no.group(1) if no else '')
def get_export_columns(s, search_id):
"""查询方案字段字典,返回 [(fd, 中文名), ...]。"""
resp = s.get(config.DATAXML_URL,
params={"s_bean": "sysSearchResultExportDictService",
"fdModelName": config.MODEL_NAME, "searchId": search_id,
"type": "opt"}, timeout=20)
return re.findall(r'<data\s+key0="([^"]+)"\s+key1="([^"]*)"', resp.text)
def export_plan_once(s, search_id, day, out_path):
"""单次导出一个查询方案,返回 (行数, 错误信息);错误信息为空即成功。"""
ts = str(int(time.time() * 1000))
# 条件槽位与门户抓包一致:v0_2=eq & v1_2=创建时间(当天)
base = {"method": "result", "fdModelName": config.MODEL_NAME,
"searchId": search_id, "canClose": "false", "isNew": "true",
"j_aside": "false", "j_rIframe": "true", "j_iframe": "true",
"v0_2": "eq", "v1_2": day, "j_timestamp": ts}
resp = s.get(config.SEARCH_URL, params=base, timeout=30,
headers={"Referer": config.SEARCH_URL})
m = re.search(r'exportNum:\s*"(\d+)"', resp.text)
if not m:
return None, '结果页未取得 exportNum(OA 登录态可能失效,请重新登录)'
total = int(m.group(1))
if total == 0:
return 0, ''
cols = [fd for fd, name in get_export_columns(s, search_id)
if any(kw in name for kw in config.EXPORT_KEYWORDS)]
if not cols:
return None, '字段字典未匹配到导出列'
data = dict(base)
data["method"] = "exportResult"
data.update({"fdColumns": ';'.join(cols), "fdNum": str(total),
"fdNumStart": "1", "fdNumEnd": str(total),
"fdKeepRtfStyle": "true", "checkIdValues": ""})
resp = s.post(config.SEARCH_URL, data=data, timeout=120,
headers={"Referer": config.SEARCH_URL})
ctype = resp.headers.get('Content-Type', '')
if 'application/vnd.ms-excel' not in ctype:
text = re.sub(r'\s+', ' ', re.sub(r'<[^>]+>', ' ', resp.text))
return None, '导出返回非 xls(%s):%s' % (ctype, text[:150])
with open(out_path, 'wb') as f:
f.write(resp.content)
return total, ''