1.排程管理支持主表单元格合并,明细表显示
2.企业微信userid改为下拉框显示姓名+工号
This commit is contained in:
1 parent
05913f742f
commit
7d046862f7
5 files changed
+325
-13
No files matched your search
@@ -0,0 +1,102 @@
|
||||
from __future__ import annotations
|
||||
|
||||
"""
|
||||
WeCom mobile -> userid diagnostic script.
|
||||
|
||||
Usage:
|
||||
cd backend
|
||||
WECOM_DIAGNOSTIC_MOBILE=13800138000 .\\venv\\Scripts\\python.exe tests\\test_wecom_get_userid_by_mobile.py
|
||||
|
||||
Optional:
|
||||
WECOM_DIAGNOSTIC_VERIFY_USER_GET=1 After getuserid, call user/get for the returned userid.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import requests
|
||||
|
||||
BACKEND_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(BACKEND_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(BACKEND_ROOT))
|
||||
|
||||
from app import create_app # noqa: E402
|
||||
from app.services.wecom_notify import _get_access_token # noqa: E402
|
||||
|
||||
|
||||
def _get_userid_by_mobile(mobile: str) -> dict[str, Any]:
|
||||
token = _get_access_token()
|
||||
resp = requests.post(
|
||||
"https://qyapi.weixin.qq.com/cgi-bin/user/getuserid",
|
||||
params={"access_token": token},
|
||||
json={"mobile": mobile},
|
||||
timeout=8,
|
||||
)
|
||||
return {"http_status": resp.status_code, "response": resp.json()}
|
||||
|
||||
|
||||
def _user_get(userid: str) -> dict[str, Any]:
|
||||
token = _get_access_token()
|
||||
resp = requests.get(
|
||||
"https://qyapi.weixin.qq.com/cgi-bin/user/get",
|
||||
params={"access_token": token, "userid": userid},
|
||||
timeout=8,
|
||||
)
|
||||
return {"http_status": resp.status_code, "response": resp.json()}
|
||||
|
||||
|
||||
def main() -> int:
|
||||
mobile = os.getenv("WECOM_DIAGNOSTIC_MOBILE", "").strip()
|
||||
if not mobile:
|
||||
print("Missing WECOM_DIAGNOSTIC_MOBILE", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
app = create_app("development")
|
||||
with app.app_context():
|
||||
if not app.config.get("WECOM_CORP_ID") or not app.config.get("WECOM_SECRET"):
|
||||
print("WECOM_CORP_ID / WECOM_SECRET is not configured", file=sys.stderr)
|
||||
return 2
|
||||
if not app.config.get("WECOM_AGENT_ID"):
|
||||
print("WECOM_AGENT_ID is not configured", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
data = _get_userid_by_mobile(mobile)
|
||||
print(json.dumps({"mobile": mobile, "getuserid": data}, ensure_ascii=False, indent=2))
|
||||
|
||||
resp = data["response"] or {}
|
||||
if resp.get("errcode") != 0:
|
||||
print(
|
||||
f"user/getuserid failed for mobile={mobile}: {resp}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
userid = str(resp.get("userid") or "").strip()
|
||||
if not userid:
|
||||
print(
|
||||
f"user/getuserid succeeded but userid is empty for mobile={mobile}: {resp}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
if os.getenv("WECOM_DIAGNOSTIC_VERIFY_USER_GET", "1") == "1":
|
||||
user_get = _user_get(userid)
|
||||
print(json.dumps({"user_get": user_get}, ensure_ascii=False, indent=2))
|
||||
ug = user_get["response"] or {}
|
||||
if ug.get("errcode") != 0:
|
||||
print(
|
||||
f"user/get failed for userid={userid}: {ug}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
print("OK")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
from __future__ import annotations
|
||||
|
||||
"""
|
||||
WeCom userid diagnostic script.
|
||||
|
||||
Usage:
|
||||
cd backend
|
||||
WECOM_DIAGNOSTIC_TARGET=sophie .\\venv\\Scripts\\python.exe tests\\test_wecom_userid_diagnostic.py
|
||||
|
||||
Optional:
|
||||
WECOM_DIAGNOSTIC_SEND=1 Also try sending a minimal textcard message
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import or_
|
||||
|
||||
BACKEND_ROOT = Path(__file__).resolve().parents[1]
|
||||
if str(BACKEND_ROOT) not in sys.path:
|
||||
sys.path.insert(0, str(BACKEND_ROOT))
|
||||
|
||||
from app import create_app, db # noqa: E402
|
||||
from app.models import User # noqa: E402
|
||||
from app.services.wecom_notify import _get_access_token, send_wecom_textcard_message # noqa: E402
|
||||
|
||||
|
||||
def _resolve_target_userid(app, target: str) -> tuple[str, dict[str, Any] | None]:
|
||||
target = (target or "").strip()
|
||||
if not target:
|
||||
raise ValueError("WECOM_DIAGNOSTIC_TARGET is empty")
|
||||
|
||||
with app.app_context():
|
||||
query = db.session.query(User).filter(
|
||||
or_(
|
||||
User.username == target,
|
||||
User.real_name == target,
|
||||
User.employee_id == target,
|
||||
User.employee_id == target if target.isdigit() else False,
|
||||
)
|
||||
)
|
||||
user = query.first()
|
||||
if not user:
|
||||
# Allow direct WeCom userid input
|
||||
return target, None
|
||||
|
||||
snapshot = {
|
||||
"id": user.id,
|
||||
"username": user.username,
|
||||
"real_name": user.real_name,
|
||||
"employee_id": user.employee_id,
|
||||
"wecom_userid": user.wecom_userid,
|
||||
"wecom_unionid": user.wecom_unionid,
|
||||
"department": user.department,
|
||||
"status": user.status,
|
||||
}
|
||||
if not user.wecom_userid:
|
||||
raise RuntimeError(f"Matched DB user {user.username} but wecom_userid is empty")
|
||||
return str(user.wecom_userid).strip(), snapshot
|
||||
|
||||
|
||||
def _wecom_user_get(userid: str) -> dict[str, Any]:
|
||||
token = _get_access_token()
|
||||
import requests
|
||||
|
||||
resp = requests.get(
|
||||
"https://qyapi.weixin.qq.com/cgi-bin/user/get",
|
||||
params={"access_token": token, "userid": userid},
|
||||
timeout=8,
|
||||
)
|
||||
return {"http_status": resp.status_code, "response": resp.json()}
|
||||
|
||||
|
||||
def main() -> int:
|
||||
target = os.getenv("WECOM_DIAGNOSTIC_TARGET", "").strip()
|
||||
if not target:
|
||||
print("Missing WECOM_DIAGNOSTIC_TARGET", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
app = create_app("development")
|
||||
with app.app_context():
|
||||
if not app.config.get("WECOM_CORP_ID") or not app.config.get("WECOM_SECRET"):
|
||||
print("WECOM_CORP_ID / WECOM_SECRET is not configured", file=sys.stderr)
|
||||
return 2
|
||||
if not app.config.get("WECOM_AGENT_ID"):
|
||||
print("WECOM_AGENT_ID is not configured", file=sys.stderr)
|
||||
return 2
|
||||
|
||||
userid, matched_user = _resolve_target_userid(app, target)
|
||||
print(json.dumps({
|
||||
"target": target,
|
||||
"resolved_userid": userid,
|
||||
"matched_user": matched_user,
|
||||
}, ensure_ascii=False, indent=2))
|
||||
|
||||
user_get = _wecom_user_get(userid)
|
||||
print(json.dumps({"user_get": user_get}, ensure_ascii=False, indent=2))
|
||||
|
||||
user_get_resp = user_get["response"] or {}
|
||||
if user_get_resp.get("errcode") != 0:
|
||||
print(
|
||||
f"user/get failed for userid={userid}: {user_get_resp}. "
|
||||
f"This usually means the userid is invalid or the app has no access to that user.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
if os.getenv("WECOM_DIAGNOSTIC_SEND", "0") == "1":
|
||||
send_resp = send_wecom_textcard_message(
|
||||
userid,
|
||||
title="WeCom userid diagnostic",
|
||||
description=f"userid={userid}",
|
||||
jump_url="",
|
||||
)
|
||||
print(json.dumps({"send_response": send_resp}, ensure_ascii=False, indent=2))
|
||||
if send_resp.get("errcode") != 0:
|
||||
print(
|
||||
f"message/send failed for userid={userid}: {send_resp}. "
|
||||
f"If user/get passed, the userid is valid and the issue is likely app scope or agent permissions.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
print("OK")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
|
||||
Reference in new issue
Block a user