- 金贵微信端管理平台(vip6.wechat.goldenyun.cn)门票使用统计导出:接口登录 → 搜索接口取核销明细 → 调平台导出接口生成 xlsx - 核销时间日期段界面可选(默认昨天 00:00:00 ~ 今天 23:59:59);导出接口异常时按同一份数据本地生成同列序 xlsx 兜底
120 lines
4.8 KiB
Python
120 lines
4.8 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""金贵微信端管理平台登录(接口登录,无需浏览器)。
|
||
|
||
登录页为传统 ASP.NET MVC 表单:
|
||
POST /chqhpwq.web/login?returnurl=<目标页>
|
||
表单字段:VpnCode(门店 GUID) / VpnName / UserName / Password / Channel
|
||
无验证码、无 CSRF Token、无前端密码加密;失败时服务端回渲染登录页并在
|
||
validation-summary-errors 里给出「帐号密码错误!」。
|
||
"""
|
||
import re
|
||
|
||
import requests
|
||
|
||
from .. import config
|
||
|
||
_ERROR_HINTS = ('帐号密码错误', '账号密码错误', '密码错误', '已被锁定', '锁定', '验证码', '无权')
|
||
|
||
|
||
def new_session():
|
||
session = requests.Session()
|
||
session.headers.update({
|
||
'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
|
||
'(KHTML, like Gecko) Chrome/120.0 Safari/537.36'),
|
||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
||
'Accept-Language': 'zh-CN,zh;q=0.9',
|
||
})
|
||
return session
|
||
|
||
|
||
def fetch_login_page(session):
|
||
url = f"{config.BASE_URL}{config.APP_PATH}{config.LOGIN_PATH}"
|
||
config.logger.info('获取登录页: %s', url)
|
||
resp = session.get(url, timeout=config.TIMEOUT)
|
||
resp.raise_for_status()
|
||
resp.encoding = 'utf-8'
|
||
return resp.text
|
||
|
||
|
||
def parse_vpn_list(html):
|
||
"""解析门店列表:<li data-id="GUID" data-name="门店名">门店名</li>"""
|
||
block = html
|
||
start = html.find('class="vpn-list"')
|
||
if start != -1:
|
||
end = html.find('</ul>', start)
|
||
block = html[start:end if end != -1 else len(html)]
|
||
|
||
vpns = []
|
||
for match in re.finditer(r'<li([^>]*)>([^<]*)</li>', block):
|
||
code = re.search(r'data-id="([^"]+)"', match.group(1))
|
||
if not code:
|
||
continue
|
||
name = re.search(r'data-name="([^"]*)"', match.group(1))
|
||
vpns.append({'code': code.group(1), 'name': (name.group(1) if name else match.group(2)).strip()})
|
||
return vpns
|
||
|
||
|
||
def pick_vpn(vpns, want_name=''):
|
||
"""按门店名匹配,未命中则取登录页第一个门店。"""
|
||
want = str(want_name or '').strip()
|
||
if want:
|
||
for vpn in vpns:
|
||
if vpn['name'] == want:
|
||
return vpn
|
||
for vpn in vpns:
|
||
if want in vpn['name'] or vpn['name'] in want:
|
||
return vpn
|
||
config.logger.warning('未匹配到门店「%s」,回退登录页第一个门店', want)
|
||
if not vpns:
|
||
raise RuntimeError('登录页未解析到门店列表,无法选择门店登录')
|
||
return vpns[0]
|
||
|
||
|
||
def _extract_error(html):
|
||
for match in re.finditer(r'validation-summary-errors[^>]*>\s*<ul>(.*?)</ul>', html, re.S):
|
||
items = [re.sub(r'<[^>]+>', '', li).strip()
|
||
for li in re.findall(r'<li[^>]*>(.*?)</li>', match.group(1), re.S)]
|
||
items = [i for i in items if i]
|
||
if items:
|
||
return ' / '.join(items)
|
||
for hint in _ERROR_HINTS:
|
||
if hint in html:
|
||
return f'页面出现关键字「{hint}」'
|
||
return ''
|
||
|
||
|
||
def login(username, password, vpn_name=''):
|
||
"""返回已认证的 requests.Session;失败抛 RuntimeError(凭据错误不重试,避免锁号)。"""
|
||
username = str(username or '').strip()
|
||
password = str(password or '').strip() # 防误存首尾空格导致平台判密码错误
|
||
if not username or not password:
|
||
raise RuntimeError('登录账号或密码为空:请在插件界面填写并保存配置')
|
||
|
||
session = new_session()
|
||
html = fetch_login_page(session)
|
||
vpn = pick_vpn(parse_vpn_list(html), vpn_name)
|
||
config.logger.info('登录门店:%s(%s)', vpn['name'], vpn['code'])
|
||
|
||
from urllib.parse import quote
|
||
return_url = quote(config.APP_PATH + config.LIST_PATH, safe='')
|
||
url = f"{config.BASE_URL}{config.APP_PATH}{config.LOGIN_PATH}?returnurl={return_url}"
|
||
payload = {
|
||
'VpnCode': vpn['code'],
|
||
'VpnName': vpn['name'],
|
||
'UserName': username,
|
||
'Password': password,
|
||
'Channel': config.CHANNEL_WECHAT,
|
||
}
|
||
resp = session.post(url, data=payload, timeout=config.TIMEOUT, allow_redirects=True)
|
||
resp.encoding = 'utf-8'
|
||
config.logger.info('登录响应:状态=%s 最终URL=%s', resp.status_code, resp.url)
|
||
|
||
error = _extract_error(resp.text)
|
||
if error:
|
||
hint = '(请检查账号密码首尾空格后重试)' if '密码' in error else ''
|
||
raise RuntimeError(f'登录失败:{error}{hint}。为免触发平台失败计数锁定,本次不重试')
|
||
if config.LOGIN_PATH in resp.url and 'id="Password"' in resp.text:
|
||
raise RuntimeError('登录失败:仍停留在登录页,请确认账号密码与门店是否正确')
|
||
config.logger.info('登录成功,Cookie=%s', ', '.join(session.cookies.keys()) or '无')
|
||
return session
|