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
@@ -5,8 +5,15 @@ data dictionary §2: ``wechatId, nickName, alias, phone, userId,
userName(员工真名), contactsNum, groupNum, sendNum/receiveNum, isOnJob,
dbSize, 在线状态, lastUpdate``; total=55).
The envelope differs from ``wxFriends/friend_list/es``: there is **no**
``basicResult`` wrapper. The pagination fields sit directly under ``data``::
The REQUEST is **form-urlencoded** (NOT JSON): the endpoint binds Spring
``@RequestParam`` from the form body and REQUIRES ``isOnJob`` /
``departmentId`` (the company-root dept id) / ``startdate`` / ``enddate`` or
it answers ``code -90000`` ("缺少..."). ``userId`` / ``salesname`` /
``weChatNickName`` are sent empty to mirror the real client.
The RESPONSE envelope differs from ``wxFriends/friend_list/es``: there is
**no** ``basicResult`` wrapper. The pagination fields sit directly under
``data``::
{"code":"10000","data":{"data":[...], "total":.., "pageNum":.., "pageSize":..}}
+93 -2
View File
@@ -29,7 +29,7 @@ import sqlalchemy as sa
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from app.db.base import Base
from app.db.models import SyncRun, SyncState
from app.db.models import Department, SyncRun, SyncState
from app.sync import orchestrator
from app.sync.watermarks import get_last_timestamp
@@ -477,7 +477,7 @@ async def test_sync_all_wechat_runs_each_entity_then_messages(monkeypatch):
try:
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
async def empty_async_gen(_client=None):
async def empty_async_gen(_client=None, **_kwargs):
if False: # pragma: no cover
yield {}
@@ -496,6 +496,11 @@ async def test_sync_all_wechat_runs_each_entity_then_messages(monkeypatch):
# orchestrator namespace) are never called. No need to patch them.
async with SessionLocal() as session:
# sync_wechat_accounts resolves the company-root dept id required
# by the wxUser/list form body from the Department table, so seed
# a root (parent_id=None) before running the coordinator.
session.add(Department(dept_id="ROOT_DEPT", parent_id=None, name="Root"))
await session.flush()
results = await orchestrator.sync_all_wechat(
session,
client=object(),
@@ -520,6 +525,92 @@ async def test_sync_all_wechat_runs_each_entity_then_messages(monkeypatch):
await engine.dispose()
async def test_sync_wechat_accounts_skips_when_no_root_department(monkeypatch):
"""``wxUser/list`` REQUIRES a ``departmentId`` filter. When the org tree is
empty (no root resolvable from the Department table) the orchestrator must
NOT send a known-bad empty filter; it skips with a logged warning and
returns a ``status='skipped'`` result without ever calling the scraper."""
engine = await _make_engine()
try:
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
called = False
async def fake_scrape_accounts(client, *, department_id, **_kwargs):
nonlocal called
called = True
if False: # pragma: no cover
yield {}
monkeypatch.setattr(
orchestrator, "scrape_wechat_accounts", fake_scrape_accounts
)
async with SessionLocal() as session:
result = await orchestrator.sync_wechat_accounts(
session, client=object()
)
await session.commit()
assert result.status == "skipped"
assert result.fetched == 0
assert result.upserted == 0
assert not called
# No SyncRun recorded for a skip (it is not a success or a failure).
async with SessionLocal() as session:
rows = (
await session.execute(
sa.select(SyncRun).where(
SyncRun.entity_type == "wechat_account"
)
)
).scalars().all()
assert rows == []
finally:
await engine.dispose()
async def test_sync_wechat_accounts_passes_resolved_root_to_scraper(monkeypatch):
"""When a root department exists in the DB, the orchestrator resolves its
id and passes it as ``department_id`` to the scraper."""
engine = await _make_engine()
try:
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
captured: dict = {}
async def fake_scrape_accounts(client, *, department_id, **_kwargs):
captured["department_id"] = department_id
if False: # pragma: no cover
yield {}
async def fake_upsert(session, rec):
return None
monkeypatch.setattr(
orchestrator, "scrape_wechat_accounts", fake_scrape_accounts
)
monkeypatch.setattr(orchestrator, "upsert_wechat_account", fake_upsert)
async with SessionLocal() as session:
session.add(
Department(
dept_id="ROOT_DEPT", parent_id=None, name="Root"
)
)
await session.flush()
result = await orchestrator.sync_wechat_accounts(
session, client=object()
)
await session.commit()
assert result.status == "success"
assert captured["department_id"] == "ROOT_DEPT"
finally:
await engine.dispose()
# ===========================================================================
# Phone calls + SMS — full re-scrape entities (M4)
# ===========================================================================
@@ -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()