fix: wechat account scrape (form-data+dates) + frontend staff page

Backend (scrape_wechat_accounts):
- Send wxUser/list as form-urlencoded (not JSON) with the REQUIRED filters
  isOnJob / departmentId / startdate / enddate + empty userId/salesname/
  weChatNickName, matching the live request (empty fails with code -90000).
- scrape_wechat_accounts now takes department_id (the company-root dept id)
  plus optional start/end defaulting to a recent 7-day UTC window.
- paginate_by_page gains a form=True mode (data= + form content-type).
- Orchestrator sync_wechat_accounts resolves the root dept from the
  Department table (or accepts one) and skips with a warning when unknown.
- seed_accounts.py resolves the root via scrape_departments.

Frontend (yunkes):
- Add StaffList.vue (/staff) with is_on_job filter + pagination.
- Add /staff route and a 员工 nav item between 客户 and 群.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
浅念
2026-08-01 17:11:17 +08:00
parent 4dcfc66741
commit 0df53c6b1a
10 changed files with 638 additions and 30 deletions
@@ -16,15 +16,20 @@ snake_case columns + ``raw_json``), make exactly 3 calls, and stop without a
4th. The empty-page stop is the primary signal; ``total=4`` is only a
defensive over-fetch ceiling that must NOT trigger early (we yield exactly
4 == total, so the strict-``>`` ceiling never fires here).
The REQUEST body is form-urlencoded (NOT JSON) and must carry the required
``isOnJob`` / ``departmentId`` / ``startdate`` / ``enddate`` filters; the
"send pagination body" test asserts those keys land on the wire.
"""
from __future__ import annotations
import json as _json
from urllib.parse import parse_qs
import httpx
import respx
from app.scraper.base import FORM_CONTENT_TYPE
from app.scraper.wechat import scrape_wechat_accounts
from app.session.yunke_client import YunkeClient
from tests.cassettes.wxUser_fixture import (
@@ -40,12 +45,30 @@ WXUSER_LIST_URL = f"{BASE}/wechatweb/wxUser/list"
USERID = "a" * 32
COMPANY = "8m0atd"
# The company-root department id used as the required ``departmentId`` filter.
# Mirrors the shape of the real root dept id (32 hex chars) without hardcoding
# the live value in the scraper itself.
ROOT_DEPARTMENT_ID = "0E9C4B803E134C7687CBA824886319DA"
def _primed_client() -> YunkeClient:
"""A YunkeClient already in a logged-in state (login flow skipped)."""
return YunkeClient(base_url=BASE, userid=USERID, company=COMPANY)
def _parse_form(request: httpx.Request) -> dict[str, str]:
"""Decode a form-urlencoded request body into a flat ``{key: value}`` dict.
``parse_qs`` returns lists per key; the wxUser body has no repeated keys,
so we collapse each to its single value for ergonomic assertions.
``keep_blank_values=True`` so the always-empty ``userId`` / ``salesname``
filters (sent as ``userId=``) survive decoding and can be asserted on.
"""
body = request.content.decode()
parsed = parse_qs(body, keep_blank_values=True)
return {k: v[0] for k, v in parsed.items()}
# ---------------------------------------------------------------------------
# End-to-end: pagination + field mapping + raw_json
# ---------------------------------------------------------------------------
@@ -77,7 +100,9 @@ async def test_scrape_wechat_accounts_yields_records_and_paginates():
results = [
r
async for r in scrape_wechat_accounts(
client, page_size=DEFAULT_PAGE_SIZE
client,
department_id=ROOT_DEPARTMENT_ID,
page_size=DEFAULT_PAGE_SIZE,
)
]
finally:
@@ -86,6 +111,16 @@ async def test_scrape_wechat_accounts_yields_records_and_paginates():
# Pagination: exactly 3 calls (page1, page2, empty-page stop).
assert route.call_count == 3
# The required form filters ride on every request (the live endpoint
# answers code -90000 without them).
for call in route.calls:
body = _parse_form(call.request)
assert body["departmentId"] == ROOT_DEPARTMENT_ID
assert body["isOnJob"] == "1"
assert body["startdate"]
assert body["enddate"]
assert call.request.headers["content-type"] == FORM_CONTENT_TYPE
# Exactly the 4 fixture records (not duplicated, not dropped).
assert len(results) == 4
assert {r["wx_id"] for r in results} == {
@@ -132,7 +167,8 @@ async def test_scrape_wechat_accounts_yields_records_and_paginates():
@respx.mock
async def test_scrape_wechat_accounts_sends_pagination_body():
"""Outgoing request body carries pageNum/pageSize; advances per page."""
"""Outgoing form body carries pageNum/pageSize + the required filters; the
page number advances per request."""
page1 = make_wxuser_list_response(
WXUSER_RECORDS[:1], total=TOTAL, page_num=1, page_size=1
)
@@ -149,7 +185,14 @@ async def test_scrape_wechat_accounts_sends_pagination_body():
client = _primed_client()
try:
results = [
r async for r in scrape_wechat_accounts(client, page_size=1)
r
async for r in scrape_wechat_accounts(
client,
department_id=ROOT_DEPARTMENT_ID,
page_size=1,
start="2026-07-01",
end="2026-07-08",
)
]
finally:
await client.aclose()
@@ -158,15 +201,28 @@ async def test_scrape_wechat_accounts_sends_pagination_body():
assert len(results) == 1
assert route.call_count == 2
# First request: pageNum=1.
body1 = _json.loads(route.calls[0].request.content.decode())
assert body1["pageNum"] == 1
assert body1["pageSize"] == 1
# Every request is form-urlencoded (NOT JSON) and carries the required
# filters the live endpoint demands (isOnJob / departmentId / startdate /
# enddate), plus the always-empty free-text filters from the real client.
for call in route.calls:
assert call.request.headers["content-type"] == FORM_CONTENT_TYPE
body = _parse_form(call.request)
assert body["isOnJob"] == "1"
assert body["departmentId"] == ROOT_DEPARTMENT_ID
assert body["startdate"] == "2026-07-01"
assert body["enddate"] == "2026-07-08"
assert body["userId"] == ""
assert body["salesname"] == ""
assert body["weChatNickName"] == ""
# Second request: pageNum advanced to 2.
body2 = _json.loads(route.calls[1].request.content.decode())
assert body2["pageNum"] == 2
assert body2["pageSize"] == 1
# pageNum/pageSize advance correctly across the form body.
body1 = _parse_form(route.calls[0].request)
assert body1["pageNum"] == "1"
assert body1["pageSize"] == "1"
body2 = _parse_form(route.calls[1].request)
assert body2["pageNum"] == "2"
assert body2["pageSize"] == "1"
# ---------------------------------------------------------------------------
@@ -184,7 +240,12 @@ async def test_scrape_wechat_accounts_stops_on_empty_first_page():
client = _primed_client()
try:
results = [r async for r in scrape_wechat_accounts(client)]
results = [
r
async for r in scrape_wechat_accounts(
client, department_id=ROOT_DEPARTMENT_ID
)
]
finally:
await client.aclose()