rescue: 断电事故后工作区全量备份
This commit is contained in:
+181
-181
@@ -1,181 +1,181 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""金蝶 K3Cloud(HTML5)ERP 登录模块(xq0031_xq0032 插件内自包含)。
|
||||
|
||||
自 workflows/share/erp_login.py 迁移,插件化改造点:
|
||||
- 账号密码由插件界面配置经 fields 注入(不再读 .env),个性化归属明确;
|
||||
- 使用固定默认用户目录(不再每次独立临时目录),保留登录态与浏览器配置;
|
||||
用完由调用方 page.quit() 关闭;
|
||||
- print → config.log。
|
||||
|
||||
登录页元素(新旧版一致):
|
||||
用户名输入框 <input id="user" placeholder="用户名">
|
||||
密码输入框 <input id="password" type="password">
|
||||
登录按钮 <button id="btnLogin" class="btnlogin k-button">登录</button>
|
||||
登录成功判定:URL 包含 /k3cloud/html5/dform.aspx,或站点页面无登录输入框(已登录态)。
|
||||
特殊弹窗:登录时若弹出「该帐号已登录在线,确认让对方下线」警告框,自动点确认。
|
||||
"""
|
||||
import time
|
||||
|
||||
from DrissionPage import ChromiumPage, ChromiumOptions
|
||||
|
||||
from . import config
|
||||
|
||||
# ================== 站点定义(新旧 ERP) ==================
|
||||
ERP_URLS = {
|
||||
'old': 'https://erp.37du.co/k3cloud/html5/index.aspx',
|
||||
'new': 'https://criterion.erp.37du.co/k3cloud/html5/index.aspx',
|
||||
}
|
||||
|
||||
# 登录页元素定位(新旧版元素 id 一致)
|
||||
USER_INPUT = '#user'
|
||||
PWD_INPUT = '#password'
|
||||
LOGIN_BTN = '#btnLogin'
|
||||
# 「该帐号已登录在线」弹窗的确认按钮(蓝色样式 class 固定,id 为动态 UUID)
|
||||
KICK_CONFIRM_BTN = 'css:.k-window .BlueButtonStyle'
|
||||
|
||||
# 登录成功标志:URL 包含该关键字即视为已登录(跳转到默认表单页)
|
||||
SUCCESS_KEYWORD = '/k3cloud/html5/dform.aspx'
|
||||
|
||||
# ================== 运行参数 ==================
|
||||
RETRY_MAX = config.RETRY_MAX # 全局重试次数(项目规范:最多 3 次)
|
||||
TIMEOUT = 10 # 元素等待超时(秒)
|
||||
LOGIN_WAIT = 3 # 点击登录后等待(秒)
|
||||
HEADLESS = False
|
||||
|
||||
|
||||
def _resolve_url(erp):
|
||||
"""把 'old'/'new'/完整URL 统一解析为登录页 URL。"""
|
||||
if erp in ERP_URLS:
|
||||
return ERP_URLS[erp]
|
||||
if erp and erp.lower().startswith('http'):
|
||||
return erp
|
||||
raise ValueError(f"无效的 ERP 标识:{erp},可选 'old'、'new' 或完整登录页 URL")
|
||||
|
||||
|
||||
def _is_login_page(url):
|
||||
"""判断当前 URL 是否仍停留在登录页。"""
|
||||
return 'index.aspx' in (url or '').lower()
|
||||
|
||||
|
||||
def _is_login_success(url):
|
||||
"""登录成功判定:URL 包含 dform.aspx 即视为已登录。"""
|
||||
return SUCCESS_KEYWORD in (url or '')
|
||||
|
||||
|
||||
def _handle_kick_dialog(page):
|
||||
"""处理「该帐号已登录在线,确认让对方下线」弹窗:存在则点击蓝色确认按钮。"""
|
||||
for _ in range(3):
|
||||
btn = page.ele(KICK_CONFIRM_BTN, timeout=2)
|
||||
if not btn:
|
||||
return False
|
||||
try:
|
||||
btn.click()
|
||||
config.log("[ERP登录] 检测到「已登录在线」弹窗,点击确认(让对方下线)")
|
||||
return True
|
||||
except Exception as e:
|
||||
config.log(f"[ERP登录] 点击弹窗确认异常:{e}")
|
||||
time.sleep(1)
|
||||
return False
|
||||
|
||||
|
||||
def _open_browser(headless):
|
||||
"""启动浏览器(DrissionPage 固定默认用户目录,不再每次独立临时目录,保留登录态/配置)。
|
||||
附加禁用浏览器自身弹窗的 prefs/启动参数:保存密码、权限请求(通知/定位/音视频)、
|
||||
自动化测试 infobar、首次运行向导、打开外部应用确认框(K3 点引出/下载会请求打开
|
||||
Excel 等,防止弹「访问应用和服务」)等。"""
|
||||
co = ChromiumOptions()
|
||||
if headless:
|
||||
co.headless()
|
||||
co.set_argument('--start-maximized')
|
||||
# —— 禁用浏览器自身弹窗(避免干扰自动化)——
|
||||
co.set_pref('credentials_enable_service', False) # 不保存密码
|
||||
co.set_pref('profile.password_manager_enabled', False) # 关闭密码管理器
|
||||
return ChromiumPage(co)
|
||||
|
||||
|
||||
def erp_login(erp='new', username='', password='', headless=None):
|
||||
"""
|
||||
登录金蝶 K3Cloud ERP(新旧系统通用,现场真登录)。
|
||||
|
||||
参数:
|
||||
erp 'old' 旧版 erp.37du.co / 'new' 新版 criterion.erp.37du.co / 完整登录页 URL
|
||||
username 用户名(插件界面配置注入,必填)
|
||||
password 密码(插件界面配置注入,必填)
|
||||
|
||||
返回:
|
||||
(success, page) success=True 时 page 为已登录的 ChromiumPage;失败时 page 为 None。
|
||||
调用方使用完毕应 page.quit() 关闭浏览器(不保存任何目录/cookies)。
|
||||
"""
|
||||
login_url = _resolve_url(erp)
|
||||
username = (username or '').strip()
|
||||
password = password or ''
|
||||
if not (username and password):
|
||||
config.log("[ERP登录] 登录账号/密码缺失(请在插件界面填写并保存配置)")
|
||||
return False, None
|
||||
headless = HEADLESS if headless is None else bool(headless)
|
||||
|
||||
for attempt in range(1, RETRY_MAX + 1):
|
||||
page = None
|
||||
try:
|
||||
config.log(f"[ERP登录] 第 {attempt}/{RETRY_MAX} 次 → {login_url}")
|
||||
page = _open_browser(headless)
|
||||
config.log("[ERP登录] 浏览器已启动(默认用户目录,保留登录态)")
|
||||
|
||||
# 回到目标登录页;已登录站点会跳转/保留会话,未登录则停在登录页
|
||||
page.get(login_url)
|
||||
time.sleep(2)
|
||||
|
||||
# 3) 已登录判定:页面无登录输入框 → 会话有效,直接复用
|
||||
if page.ele(USER_INPUT, timeout=3) is None:
|
||||
config.log("[ERP登录] 检测到已登录会话,直接复用")
|
||||
return True, page
|
||||
|
||||
# 4) 未登录 → 填表登录
|
||||
user = page.ele(USER_INPUT, timeout=TIMEOUT)
|
||||
if not user:
|
||||
config.log("[ERP登录] 找不到用户名输入框")
|
||||
continue
|
||||
user.clear()
|
||||
user.input(username)
|
||||
time.sleep(0.5)
|
||||
# 用户名框是 autocomplete 控件,需按回车关闭弹出的下拉提示框
|
||||
page.actions.key_down('Enter')
|
||||
page.actions.key_up('Enter')
|
||||
time.sleep(0.3)
|
||||
|
||||
pwd = page.ele(PWD_INPUT, timeout=TIMEOUT)
|
||||
if not pwd:
|
||||
config.log("[ERP登录] 找不到密码输入框")
|
||||
continue
|
||||
pwd.clear()
|
||||
pwd.input(password)
|
||||
time.sleep(0.3)
|
||||
|
||||
btn = page.ele(LOGIN_BTN, timeout=TIMEOUT)
|
||||
if not btn:
|
||||
config.log("[ERP登录] 找不到登录按钮")
|
||||
continue
|
||||
btn.click()
|
||||
|
||||
# 登录后轮询:处理「已登录在线」弹窗 + 等待跳转到 dform.aspx
|
||||
for _ in range(6):
|
||||
time.sleep(LOGIN_WAIT)
|
||||
if _is_login_success(page.url):
|
||||
config.log(f"[ERP登录] 登录成功 → {page.url}")
|
||||
return True, page
|
||||
_handle_kick_dialog(page)
|
||||
|
||||
config.log("[ERP登录] 点击登录后未跳转到 dform.aspx(账号密码错误或网络异常)")
|
||||
except Exception as e:
|
||||
config.log(f"[ERP登录] 异常:{e}")
|
||||
|
||||
# 走到这里说明本轮失败,关闭浏览器后重试(成功时在上方已直接 return)
|
||||
if page is not None:
|
||||
try:
|
||||
page.quit()
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(1)
|
||||
|
||||
config.log("[ERP登录] 重试 3 次均失败")
|
||||
return False, None
|
||||
# -*- coding: utf-8 -*-
|
||||
"""金蝶 K3Cloud(HTML5)ERP 登录模块(xq0031_xq0032 插件内自包含)。
|
||||
|
||||
自 workflows/share/erp_login.py 迁移,插件化改造点:
|
||||
- 账号密码由插件界面配置经 fields 注入(不再读 .env),个性化归属明确;
|
||||
- 使用固定默认用户目录(不再每次独立临时目录),保留登录态与浏览器配置;
|
||||
用完由调用方 page.quit() 关闭;
|
||||
- print → config.log。
|
||||
|
||||
登录页元素(新旧版一致):
|
||||
用户名输入框 <input id="user" placeholder="用户名">
|
||||
密码输入框 <input id="password" type="password">
|
||||
登录按钮 <button id="btnLogin" class="btnlogin k-button">登录</button>
|
||||
登录成功判定:URL 包含 /k3cloud/html5/dform.aspx,或站点页面无登录输入框(已登录态)。
|
||||
特殊弹窗:登录时若弹出「该帐号已登录在线,确认让对方下线」警告框,自动点确认。
|
||||
"""
|
||||
import time
|
||||
|
||||
from DrissionPage import ChromiumPage, ChromiumOptions
|
||||
|
||||
from . import config
|
||||
|
||||
# ================== 站点定义(新旧 ERP) ==================
|
||||
ERP_URLS = {
|
||||
'old': 'https://erp.37du.co/k3cloud/html5/index.aspx',
|
||||
'new': 'https://criterion.erp.37du.co/k3cloud/html5/index.aspx',
|
||||
}
|
||||
|
||||
# 登录页元素定位(新旧版元素 id 一致)
|
||||
USER_INPUT = '#user'
|
||||
PWD_INPUT = '#password'
|
||||
LOGIN_BTN = '#btnLogin'
|
||||
# 「该帐号已登录在线」弹窗的确认按钮(蓝色样式 class 固定,id 为动态 UUID)
|
||||
KICK_CONFIRM_BTN = 'css:.k-window .BlueButtonStyle'
|
||||
|
||||
# 登录成功标志:URL 包含该关键字即视为已登录(跳转到默认表单页)
|
||||
SUCCESS_KEYWORD = '/k3cloud/html5/dform.aspx'
|
||||
|
||||
# ================== 运行参数 ==================
|
||||
RETRY_MAX = config.RETRY_MAX # 全局重试次数(项目规范:最多 3 次)
|
||||
TIMEOUT = 10 # 元素等待超时(秒)
|
||||
LOGIN_WAIT = 3 # 点击登录后等待(秒)
|
||||
HEADLESS = False
|
||||
|
||||
|
||||
def _resolve_url(erp):
|
||||
"""把 'old'/'new'/完整URL 统一解析为登录页 URL。"""
|
||||
if erp in ERP_URLS:
|
||||
return ERP_URLS[erp]
|
||||
if erp and erp.lower().startswith('http'):
|
||||
return erp
|
||||
raise ValueError(f"无效的 ERP 标识:{erp},可选 'old'、'new' 或完整登录页 URL")
|
||||
|
||||
|
||||
def _is_login_page(url):
|
||||
"""判断当前 URL 是否仍停留在登录页。"""
|
||||
return 'index.aspx' in (url or '').lower()
|
||||
|
||||
|
||||
def _is_login_success(url):
|
||||
"""登录成功判定:URL 包含 dform.aspx 即视为已登录。"""
|
||||
return SUCCESS_KEYWORD in (url or '')
|
||||
|
||||
|
||||
def _handle_kick_dialog(page):
|
||||
"""处理「该帐号已登录在线,确认让对方下线」弹窗:存在则点击蓝色确认按钮。"""
|
||||
for _ in range(3):
|
||||
btn = page.ele(KICK_CONFIRM_BTN, timeout=2)
|
||||
if not btn:
|
||||
return False
|
||||
try:
|
||||
btn.click()
|
||||
config.log("[ERP登录] 检测到「已登录在线」弹窗,点击确认(让对方下线)")
|
||||
return True
|
||||
except Exception as e:
|
||||
config.log(f"[ERP登录] 点击弹窗确认异常:{e}")
|
||||
time.sleep(1)
|
||||
return False
|
||||
|
||||
|
||||
def _open_browser(headless):
|
||||
"""启动浏览器(DrissionPage 固定默认用户目录,不再每次独立临时目录,保留登录态/配置)。
|
||||
附加禁用浏览器自身弹窗的 prefs/启动参数:保存密码、权限请求(通知/定位/音视频)、
|
||||
自动化测试 infobar、首次运行向导、打开外部应用确认框(K3 点引出/下载会请求打开
|
||||
Excel 等,防止弹「访问应用和服务」)等。"""
|
||||
co = ChromiumOptions()
|
||||
if headless:
|
||||
co.headless()
|
||||
co.set_argument('--start-maximized')
|
||||
# —— 禁用浏览器自身弹窗(避免干扰自动化)——
|
||||
co.set_pref('credentials_enable_service', False) # 不保存密码
|
||||
co.set_pref('profile.password_manager_enabled', False) # 关闭密码管理器
|
||||
return ChromiumPage(co)
|
||||
|
||||
|
||||
def erp_login(erp='new', username='', password='', headless=None):
|
||||
"""
|
||||
登录金蝶 K3Cloud ERP(新旧系统通用,现场真登录)。
|
||||
|
||||
参数:
|
||||
erp 'old' 旧版 erp.37du.co / 'new' 新版 criterion.erp.37du.co / 完整登录页 URL
|
||||
username 用户名(插件界面配置注入,必填)
|
||||
password 密码(插件界面配置注入,必填)
|
||||
|
||||
返回:
|
||||
(success, page) success=True 时 page 为已登录的 ChromiumPage;失败时 page 为 None。
|
||||
调用方使用完毕应 page.quit() 关闭浏览器(不保存任何目录/cookies)。
|
||||
"""
|
||||
login_url = _resolve_url(erp)
|
||||
username = (username or '').strip()
|
||||
password = password or ''
|
||||
if not (username and password):
|
||||
config.log("[ERP登录] 登录账号/密码缺失(请在插件界面填写并保存配置)")
|
||||
return False, None
|
||||
headless = HEADLESS if headless is None else bool(headless)
|
||||
|
||||
for attempt in range(1, RETRY_MAX + 1):
|
||||
page = None
|
||||
try:
|
||||
config.log(f"[ERP登录] 第 {attempt}/{RETRY_MAX} 次 → {login_url}")
|
||||
page = _open_browser(headless)
|
||||
config.log("[ERP登录] 浏览器已启动(默认用户目录,保留登录态)")
|
||||
|
||||
# 回到目标登录页;已登录站点会跳转/保留会话,未登录则停在登录页
|
||||
page.get(login_url)
|
||||
time.sleep(2)
|
||||
|
||||
# 3) 已登录判定:页面无登录输入框 → 会话有效,直接复用
|
||||
if page.ele(USER_INPUT, timeout=3) is None:
|
||||
config.log("[ERP登录] 检测到已登录会话,直接复用")
|
||||
return True, page
|
||||
|
||||
# 4) 未登录 → 填表登录
|
||||
user = page.ele(USER_INPUT, timeout=TIMEOUT)
|
||||
if not user:
|
||||
config.log("[ERP登录] 找不到用户名输入框")
|
||||
continue
|
||||
user.clear()
|
||||
user.input(username)
|
||||
time.sleep(0.5)
|
||||
# 用户名框是 autocomplete 控件,需按回车关闭弹出的下拉提示框
|
||||
page.actions.key_down('Enter')
|
||||
page.actions.key_up('Enter')
|
||||
time.sleep(0.3)
|
||||
|
||||
pwd = page.ele(PWD_INPUT, timeout=TIMEOUT)
|
||||
if not pwd:
|
||||
config.log("[ERP登录] 找不到密码输入框")
|
||||
continue
|
||||
pwd.clear()
|
||||
pwd.input(password)
|
||||
time.sleep(0.3)
|
||||
|
||||
btn = page.ele(LOGIN_BTN, timeout=TIMEOUT)
|
||||
if not btn:
|
||||
config.log("[ERP登录] 找不到登录按钮")
|
||||
continue
|
||||
btn.click()
|
||||
|
||||
# 登录后轮询:处理「已登录在线」弹窗 + 等待跳转到 dform.aspx
|
||||
for _ in range(6):
|
||||
time.sleep(LOGIN_WAIT)
|
||||
if _is_login_success(page.url):
|
||||
config.log(f"[ERP登录] 登录成功 → {page.url}")
|
||||
return True, page
|
||||
_handle_kick_dialog(page)
|
||||
|
||||
config.log("[ERP登录] 点击登录后未跳转到 dform.aspx(账号密码错误或网络异常)")
|
||||
except Exception as e:
|
||||
config.log(f"[ERP登录] 异常:{e}")
|
||||
|
||||
# 走到这里说明本轮失败,关闭浏览器后重试(成功时在上方已直接 return)
|
||||
if page is not None:
|
||||
try:
|
||||
page.quit()
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(1)
|
||||
|
||||
config.log("[ERP登录] 重试 3 次均失败")
|
||||
return False, None
|
||||
|
||||
Reference in New Issue
Block a user