Add VIP payment, PocketBase schema, and full backend sync under apps/hackrobot.
Pull payjsapi/nomadmini/vip-track/PB and extend deploy backups so VIP backends are git-managed locally. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
1dac5fd5fd
commit
525fbf7766
@ -4,13 +4,15 @@
|
||||
|
||||
| 线上 | 本地 |
|
||||
|---|---|
|
||||
| vip.hackrobot.cn(`/www/wwwroot/nomadvip` · `:4007`) | `apps/hackrobot/vip/` |
|
||||
| vip.hackrobot.cn(`/www/wwwroot/nomadvip` · `:4007`,含 `app/api`) | `apps/hackrobot/vip/` |
|
||||
| admin.hackrobot.cn(静态 SPA) | `apps/hackrobot/admin-site/` |
|
||||
| LIVE API `:8010` | `apps/hackrobot/nomadmini/` |
|
||||
| 支付 payjsapi | `apps/hackrobot/payjsapi/` |
|
||||
| 埋点 `:4011` | `apps/hackrobot/vip-track/` |
|
||||
| PocketBase `:8090` | `apps/hackrobot/pocketbase/` |
|
||||
|
||||
同步:`python apps/hackrobot/deploy/pull_from_129.py`
|
||||
发版:`deploy_admin_site.py` / `deploy_vip.py` / `deploy_nomadmini.py`(**一律先** `hackrobot-backup`)
|
||||
同步:`pull_from_129.py` + `pull_backends_from_129.py`
|
||||
发版:`deploy_admin_site.py` / `deploy_vip.py` / `deploy_nomadmini.py` / `deploy_payjsapi.py`(**一律先** `hackrobot-backup`)
|
||||
|
||||
## 铁律
|
||||
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -99,6 +99,7 @@ next-env.d.ts
|
||||
/apps/hackrobot/**/*.bak-*
|
||||
/apps/hackrobot/**/*.db
|
||||
/apps/hackrobot/**/*.sqlite*
|
||||
!/apps/hackrobot/pocketbase/data.db
|
||||
|
||||
/apps/hackrobot/vip/config/server-actions.key
|
||||
|
||||
|
||||
28
apps/hackrobot/BACKEND_API_ROUTES.txt
Normal file
28
apps/hackrobot/BACKEND_API_ROUTES.txt
Normal file
@ -0,0 +1,28 @@
|
||||
/www/wwwroot/nomadvip/app/api/auth/email/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/auth/link-email/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/auth/logout/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/auth/me/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/auth/store-email-link/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/auth/sync-session/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/ops/analytics/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/ops/config/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/ops/control/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/ops/leads/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/ops/login/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/ops/members/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/ops/ntfy/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/ops/orders/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/ops/overview/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/ops/users/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/pay/confirm/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/pay/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/pay/status/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/probe-sink/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/promo/recognize-discount/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/vip/check-by-email/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/vip/check-by-user/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/vip/check/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/vip/_helpers.ts
|
||||
/www/wwwroot/nomadvip/app/api/vip/identity-diagnose/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/vip/linked-email/route.ts
|
||||
/www/wwwroot/nomadvip/app/api/vip/wechat-profile/route.ts
|
||||
@ -1,40 +1,42 @@
|
||||
# apps/hackrobot — 异度星球 VIP(与 Nomadro 隔离)
|
||||
|
||||
**域名写死,禁止与 Nomadro 后台混淆:**
|
||||
**域名写死,禁止与 Nomadro 后台混淆。**
|
||||
|
||||
| 域名 | 本目录 | 线上 |
|
||||
## 目录 ↔ 线上
|
||||
|
||||
| 线上 | 端口 | 本地 |
|
||||
|---|---|---|
|
||||
| [vip.hackrobot.cn](https://vip.hackrobot.cn/) | `vip/` | `/www/wwwroot/nomadvip` · Next `:4007` |
|
||||
| [admin.hackrobot.cn](https://admin.hackrobot.cn/) | `admin-site/` | `/www/wwwroot/admin.hackrobot.cn` 静态 SPA |
|
||||
| LIVE / 拉黑 API | `nomadmini/` | `/opt/nomadmini` · uvicorn `:8010` |
|
||||
| 埋点 | `vip-track/` | `/opt/vip-track` · `:4011` |
|
||||
| [vip.hackrobot.cn](https://vip.hackrobot.cn/) Next(含 `app/api` 后端) | `:4007` | `vip/` ← `/www/wwwroot/nomadvip` |
|
||||
| [admin.hackrobot.cn](https://admin.hackrobot.cn/) 运营台 SPA | nginx 静态 | `admin-site/` |
|
||||
| LIVE / 拉黑 API(nomadmini) | `:8010` | `nomadmini/` ← `/opt/nomadmini` |
|
||||
| 支付后端 payjsapi | (pay 回调) | `payjsapi/` ← `/www/wwwroot/payjsapi` |
|
||||
| 埋点 vip-track | `:4011` | `vip-track/` ← `/opt/vip-track` |
|
||||
| PocketBase | `:8090` | `pocketbase/`(`data.db` + schema,不含 storage 大文件) |
|
||||
|
||||
`apps/admin` = **admin.nomadro.cn**(微信/抖音),**禁止**部署到 `admin.hackrobot.cn`。
|
||||
`apps/admin` = **admin.nomadro.cn**,**禁止**部署到 `admin.hackrobot.cn`。
|
||||
|
||||
VIP API 路由清单:`BACKEND_API_ROUTES.txt`(ops / pay / auth / vip)。
|
||||
|
||||
## 同步(线上 → 本地)
|
||||
|
||||
```bash
|
||||
# 全栈(前台 + 运营台 + 后端)
|
||||
python apps/hackrobot/deploy/pull_from_129.py
|
||||
python apps/hackrobot/deploy/pull_backends_from_129.py
|
||||
```
|
||||
|
||||
会刷新 `SYNC_MANIFEST.json`。不含 `node_modules` / `.next` / 真实 `.env`。
|
||||
不含 `node_modules` / `.next` / 真实 `.env`。
|
||||
|
||||
## 发版(本地 → 线上,强制先备份)
|
||||
|
||||
```bash
|
||||
# 仅 admin 静态站
|
||||
python apps/hackrobot/deploy/deploy_admin_site.py
|
||||
|
||||
# VIP Next(需本轮明确允许上前端)
|
||||
python apps/hackrobot/deploy/deploy_vip.py
|
||||
|
||||
# nomadmini API
|
||||
python apps/hackrobot/deploy/deploy_vip.py # 源码;加 --build 才编译重启
|
||||
python apps/hackrobot/deploy/deploy_nomadmini.py
|
||||
python apps/hackrobot/deploy/deploy_payjsapi.py
|
||||
```
|
||||
|
||||
每次发版脚本会先在 129 执行 `/opt/hackrobot-backup/backup_before_deploy.sh`,产物进:
|
||||
|
||||
`/opt/hackrobot-backup/snapshots/<ts>/`
|
||||
快照:`/opt/hackrobot-backup/snapshots/`(含 admin / vip / nomadmini / payjsapi / pb)。
|
||||
|
||||
## 安装服务器备份钩子(一次性)
|
||||
|
||||
|
||||
@ -13,5 +13,16 @@
|
||||
"admin": "/www/wwwroot/admin.hackrobot.cn static SPA",
|
||||
"nomadmini": "/opt/nomadmini → :8010 live ops",
|
||||
"vip_track": "/opt/vip-track → :4011"
|
||||
}
|
||||
},
|
||||
"backends": {
|
||||
"pulled_at": "2026-11-22T10:53:18+0800",
|
||||
"files": {
|
||||
"vip-track.tgz": 19846,
|
||||
"pocketbase-schema.tgz": 500228,
|
||||
"vip-api.tgz": 44933,
|
||||
"payjsapi.tgz": 200046,
|
||||
"nomadmini.tgz": 562567
|
||||
}
|
||||
},
|
||||
"backends_pulled_at": "2026-11-22T10:53:18+0800"
|
||||
}
|
||||
@ -2,10 +2,12 @@
|
||||
|
||||
| 脚本 | 作用 |
|
||||
|---|---|
|
||||
| `pull_from_129.py` | 线上 → 本地 `apps/hackrobot/*` |
|
||||
| `pull_from_129.py` | 前台/运营台/初版后端 → 本地 |
|
||||
| `pull_backends_from_129.py` | **补全后端**:payjsapi / nomadmini / vip-track / PB / vip `app/api` |
|
||||
| `install_server_backup.py` | 安装 `/opt/hackrobot-backup` + `hackrobot-backup` |
|
||||
| `deploy_admin_site.py` | 发 `admin.hackrobot.cn`(先备份) |
|
||||
| `deploy_vip.py` | 同步 VIP 源码;`--build` 才编译重启 |
|
||||
| `deploy_nomadmini.py` | 发 LIVE API `:8010`(先备份) |
|
||||
| `deploy_payjsapi.py` | 发支付后端(先备份) |
|
||||
|
||||
机器:`129.204.21.129`。快照:`/opt/hackrobot-backup/snapshots/`。
|
||||
|
||||
71
apps/hackrobot/deploy/_probe_backends.py
Normal file
71
apps/hackrobot/deploy/_probe_backends.py
Normal file
@ -0,0 +1,71 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""List every VIP-related backend on 129."""
|
||||
from __future__ import annotations
|
||||
|
||||
import paramiko
|
||||
|
||||
HOST, USER, PASS = "129.204.21.129", "ubuntu", "Xiao4669805"
|
||||
CMD = r"""
|
||||
set +e
|
||||
echo '=== listeners ==='
|
||||
sudo ss -lntp | grep -E '4007|4011|8010|8090|8080|3000|8091|8092|5001|8099' || true
|
||||
|
||||
echo
|
||||
echo '=== systemd vip/nomad/pay/pb ==='
|
||||
systemctl list-units --type=service --all 2>/dev/null | grep -iE 'vip|nomad|pay|pocket|pb|track|admini|xor|zpay' | head -40
|
||||
|
||||
echo
|
||||
echo '=== /opt top ==='
|
||||
ls -lah /opt/ | head -60
|
||||
|
||||
echo
|
||||
echo '=== nomadvip app/api tree ==='
|
||||
find /www/wwwroot/nomadvip/app/api -type d 2>/dev/null | head -80
|
||||
ls /www/wwwroot/nomadvip/app/api/ops 2>/dev/null
|
||||
|
||||
echo
|
||||
echo '=== pocketbase for vip ==='
|
||||
ls -lah /www/wwwroot/pocketbase.hackrobot.cn 2>/dev/null | head
|
||||
ps aux | grep -i pocketbase | grep -v grep | head
|
||||
sudo find /opt /home/ubuntu /www/wwwroot -maxdepth 3 -iname '*pocketbase*' -o -iname 'pb_data' 2>/dev/null | head -40
|
||||
|
||||
echo
|
||||
echo '=== pay related ==='
|
||||
ls /www/wwwroot/payjsapi 2>/dev/null | head
|
||||
ls /opt | grep -iE 'pay|xor|zpay'
|
||||
sudo find /opt /www/wwwroot -maxdepth 2 -iname '*pay*' 2>/dev/null | head -30
|
||||
|
||||
echo
|
||||
echo '=== vip-track + nomadmini sizes ==='
|
||||
du -sh /opt/vip-track /opt/nomadmini /www/wwwroot/nomadvip 2>/dev/null
|
||||
ls -la /opt/vip-track/ | head -30
|
||||
ls -la /opt/nomadmini/ | head -20
|
||||
ls -la /opt/nomadmini/backend/ | head -30
|
||||
|
||||
echo
|
||||
echo '=== what local pull missed: compare key backend files ==='
|
||||
# list python/go services under /opt
|
||||
for d in /opt/*/; do
|
||||
[ -d "$d" ] || continue
|
||||
name=$(basename "$d")
|
||||
case "$name" in
|
||||
containerd|google|puppet*|rancher*|bitnami) continue ;;
|
||||
esac
|
||||
echo "-- $name --"
|
||||
ls "$d" 2>/dev/null | head -8
|
||||
done
|
||||
"""
|
||||
|
||||
c = paramiko.SSHClient()
|
||||
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
c.connect(HOST, username=USER, password=PASS, timeout=50, allow_agent=False, look_for_keys=False)
|
||||
chan = c.get_transport().open_session()
|
||||
chan.settimeout(120)
|
||||
chan.get_pty()
|
||||
chan.exec_command(CMD)
|
||||
while True:
|
||||
chunk = chan.recv(65536)
|
||||
if not chunk:
|
||||
break
|
||||
print(chunk.decode("utf-8", "replace"), end="", flush=True)
|
||||
c.close()
|
||||
@ -42,7 +42,24 @@ fi
|
||||
# vip-track
|
||||
if [[ -d /opt/vip-track ]]; then
|
||||
tar -C /opt -czf "$DEST/vip-track.tgz" \
|
||||
--exclude='vip-track/.venv' --exclude='vip-track/.env' vip-track 2>/dev/null || true
|
||||
--exclude='vip-track/.venv' --exclude='vip-track/.env' --exclude='vip-track/*.db' vip-track 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# payjsapi (payment backend)
|
||||
if [[ -d /www/wwwroot/payjsapi ]]; then
|
||||
tar -C /www/wwwroot -czf "$DEST/payjsapi.tgz" \
|
||||
--exclude='payjsapi/venv' --exclude='payjsapi/.git' --exclude='payjsapi/*.log' \
|
||||
--exclude='payjsapi/.env' --exclude='payjsapi/.env.local' payjsapi 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# PocketBase schema + data.db (no storage blobs)
|
||||
if [[ -d /home/ubuntu/pb_data ]]; then
|
||||
mkdir -p "$DEST/pb"
|
||||
cp -a /home/ubuntu/pocketbase.sh "$DEST/pb/" 2>/dev/null || true
|
||||
cp -a /home/ubuntu/pb_data/types.d.ts "$DEST/pb/" 2>/dev/null || true
|
||||
cp -a /home/ubuntu/pb_data/data.db "$DEST/pb/" 2>/dev/null || true
|
||||
sqlite3 /home/ubuntu/pb_data/data.db '.schema' > "$DEST/pb/data.schema.sql" 2>/dev/null || true
|
||||
tar -C "$DEST/pb" -czf "$DEST/pocketbase.tgz" . 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# nginx refs
|
||||
|
||||
61
apps/hackrobot/deploy/deploy_payjsapi.py
Normal file
61
apps/hackrobot/deploy/deploy_payjsapi.py
Normal file
@ -0,0 +1,61 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""Deploy apps/hackrobot/payjsapi → /www/wwwroot/payjsapi (backup first)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import tarfile
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from _ssh import connect, require_backup, run
|
||||
|
||||
SRC = Path(__file__).resolve().parents[1] / "payjsapi"
|
||||
REMOTE = "/www/wwwroot/payjsapi"
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if not (SRC / "run.py").exists() and not (SRC / "app").exists():
|
||||
raise SystemExit("missing payjsapi — run pull_backends_from_129.py first")
|
||||
|
||||
c = connect()
|
||||
print("[1] backup…")
|
||||
require_backup(c, "pre_payjsapi")
|
||||
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
tgz = Path(td) / "payjsapi.tgz"
|
||||
with tarfile.open(tgz, "w:gz") as tar:
|
||||
for p in SRC.rglob("*"):
|
||||
if not p.is_file():
|
||||
continue
|
||||
rel = str(p.relative_to(SRC)).replace("\\", "/")
|
||||
if p.name.startswith(".env") and "example" not in p.name and "keys" not in p.name:
|
||||
continue
|
||||
if ".bak" in p.name.lower():
|
||||
continue
|
||||
tar.add(p, arcname=rel)
|
||||
sftp = c.open_sftp()
|
||||
sftp.put(str(tgz), "/tmp/payjsapi-deploy.tgz")
|
||||
sftp.close()
|
||||
|
||||
print("[2] sync (keep .env / venv)…")
|
||||
run(
|
||||
c,
|
||||
rf"""
|
||||
set -e
|
||||
TMP=/tmp/payjsapi-unpack-$$
|
||||
rm -rf "$TMP" && mkdir -p "$TMP"
|
||||
tar -xzf /tmp/payjsapi-deploy.tgz -C "$TMP"
|
||||
sudo rsync -a \
|
||||
--exclude venv --exclude .git --exclude .env --exclude .env.local \
|
||||
--exclude '*.log' \
|
||||
"$TMP"/ {REMOTE}/
|
||||
echo DEPLOY_PAYJSAPI_OK
|
||||
ls -lah {REMOTE} | head
|
||||
""",
|
||||
timeout=120,
|
||||
)
|
||||
c.close()
|
||||
print("[done] payjsapi")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
229
apps/hackrobot/deploy/pull_backends_from_129.py
Normal file
229
apps/hackrobot/deploy/pull_backends_from_129.py
Normal file
@ -0,0 +1,229 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Pull VIP backends that were incomplete:
|
||||
- payjsapi (payment API)
|
||||
- nomadmini (full, re-sync)
|
||||
- vip-track (re-sync)
|
||||
- pocketbase runtime scripts + schema (not secrets; optional data.db)
|
||||
- vip Next app/api already in vip/ — re-confirm ops routes
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import shutil
|
||||
import tarfile
|
||||
import tempfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import paramiko
|
||||
|
||||
HOST, USER, PASS = "129.204.21.129", "ubuntu", "Xiao4669805"
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
REMOTE = r"""
|
||||
set -e
|
||||
OUT=/tmp/hackrobot_backends_$$
|
||||
mkdir -p "$OUT/extra"
|
||||
|
||||
# payjsapi source
|
||||
sudo tar -C /www/wwwroot/payjsapi -czf "$OUT/payjsapi.tgz" \
|
||||
--exclude=venv --exclude=.git --exclude='*.log' --exclude='*.bak*' \
|
||||
--exclude='.env' --exclude='.env.local' \
|
||||
app run.py requirements.txt README.md payjs.sh androidh5api.sh \
|
||||
run-with-logs.sh run-with-logs.bat .env.example .gitignore \
|
||||
'手机微信支付调试说明.md' 2>/dev/null || \
|
||||
sudo tar -C /www/wwwroot/payjsapi -czf "$OUT/payjsapi.tgz" \
|
||||
--exclude=venv --exclude=.git --exclude='*.log' --exclude='*.bak*' \
|
||||
--exclude='.env' --exclude='.env.local' .
|
||||
|
||||
# env keys only for payjsapi
|
||||
python3 - <<'PY'
|
||||
from pathlib import Path
|
||||
for name in ['.env','.env.local','.env.example']:
|
||||
p=Path('/www/wwwroot/payjsapi')/name
|
||||
if not p.exists(): continue
|
||||
lines=[]
|
||||
for line in p.read_text('utf-8','ignore').splitlines():
|
||||
if not line.strip() or line.strip().startswith('#') or '=' not in line:
|
||||
lines.append(line); continue
|
||||
lines.append(line.split('=',1)[0]+'=')
|
||||
Path('/tmp/hackrobot_backends_keys_pay').write_text('\n'.join(lines)+'\n','utf-8')
|
||||
break
|
||||
PY
|
||||
cp /tmp/hackrobot_backends_keys_pay "$OUT/extra/payjsapi.env.keys.example" 2>/dev/null || true
|
||||
|
||||
# full nomadmini (no venv/env/bak)
|
||||
sudo tar -C /opt/nomadmini -czf "$OUT/nomadmini.tgz" \
|
||||
--exclude='backend/.venv' --exclude='**/__pycache__' --exclude='**/*.pyc' \
|
||||
--exclude='**/.env' --exclude='**/.env.*' --exclude='**/*.bak*' \
|
||||
--exclude='**/*.db' --exclude='.git' .
|
||||
|
||||
# vip-track source only
|
||||
sudo tar -C /opt/vip-track -czf "$OUT/vip-track.tgz" \
|
||||
server.py vip-track.js healthcheck.sh \
|
||||
--exclude='.env' --exclude='ntfy.env' --exclude='*.db' --exclude='__pycache__' 2>/dev/null || \
|
||||
sudo tar -C /opt/vip-track -czf "$OUT/vip-track.tgz" \
|
||||
--exclude='.env' --exclude='ntfy.env' --exclude='*.db' --exclude='__pycache__' \
|
||||
--exclude='*.log' --exclude='health.state' --exclude='health.streak' .
|
||||
|
||||
# pocketbase: binary too big optional — ship script + types + schema dump
|
||||
mkdir -p "$OUT/pb"
|
||||
cp /home/ubuntu/pocketbase.sh "$OUT/pb/" 2>/dev/null || true
|
||||
cp /home/ubuntu/pb_data/types.d.ts "$OUT/pb/" 2>/dev/null || true
|
||||
# sqlite schema (no row data)
|
||||
sqlite3 /home/ubuntu/pb_data/data.db '.schema' > "$OUT/pb/data.schema.sql" 2>/dev/null || true
|
||||
# collection list via sqlite
|
||||
sqlite3 /home/ubuntu/pb_data/data.db "SELECT name FROM sqlite_master WHERE type='table';" > "$OUT/pb/tables.txt" 2>/dev/null || true
|
||||
# compact data.db copy for local restore (2MB) — user asked for backend
|
||||
cp /home/ubuntu/pb_data/data.db "$OUT/pb/data.db" 2>/dev/null || true
|
||||
sudo tar -C "$OUT/pb" -czf "$OUT/pocketbase-schema.tgz" .
|
||||
|
||||
# vip Next API surface checklist
|
||||
find /www/wwwroot/nomadvip/app/api -type f -name '*.ts' | sort > "$OUT/extra/vip-api-routes.txt"
|
||||
# also pack app/api alone for safety overwrite
|
||||
sudo tar -C /www/wwwroot/nomadvip -czf "$OUT/vip-api.tgz" app/api app/lib
|
||||
|
||||
python3 - <<PY
|
||||
import json,time
|
||||
from pathlib import Path
|
||||
out=Path('$OUT')
|
||||
meta={
|
||||
'pulled_at': time.strftime('%Y-%m-%dT%H:%M:%S%z'),
|
||||
'files': {p.name: p.stat().st_size for p in out.glob('*.tgz')},
|
||||
}
|
||||
(out/'MANIFEST.json').write_text(json.dumps(meta,ensure_ascii=False,indent=2))
|
||||
print(json.dumps(meta,indent=2))
|
||||
PY
|
||||
sudo rm -f /tmp/hackrobot_backends_bundle.tgz
|
||||
sudo tar -C "$OUT" -czf /tmp/hackrobot_backends_bundle.tgz .
|
||||
sudo chown ubuntu:ubuntu /tmp/hackrobot_backends_bundle.tgz
|
||||
ls -lah /tmp/hackrobot_backends_bundle.tgz
|
||||
echo BUNDLE_OK
|
||||
"""
|
||||
|
||||
|
||||
def wipe_dir(dest: Path) -> None:
|
||||
if not dest.exists():
|
||||
dest.mkdir(parents=True)
|
||||
return
|
||||
for child in dest.iterdir():
|
||||
if child.name in {".gitkeep", ".gitignore"}:
|
||||
continue
|
||||
if child.is_dir():
|
||||
shutil.rmtree(child)
|
||||
else:
|
||||
child.unlink()
|
||||
|
||||
|
||||
def extract(tgz: Path, dest: Path) -> None:
|
||||
wipe_dir(dest)
|
||||
dest.mkdir(parents=True, exist_ok=True)
|
||||
with tarfile.open(tgz, "r:gz") as tar:
|
||||
for m in tar.getmembers():
|
||||
name = m.name.replace("\r", "").replace("\n", "").strip()
|
||||
if not name or ".." in Path(name).parts:
|
||||
continue
|
||||
if ".bak" in name.lower() or name.endswith(".pyc") or "__pycache__" in name:
|
||||
continue
|
||||
m.name = name
|
||||
try:
|
||||
tar.extract(m, path=dest)
|
||||
except OSError as e:
|
||||
print("[skip bad]", name, e)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
c = paramiko.SSHClient()
|
||||
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
c.connect(HOST, username=USER, password=PASS, timeout=60, allow_agent=False, look_for_keys=False)
|
||||
print("[remote] packing backends…")
|
||||
chan = c.get_transport().open_session()
|
||||
chan.settimeout(300)
|
||||
chan.get_pty()
|
||||
chan.exec_command(REMOTE)
|
||||
out = b""
|
||||
while True:
|
||||
chunk = chan.recv(65536)
|
||||
if not chunk:
|
||||
break
|
||||
out += chunk
|
||||
print(chunk.decode("utf-8", "replace"), end="", flush=True)
|
||||
if b"BUNDLE_OK" not in out:
|
||||
raise SystemExit("pack failed")
|
||||
|
||||
cache = ROOT / "deploy" / ".cache"
|
||||
cache.mkdir(parents=True, exist_ok=True)
|
||||
bundle = cache / "hackrobot_backends_bundle.tgz"
|
||||
sftp = c.open_sftp()
|
||||
sftp.get("/tmp/hackrobot_backends_bundle.tgz", str(bundle))
|
||||
sftp.close()
|
||||
c.close()
|
||||
print("[local]", bundle.stat().st_size)
|
||||
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
td_path = Path(td)
|
||||
with tarfile.open(bundle, "r:gz") as tar:
|
||||
tar.extractall(td_path)
|
||||
|
||||
mapping = {
|
||||
"payjsapi.tgz": ROOT / "payjsapi",
|
||||
"nomadmini.tgz": ROOT / "nomadmini",
|
||||
"vip-track.tgz": ROOT / "vip-track",
|
||||
"pocketbase-schema.tgz": ROOT / "pocketbase",
|
||||
}
|
||||
for name, dest in mapping.items():
|
||||
src = td_path / name
|
||||
if src.exists():
|
||||
extract(src, dest)
|
||||
print("[ok]", name, "→", dest)
|
||||
|
||||
# merge vip API into vip/
|
||||
vip_api = td_path / "vip-api.tgz"
|
||||
if vip_api.exists():
|
||||
vip_dest = ROOT / "vip"
|
||||
with tarfile.open(vip_api, "r:gz") as tar:
|
||||
tar.extractall(vip_dest)
|
||||
print("[ok] vip-api → vip/app/api + vip/app/lib")
|
||||
|
||||
extra = td_path / "extra"
|
||||
if extra.exists():
|
||||
for f in extra.iterdir():
|
||||
if f.name.endswith("payjsapi.env.keys.example"):
|
||||
(ROOT / "payjsapi" / "payjsapi.env.keys.example").write_bytes(f.read_bytes())
|
||||
elif f.name == "vip-api-routes.txt":
|
||||
(ROOT / "BACKEND_API_ROUTES.txt").write_bytes(f.read_bytes())
|
||||
print("[extra]", f.name)
|
||||
|
||||
man = td_path / "MANIFEST.json"
|
||||
if man.exists():
|
||||
prev = {}
|
||||
mp = ROOT / "SYNC_MANIFEST.json"
|
||||
if mp.exists():
|
||||
try:
|
||||
prev = json.loads(mp.read_text("utf-8"))
|
||||
except Exception:
|
||||
prev = {}
|
||||
cur = json.loads(man.read_text("utf-8"))
|
||||
prev["backends"] = cur
|
||||
prev["backends_pulled_at"] = cur.get("pulled_at")
|
||||
mp.write_text(json.dumps(prev, ensure_ascii=False, indent=2), "utf-8")
|
||||
|
||||
# scrub secrets if any slipped
|
||||
for p in ROOT.rglob("*"):
|
||||
if not p.is_file():
|
||||
continue
|
||||
if p.name.startswith(".env") and "example" not in p.name and "keys" not in p.name:
|
||||
print("[scrub]", p)
|
||||
p.unlink()
|
||||
if p.suffix == ".db" and "pocketbase" not in str(p).replace("\\", "/"):
|
||||
# keep pocketbase/data.db only
|
||||
if "pocketbase" not in p.parts:
|
||||
print("[scrub db]", p)
|
||||
p.unlink()
|
||||
|
||||
print("[done]")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@ -1 +0,0 @@
|
||||
|
||||
37
apps/hackrobot/payjsapi/.env.example
Normal file
37
apps/hackrobot/payjsapi/.env.example
Normal file
@ -0,0 +1,37 @@
|
||||
# ========== 本地调试 ==========
|
||||
# PORT: 默认 8007,与前端 PAYMENT_API_URL 一致
|
||||
# BASE_URL 需为 ZPAY 可访问的回调地址。本地调试时 ZPAY 无法回调 localhost/内网,
|
||||
# 可用 ngrok 等隧道:BASE_URL=https://xxx.ngrok.io
|
||||
# 或直接使用生产地址测试回调(需 payjsapi 已部署到公网)
|
||||
PORT=8007
|
||||
BASE_URL=https://api.hackrobot.cn
|
||||
|
||||
# 支付渠道:zpay | xorpay
|
||||
PAYMENT_PROVIDER=zpay
|
||||
|
||||
# XorPay
|
||||
XORPAY_AID=8220
|
||||
XORPAY_SECRET=your_secret
|
||||
|
||||
# ZPAY(网关: https://zpayz.cn/)
|
||||
ZPAY_PID=2025121809351743
|
||||
ZPAY_KEY=tpEi7wWIWI2kXiYVTpIG6j7it0mjVW89
|
||||
|
||||
# PocketBase(check-user、ensure-user、支付回调、nomadvip/digital/cnomadcna 共用)
|
||||
# 公网 PB_URL 会被忽略并回落本机 8090;本地无 PocketBase 时复制 .env.local(含 PB_INTERNAL_URL)
|
||||
PB_URL=https://pocketbase.hackrobot.cn
|
||||
# PB_INTERNAL_URL=https://pocketbase.hackrobot.cn
|
||||
PB_ADMIN_EMAIL=xiaoshuang.eric@gmail.com
|
||||
PB_ADMIN_PASSWORD=Xiao4669805@
|
||||
|
||||
# Memos(nomadvip 支付成功后创建账号,与 payjsapi 同源)
|
||||
MEMOS_API=https://qun.hackrobot.cn/api/v1/users
|
||||
MEMOS_TOKEN=eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoiaGFja3JvYm90IiwiaXNzIjoibWVtb3MiLCJzdWIiOiIxIiwiYXVkIjpbInVzZXIuYWNjZXNzLXRva2VuIl0sImlhdCI6MTc1NzQwNTE0OX0.Idn7kBlxE-CoSOXwWuZ1tHGIRKHAIeDyXSafGS5OHsg
|
||||
|
||||
# MinIO 对象存储(可选)
|
||||
MINIO_ENDPOINT=minioweb.hackrobot.cn
|
||||
MINIO_PORT=443
|
||||
MINIO_ACCESS_KEY=
|
||||
MINIO_SECRET_KEY=
|
||||
MINIO_BUCKET=hackrobot
|
||||
MINIO_UPLOAD_PREFIX=joins
|
||||
18
apps/hackrobot/payjsapi/.gitignore
vendored
Normal file
18
apps/hackrobot/payjsapi/.gitignore
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
# 日志文件(不产生,避免 git 本地/线上不一致)
|
||||
*.log
|
||||
payment_notify.log
|
||||
xorpay_notify.log
|
||||
|
||||
# Python
|
||||
__pycache__/
|
||||
*.pyc
|
||||
*.py[cod]
|
||||
venv/
|
||||
.venv/
|
||||
# .env
|
||||
# .env.local
|
||||
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*.pyo
|
||||
*.pyd
|
||||
122
apps/hackrobot/payjsapi/README.md
Normal file
122
apps/hackrobot/payjsapi/README.md
Normal file
@ -0,0 +1,122 @@
|
||||
# PayJS API
|
||||
|
||||
支付 API 服务,支持多支付渠道(XorPay、ZPAY),按项目模块化拆分。
|
||||
|
||||
## 路由与项目对应
|
||||
|
||||
| 项目 | 路由前缀 | 说明 |
|
||||
|------|----------|------|
|
||||
| nomadvip | `/nomadvip/*` | 固定 ZPAY,独立实例 |
|
||||
| digital | `/digital/*` | 使用 PAYMENT_PROVIDER |
|
||||
| cnomadcna | `/cnomadcna/*` | 使用 PAYMENT_PROVIDER |
|
||||
| nomadlms | `/nomadlms/*` | 使用 PAYMENT_PROVIDER |
|
||||
| legacy | `/payh5`、`/zpay_notify` 等 | 向后兼容 |
|
||||
|
||||
各项目业务互不影响,可单独配置。
|
||||
|
||||
## 环境
|
||||
|
||||
本地调试:`BASE_URL` 需为 ZPAY 可访问地址(如 ngrok 暴露 8700 端口)
|
||||
线上部署:`BASE_URL=https://api.hackrobot.cn`
|
||||
|
||||
## 模块结构
|
||||
|
||||
```
|
||||
app/
|
||||
├── config.py # 配置(环境变量)
|
||||
├── main.py # FastAPI 入口,挂载路由
|
||||
├── sdk/ # SDK 组件化封装
|
||||
│ ├── payment/ # 支付网关(ZPAY/XorPay)
|
||||
│ ├── pocketbase.py # PocketBase 数据库
|
||||
│ ├── minio.py # MinIO 对象存储
|
||||
│ └── memos.py # Memos 用户(仅 nomadvip 使用)
|
||||
├── services/ # 共享业务逻辑(基于 SDK)
|
||||
│ ├── pb.py # PocketBase 客户端
|
||||
│ ├── memos.py # Memos 用户创建
|
||||
│ ├── minio.py # MinIO 上传
|
||||
│ └── payment.py # 支付成功处理
|
||||
├── routers/ # 项目路由
|
||||
│ ├── nomadvip.py # /nomadvip/*
|
||||
│ ├── digital.py # /digital/*
|
||||
│ ├── cnomadcna.py # /cnomadcna/*
|
||||
│ ├── nomadlms.py # /nomadlms/*
|
||||
│ ├── legacy.py # /payh5 等(向后兼容)
|
||||
│ └── common.py # /submit_meetup_application、/create_user
|
||||
└── payment/
|
||||
├── base.py # 支付抽象基类
|
||||
├── xorpay.py # XorPay 实现
|
||||
├── zpay.py # ZPAY 实现(易支付兼容)
|
||||
└── factory.py # 渠道工厂
|
||||
```
|
||||
|
||||
## 支付渠道
|
||||
|
||||
| 渠道 | 环境变量 | 说明 |
|
||||
|------|----------|------|
|
||||
| XorPay | `PAYMENT_PROVIDER=xorpay` | 默认 |
|
||||
| ZPAY | `PAYMENT_PROVIDER=zpay` | 需配置 `ZPAY_PID`、`ZPAY_KEY` |
|
||||
|
||||
切换渠道:设置环境变量 `PAYMENT_PROVIDER=xorpay` 或 `zpay`,参考 `.env.example`。
|
||||
|
||||
## 本地调试
|
||||
|
||||
```bash
|
||||
# 复制配置
|
||||
cp .env.example .env
|
||||
|
||||
# 本地调试时 BASE_URL 需 ZPAY 可访问,可用 ngrok:
|
||||
# ngrok http 8700 -> 将 BASE_URL 设为 ngrok 提供的 https 地址
|
||||
|
||||
python run.py
|
||||
# 或 uvicorn app.main:app --host 0.0.0.0 --port 8700 --reload
|
||||
```
|
||||
|
||||
## 线上部署
|
||||
|
||||
域名:`https://api.hackrobot.cn`
|
||||
|
||||
```bash
|
||||
# .env 中设置
|
||||
BASE_URL=https://api.hackrobot.cn
|
||||
PAYMENT_PROVIDER=zpay
|
||||
# 其他 ZPAY、PocketBase、Memos 配置见 .env.example
|
||||
```
|
||||
|
||||
## 线上「支付失败」排查
|
||||
|
||||
本地正常、Ubuntu 部署后 api.hackrobot.cn 调用支付失败,常见原因:
|
||||
|
||||
1. **BASE_URL 配置错误**:必须为 ZPAY 可公网访问的地址。错误示例:`http://127.0.0.1:8700`、`http://localhost`。正确:`https://api.hackrobot.cn`(域名需解析到本机,nginx 反向代理对应端口)。
|
||||
|
||||
2. **服务器无法访问 zpayz.cn**:防火墙、DNS 或网络限制。访问 `https://api.hackrobot.cn/health?check_zpay=1` 自检:
|
||||
- `zpayz_reachable: false` → 开放出站或检查网络
|
||||
- `zpayz_error` 含 "SSL" → 执行 `apt install ca-certificates` 后重启
|
||||
|
||||
3. **client_ip 内网导致 ZPAY 风控拒单**:前端(digital/cnomadcna/nomadvip)通过 nginx 代理时,需配置 `proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for`,否则 payjsapi 收到 127.0.0.1 会触发风控。已做兼容:内网 IP 时自动省略 clientip 参数。
|
||||
|
||||
4. **启动时控制台有 BASE_URL 警告**:说明配置了内网地址,需修改 `.env` 中的 `BASE_URL` 为公网地址后重启。
|
||||
|
||||
5. **查看详细错误**:支付失败时,payjsapi 会输出 `ZPAY create_order_api 失败` 日志,含 status/code/msg,便于定位。
|
||||
|
||||
## 自检结果(2026-04-01)
|
||||
|
||||
本次联动自检覆盖了 `nomadvip` 前端与本仓库 `nomadvip` 路由的支付闭环:
|
||||
|
||||
- 创建订单:`/nomadvip/payh5`、`/nomadvip/payh5/redirect`
|
||||
- 异步通知:`/nomadvip/xorpay_notify`、`/nomadvip/zpay_notify`
|
||||
- 支付查询:`/nomadvip/order_status`
|
||||
- VIP 识别:`/nomadvip/check_vip`、`/nomadvip/check_vip_by_email`
|
||||
|
||||
### 发现问题
|
||||
|
||||
- 业务链路问题发生在前端 `nomadvip`:`/api/pay/confirm` 兜底写 `payments` 时使用默认金额,可能与实际支付金额不一致(例如测试价、活动价)。
|
||||
|
||||
### 处理状态
|
||||
|
||||
- 已在前端仓库修复:confirm 兜底写库时优先使用网关返回 `pay_price`,失败才回退默认金额。
|
||||
- 本仓库暂未发现新的阻断型业务 bug(以静态代码审阅为准)。
|
||||
|
||||
## 维护建议
|
||||
|
||||
- 建议保持 `payjsapi` 为唯一支付入账源,前端 confirm 仅作为短时兜底。
|
||||
- 建议为 `order_status` 返回结构固定增加字段约束(如统一返回 `pay_price` 字段),避免调用侧重复兼容。
|
||||
1
apps/hackrobot/payjsapi/androidh5api.sh
Normal file
1
apps/hackrobot/payjsapi/androidh5api.sh
Normal file
@ -0,0 +1 @@
|
||||
python3 run.py
|
||||
0
apps/hackrobot/payjsapi/app/__init__.py
Normal file
0
apps/hackrobot/payjsapi/app/__init__.py
Normal file
78
apps/hackrobot/payjsapi/app/config.py
Normal file
78
apps/hackrobot/payjsapi/app/config.py
Normal file
@ -0,0 +1,78 @@
|
||||
"""
|
||||
应用配置
|
||||
支付渠道可通过 PAYMENT_PROVIDER 切换:xorpay | zpay
|
||||
"""
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 强制从 payjsapi 项目根目录加载 .env、.env.local,避免 cwd 不同导致加载失败
|
||||
_root = Path(__file__).resolve().parent.parent
|
||||
load_dotenv(_root / ".env")
|
||||
load_dotenv(_root / ".env.local", override=True)
|
||||
|
||||
PaymentChannel = Literal["xorpay", "zpay"]
|
||||
|
||||
# 当前使用的支付渠道(zpay 替代 xorpay,payjs2 接口业务保留)
|
||||
PAYMENT_PROVIDER: PaymentChannel = os.getenv("PAYMENT_PROVIDER", "zpay").lower()
|
||||
|
||||
# XorPay 配置
|
||||
XORPAY_AID = os.getenv("XORPAY_AID", "8220")
|
||||
XORPAY_SECRET = os.getenv("XORPAY_SECRET", "afcacd99570945f88de62624aaa3578e")
|
||||
XORPAY_CASHIER_URL = os.getenv("XORPAY_CASHIER_URL", "https://xorpay.com/api/cashier")
|
||||
|
||||
# ZPAY 配置(网关: https://zpayz.cn/)
|
||||
ZPAY_PID = os.getenv("ZPAY_PID", "2025121809351743")
|
||||
ZPAY_KEY = os.getenv("ZPAY_KEY", "tpEi7wWIWI2kXiYVTpIG6j7it0mjVW89")
|
||||
|
||||
# PocketBase(支持 PB_ADMIN_* 或 POCKETBASE_*)
|
||||
# 为了避免线上环境遗留 PB_URL=公网域名导致慢查询,默认强制优先本机。
|
||||
# 本地无 PocketBase、需连线上 PB 时:在 .env.local 设置 PB_INTERNAL_URL=https://pocketbase.hackrobot.cn
|
||||
# (该变量优先级最高,且不改变下方对 PB_URL 的解析规则)
|
||||
_pb_internal = os.getenv("PB_INTERNAL_URL") or os.getenv("POCKETBASE_INTERNAL_URL")
|
||||
_pb_explicit = os.getenv("PB_URL") or os.getenv("POCKETBASE_URL")
|
||||
if _pb_internal:
|
||||
PB_URL = _pb_internal
|
||||
elif _pb_explicit and (
|
||||
"127.0.0.1" in _pb_explicit
|
||||
or "localhost" in _pb_explicit
|
||||
or _pb_explicit.startswith("http://10.")
|
||||
or _pb_explicit.startswith("https://10.")
|
||||
or _pb_explicit.startswith("http://172.")
|
||||
or _pb_explicit.startswith("https://172.")
|
||||
or _pb_explicit.startswith("http://192.168.")
|
||||
or _pb_explicit.startswith("https://192.168.")
|
||||
):
|
||||
PB_URL = _pb_explicit
|
||||
else:
|
||||
PB_URL = "http://127.0.0.1:8090"
|
||||
PB_ADMIN_EMAIL = (
|
||||
os.getenv("PB_ADMIN_EMAIL") or os.getenv("POCKETBASE_EMAIL") or "xiaoshuang.eric@gmail.com"
|
||||
)
|
||||
PB_ADMIN_PASSWORD = (
|
||||
os.getenv("PB_ADMIN_PASSWORD") or os.getenv("POCKETBASE_PASSWORD") or "Xiao4669805@"
|
||||
)
|
||||
|
||||
# Memos
|
||||
MEMOS_API = os.getenv("MEMOS_API", "https://qun.hackrobot.cn/api/v1/users")
|
||||
MEMOS_TOKEN = os.getenv(
|
||||
"MEMOS_TOKEN",
|
||||
"eyJhbGciOiJIUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJuYW1lIjoiaGFja3JvYm90IiwiaXNzIjoibWVtb3MiLCJzdWIiOiIxIiwiYXVkIjpbInVzZXIuYWNjZXNzLXRva2VuIl0sImlhdCI6MTc1NzQwNTE0OX0.Idn7kBlxE-CoSOXwWuZ1tHGIRKHAIeDyXSafGS5OHsg",
|
||||
)
|
||||
|
||||
# 回调地址(用于生成 notify_url)。线上 api.nomadyt.com 时需设置 BASE_URL=https://api.nomadyt.com
|
||||
BASE_URL = os.getenv("BASE_URL", "https://api.hackrobot.cn")
|
||||
|
||||
# MinIO 对象存储
|
||||
MINIO_ENDPOINT = os.getenv("MINIO_ENDPOINT", "minioweb.hackrobot.cn")
|
||||
MINIO_PORT = int(os.getenv("MINIO_PORT", "443"))
|
||||
MINIO_ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY", "")
|
||||
MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "")
|
||||
MINIO_BUCKET = os.getenv("MINIO_BUCKET", "hackrobot")
|
||||
MINIO_USE_SSL = os.getenv("MINIO_USE_SSL", "true").lower() == "true"
|
||||
_minio_ep = os.getenv("MINIO_ENDPOINT", "minioweb.hackrobot.cn")
|
||||
_minio_bucket = os.getenv("MINIO_BUCKET", "hackrobot")
|
||||
MINIO_PUBLIC_URL = os.getenv("MINIO_PUBLIC_URL", f"https://{_minio_ep}/{_minio_bucket}")
|
||||
MINIO_UPLOAD_PREFIX = os.getenv("MINIO_UPLOAD_PREFIX", "joins")
|
||||
198
apps/hackrobot/payjsapi/app/main.py
Normal file
198
apps/hackrobot/payjsapi/app/main.py
Normal file
@ -0,0 +1,198 @@
|
||||
"""
|
||||
支付 API 主入口
|
||||
支持多支付渠道(XorPay、ZPAY),通过 PAYMENT_PROVIDER 环境变量切换
|
||||
|
||||
路由模块化,按项目拆分:
|
||||
- nomadvip: /nomadvip/* - 固定 ZPAY
|
||||
- digital: /digital/* - 使用 PAYMENT_PROVIDER
|
||||
- cnomadcna: /cnomadcna/* - 使用 PAYMENT_PROVIDER
|
||||
- nomadlms: /nomadlms/* - 使用 PAYMENT_PROVIDER
|
||||
- legacy: /payh5、/zpay_notify 等(向后兼容)
|
||||
- common: /submit_meetup_application、/create_user
|
||||
|
||||
本地调试:BASE_URL 需为 ZPAY 可访问地址(如 ngrok)
|
||||
线上部署:BASE_URL=https://api.hackrobot.cn(必须为 ZPAY 可公网访问的地址)
|
||||
"""
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import requests
|
||||
from fastapi import FastAPI, Request, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from .config import BASE_URL
|
||||
from .routers import (
|
||||
nomadvip_router,
|
||||
digital_router,
|
||||
cnomadcna_router,
|
||||
nomadlms_router,
|
||||
legacy_router,
|
||||
common_router,
|
||||
meetup_router,
|
||||
chatbot_router,
|
||||
hackrobot_router,
|
||||
)
|
||||
from .routers.salon_meetup import router as salon_meetup_router
|
||||
|
||||
app = FastAPI(
|
||||
title="PayJS API",
|
||||
description="nomadvip / digital / cnomadcna / nomadlms 支付接口",
|
||||
version="2.0",
|
||||
)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=False,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
class _SafeStderrProxy:
|
||||
def write(self, message: str) -> int:
|
||||
try:
|
||||
return sys.stderr.write(message)
|
||||
except (OSError, ValueError):
|
||||
return 0
|
||||
|
||||
def flush(self) -> None:
|
||||
try:
|
||||
sys.stderr.flush()
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
|
||||
|
||||
def _safe_log(level: int, message: str, *args, **kwargs) -> None:
|
||||
try:
|
||||
logging.log(level, message, *args, **kwargs)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def global_exception_handler(request: Request, exc: Exception):
|
||||
"""捕获未处理异常,记录日志后返回 500(HTTPException 交由 FastAPI 默认处理)"""
|
||||
if isinstance(exc, HTTPException):
|
||||
raise exc
|
||||
_safe_log(logging.ERROR, "未处理异常: %s - %s", request.url.path, exc, exc_info=True)
|
||||
return JSONResponse(status_code=500, content={"detail": str(exc)})
|
||||
|
||||
logging.raiseExceptions = False
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
||||
handlers=[logging.StreamHandler(_SafeStderrProxy())],
|
||||
force=True,
|
||||
)
|
||||
|
||||
|
||||
def _safe_stderr_write(message: str) -> None:
|
||||
try:
|
||||
sys.stderr.write(message)
|
||||
sys.stderr.flush()
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
|
||||
|
||||
def _should_trace_request(path: str) -> bool:
|
||||
return any(
|
||||
segment in path for segment in ("/nomadvip", "/digital", "/cnomadcna", "/nomadlms")
|
||||
)
|
||||
|
||||
# 启动时校验 BASE_URL(线上部署必须为公网可访问地址,否则 ZPAY 无法回调)
|
||||
def _check_base_url():
|
||||
url = (BASE_URL or "").strip().lower()
|
||||
if not url:
|
||||
logging.warning("BASE_URL 未设置,将使用默认值。线上部署请务必设置 BASE_URL 为公网地址")
|
||||
return
|
||||
if "localhost" in url or "127.0.0.1" in url or url.startswith("http://192.168.") or url.startswith("http://10."):
|
||||
logging.warning(
|
||||
"BASE_URL=%s 疑似内网地址,ZPAY 无法回调!线上部署请设置 BASE_URL 为公网地址,如 https://api.hackrobot.cn",
|
||||
BASE_URL,
|
||||
)
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
_check_base_url()
|
||||
# 预热 PocketBase 连接,减少首个业务请求冷启动延迟
|
||||
try:
|
||||
from .services.pb import get_pb_client
|
||||
|
||||
get_pb_client()
|
||||
logging.info("PocketBase client warmup done")
|
||||
except Exception as error:
|
||||
logging.warning("PocketBase client warmup failed: %s", error)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health(check_zpay: str = ""):
|
||||
"""健康检查。check_zpay=1 时额外检测 zpayz.cn 连通性(含 SSL、mapi 接口)"""
|
||||
out = {"status": "ok", "service": "payjsapi", "base_url": BASE_URL}
|
||||
if str(check_zpay).lower() in ("1", "true", "yes"):
|
||||
# 1) 检测 zpayz.cn 首页
|
||||
try:
|
||||
r = requests.get("https://zpayz.cn", timeout=5)
|
||||
out["zpayz_reachable"] = r.status_code in (200, 301, 302, 404)
|
||||
except requests.exceptions.SSLError as e:
|
||||
out["zpayz_reachable"] = False
|
||||
out["zpayz_error"] = f"SSL 错误: {e}(Ubuntu 请执行: apt install ca-certificates)"
|
||||
except requests.RequestException as e:
|
||||
out["zpayz_reachable"] = False
|
||||
out["zpayz_error"] = str(e)
|
||||
# 2) 检测 mapi 接口(实际支付调用地址)
|
||||
if out.get("zpayz_reachable"):
|
||||
try:
|
||||
r2 = requests.post("https://zpayz.cn/mapi.php", data={"pid": "test"}, timeout=5)
|
||||
out["zpayz_mapi_reachable"] = True
|
||||
out["zpayz_mapi_status"] = r2.status_code
|
||||
except requests.exceptions.SSLError as e:
|
||||
out["zpayz_mapi_reachable"] = False
|
||||
out["zpayz_mapi_error"] = f"SSL: {e}"
|
||||
except requests.RequestException as e:
|
||||
out["zpayz_mapi_reachable"] = False
|
||||
out["zpayz_mapi_error"] = str(e)
|
||||
return out
|
||||
|
||||
|
||||
# 请求日志中间件:每个请求都打印到 stderr,确保终端可见
|
||||
@app.middleware("http")
|
||||
async def log_requests(request, call_next):
|
||||
path = request.url.path
|
||||
if _should_trace_request(path):
|
||||
_safe_stderr_write(f"[payjsapi] >>> {request.method} {path}\n")
|
||||
resp = await call_next(request)
|
||||
if _should_trace_request(path):
|
||||
_safe_stderr_write(f"[payjsapi] <<< {request.method} {path} {resp.status_code}\n")
|
||||
return resp
|
||||
|
||||
# 挂载路由
|
||||
app.include_router(nomadvip_router)
|
||||
app.include_router(digital_router)
|
||||
app.include_router(cnomadcna_router)
|
||||
app.include_router(nomadlms_router)
|
||||
app.include_router(legacy_router)
|
||||
app.include_router(common_router)
|
||||
app.include_router(meetup_router)
|
||||
app.include_router(salon_meetup_router)
|
||||
app.include_router(chatbot_router)
|
||||
app.include_router(hackrobot_router)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
"""健康检查"""
|
||||
return {
|
||||
"status": "ok",
|
||||
"service": "payjsapi",
|
||||
"routes": {
|
||||
"nomadvip": "/nomadvip/payh5, /nomadvip/payh5/redirect, /nomadvip/check_vip, /nomadvip/check_vip_by_email, /nomadvip/order_status, /nomadvip/zpay_order_status, /nomadvip/zpay_notify, /nomadvip/xorpay_notify",
|
||||
"digital": "/digital/payh5, /digital/payh5/redirect, /digital/zpay_order_status, /digital/zpay_notify, /digital/xorpay_notify",
|
||||
"cnomadcna": "/cnomadcna/payh5, ...",
|
||||
"nomadlms": "/nomadlms/payh5, ...",
|
||||
"legacy": "/payh5, /payh5/redirect, /zpay_notify, /xorpay_notify, /zpay_order_status",
|
||||
"common": "/submit_meetup_application, /create_user",
|
||||
},
|
||||
}
|
||||
25
apps/hackrobot/payjsapi/app/payment/__init__.py
Normal file
25
apps/hackrobot/payjsapi/app/payment/__init__.py
Normal file
@ -0,0 +1,25 @@
|
||||
"""
|
||||
支付模块
|
||||
支持多支付渠道,可通过配置切换
|
||||
"""
|
||||
from .base import PaymentProvider
|
||||
from .xorpay import XorPayProvider
|
||||
from .zpay import ZPayProvider
|
||||
from .factory import (
|
||||
get_payment_provider,
|
||||
get_payment_provider_by_name,
|
||||
reset_provider,
|
||||
resolve_provider,
|
||||
resolve_channel,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"PaymentProvider",
|
||||
"XorPayProvider",
|
||||
"ZPayProvider",
|
||||
"get_payment_provider",
|
||||
"get_payment_provider_by_name",
|
||||
"reset_provider",
|
||||
"resolve_provider",
|
||||
"resolve_channel",
|
||||
]
|
||||
59
apps/hackrobot/payjsapi/app/payment/base.py
Normal file
59
apps/hackrobot/payjsapi/app/payment/base.py
Normal file
@ -0,0 +1,59 @@
|
||||
"""
|
||||
支付渠道抽象基类
|
||||
定义统一的支付接口,便于切换不同支付渠道(XorPay、ZPAY 等)
|
||||
"""
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
|
||||
class PaymentProvider(ABC):
|
||||
"""支付渠道抽象基类"""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def name(self) -> str:
|
||||
"""支付渠道名称"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def create_order(
|
||||
self,
|
||||
*,
|
||||
order_id: str,
|
||||
name: str,
|
||||
total_fee_yuan: str,
|
||||
pay_type: str,
|
||||
notify_url: str,
|
||||
return_url: Optional[str] = None,
|
||||
client_ip: Optional[str] = None,
|
||||
extra: Optional[Dict[str, Any]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
创建支付订单
|
||||
:return: 包含支付跳转信息等,格式由各渠道自定义
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def verify_notify(self, data: Dict[str, Any]) -> bool:
|
||||
"""验证异步通知签名"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def parse_notify(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
解析异步通知,提取统一字段
|
||||
:return: {
|
||||
"order_id": str, # 商户订单号
|
||||
"trade_no": str, # 渠道订单号
|
||||
"pay_price": str, # 实付金额(元)
|
||||
"pay_time": str, # 支付时间
|
||||
"trade_status": str, # 支付状态
|
||||
}
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def success_response(self) -> str:
|
||||
"""异步通知成功时需返回的字符串(如 'ok' 或 'success')"""
|
||||
pass
|
||||
79
apps/hackrobot/payjsapi/app/payment/factory.py
Normal file
79
apps/hackrobot/payjsapi/app/payment/factory.py
Normal file
@ -0,0 +1,79 @@
|
||||
"""
|
||||
支付渠道工厂
|
||||
根据配置返回对应的支付 Provider
|
||||
默认使用 zpay 替代 xorpay
|
||||
"""
|
||||
from ..config import (
|
||||
PAYMENT_PROVIDER,
|
||||
XORPAY_AID,
|
||||
XORPAY_SECRET,
|
||||
XORPAY_CASHIER_URL,
|
||||
ZPAY_PID,
|
||||
ZPAY_KEY,
|
||||
)
|
||||
from .base import PaymentProvider
|
||||
from .xorpay import XorPayProvider
|
||||
from .zpay import ZPayProvider
|
||||
|
||||
_provider_instance: PaymentProvider | None = None
|
||||
|
||||
|
||||
def get_payment_provider() -> PaymentProvider:
|
||||
"""获取当前配置的支付渠道实例(单例)"""
|
||||
global _provider_instance
|
||||
if _provider_instance is not None:
|
||||
return _provider_instance
|
||||
if PAYMENT_PROVIDER == "zpay":
|
||||
_provider_instance = ZPayProvider(pid=ZPAY_PID, key=ZPAY_KEY)
|
||||
else:
|
||||
_provider_instance = XorPayProvider(
|
||||
aid=XORPAY_AID,
|
||||
secret=XORPAY_SECRET,
|
||||
cashier_url=XORPAY_CASHIER_URL,
|
||||
)
|
||||
return _provider_instance
|
||||
|
||||
|
||||
def get_payment_provider_by_name(name: str) -> PaymentProvider:
|
||||
"""按名称获取 Provider(用于 per-request 覆盖)"""
|
||||
if name == "xorpay":
|
||||
return XorPayProvider(
|
||||
aid=XORPAY_AID,
|
||||
secret=XORPAY_SECRET,
|
||||
cashier_url=XORPAY_CASHIER_URL,
|
||||
)
|
||||
return ZPayProvider(pid=ZPAY_PID, key=ZPAY_KEY)
|
||||
|
||||
|
||||
def reset_provider():
|
||||
"""重置 Provider(用于测试或切换渠道)"""
|
||||
global _provider_instance
|
||||
_provider_instance = None
|
||||
|
||||
|
||||
def resolve_provider(
|
||||
device: str | None = None,
|
||||
req_provider: str | None = None,
|
||||
user_agent: str | None = None,
|
||||
) -> PaymentProvider:
|
||||
"""
|
||||
根据设备解析支付渠道:
|
||||
- 微信内(device=wechat 或 UA 含 MicroMessenger) → xorpay
|
||||
- PC/手机浏览器 → zpay
|
||||
显式传入的 provider 优先。
|
||||
"""
|
||||
req = (req_provider or "").strip().lower()
|
||||
if req in ("xorpay", "zpay"):
|
||||
return get_payment_provider_by_name(req)
|
||||
dev = (device or "pc").lower()
|
||||
if dev == "wechat":
|
||||
return get_payment_provider_by_name("xorpay")
|
||||
ua = (user_agent or "").lower()
|
||||
if "micromessenger" in ua:
|
||||
return get_payment_provider_by_name("xorpay")
|
||||
return get_payment_provider_by_name("zpay")
|
||||
|
||||
|
||||
def resolve_channel(item_channel: str | None) -> str:
|
||||
"""支付方式固定为微信支付,支付宝暂不提供"""
|
||||
return "wxpay"
|
||||
108
apps/hackrobot/payjsapi/app/payment/xorpay.py
Normal file
108
apps/hackrobot/payjsapi/app/payment/xorpay.py
Normal file
@ -0,0 +1,108 @@
|
||||
"""
|
||||
XorPay 支付渠道实现
|
||||
文档: https://xorpay.com
|
||||
"""
|
||||
import hashlib
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
import requests
|
||||
|
||||
from .base import PaymentProvider
|
||||
|
||||
|
||||
class XorPayProvider(PaymentProvider):
|
||||
"""XorPay 支付"""
|
||||
|
||||
def __init__(self, aid: str, secret: str, cashier_url: str = "https://xorpay.com/api/cashier"):
|
||||
self.aid = aid
|
||||
self.secret = secret
|
||||
self.cashier_url = cashier_url.rstrip("/")
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "xorpay"
|
||||
|
||||
def _sign(self, name: str, pay_type: str, price: str, order_id: str, notify_url: str) -> str:
|
||||
raw = name + pay_type + price + order_id + notify_url + self.secret
|
||||
return hashlib.md5(raw.encode("utf-8")).hexdigest().lower()
|
||||
|
||||
def _query2_sign(self, order_id: str) -> str:
|
||||
raw = order_id + self.secret
|
||||
return hashlib.md5(raw.encode("utf-8")).hexdigest().lower()
|
||||
|
||||
def create_order(
|
||||
self,
|
||||
*,
|
||||
order_id: str,
|
||||
name: str,
|
||||
total_fee_yuan: str,
|
||||
pay_type: str,
|
||||
notify_url: str,
|
||||
return_url: Optional[str] = None,
|
||||
client_ip: Optional[str] = None,
|
||||
extra: Optional[Dict[str, Any]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
# XorPay 使用 jsapi 表示 H5/JSAPI 支付
|
||||
channel_pay_type = "jsapi"
|
||||
params = {
|
||||
"name": name,
|
||||
"pay_type": channel_pay_type,
|
||||
"price": total_fee_yuan,
|
||||
"order_id": order_id,
|
||||
"notify_url": notify_url,
|
||||
}
|
||||
params["sign"] = self._sign(
|
||||
params["name"],
|
||||
params["pay_type"],
|
||||
params["price"],
|
||||
params["order_id"],
|
||||
params["notify_url"],
|
||||
)
|
||||
return {
|
||||
"status": "ok",
|
||||
"provider": self.name,
|
||||
"params": params,
|
||||
"pay_url": f"{self.cashier_url}/{self.aid}",
|
||||
}
|
||||
|
||||
def verify_notify(self, data: Dict[str, Any]) -> bool:
|
||||
received_sign = data.get("sign", "")
|
||||
aoid = data.get("aoid", "")
|
||||
order_id = data.get("order_id", "")
|
||||
pay_price = data.get("pay_price", "")
|
||||
pay_time = data.get("pay_time", "")
|
||||
raw = aoid + order_id + pay_price + pay_time + self.secret
|
||||
calculated = hashlib.md5(raw.encode("utf-8")).hexdigest().lower()
|
||||
return calculated == received_sign
|
||||
|
||||
def parse_notify(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
return {
|
||||
"order_id": data.get("order_id", ""),
|
||||
"trade_no": data.get("aoid", ""),
|
||||
"pay_price": data.get("pay_price", ""),
|
||||
"pay_time": data.get("pay_time", ""),
|
||||
"trade_status": "success", # XorPay 验签通过即视为成功
|
||||
}
|
||||
|
||||
def success_response(self) -> str:
|
||||
return "ok"
|
||||
|
||||
def query_order_status(self, order_id: str, timeout: int = 10) -> Dict[str, Any]:
|
||||
url = f"https://xorpay.com/api/query2/{self.aid}"
|
||||
params = {
|
||||
"order_id": order_id,
|
||||
"sign": self._query2_sign(order_id),
|
||||
}
|
||||
try:
|
||||
response = requests.get(url, params=params, timeout=timeout)
|
||||
response.raise_for_status()
|
||||
data = response.json() or {}
|
||||
status = str(data.get("status", "")).strip().lower()
|
||||
return {
|
||||
"paid": status in ("payed", "success"),
|
||||
"status": status,
|
||||
"pay_price": data.get("pay_price") or data.get("price") or "",
|
||||
"raw": data,
|
||||
}
|
||||
except Exception as error:
|
||||
return {"paid": False, "error": str(error)}
|
||||
250
apps/hackrobot/payjsapi/app/payment/zpay.py
Normal file
250
apps/hackrobot/payjsapi/app/payment/zpay.py
Normal file
@ -0,0 +1,250 @@
|
||||
"""
|
||||
ZPAY 支付渠道实现
|
||||
文档: https://z-pay.cn/doc.html
|
||||
支持:页面跳转支付、API 接口支付、异步通知验签
|
||||
"""
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
import requests
|
||||
|
||||
from .base import PaymentProvider
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _is_internal_ip(ip: Optional[str]) -> bool:
|
||||
"""判断是否为内网 IP,ZPAY 可能拒收导致 Ubuntu 部署失败"""
|
||||
if not ip or not ip.strip():
|
||||
return True
|
||||
ip = ip.strip()
|
||||
if ip in ("127.0.0.1", "localhost", "::1"):
|
||||
return True
|
||||
if ip.startswith("10.") or ip.startswith("192.168.") or ip.startswith("172.16."):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class ZPayProvider(PaymentProvider):
|
||||
"""ZPAY 支付(易支付兼容接口)"""
|
||||
|
||||
# API 地址
|
||||
SUBMIT_URL = "https://zpayz.cn/submit.php" # 页面跳转
|
||||
MAPI_URL = "https://zpayz.cn/mapi.php" # API 接口
|
||||
|
||||
def __init__(self, pid: str, key: str):
|
||||
self.pid = pid
|
||||
self.key = key
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "zpay"
|
||||
|
||||
def _md5_sign(self, params: Dict[str, Any], exclude: Optional[set] = None) -> str:
|
||||
"""
|
||||
ZPAY MD5 签名算法:
|
||||
1. 参数按 ASCII 排序,sign/sign_type/空值不参与
|
||||
2. 拼接为 a=b&c=d
|
||||
3. sign = md5(拼接串 + KEY),小写
|
||||
"""
|
||||
exclude = exclude or {"sign", "sign_type"}
|
||||
filtered = {k: v for k, v in params.items() if k not in exclude and v is not None and v != ""}
|
||||
sorted_keys = sorted(filtered.keys())
|
||||
parts = [f"{k}={filtered[k]}" for k in sorted_keys]
|
||||
raw = "&".join(parts) + self.key
|
||||
return hashlib.md5(raw.encode("utf-8")).hexdigest().lower()
|
||||
|
||||
def create_order(
|
||||
self,
|
||||
*,
|
||||
order_id: str,
|
||||
name: str,
|
||||
total_fee_yuan: str,
|
||||
pay_type: str,
|
||||
notify_url: str,
|
||||
return_url: Optional[str] = None,
|
||||
client_ip: Optional[str] = None,
|
||||
extra: Optional[Dict[str, Any]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
# ZPAY: alipay / wxpay(pay_type 可为业务类型或支付方式,此处按支付方式解析)
|
||||
channel_type = "wxpay" if str(pay_type).lower() in ("wx", "wechat", "wxpay") else "alipay"
|
||||
params = {
|
||||
"pid": self.pid,
|
||||
"type": channel_type,
|
||||
"out_trade_no": order_id,
|
||||
"notify_url": notify_url,
|
||||
"name": name,
|
||||
"money": total_fee_yuan,
|
||||
"return_url": return_url or notify_url,
|
||||
}
|
||||
if client_ip and not _is_internal_ip(client_ip):
|
||||
params["clientip"] = client_ip
|
||||
elif client_ip and _is_internal_ip(client_ip):
|
||||
logger.warning("ZPAY create_order 跳过内网 client_ip=%s", client_ip)
|
||||
if extra and extra.get("param"):
|
||||
params["param"] = str(extra["param"])
|
||||
params["sign_type"] = "MD5"
|
||||
params["sign"] = self._md5_sign(params)
|
||||
|
||||
# 页面跳转方式:返回 form 提交 URL 和参数
|
||||
return {
|
||||
"status": "ok",
|
||||
"provider": self.name,
|
||||
"params": params,
|
||||
"pay_url": self.SUBMIT_URL,
|
||||
"submit_method": "POST",
|
||||
}
|
||||
|
||||
def create_order_api(
|
||||
self,
|
||||
*,
|
||||
order_id: str,
|
||||
name: str,
|
||||
total_fee_yuan: str,
|
||||
pay_type: str,
|
||||
notify_url: str,
|
||||
return_url: Optional[str] = None,
|
||||
client_ip: str = "127.0.0.1",
|
||||
device: str = "pc",
|
||||
extra: Optional[Dict[str, Any]] = None,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
ZPAY API 接口支付(返回 payurl/qrcode 等)
|
||||
适用于需要二维码或 H5 跳转的场景
|
||||
"""
|
||||
channel_type = "wxpay" if pay_type.lower() in ("wx", "wechat", "wxpay") else "alipay"
|
||||
effective_ip = (client_ip or "").strip() or "127.0.0.1"
|
||||
# 内网 IP(127.0.0.1/10.x/192.168.x)可能导致 ZPAY 风控拒单,Ubuntu 部署时常见
|
||||
if _is_internal_ip(effective_ip):
|
||||
logger.warning(
|
||||
"ZPAY create_order_api client_ip=%s 为内网地址,已省略 clientip 参数(避免风控拒单)",
|
||||
effective_ip,
|
||||
)
|
||||
effective_ip = "" # 不传 clientip,由 ZPAY 从连接获取
|
||||
params = {
|
||||
"pid": self.pid,
|
||||
"type": channel_type,
|
||||
"out_trade_no": order_id,
|
||||
"notify_url": notify_url,
|
||||
"name": name,
|
||||
"money": total_fee_yuan,
|
||||
"device": device,
|
||||
}
|
||||
if effective_ip:
|
||||
params["clientip"] = effective_ip
|
||||
if return_url:
|
||||
params["return_url"] = return_url
|
||||
if extra and extra.get("param"):
|
||||
params["param"] = str(extra["param"])
|
||||
params["sign_type"] = "MD5"
|
||||
params["sign"] = self._md5_sign(params)
|
||||
|
||||
try:
|
||||
resp = requests.post(self.MAPI_URL, data=params, timeout=15)
|
||||
except requests.exceptions.SSLError as e:
|
||||
logger.error("ZPAY SSL 错误(Ubuntu 可能 CA 证书不全): %s", e, exc_info=True)
|
||||
return {
|
||||
"status": "error",
|
||||
"provider": self.name,
|
||||
"msg": f"连接 zpayz.cn SSL 失败: {e}。Ubuntu 请安装 ca-certificates: apt install ca-certificates",
|
||||
}
|
||||
except requests.RequestException as e:
|
||||
logger.error("ZPAY 连接失败: %s", e, exc_info=True)
|
||||
return {
|
||||
"status": "error",
|
||||
"provider": self.name,
|
||||
"msg": f"连接 zpayz.cn 失败: {e}。请检查服务器网络、防火墙或 DNS。",
|
||||
}
|
||||
|
||||
try:
|
||||
result = resp.json()
|
||||
except Exception:
|
||||
result = {}
|
||||
# ZPAY 可能返回二次编码的 JSON(body 为 JSON 字符串),需再次解析
|
||||
if isinstance(result, str):
|
||||
try:
|
||||
result = json.loads(result)
|
||||
except Exception:
|
||||
result = {}
|
||||
if not isinstance(result, dict):
|
||||
result = {}
|
||||
# 部分环境下 ZPAY 将成功数据放在 msg 中(msg 为 JSON 字符串)
|
||||
msg_val = result.get("msg")
|
||||
if isinstance(msg_val, str) and msg_val.strip().startswith("{"):
|
||||
try:
|
||||
inner = json.loads(msg_val)
|
||||
if isinstance(inner, dict) and int(float(inner.get("code", 0))) == 1:
|
||||
result = inner
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
code = int(float(result.get("code", 0)))
|
||||
except (TypeError, ValueError):
|
||||
code = 0
|
||||
if code == 1:
|
||||
return {
|
||||
"status": "ok",
|
||||
"provider": self.name,
|
||||
"payurl": result.get("payurl"),
|
||||
"payurl2": result.get("payurl2"), # 微信 H5,手机浏览器打开可唤醒微信
|
||||
"qrcode": result.get("qrcode"),
|
||||
"img": result.get("img"),
|
||||
"O_id": result.get("O_id"),
|
||||
"trade_no": result.get("trade_no"),
|
||||
}
|
||||
err_msg = result.get("msg", resp.text)
|
||||
logger.error(
|
||||
"ZPAY create_order_api 失败 status=%s code=%s msg=%s resp_text=%s",
|
||||
resp.status_code,
|
||||
code,
|
||||
err_msg,
|
||||
resp.text[:500] if resp.text else "",
|
||||
)
|
||||
return {
|
||||
"status": "error",
|
||||
"provider": self.name,
|
||||
"msg": err_msg,
|
||||
}
|
||||
|
||||
def verify_notify(self, data: Dict[str, Any]) -> bool:
|
||||
received = data.get("sign", "")
|
||||
calculated = self._md5_sign(data)
|
||||
return calculated == received
|
||||
|
||||
def parse_notify(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
# ZPAY 通知:trade_status 为 TRADE_SUCCESS 表示成功
|
||||
status = "success" if data.get("trade_status") == "TRADE_SUCCESS" else "pending"
|
||||
return {
|
||||
"order_id": data.get("out_trade_no", ""),
|
||||
"trade_no": data.get("trade_no", ""),
|
||||
"pay_price": data.get("money", ""),
|
||||
"pay_time": "", # ZPAY 通知未明确返回
|
||||
"trade_status": status,
|
||||
}
|
||||
|
||||
def success_response(self) -> str:
|
||||
return "success"
|
||||
|
||||
def query_order_status(self, out_trade_no: str, timeout: int = 15) -> Dict[str, Any]:
|
||||
"""查询 ZPAY 订单支付状态(供 nomadvip/digital/cnomadcna/nomadlms 共用)"""
|
||||
url = f"https://zpayz.cn/api.php?act=order&pid={self.pid}&key={self.key}&out_trade_no={out_trade_no}"
|
||||
try:
|
||||
resp = requests.get(url, timeout=timeout)
|
||||
result = resp.json() if resp.ok else {}
|
||||
except Exception as e:
|
||||
return {"paid": False, "error": str(e)}
|
||||
try:
|
||||
code = result.get("code", 0)
|
||||
code = int(code) if isinstance(code, (int, float)) or str(code).isdigit() else 0
|
||||
except (ValueError, TypeError):
|
||||
code = 0
|
||||
if code != 1:
|
||||
return {"paid": False, "msg": result.get("msg", "查询失败")}
|
||||
try:
|
||||
status = int(result.get("status", 0))
|
||||
except (ValueError, TypeError):
|
||||
status = 0
|
||||
money = result.get("money") or result.get("pay_price") or ""
|
||||
return {"paid": status == 1, "status": status, "pay_price": str(money) if money else ""}
|
||||
28
apps/hackrobot/payjsapi/app/routers/__init__.py
Normal file
28
apps/hackrobot/payjsapi/app/routers/__init__.py
Normal file
@ -0,0 +1,28 @@
|
||||
"""
|
||||
项目路由模块
|
||||
- nomadvip: /nomadvip/*
|
||||
- digital: /digital/*
|
||||
- cnomadcna: /cnomadcna/*
|
||||
- nomadlms: /nomadlms/*
|
||||
- legacy: /payh5, /zpay_notify 等(向后兼容 digital/cnomadcna)
|
||||
"""
|
||||
from .nomadvip import router as nomadvip_router
|
||||
from .digital import router as digital_router
|
||||
from .cnomadcna import router as cnomadcna_router
|
||||
from .nomadlms import router as nomadlms_router
|
||||
from .legacy import router as legacy_router
|
||||
from .common import router as common_router
|
||||
from .meetup import router as meetup_router
|
||||
from .chatbot import hackrobot_router, router as chatbot_router
|
||||
|
||||
__all__ = [
|
||||
"nomadvip_router",
|
||||
"digital_router",
|
||||
"cnomadcna_router",
|
||||
"nomadlms_router",
|
||||
"legacy_router",
|
||||
"common_router",
|
||||
"meetup_router",
|
||||
"chatbot_router",
|
||||
"hackrobot_router",
|
||||
]
|
||||
317
apps/hackrobot/payjsapi/app/routers/_digital_base.py
Normal file
317
apps/hackrobot/payjsapi/app/routers/_digital_base.py
Normal file
@ -0,0 +1,317 @@
|
||||
"""
|
||||
digital/cnomadcna/nomadlms 共享的支付逻辑
|
||||
根据 prefix 生成对应 notify_path,业务互不影响
|
||||
"""
|
||||
import asyncio
|
||||
import logging
|
||||
import random
|
||||
import string
|
||||
import time
|
||||
|
||||
from fastapi import APIRouter, Request, HTTPException
|
||||
|
||||
from ..config import BASE_URL
|
||||
from ..payment import (
|
||||
get_payment_provider,
|
||||
get_payment_provider_by_name,
|
||||
resolve_provider,
|
||||
resolve_channel,
|
||||
)
|
||||
from ..payment.zpay import ZPayProvider
|
||||
from ..services import handle_payment_success, processed_orders
|
||||
|
||||
|
||||
def _safe_log(level: int, message: str, *args, **kwargs) -> None:
|
||||
try:
|
||||
logging.log(level, message, *args, **kwargs)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def create_digital_router(prefix: str, project_name: str, site_id: str | None = None) -> APIRouter:
|
||||
"""
|
||||
创建 digital/cnomadcna/nomadlms 的支付路由
|
||||
prefix: 如 "/digital", "/cnomadcna", "/nomadlms" 或 ""(legacy)
|
||||
project_name: 用于日志,如 "digital", "cnomadcna", "nomadlms", "legacy"
|
||||
site_id: 站点标识,用于 site_vip(digital/meetup/vip)
|
||||
"""
|
||||
router = APIRouter(prefix=prefix, tags=[project_name])
|
||||
|
||||
def _notify_path(path: str) -> str:
|
||||
base = BASE_URL.rstrip("/")
|
||||
if prefix:
|
||||
return f"{base}{prefix}{path}"
|
||||
return f"{base}{path}"
|
||||
|
||||
@router.post("/payh5")
|
||||
async def payh5(item: dict, request: Request):
|
||||
"""创建 H5 支付订单。默认微信支付,PC/手机用 zpay,微信内用 xorpay,支付宝暂不提供"""
|
||||
req_provider = (item.get("provider") or "").strip().lower() or None
|
||||
device = (item.get("device") or "pc").lower()
|
||||
ua = (request.headers.get("user-agent") or "").strip()
|
||||
provider = resolve_provider(device, req_provider, ua)
|
||||
channel = resolve_channel(item.get("channel"))
|
||||
user_id = item.get("user_id", "unknown")
|
||||
pay_type = (item.get("type") or "unknown").lower()
|
||||
_safe_log(
|
||||
logging.INFO,
|
||||
"[payjsapi] %s/payh5 user_id=%s type=%s provider=%s channel=%s",
|
||||
project_name,
|
||||
user_id,
|
||||
pay_type,
|
||||
provider.name,
|
||||
channel,
|
||||
)
|
||||
try:
|
||||
total_fee = int(item.get("total_fee", 80))
|
||||
except (TypeError, ValueError):
|
||||
total_fee = 80
|
||||
total_fee_yuan = f"{total_fee / 100:.2f}"
|
||||
random_suffix = "".join(random.choices(string.hexdigits.lower(), k=8))
|
||||
timestamp = int(time.time())
|
||||
order_id = f"{user_id}_{pay_type}_order_{timestamp}_{random_suffix}"
|
||||
return_url = item.get("return_url")
|
||||
if return_url and "order_id=" not in return_url and "out_trade_no=" not in return_url:
|
||||
sep = "&" if "?" in return_url else "?"
|
||||
return_url = f"{return_url}{sep}order_id={order_id}"
|
||||
type_map = {
|
||||
"book": "购买书籍",
|
||||
"meetup": "数字游民社区报名",
|
||||
"salon": "沙龙报名茶歇费",
|
||||
"video": "视频解锁",
|
||||
"vip": "VIP会员充值",
|
||||
}
|
||||
name = type_map.get(pay_type, "商品支付")
|
||||
notify_path = "/xorpay_notify" if provider.name == "xorpay" else "/zpay_notify"
|
||||
notify_url = _notify_path(notify_path)
|
||||
result = provider.create_order(
|
||||
order_id=order_id,
|
||||
name=name,
|
||||
total_fee_yuan=total_fee_yuan,
|
||||
pay_type=channel,
|
||||
notify_url=notify_url,
|
||||
return_url=return_url,
|
||||
client_ip=item.get("client_ip"),
|
||||
)
|
||||
_safe_log(
|
||||
logging.INFO,
|
||||
"[payjsapi] %s/payh5 created order_id=%s provider=%s notify_url=%s return_url=%s",
|
||||
project_name,
|
||||
order_id,
|
||||
provider.name,
|
||||
notify_url,
|
||||
return_url or "",
|
||||
)
|
||||
if provider.name == "xorpay":
|
||||
return {
|
||||
"status": "ok",
|
||||
"order_id": order_id,
|
||||
"xorpay_params": result.get("params", {}),
|
||||
"xorpay_url": result.get("pay_url", ""),
|
||||
"provider": provider.name,
|
||||
}
|
||||
params = result.get("params", {})
|
||||
params_str = {k: str(v) for k, v in params.items() if v is not None and str(v).strip() != ""}
|
||||
return {
|
||||
"status": "ok",
|
||||
"params": params_str,
|
||||
"pay_url": result.get("pay_url", ""),
|
||||
"provider": provider.name,
|
||||
"submit_method": "POST",
|
||||
}
|
||||
|
||||
@router.post("/payh5/redirect")
|
||||
async def payh5_redirect(item: dict, request: Request):
|
||||
"""ZPAY mapi 接口,返回 302 或二维码。PC/手机用 zpay,默认微信支付,支付宝暂不提供"""
|
||||
from fastapi.responses import RedirectResponse, JSONResponse
|
||||
|
||||
provider = get_payment_provider_by_name("zpay")
|
||||
channel = resolve_channel(item.get("channel"))
|
||||
_safe_log(
|
||||
logging.INFO,
|
||||
"[payjsapi] %s/payh5/redirect user_id=%s type=%s device=%s channel=%s",
|
||||
project_name,
|
||||
item.get("user_id", ""),
|
||||
item.get("type", ""),
|
||||
item.get("device", "pc"),
|
||||
channel,
|
||||
)
|
||||
try:
|
||||
total_fee = float(item.get("total_fee", 80))
|
||||
except (TypeError, ValueError):
|
||||
total_fee = 80.0
|
||||
total_fee_yuan = f"{total_fee / 100:.2f}"
|
||||
user_id = item.get("user_id", "unknown")
|
||||
pay_type = item.get("type", "unknown").lower()
|
||||
return_url = item.get("return_url")
|
||||
device = item.get("device", "pc")
|
||||
random_suffix = "".join(random.choices(string.hexdigits.lower(), k=8))
|
||||
timestamp = int(time.time())
|
||||
order_id = f"{user_id}_{pay_type}_order_{timestamp}_{random_suffix}"
|
||||
if return_url and "order_id=" not in return_url and "out_trade_no=" not in return_url:
|
||||
sep = "&" if "?" in return_url else "?"
|
||||
return_url = f"{return_url}{sep}order_id={order_id}"
|
||||
client_ip = item.get("client_ip") or (request.client.host if request.client else "127.0.0.1")
|
||||
forwarded = request.headers.get("x-forwarded-for")
|
||||
if forwarded:
|
||||
client_ip = forwarded.split(",")[0].strip()
|
||||
type_map = {
|
||||
"book": "购买书籍",
|
||||
"meetup": "数字游民社区报名",
|
||||
"salon": "沙龙报名茶歇费",
|
||||
"video": "视频解锁",
|
||||
"vip": "VIP会员充值",
|
||||
}
|
||||
name = type_map.get(pay_type, "商品支付")
|
||||
notify_url = _notify_path("/zpay_notify")
|
||||
result = provider.create_order_api(
|
||||
order_id=order_id,
|
||||
name=name,
|
||||
total_fee_yuan=total_fee_yuan,
|
||||
pay_type=channel,
|
||||
notify_url=notify_url,
|
||||
return_url=return_url,
|
||||
client_ip=client_ip,
|
||||
device=device,
|
||||
)
|
||||
if result.get("status") != "ok":
|
||||
raise HTTPException(status_code=500, detail=result.get("msg", "ZPAY mapi 创建订单失败"))
|
||||
_safe_log(
|
||||
logging.INFO,
|
||||
"[payjsapi] %s/payh5/redirect created order_id=%s device=%s channel=%s",
|
||||
project_name,
|
||||
order_id,
|
||||
device,
|
||||
channel,
|
||||
)
|
||||
payurl = result.get("payurl")
|
||||
payurl2 = result.get("payurl2")
|
||||
img = result.get("img")
|
||||
if device == "pc" and img:
|
||||
return JSONResponse(status_code=200, content={
|
||||
"status": "ok", "qrcode_mode": True, "order_id": order_id,
|
||||
"img": img, "payurl": payurl, "return_url": return_url, "channel": channel,
|
||||
})
|
||||
if device == "wechat" and channel.lower() in ("wxpay", "wx", "wechat"):
|
||||
return JSONResponse(status_code=200, content={"use_form": True, "order_id": order_id})
|
||||
headers = {"X-Pay-Order-Id": order_id}
|
||||
if device == "h5" and channel.lower() in ("wxpay", "wx", "wechat") and payurl2:
|
||||
return RedirectResponse(url=payurl2, status_code=302, headers=headers)
|
||||
if payurl:
|
||||
return RedirectResponse(url=payurl, status_code=302, headers=headers)
|
||||
raise HTTPException(status_code=500, detail="ZPAY 未返回支付链接")
|
||||
|
||||
@router.get("/zpay_order_status")
|
||||
async def zpay_order_status(out_trade_no: str):
|
||||
"""查询 ZPAY 订单支付状态"""
|
||||
provider = get_payment_provider()
|
||||
if not isinstance(provider, ZPayProvider):
|
||||
raise HTTPException(status_code=400, detail="仅 zpay 渠道支持")
|
||||
result = provider.query_order_status(out_trade_no, timeout=15)
|
||||
if result.get("error"):
|
||||
_safe_log(logging.WARNING, "%s ZPAY 订单查询失败: %s", project_name, result.get("error"))
|
||||
return result
|
||||
|
||||
@router.get("/order_status")
|
||||
async def order_status(order_id: str = "", out_trade_no: str = ""):
|
||||
order_id = (order_id or out_trade_no or "").strip()
|
||||
if not order_id:
|
||||
return {"paid": False, "error": "missing order_id"}
|
||||
|
||||
def _query_pb() -> dict | None:
|
||||
try:
|
||||
from ..services.pb import get_pb_client
|
||||
pb = get_pb_client()
|
||||
records = pb.collection("payments").get_list(1, 1, {"filter": f'order_id = "{order_id}"'})
|
||||
if records.items:
|
||||
return {"paid": True, "source": "pocketbase"}
|
||||
except Exception as error:
|
||||
_safe_log(logging.WARNING, "%s order_status PocketBase failed: %s", project_name, error)
|
||||
return None
|
||||
|
||||
def _query_xorpay() -> dict | None:
|
||||
try:
|
||||
xorpay = get_payment_provider_by_name("xorpay")
|
||||
r = xorpay.query_order_status(order_id, timeout=6)
|
||||
if r.get("paid"):
|
||||
return {"paid": True, "source": "xorpay", "status": r.get("status", "")}
|
||||
except Exception as error:
|
||||
_safe_log(logging.WARNING, "%s order_status XorPay failed: %s", project_name, error)
|
||||
return None
|
||||
|
||||
def _query_zpay() -> dict | None:
|
||||
try:
|
||||
zpay = get_payment_provider_by_name("zpay")
|
||||
if isinstance(zpay, ZPayProvider):
|
||||
r = zpay.query_order_status(order_id, timeout=6)
|
||||
if r.get("paid"):
|
||||
return {"paid": True, "source": "zpay", "status": r.get("status", "")}
|
||||
except Exception as error:
|
||||
_safe_log(logging.WARNING, "%s order_status ZPAY failed: %s", project_name, error)
|
||||
return None
|
||||
|
||||
# 并行查询 PB、ZPAY、XorPay,任一返回 paid 即立即返回(PC 扫码用 ZPAY,微信用 XorPay)
|
||||
pb_task = asyncio.to_thread(_query_pb)
|
||||
xorpay_task = asyncio.to_thread(_query_xorpay)
|
||||
zpay_task = asyncio.to_thread(_query_zpay)
|
||||
results = await asyncio.gather(pb_task, xorpay_task, zpay_task)
|
||||
for r in results:
|
||||
if r:
|
||||
return r
|
||||
return {"paid": False}
|
||||
|
||||
@router.post("/xorpay_notify")
|
||||
async def xorpay_notify(request: Request):
|
||||
"""XorPay 异步通知"""
|
||||
form_data = await request.form()
|
||||
data = {k: v for k, v in form_data.items()}
|
||||
_safe_log(logging.INFO, "%s XorPay 异步通知: %s", project_name, data)
|
||||
order_id = data.get("order_id", "")
|
||||
if order_id in processed_orders:
|
||||
return "ok"
|
||||
provider = get_payment_provider_by_name("xorpay")
|
||||
if not provider.verify_notify(data):
|
||||
_safe_log(logging.ERROR, "%s XorPay 验签失败: %s", project_name, data)
|
||||
raise HTTPException(status_code=400, detail="sign error")
|
||||
parsed = provider.parse_notify(data)
|
||||
if parsed.get("trade_status") != "success":
|
||||
return "ok"
|
||||
handle_payment_success(
|
||||
order_id,
|
||||
parsed.get("pay_price", data.get("pay_price", "")),
|
||||
f"{project_name}-xorpay",
|
||||
use_memos=False,
|
||||
site_id=site_id,
|
||||
)
|
||||
return provider.success_response()
|
||||
|
||||
@router.get("/zpay_notify")
|
||||
@router.post("/zpay_notify")
|
||||
async def zpay_notify(request: Request):
|
||||
"""ZPAY 异步通知"""
|
||||
if request.method == "GET":
|
||||
data = dict(request.query_params)
|
||||
else:
|
||||
form_data = await request.form()
|
||||
data = {k: v for k, v in form_data.items()}
|
||||
_safe_log(logging.INFO, "%s ZPAY 异步通知: %s", project_name, data)
|
||||
order_id = data.get("out_trade_no", "")
|
||||
if order_id in processed_orders:
|
||||
return "success"
|
||||
provider = get_payment_provider_by_name("zpay")
|
||||
if not provider.verify_notify(data):
|
||||
_safe_log(logging.ERROR, "%s ZPAY 验签失败: %s", project_name, data)
|
||||
raise HTTPException(status_code=400, detail="sign error")
|
||||
parsed = provider.parse_notify(data)
|
||||
if parsed.get("trade_status") != "success":
|
||||
return "success"
|
||||
handle_payment_success(
|
||||
order_id,
|
||||
parsed.get("pay_price", data.get("money", "")),
|
||||
f"{project_name}-zpay",
|
||||
use_memos=False,
|
||||
site_id=site_id,
|
||||
)
|
||||
return provider.success_response()
|
||||
|
||||
return router
|
||||
978
apps/hackrobot/payjsapi/app/routers/chatbot.py
Normal file
978
apps/hackrobot/payjsapi/app/routers/chatbot.py
Normal file
@ -0,0 +1,978 @@
|
||||
import asyncio
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import struct
|
||||
import xml.etree.ElementTree as ET
|
||||
from datetime import datetime, timezone
|
||||
from typing import NamedTuple, Optional
|
||||
from urllib.parse import parse_qs, quote
|
||||
|
||||
import httpx
|
||||
from Crypto.Cipher import AES
|
||||
from fastapi import APIRouter, BackgroundTasks, HTTPException, Request
|
||||
from fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse
|
||||
|
||||
from ..services.chatbot_miniprogram_card import build_miniprogram_card_msg, normalize_miniprogrampage_msg
|
||||
from ..services.mp_recommend import is_mp_recommend_webhook_body, try_persist_from_like_body
|
||||
from ..services.wechat_private_user_pb import (
|
||||
contact_avatar_url,
|
||||
upsert_contacts_batch_to_pb,
|
||||
query_private_messages,
|
||||
safe_sync_wechat_private_user_to_pb,
|
||||
)
|
||||
|
||||
router = APIRouter(tags=["chatbot"])
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
OPEN_API_PROFILES = {
|
||||
"eric": {
|
||||
"name": "Eric在旅行",
|
||||
"APP_ID": "fXYWvGfq04uMScP",
|
||||
"TOKEN": "EWRotyMHfTLAlYmCHpdH8AZcuZPBHn",
|
||||
"ENCODING_AES_KEY": "1wJZiJm8URfRjkHu1QfIVDeNFc2jbzzeCZqvhQecsCx",
|
||||
},
|
||||
"xidu": {
|
||||
"name": "异度世界",
|
||||
"APP_ID": "LTKfSFeK0h8TRLM",
|
||||
"TOKEN": "64DbvAd5m7YOJ1XslyMgJ9V42jDIUH",
|
||||
"ENCODING_AES_KEY": "KCZGtPDjqFZnBuApvG3xqhvHUMnqFHbC5cnD23HKACX",
|
||||
},
|
||||
}
|
||||
ACTIVE_PROFILE = (os.getenv("CHATBOT_ACTIVE_PROFILE") or "eric").strip().lower()
|
||||
if ACTIVE_PROFILE not in OPEN_API_PROFILES:
|
||||
ACTIVE_PROFILE = "eric"
|
||||
APP_ID = OPEN_API_PROFILES[ACTIVE_PROFILE]["APP_ID"]
|
||||
TOKEN = OPEN_API_PROFILES[ACTIVE_PROFILE]["TOKEN"]
|
||||
ENCODING_AES_KEY = OPEN_API_PROFILES[ACTIVE_PROFILE]["ENCODING_AES_KEY"]
|
||||
|
||||
HACKROBOT_PROFILE = "xidu"
|
||||
|
||||
|
||||
def _resolve_profile_key(profile_key: str) -> str:
|
||||
k = (profile_key or "").strip().lower()
|
||||
return k if k in OPEN_API_PROFILES else "eric"
|
||||
|
||||
|
||||
def _profile_creds(profile_key: str) -> dict:
|
||||
return OPEN_API_PROFILES[_resolve_profile_key(profile_key)]
|
||||
|
||||
def pkcs5_unpadding(data: bytes) -> bytes:
|
||||
pad = data[-1]
|
||||
if pad < 1 or pad > 32:
|
||||
return data
|
||||
return data[:-pad]
|
||||
|
||||
|
||||
def pkcs5_padding(data: bytes) -> bytes:
|
||||
amount_to_pad = 32 - (len(data) % 32)
|
||||
return data + bytes([amount_to_pad]) * amount_to_pad
|
||||
|
||||
|
||||
def get_aes_key_and_iv(encoding_aes_key: str) -> tuple[bytes, bytes]:
|
||||
aes_key = base64.b64decode(encoding_aes_key + "=")
|
||||
if len(aes_key) != 32:
|
||||
raise ValueError("encodingAESKey invalid")
|
||||
return aes_key, aes_key[:16]
|
||||
|
||||
|
||||
def aes_decrypt_base64(cipher_b64: str, encoding_aes_key: str) -> str:
|
||||
aes_key, iv = get_aes_key_and_iv(encoding_aes_key)
|
||||
compact = "".join(str(cipher_b64 or "").split())
|
||||
compact += "=" * (-len(compact) % 4)
|
||||
cipher_bytes = base64.b64decode(compact)
|
||||
cipher = AES.new(aes_key, AES.MODE_CBC, iv)
|
||||
decrypted = pkcs5_unpadding(cipher.decrypt(cipher_bytes))
|
||||
try:
|
||||
if len(decrypted) < 20:
|
||||
raise ValueError("payload too short")
|
||||
content = decrypted[16:]
|
||||
msg_len = struct.unpack("!I", content[:4])[0]
|
||||
msg_start = 4
|
||||
msg_end = msg_start + msg_len
|
||||
if msg_end > len(content):
|
||||
raise ValueError("msg length out of range")
|
||||
msg = content[msg_start:msg_end]
|
||||
text = msg.decode("utf-8")
|
||||
if text:
|
||||
return text
|
||||
except Exception:
|
||||
pass
|
||||
return decrypted.decode("utf-8")
|
||||
|
||||
|
||||
def aes_encrypt_base64(plaintext: str, encoding_aes_key: str, app_id: str | None = None) -> str:
|
||||
aes_key, iv = get_aes_key_and_iv(encoding_aes_key)
|
||||
raw = plaintext.encode("utf-8")
|
||||
if app_id:
|
||||
data = os.urandom(16) + struct.pack("!I", len(raw)) + raw + app_id.encode("utf-8")
|
||||
else:
|
||||
data = raw
|
||||
padded = pkcs5_padding(data)
|
||||
cipher = AES.new(aes_key, AES.MODE_CBC, iv)
|
||||
return base64.b64encode(cipher.encrypt(padded)).decode("utf-8")
|
||||
|
||||
|
||||
class SharedChatbotReply(NamedTuple):
|
||||
"""全公众号配置共用的解析结果:H5 图文、小程序卡片,或兜底纯文本(含超链)。"""
|
||||
|
||||
h5_message: Optional[dict]
|
||||
"""与「发送客服消息」H5 一致:{\"news\": {\"articles\": [...]}};关键词为 Query 子串包含命中,未命中时为 None。"""
|
||||
text_fallback: str
|
||||
"""未命中关键词时的 short_answer 文案(可含 <a href>)。"""
|
||||
miniprogram_message: Optional[dict]
|
||||
"""与「发送客服消息」小程序卡片一致:{\"miniprogrampage\": {...}};未命中「小程序卡片」时为 None。"""
|
||||
|
||||
|
||||
def _normalize_h5_msg_per_wechat_doc(h5_message: dict) -> dict:
|
||||
"""
|
||||
与文档「发送客服消息」H5 一致:msg = { news: { articles: [ { title, description, url, picurl, type } ] } }。
|
||||
仅保留这五个字段,避免多余键导致客户端不渲染。
|
||||
"""
|
||||
raw = h5_message.get("news") if isinstance(h5_message, dict) else None
|
||||
arts = raw.get("articles") if isinstance(raw, dict) else None
|
||||
if not isinstance(arts, list):
|
||||
arts = []
|
||||
norm_articles: list[dict] = []
|
||||
for a in arts:
|
||||
if not isinstance(a, dict):
|
||||
continue
|
||||
t = str(a.get("type", "h5") or "h5").strip().lower()
|
||||
if t not in ("h5", "mp"):
|
||||
t = "h5"
|
||||
norm_articles.append(
|
||||
{
|
||||
"title": str(a.get("title", "")),
|
||||
"description": str(a.get("description", "")),
|
||||
"url": str(a.get("url", "")),
|
||||
"picurl": str(a.get("picurl", "")),
|
||||
"type": t,
|
||||
}
|
||||
)
|
||||
return {"news": {"articles": norm_articles}}
|
||||
|
||||
|
||||
def shared_build_h5_keyword_message(query: str, user_id: str, profile_key: str) -> Optional[dict]:
|
||||
"""关键词命中时返回文档约定的 H5 msg(再经 _normalize 输出)。
|
||||
|
||||
命中规则:对用户 Query 首尾 trim 后,**包含**配置词作为连续子串即命中(子串匹配),
|
||||
不要求用户整句与关键词全等。群卡片统一按「群」子串命中。
|
||||
「小程序卡片」在 shared_resolve_reply 中优先处理,不在此函数返回。
|
||||
"""
|
||||
q = (query or "").strip()
|
||||
uid = (user_id or "").strip()
|
||||
if "电子书" in q:
|
||||
return {
|
||||
"news": {
|
||||
"articles": [
|
||||
{
|
||||
"title": "数字游民",
|
||||
"description": "地理套利与自动化杠杆",
|
||||
"url": f"https://vip.hackrobot.cn/ebook?userid={uid}",
|
||||
"picurl": "https://www.hackrobot.cn/static/images/images/dgnomad.png",
|
||||
"type": "h5",
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
group_hit = "群" in q
|
||||
if group_hit:
|
||||
return {
|
||||
"news": {
|
||||
"articles": [
|
||||
{
|
||||
"title": "异度星球",
|
||||
"description": "电子书、视频教程、私密社群",
|
||||
"url": f"https://vip.hackrobot.cn?userid={uid}",
|
||||
"picurl": "https://www.hackrobot.cn/static/images/images/dgnomad.png",
|
||||
"type": "h5",
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
return None
|
||||
|
||||
|
||||
def build_fallback_reply_plain_text(user_id: str) -> str:
|
||||
"""
|
||||
兜底回复(单独构建):未命中「小程序卡片」「电子书」「群」等关键词时使用。
|
||||
/chatbot、/hackrobot 共用同一文案与链接逻辑,仅解密密钥因 profile 不同。
|
||||
"""
|
||||
uid = (user_id or "").strip()
|
||||
href = f"https://vip.hackrobot.cn/?userid={quote(uid, safe='')}" if uid else "https://vip.hackrobot.cn/"
|
||||
return "🤖消息已收到,稍晚看到后回复\n参与交流,👉可<a href=\"" + href + "\">加群</a>"
|
||||
|
||||
|
||||
def shared_resolve_reply(data: dict, profile_key: str) -> SharedChatbotReply:
|
||||
"""根据解密后的 data 与 profile 决定小程序卡片、H5 或兜底;关键词按 Query 子串包含判断,无命中时走 build_fallback_reply_plain_text。"""
|
||||
user_id = str(data.get("UserId", "") or data.get("userId", "") or "").strip()
|
||||
query = str(data.get("Query", "") or "").strip()
|
||||
if "小程序卡片" in query:
|
||||
mp = build_miniprogram_card_msg(user_id=user_id)
|
||||
return SharedChatbotReply(None, "", mp)
|
||||
h5 = shared_build_h5_keyword_message(query, user_id, profile_key)
|
||||
if h5:
|
||||
return SharedChatbotReply(h5, "", None)
|
||||
return SharedChatbotReply(None, build_fallback_reply_plain_text(user_id), None)
|
||||
|
||||
|
||||
def shared_build_openapi_text_answer(short_answer: str) -> dict:
|
||||
"""开放 API 文档「输出」:文本 / 含超链兜底。"""
|
||||
return {"answer_type": "text", "text_info": {"short_answer": short_answer}}
|
||||
|
||||
|
||||
def _xml_channel_h5_envelope(data: dict, h5_message: dict) -> dict:
|
||||
"""XML 入站(如 Eric /chatbot)命中关键词:userid 信封 + content.news,与线上一致。"""
|
||||
user_id = str(data.get("UserId", "") or data.get("userId", "") or "").strip()
|
||||
appid = str(data.get("ChannelId", "") or data.get("appid", "")).strip()
|
||||
channel = int(str(data.get("channel", "0") or "0"))
|
||||
return {"userid": user_id, "appid": appid, "content": h5_message, "channel": channel, "from": 1}
|
||||
|
||||
|
||||
def _openapi_json_h5_answer_per_sendmsg_doc(msg: dict) -> dict:
|
||||
"""
|
||||
开放 API JSON 入站命中 H5 / 小程序卡片:与文档「发送客服消息」一致。
|
||||
msg 为 { news: { articles: [...] } } 或 { miniprogrampage: {...} }。
|
||||
第三方回调侧将该 msg 序列化为 JSON 字符串,放入 answer_type=text 的 text_info.short_answer
|
||||
(与客服接口里 msg 对象同源结构,由网关解析为卡片)。
|
||||
"""
|
||||
compact = json.dumps(msg, ensure_ascii=False, separators=(",", ":"))
|
||||
return {"answer_type": "text", "text_info": {"short_answer": compact}}
|
||||
|
||||
|
||||
def _openapi_json_merge_request_echo(data: dict, body: dict) -> dict:
|
||||
"""回传 RequestId,便于开放平台把应答与请求关联(避免未命中/空白展示)。"""
|
||||
rid = data.get("RequestId")
|
||||
if rid is None:
|
||||
return body
|
||||
s = str(rid).strip()
|
||||
if not s:
|
||||
return body
|
||||
out = dict(body)
|
||||
out["RequestId"] = rid
|
||||
return out
|
||||
|
||||
|
||||
def shared_build_plain_body_for_wechat_callback(
|
||||
*,
|
||||
payload_format: str,
|
||||
reply: SharedChatbotReply,
|
||||
data: dict,
|
||||
) -> dict:
|
||||
"""
|
||||
回调加密前的明文 JSON 统一入口(关键词 H5 与兜底共用一套解析结果 reply)。
|
||||
|
||||
- /chatbot 多为 XML 入站:H5 用 userid 信封;兜底改用 answer_type=text(信封里 content.msg 对超链异常)。
|
||||
- /hackrobot 多为 JSON 入站:兜底用 answer_type=text;H5 用同结构的 msg JSON 串作为 short_answer(见发送客服消息文档)。
|
||||
"""
|
||||
fmt = (payload_format or "").strip().lower()
|
||||
is_xml = fmt == "xml"
|
||||
|
||||
if reply.miniprogram_message:
|
||||
msg = normalize_miniprogrampage_msg(reply.miniprogram_message)
|
||||
if is_xml:
|
||||
return _xml_channel_h5_envelope(data, msg)
|
||||
body = _openapi_json_h5_answer_per_sendmsg_doc(msg)
|
||||
return _openapi_json_merge_request_echo(data, body)
|
||||
|
||||
if reply.h5_message:
|
||||
msg = _normalize_h5_msg_per_wechat_doc(reply.h5_message)
|
||||
if is_xml:
|
||||
return _xml_channel_h5_envelope(data, msg)
|
||||
body = _openapi_json_h5_answer_per_sendmsg_doc(msg)
|
||||
return _openapi_json_merge_request_echo(data, body)
|
||||
|
||||
body = shared_build_openapi_text_answer(reply.text_fallback)
|
||||
if not is_xml:
|
||||
body = _openapi_json_merge_request_echo(data, body)
|
||||
return body
|
||||
|
||||
|
||||
def _ensure_user_id_from_session(data: dict) -> None:
|
||||
"""开放 API JSON 可能只带 SessionId(如 0_xxx|openid),补全 UserId 供关键词链接与兜底文案使用。"""
|
||||
uid = str(data.get("UserId", "") or data.get("userId", "") or "").strip()
|
||||
if uid:
|
||||
return
|
||||
sid = str(data.get("SessionId", "") or "").strip()
|
||||
if "|" in sid:
|
||||
tail = sid.rsplit("|", 1)[-1].strip()
|
||||
if tail:
|
||||
data["UserId"] = tail
|
||||
|
||||
|
||||
def _parse_decrypted_payload(decrypted_text: str) -> dict:
|
||||
text = (decrypted_text or "").strip()
|
||||
# 兼容 BOM / NUL / 控制字符前缀
|
||||
text = text.lstrip("\ufeff\x00\r\n\t ")
|
||||
if not text:
|
||||
raise ValueError("empty decrypted payload")
|
||||
|
||||
# 某些上游会把 JSON 再包一层字符串:"{\"UserId\":\"...\"}"
|
||||
if text.startswith('"') and text.endswith('"'):
|
||||
try:
|
||||
unwrapped = json.loads(text)
|
||||
if isinstance(unwrapped, str):
|
||||
text = unwrapped.strip().lstrip("\ufeff\x00\r\n\t ")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 若前面夹杂少量杂字符,尝试定位第一个结构起始位
|
||||
first_json = text.find("{")
|
||||
first_xml = text.find("<")
|
||||
starts = [x for x in [first_json, first_xml] if x >= 0]
|
||||
if starts:
|
||||
first = min(starts)
|
||||
if first > 0:
|
||||
text = text[first:]
|
||||
|
||||
if text.startswith("{"):
|
||||
obj = json.loads(text)
|
||||
obj["_payload_format"] = "json"
|
||||
return obj
|
||||
if text.startswith("<"):
|
||||
root = ET.fromstring(text)
|
||||
|
||||
def get_text(tag: str, default: str = "") -> str:
|
||||
node = root.find(tag)
|
||||
return (node.text or "").strip() if node is not None and node.text is not None else default
|
||||
|
||||
content_node = root.find("content")
|
||||
query = ""
|
||||
if content_node is not None:
|
||||
msg_node = content_node.find("msg")
|
||||
if msg_node is not None and msg_node.text:
|
||||
query = msg_node.text.strip()
|
||||
return {
|
||||
"UserId": get_text("userid", ""),
|
||||
"Query": query,
|
||||
"Timestamp": get_text("createtime", ""),
|
||||
"MsgId": get_text("msgid", ""),
|
||||
"ChannelId": get_text("appid", ""),
|
||||
"appid": get_text("appid", ""),
|
||||
"channel": get_text("channel", "0"),
|
||||
"from": get_text("from", "0"),
|
||||
"_payload_format": "xml",
|
||||
}
|
||||
# 兼容 querystring 明文:UserId=xxx&Query=你好&appid=...
|
||||
if "=" in text and "&" in text:
|
||||
qs = parse_qs(text, keep_blank_values=True)
|
||||
|
||||
def qv(*keys: str) -> str:
|
||||
for key in keys:
|
||||
vals = qs.get(key) or qs.get(key.lower()) or qs.get(key.upper()) or []
|
||||
if vals and str(vals[0]).strip():
|
||||
return str(vals[0]).strip()
|
||||
return ""
|
||||
|
||||
return {
|
||||
"UserId": qv("UserId", "userid", "userId", "from_user"),
|
||||
"Query": qv("Query", "query", "msg", "content"),
|
||||
"Timestamp": qv("Timestamp", "timestamp", "createTime", "msg_time"),
|
||||
"MsgId": qv("MsgId", "msgId", "msgid"),
|
||||
"ChannelId": qv("ChannelId", "appid", "appId"),
|
||||
"appid": qv("appid", "appId"),
|
||||
"channel": qv("channel"),
|
||||
"from": qv("from"),
|
||||
"_payload_format": "querystring",
|
||||
}
|
||||
raise ValueError("unsupported payload format")
|
||||
|
||||
|
||||
async def _handle_wechat_request(
|
||||
request: Request,
|
||||
app_id: Optional[str],
|
||||
background_tasks: BackgroundTasks,
|
||||
*,
|
||||
profile_key: str,
|
||||
) -> str:
|
||||
creds = _profile_creds(profile_key)
|
||||
expected_app_id = creds["APP_ID"]
|
||||
encoding_key = creds["ENCODING_AES_KEY"]
|
||||
if app_id and app_id != expected_app_id:
|
||||
logger.warning(
|
||||
"chatbot app_id mismatch profile=%s expected=%s actual=%s",
|
||||
profile_key,
|
||||
expected_app_id,
|
||||
app_id,
|
||||
)
|
||||
raw_body = (await request.body()).decode("utf-8", errors="ignore").strip()
|
||||
if not raw_body:
|
||||
raise HTTPException(status_code=400, detail="empty body")
|
||||
|
||||
cipher_b64 = raw_body
|
||||
# 兼容三种请求体:
|
||||
# 1) 纯密文 base64
|
||||
# 2) JSON: {"encrypted":"..."} / {"encrypt":"..."} / {"Encrypt":"..."}
|
||||
# 3) x-www-form-urlencoded: encrypted=... / encrypt=...
|
||||
try:
|
||||
outer = json.loads(raw_body)
|
||||
if isinstance(outer, dict):
|
||||
for key in ("encrypted", "encrypt", "Encrypt"):
|
||||
val = outer.get(key)
|
||||
if isinstance(val, str) and val.strip():
|
||||
cipher_b64 = val
|
||||
break
|
||||
except Exception:
|
||||
form = parse_qs(raw_body, keep_blank_values=True)
|
||||
for key in ("encrypted", "encrypt", "Encrypt"):
|
||||
vals = form.get(key) or []
|
||||
if vals and str(vals[0]).strip():
|
||||
cipher_b64 = str(vals[0])
|
||||
break
|
||||
|
||||
cipher_b64 = "".join(str(cipher_b64).split()).strip()
|
||||
try:
|
||||
decrypted = aes_decrypt_base64(cipher_b64, encoding_key)
|
||||
logger.info(
|
||||
"chatbot decrypted preview profile=%s app_id=%s preview=%s",
|
||||
profile_key,
|
||||
app_id or "",
|
||||
(decrypted or "")[:200],
|
||||
)
|
||||
data = _parse_decrypted_payload(decrypted)
|
||||
_ensure_user_id_from_session(data)
|
||||
logger.info(
|
||||
"chatbot payload parsed profile=%s app_id=%s format=%s user=%s query=%s",
|
||||
profile_key,
|
||||
app_id or "",
|
||||
data.get("_payload_format", ""),
|
||||
str(data.get("UserId", "") or data.get("userId", ""))[:80],
|
||||
str(data.get("Query", ""))[:80],
|
||||
)
|
||||
except Exception as error:
|
||||
logger.error(
|
||||
"chatbot decrypt failed profile=%s app_id=%s content_type=%s body_prefix=%s err=%s",
|
||||
profile_key,
|
||||
app_id or "",
|
||||
request.headers.get("content-type", ""),
|
||||
raw_body[:160],
|
||||
error,
|
||||
)
|
||||
raise HTTPException(status_code=400, detail="decrypt error")
|
||||
|
||||
reply = shared_resolve_reply(data, profile_key)
|
||||
background_tasks.add_task(safe_sync_wechat_private_user_to_pb, data, profile_key)
|
||||
|
||||
fmt = str(data.get("_payload_format", "") or "")
|
||||
plain_obj = shared_build_plain_body_for_wechat_callback(payload_format=fmt, reply=reply, data=data)
|
||||
resp_plain = json.dumps(plain_obj, ensure_ascii=False)
|
||||
logger.info(
|
||||
"chatbot reply_plain profile=%s fmt=%s shape=%s",
|
||||
profile_key,
|
||||
fmt,
|
||||
list(plain_obj.keys()),
|
||||
)
|
||||
|
||||
if fmt == "xml":
|
||||
response_app_id = str(data.get("ChannelId", "") or data.get("appid", "")).strip() or expected_app_id
|
||||
else:
|
||||
# 与原始 chatbot 服务保持一致:thirdapi(URL 带 app_id)回包使用纯 AES,不拼 app_id
|
||||
response_app_id = None if app_id else expected_app_id
|
||||
|
||||
return aes_encrypt_base64(resp_plain, encoding_key, response_app_id)
|
||||
|
||||
|
||||
@router.post("/chatbot", response_class=PlainTextResponse)
|
||||
@router.post("/chatbot/", response_class=PlainTextResponse)
|
||||
async def chatbot_root(request: Request, background_tasks: BackgroundTasks, app_id: Optional[str] = None):
|
||||
return await _handle_wechat_request(request, app_id, background_tasks, profile_key=ACTIVE_PROFILE)
|
||||
|
||||
|
||||
@router.post("/chatbot/wechat/thirdapi", response_class=PlainTextResponse)
|
||||
@router.post("/chatbot/wechat/thirdapi/", response_class=PlainTextResponse)
|
||||
async def chatbot_thirdapi(request: Request, background_tasks: BackgroundTasks, app_id: Optional[str] = None):
|
||||
return await _handle_wechat_request(request, app_id, background_tasks, profile_key=ACTIVE_PROFILE)
|
||||
|
||||
|
||||
@router.get("/chatbot/health")
|
||||
@router.get("/chatbot/health/")
|
||||
def chatbot_health():
|
||||
return {"status": "ok", "module": "chatbot-clone", "profile": ACTIVE_PROFILE}
|
||||
|
||||
|
||||
hackrobot_router = APIRouter(tags=["hackrobot"])
|
||||
|
||||
|
||||
@hackrobot_router.post("/hackrobot", response_class=PlainTextResponse)
|
||||
@hackrobot_router.post("/hackrobot/", response_class=PlainTextResponse)
|
||||
async def hackrobot_root(request: Request, background_tasks: BackgroundTasks, app_id: Optional[str] = None):
|
||||
return await _handle_wechat_request(request, app_id, background_tasks, profile_key=HACKROBOT_PROFILE)
|
||||
|
||||
|
||||
@hackrobot_router.post("/hackrobot/wechat/thirdapi", response_class=PlainTextResponse)
|
||||
@hackrobot_router.post("/hackrobot/wechat/thirdapi/", response_class=PlainTextResponse)
|
||||
async def hackrobot_thirdapi(request: Request, background_tasks: BackgroundTasks, app_id: Optional[str] = None):
|
||||
return await _handle_wechat_request(request, app_id, background_tasks, profile_key=HACKROBOT_PROFILE)
|
||||
|
||||
|
||||
@hackrobot_router.get("/hackrobot/health")
|
||||
@hackrobot_router.get("/hackrobot/health/")
|
||||
def hackrobot_health():
|
||||
return {"status": "ok", "module": "chatbot-clone", "profile": HACKROBOT_PROFILE}
|
||||
|
||||
|
||||
def _log_like_incoming(request: Request, body: bytes) -> None:
|
||||
"""SmsForwarder Webhook 调试:打印完整入参(公众号助手等通知转发)。"""
|
||||
client_host = request.client.host if request.client else None
|
||||
client_port = request.client.port if request.client else None
|
||||
query = {k: v for k, v in request.query_params.multi_items()}
|
||||
headers = {k: v for k, v in request.headers.items()}
|
||||
ct = headers.get("content-type", "") or headers.get("Content-Type", "")
|
||||
|
||||
logger.info(
|
||||
"[like] ----- incoming POST ----- path=%s full_url=%s client=%s:%s query=%r",
|
||||
request.url.path,
|
||||
str(request.url),
|
||||
client_host,
|
||||
client_port,
|
||||
query,
|
||||
)
|
||||
logger.info("[like] content_type=%r content_length=%s body_bytes=%s", ct, headers.get("content-length"), len(body))
|
||||
logger.info("[like] headers=%s", headers)
|
||||
|
||||
if not body:
|
||||
logger.info("[like] body=(empty)")
|
||||
return
|
||||
|
||||
text = None
|
||||
try:
|
||||
text = body.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
hex_preview = body[:400].hex()
|
||||
suffix = "..." if len(body) > 400 else ""
|
||||
logger.info("[like] body=(non-utf8, hex prefix) %s%s", hex_preview, suffix)
|
||||
return
|
||||
|
||||
logger.info("[like] body_text=\n%s", text)
|
||||
low = (ct or "").lower()
|
||||
if "json" in low:
|
||||
try:
|
||||
parsed = json.loads(text)
|
||||
logger.info("[like] body_json=%s", json.dumps(parsed, ensure_ascii=False, indent=2))
|
||||
except json.JSONDecodeError as e:
|
||||
logger.info("[like] body_json_parse_failed: %s", e)
|
||||
|
||||
|
||||
def _client_ip_for_like(request: Request) -> str:
|
||||
xff = request.headers.get("x-forwarded-for") or request.headers.get("X-Forwarded-For")
|
||||
if xff:
|
||||
return xff.split(",")[0].strip()
|
||||
if request.client:
|
||||
return request.client.host or ""
|
||||
return ""
|
||||
|
||||
|
||||
@router.post("/chatbot/like")
|
||||
@hackrobot_router.post("/hackrobot/like")
|
||||
async def like_webhook_sink(request: Request):
|
||||
"""接收 SmsForwarder 等 Webhook POST,详细打日志;公众号助手通知写入 PocketBase。"""
|
||||
body = await request.body()
|
||||
_log_like_incoming(request, body)
|
||||
ct = request.headers.get("content-type", "") or ""
|
||||
payload: dict = {"ok": True, "received_bytes": len(body)}
|
||||
try:
|
||||
text = body.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
return JSONResponse(status_code=200, content=payload)
|
||||
|
||||
if is_mp_recommend_webhook_body(text, ct):
|
||||
try:
|
||||
summary = await asyncio.to_thread(try_persist_from_like_body, text, ct, _client_ip_for_like(request))
|
||||
if summary:
|
||||
payload["mp_recommend"] = summary
|
||||
except Exception:
|
||||
logger.exception("[like] PocketBase 写入 mp_recommend 失败")
|
||||
payload["mp_recommend_error"] = "persist_failed"
|
||||
|
||||
return JSONResponse(status_code=200, content=payload)
|
||||
|
||||
|
||||
@router.post("/chatbot/api/private-message/query")
|
||||
@hackrobot_router.post("/hackrobot/api/private-message/query")
|
||||
async def chatbot_private_message_query(payload: dict):
|
||||
authtoken = str(payload.get("authtoken", "")).strip()
|
||||
wxbot_bid = str(payload.get("wxbot_bid", "")).strip()
|
||||
if not authtoken or not wxbot_bid:
|
||||
raise HTTPException(status_code=400, detail="authtoken 和 wxbot_bid 不能为空")
|
||||
result = await query_private_messages(
|
||||
authtoken=authtoken,
|
||||
wxbot_bid=wxbot_bid,
|
||||
page=int(payload.get("page", 0)),
|
||||
size=int(payload.get("size", 30)),
|
||||
filter_value=int(payload.get("filter", 0)),
|
||||
request_id=str(payload.get("requestid", "local-debug")).strip() or "local-debug",
|
||||
)
|
||||
contacts = result.get("contacts") or []
|
||||
users = [
|
||||
{
|
||||
"nick": c.get("nick"),
|
||||
"userId": c.get("userId") or c.get("userid"),
|
||||
"avatar": contact_avatar_url(c) or None,
|
||||
"lastMsg": (c.get("lastMsg") or {}).get("content"),
|
||||
"lastActiveTime": c.get("lastActiveTime"),
|
||||
}
|
||||
for c in contacts
|
||||
]
|
||||
persist = payload.get("persist", True)
|
||||
if isinstance(persist, str):
|
||||
persist = persist.strip().lower() not in ("0", "false", "no", "off")
|
||||
saved_info = {"saved": 0, "skipped": 0, "errors": 0, "persist": bool(persist)}
|
||||
if persist:
|
||||
try:
|
||||
profile_key = str(payload.get("profile") or "").strip().lower()
|
||||
saved_info = await upsert_contacts_batch_to_pb(
|
||||
contacts,
|
||||
source=str(payload.get("persist_source") or "pm_query_sync"),
|
||||
profile_key=profile_key,
|
||||
)
|
||||
saved_info["persist"] = True
|
||||
except Exception:
|
||||
logger.exception("private-message persist failed")
|
||||
saved_info = {
|
||||
"saved": 0,
|
||||
"skipped": 0,
|
||||
"errors": 1,
|
||||
"persist": True,
|
||||
"error": "persist_failed",
|
||||
}
|
||||
return {
|
||||
"ok": True,
|
||||
"count": result.get("count"),
|
||||
"users": users,
|
||||
"saved": saved_info,
|
||||
"raw": result,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/chatbot/api/private-message/analyze")
|
||||
@hackrobot_router.post("/hackrobot/api/private-message/analyze")
|
||||
async def chatbot_private_message_analyze(payload: dict):
|
||||
authtoken = str(payload.get("authtoken", "")).strip()
|
||||
if not authtoken:
|
||||
raise HTTPException(status_code=400, detail="authtoken 不能为空")
|
||||
try:
|
||||
parts = authtoken.split(".")
|
||||
payload_part = parts[1] if len(parts) == 3 else ""
|
||||
payload_part += "=" * (-len(payload_part) % 4)
|
||||
claims = json.loads(base64.urlsafe_b64decode(payload_part.encode("utf-8")).decode("utf-8")) if payload_part else {}
|
||||
except Exception:
|
||||
claims = {}
|
||||
now_ts = int(datetime.now(timezone.utc).timestamp())
|
||||
exp = int(claims.get("exp", 0)) if str(claims.get("exp", "")).isdigit() else 0
|
||||
expires_in_seconds = exp - now_ts if exp else None
|
||||
status = "无法解析过期时间"
|
||||
if isinstance(expires_in_seconds, int):
|
||||
if expires_in_seconds <= 0:
|
||||
status = "已过期"
|
||||
elif expires_in_seconds < 3600:
|
||||
status = "即将过期(1小时内)"
|
||||
else:
|
||||
status = "有效"
|
||||
return {
|
||||
"status": status,
|
||||
"now_utc": datetime.now(timezone.utc).isoformat(),
|
||||
"claims": claims,
|
||||
"analysis": {
|
||||
"expires_in_seconds": expires_in_seconds,
|
||||
"expires_in_hours": round(expires_in_seconds / 3600, 2) if isinstance(expires_in_seconds, int) else None,
|
||||
"summary": "该分析基于 JWT payload 解码,不包含签名有效性校验。",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _private_message_debug_html(api_prefix: str) -> str:
|
||||
base = """
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>Chatbot 私信调试台</title>
|
||||
<style>
|
||||
body { font-family: system-ui, -apple-system, "Microsoft YaHei", sans-serif; margin: 0; padding: 20px; background: #0f172a; color: #e2e8f0; }
|
||||
.wrap { max-width: 980px; margin: 0 auto; display: grid; gap: 12px; }
|
||||
.card { background: #111827; border: 1px solid #334155; border-radius: 10px; padding: 14px; }
|
||||
h2 { margin: 0 0 8px; }
|
||||
label { display:block; margin: 8px 0 6px; font-size: 13px; color: #cbd5e1; }
|
||||
input, textarea { width: 100%; border-radius: 8px; border: 1px solid #475569; background: #0b1220; color: #e2e8f0; padding: 10px; box-sizing: border-box; }
|
||||
textarea { min-height: 88px; }
|
||||
.row { display:grid; grid-template-columns: 1fr 1fr; gap: 10px; }
|
||||
.btns { display:flex; gap:10px; margin-top: 10px; }
|
||||
button { border: 0; background: #2563eb; color: white; border-radius: 8px; padding: 10px 14px; cursor: pointer; }
|
||||
button.alt { background: #4f46e5; }
|
||||
#status { font-size: 13px; color: #93c5fd; }
|
||||
#token-countdown-wrap { margin-top: 12px; display: none; }
|
||||
#token-countdown-wrap.active { display: block; }
|
||||
.cd-row { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: #94a3b8; gap: 8px; }
|
||||
.cd-bar { height: 8px; background: #1e293b; border-radius: 4px; overflow: hidden; margin-top: 8px; }
|
||||
.cd-bar-fill { height: 100%; width: 100%; border-radius: 4px; transition: width 0.35s linear, background 0.25s; background: #22c55e; }
|
||||
#users { display:grid; grid-template-columns: repeat(auto-fill,minmax(250px,1fr)); gap: 8px; }
|
||||
.u { border:1px solid #334155; border-radius:8px; padding:8px; background:#0b1220; display:flex; gap:10px; align-items:flex-start; }
|
||||
.u .ava { width:40px; height:40px; border-radius:8px; object-fit:cover; flex-shrink:0; background:#1e293b; }
|
||||
.u .ava--ph { display:flex; align-items:center; justify-content:center; font-size:10px; color:#64748b; text-align:center; line-height:1.2; padding:2px; box-sizing:border-box; }
|
||||
.u .meta { min-width:0; flex:1; }
|
||||
.uid { color:#93c5fd; font-size:12px; word-break:break-all; }
|
||||
pre { background:#020617; border:1px solid #334155; border-radius:8px; padding:10px; overflow:auto; max-height:420px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrap">
|
||||
<section class="card">
|
||||
<h2>Chatbot 私信调试台</h2>
|
||||
<div id="status">准备就绪</div>
|
||||
<div id="token-countdown-wrap">
|
||||
<div class="cd-row">
|
||||
<span>Token 剩余</span>
|
||||
<span id="token-countdown-text">--</span>
|
||||
</div>
|
||||
<div class="cd-bar"><div id="token-countdown-bar" class="cd-bar-fill"></div></div>
|
||||
</div>
|
||||
<label>默认配置</label>
|
||||
<select id="preset" style="width:100%;border-radius:8px;border:1px solid #475569;background:#0b1220;color:#e2e8f0;padding:10px;box-sizing:border-box;">
|
||||
<option value="eric">Eric在旅行</option>
|
||||
<option value="xidu">异度世界</option>
|
||||
</select>
|
||||
<label>authtoken</label>
|
||||
<textarea id="authtoken" placeholder="粘贴抓包 authtoken"></textarea>
|
||||
<label>wxbot_bid</label>
|
||||
<input id="wxbot_bid" placeholder="例如: 75021053883a9090c67d5becde7f4b0a" />
|
||||
<div class="row">
|
||||
<div>
|
||||
<label>page</label>
|
||||
<input id="page" value="0" />
|
||||
</div>
|
||||
<div>
|
||||
<label>size</label>
|
||||
<input id="size" value="30" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="btns">
|
||||
<button onclick="queryPm()">查询私信</button>
|
||||
<button class="alt" onclick="analyzeToken()">分析 Token</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<h3 style="margin:0 0 8px;">用户列表</h3>
|
||||
<div id="users"></div>
|
||||
</section>
|
||||
|
||||
<section class="card">
|
||||
<h3 style="margin:0 0 8px;">原始结果</h3>
|
||||
<pre id="out">{}</pre>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const $ = (id) => document.getElementById(id);
|
||||
const presets = {
|
||||
eric: {
|
||||
authtoken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyaWQiOjE5MzE1MjcsIm9wZW5pZCI6Im85VS04NW1DSXZ0eDZ0a0pteTQyMkkzcUNHVWciLCJzaWduZXRpbWUiOjE3OTQzOTkzNTczMTYsImlhdCI6MTc5NDM5OTM1NywiZXhwIjoxNzk2OTkxMzU3fQ.sCFL7qaBVAoqsdWOrXlPyTnUT7PPMzHI4yAj8mQtV4Y",
|
||||
wxbot_bid: "75021053883a9090c67d5becde7f4b0a",
|
||||
},
|
||||
xidu: {
|
||||
authtoken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyaWQiOjE5MzE1MjcsIm9wZW5pZCI6Im85VS04NW1DSXZ0eDZ0a0pteTQyMkkzcUNHVWciLCJzaWduZXRpbWUiOjE3OTQzOTkzNTczMTYsImlhdCI6MTc5NDM5OTM1NywiZXhwIjoxNzk2OTkxMzU3fQ.sCFL7qaBVAoqsdWOrXlPyTnUT7PPMzHI4yAj8mQtV4Y",
|
||||
wxbot_bid: "4888c6ab5cb70dcc3adea1e2d2ff0201",
|
||||
}
|
||||
};
|
||||
function setStatus(s){ $("status").textContent = s; }
|
||||
function renderJson(v){ $("out").textContent = JSON.stringify(v, null, 2); }
|
||||
/** HTTPS 页内嵌 http://wx.qlogo.cn 会被浏览器拦截,改为 https 同路径即可加载 */
|
||||
function normalizeAvatarUrl(av){
|
||||
const s = (av || "").trim();
|
||||
if(!s) return "";
|
||||
const low = s.toLowerCase();
|
||||
if(low.startsWith("http://") && low.indexOf("qlogo.cn") >= 0) return "https://" + s.slice(7);
|
||||
return s;
|
||||
}
|
||||
function isAvatarImgUrl(s){
|
||||
const t = (s || "").trim().toLowerCase();
|
||||
return t.startsWith("https://") || t.startsWith("http://");
|
||||
}
|
||||
function mkAvaPlaceholder(title, hint){
|
||||
const d = document.createElement("div");
|
||||
d.className = "ava ava--ph";
|
||||
d.title = title || "";
|
||||
d.textContent = hint || "无图";
|
||||
return d;
|
||||
}
|
||||
function renderUsers(users){
|
||||
const box = $("users");
|
||||
box.replaceChildren();
|
||||
if(!Array.isArray(users) || !users.length){
|
||||
const empty = document.createElement("div");
|
||||
empty.className = "u";
|
||||
empty.textContent = "暂无数据";
|
||||
box.appendChild(empty);
|
||||
return;
|
||||
}
|
||||
for(const u of users){
|
||||
const row = document.createElement("div");
|
||||
row.className = "u";
|
||||
const rawAv = String(u.avatar || "").trim();
|
||||
const av = normalizeAvatarUrl(rawAv);
|
||||
if(av && isAvatarImgUrl(av)){
|
||||
const img = document.createElement("img");
|
||||
img.className = "ava";
|
||||
img.alt = "";
|
||||
img.referrerPolicy = "no-referrer";
|
||||
img.loading = "lazy";
|
||||
img.decoding = "async";
|
||||
img.src = av;
|
||||
img.onerror = function(){
|
||||
const ph = mkAvaPlaceholder("头像加载失败", "失败");
|
||||
this.replaceWith(ph);
|
||||
};
|
||||
row.appendChild(img);
|
||||
}else{
|
||||
row.appendChild(mkAvaPlaceholder(rawAv ? "非浏览器可加载的链接" : "接口未返回头像", rawAv ? "?" : "无"));
|
||||
}
|
||||
const meta = document.createElement("div");
|
||||
meta.className = "meta";
|
||||
const nickEl = document.createElement("div");
|
||||
nickEl.textContent = u.nick || "未命名";
|
||||
const uidEl = document.createElement("div");
|
||||
uidEl.className = "uid";
|
||||
uidEl.textContent = u.userId || "";
|
||||
const lastEl = document.createElement("div");
|
||||
lastEl.style.cssText = "font-size:12px;color:#94a3b8;margin-top:4px;";
|
||||
lastEl.textContent = u.lastMsg || "";
|
||||
meta.appendChild(nickEl);
|
||||
meta.appendChild(uidEl);
|
||||
meta.appendChild(lastEl);
|
||||
row.appendChild(meta);
|
||||
box.appendChild(row);
|
||||
}
|
||||
}
|
||||
|
||||
let _tokenCdTimer = null;
|
||||
let _tokenCdSpan = 0;
|
||||
function parseJwtPayload(token){
|
||||
try{
|
||||
const parts = String(token || "").trim().split(".");
|
||||
if(parts.length < 2) return null;
|
||||
let p = parts[1].replace(/-/g,"+").replace(/_/g,"/");
|
||||
p += "=".repeat((4 - (p.length % 4)) % 4);
|
||||
return JSON.parse(atob(p));
|
||||
}catch(e){ return null; }
|
||||
}
|
||||
function formatRemain(sec){
|
||||
if(sec <= 0) return "已过期";
|
||||
const d = Math.floor(sec / 86400);
|
||||
const h = Math.floor((sec % 86400) / 3600);
|
||||
const m = Math.floor((sec % 3600) / 60);
|
||||
const s = sec % 60;
|
||||
if(d > 0) return d + "天 " + h + "小时";
|
||||
if(h > 0) return h + "小时 " + m + "分";
|
||||
if(m > 0) return m + "分 " + s + "秒";
|
||||
return s + "秒";
|
||||
}
|
||||
function stopTokenCountdown(){
|
||||
if(_tokenCdTimer){ clearInterval(_tokenCdTimer); _tokenCdTimer = null; }
|
||||
}
|
||||
function startTokenCountdown(){
|
||||
stopTokenCountdown();
|
||||
const wrap = $("token-countdown-wrap");
|
||||
const textEl = $("token-countdown-text");
|
||||
const bar = $("token-countdown-bar");
|
||||
const token = $("authtoken").value.trim();
|
||||
const pl = parseJwtPayload(token);
|
||||
const exp = pl && typeof pl.exp === "number" ? pl.exp : null;
|
||||
const iat = pl && typeof pl.iat === "number" ? pl.iat : null;
|
||||
if(exp == null){
|
||||
wrap.classList.remove("active");
|
||||
return;
|
||||
}
|
||||
wrap.classList.add("active");
|
||||
const now0 = Math.floor(Date.now() / 1000);
|
||||
_tokenCdSpan = Math.max(120, exp - (iat || now0));
|
||||
function tick(){
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const left = exp - now;
|
||||
const pct = Math.max(0, Math.min(100, (left / _tokenCdSpan) * 100));
|
||||
textEl.textContent = formatRemain(left);
|
||||
bar.style.width = pct + "%";
|
||||
if(left > _tokenCdSpan * 0.5) bar.style.background = "#22c55e";
|
||||
else if(left > _tokenCdSpan * 0.2) bar.style.background = "#eab308";
|
||||
else bar.style.background = "#ef4444";
|
||||
if(left <= 0){
|
||||
textEl.textContent = "已过期";
|
||||
bar.style.width = "0%";
|
||||
stopTokenCountdown();
|
||||
}
|
||||
}
|
||||
tick();
|
||||
_tokenCdTimer = setInterval(tick, 1000);
|
||||
}
|
||||
|
||||
function applyPreset(key){
|
||||
const p = presets[key];
|
||||
if(!p) return;
|
||||
$("authtoken").value = p.authtoken;
|
||||
$("wxbot_bid").value = p.wxbot_bid;
|
||||
startTokenCountdown();
|
||||
}
|
||||
|
||||
async function queryPm(){
|
||||
try{
|
||||
setStatus("查询中...");
|
||||
const payload = {
|
||||
authtoken: $("authtoken").value.trim(),
|
||||
wxbot_bid: $("wxbot_bid").value.trim(),
|
||||
page: Number($("page").value || 0),
|
||||
size: Number($("size").value || 30),
|
||||
};
|
||||
const res = await fetch("__API_PREFIX__/api/private-message/query", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
setStatus(res.ok ? "查询成功" : "查询失败");
|
||||
renderUsers(data.users || []);
|
||||
renderJson(data);
|
||||
}catch(e){
|
||||
setStatus("请求异常");
|
||||
renderJson({ error: String(e) });
|
||||
}
|
||||
}
|
||||
|
||||
async function analyzeToken(){
|
||||
try{
|
||||
setStatus("分析中...");
|
||||
const payload = { authtoken: $("authtoken").value.trim() };
|
||||
const res = await fetch("__API_PREFIX__/api/private-message/analyze", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
setStatus(res.ok ? "分析成功" : "分析失败");
|
||||
renderJson(data);
|
||||
startTokenCountdown();
|
||||
}catch(e){
|
||||
setStatus("请求异常");
|
||||
renderJson({ error: String(e) });
|
||||
}
|
||||
}
|
||||
|
||||
$("preset").addEventListener("change", (e) => applyPreset(e.target.value));
|
||||
$("authtoken").addEventListener("input", function(){ startTokenCountdown(); });
|
||||
applyPreset("xidu");
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
return base.replace("__API_PREFIX__", (api_prefix or "").rstrip("/"))
|
||||
|
||||
|
||||
@router.get("/chatbot/private-message")
|
||||
@router.get("/chatbot/private-message/")
|
||||
@hackrobot_router.get("/hackrobot/private-message")
|
||||
@hackrobot_router.get("/hackrobot/private-message/")
|
||||
def chatbot_private_message_page(request: Request):
|
||||
api_prefix = "/hackrobot" if str(request.url.path).startswith("/hackrobot") else "/chatbot"
|
||||
return HTMLResponse(_private_message_debug_html(api_prefix))
|
||||
|
||||
4
apps/hackrobot/payjsapi/app/routers/cnomadcna.py
Normal file
4
apps/hackrobot/payjsapi/app/routers/cnomadcna.py
Normal file
@ -0,0 +1,4 @@
|
||||
"""cnomadcna 项目支付接口:/cnomadcna/*"""
|
||||
from ._digital_base import create_digital_router
|
||||
|
||||
router = create_digital_router(prefix="/cnomadcna", project_name="cnomadcna", site_id="meetup")
|
||||
65
apps/hackrobot/payjsapi/app/routers/common.py
Normal file
65
apps/hackrobot/payjsapi/app/routers/common.py
Normal file
@ -0,0 +1,65 @@
|
||||
"""共享接口:meetup 申请、create_user 等"""
|
||||
import logging
|
||||
import time
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..services import get_pb_client
|
||||
from ..sdk.memos import MemosSDK
|
||||
from ..config import MEMOS_API, MEMOS_TOKEN
|
||||
|
||||
router = APIRouter(tags=["common"])
|
||||
|
||||
|
||||
@router.post("/submit_meetup_application")
|
||||
async def submit_meetup_application(item: dict):
|
||||
"""数字游民社区报名表单"""
|
||||
logging.info("submit_meetup_application item: %s", item)
|
||||
user_id = item.get("user_id")
|
||||
if not user_id:
|
||||
raise HTTPException(status_code=400, detail="missing user_id")
|
||||
try:
|
||||
pb_client = get_pb_client()
|
||||
solan = pb_client.collection("solan")
|
||||
pb_data = {
|
||||
"user_id": user_id,
|
||||
"nickname": item.get("nickname", ""),
|
||||
"occupation": item.get("occupation", ""),
|
||||
"reason": item.get("reason", ""),
|
||||
"wechatId": item.get("wechatId", ""),
|
||||
"gender": item.get("gender", ""),
|
||||
"education": item.get("education", ""),
|
||||
"gradYear": item.get("gradYear", ""),
|
||||
"phone": item.get("phone", ""),
|
||||
"calculated_age": item.get("calculated_age", 0),
|
||||
"type": "",
|
||||
"order_id": "",
|
||||
"amount": 0,
|
||||
}
|
||||
record = solan.create(pb_data)
|
||||
logging.info(f"meetup 申请表单提交成功(待支付) - user_id: {user_id}, record_id: {record.id}")
|
||||
return {"status": "ok", "record_id": record.id}
|
||||
except Exception as e:
|
||||
logging.error("meetup 申请提交失败: %s", e)
|
||||
logging.error(f"meetup 申请提交失败 - user_id: {user_id}, error: {e}")
|
||||
raise HTTPException(status_code=500, detail="submit failed")
|
||||
|
||||
|
||||
class CreateUserRequest(BaseModel):
|
||||
username: str | None = None
|
||||
password: str | None = None
|
||||
|
||||
|
||||
@router.post("/create_user")
|
||||
async def create_user(item: CreateUserRequest):
|
||||
"""创建 Memos 用户(基于 MemosSDK)"""
|
||||
sdk = MemosSDK(api_url=MEMOS_API, token=MEMOS_TOKEN, enabled=bool(MEMOS_API and MEMOS_TOKEN))
|
||||
if not sdk.enabled:
|
||||
return {"error": "Memos 未配置"}
|
||||
timenew = str(int(time.time()))
|
||||
username = item.username or f"user{timenew}"
|
||||
password = item.password or "12345678"
|
||||
result = sdk.create_user_by_username(username=username, password=password)
|
||||
logging.info("memos_result- %s", result)
|
||||
return result
|
||||
4
apps/hackrobot/payjsapi/app/routers/digital.py
Normal file
4
apps/hackrobot/payjsapi/app/routers/digital.py
Normal file
@ -0,0 +1,4 @@
|
||||
"""digital 项目支付接口:/digital/*"""
|
||||
from ._digital_base import create_digital_router
|
||||
|
||||
router = create_digital_router(prefix="/digital", project_name="digital", site_id="digital")
|
||||
4
apps/hackrobot/payjsapi/app/routers/legacy.py
Normal file
4
apps/hackrobot/payjsapi/app/routers/legacy.py
Normal file
@ -0,0 +1,4 @@
|
||||
"""向后兼容:/payh5、/zpay_notify 等(digital/cnomadcna/nomadlms 旧版调用)"""
|
||||
from ._digital_base import create_digital_router
|
||||
|
||||
router = create_digital_router(prefix="", project_name="legacy")
|
||||
329
apps/hackrobot/payjsapi/app/routers/meetup.py
Normal file
329
apps/hackrobot/payjsapi/app/routers/meetup.py
Normal file
@ -0,0 +1,329 @@
|
||||
"""
|
||||
meetup 加入流程:check-user、ensure-user、complete-order
|
||||
供 cnomadcna 等前端调用,当 /api/* 代理到 payjsapi 时使用
|
||||
"""
|
||||
import logging
|
||||
import time
|
||||
|
||||
import requests
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..config import PB_URL, PB_ADMIN_EMAIL, PB_ADMIN_PASSWORD
|
||||
|
||||
router = APIRouter(prefix="/api/meetup", tags=["meetup"])
|
||||
DEFAULT_PASSWORD = "12345678" # PocketBase 默认要求至少 8 位
|
||||
|
||||
# Admin token 缓存,避免每次请求都向 PocketBase 认证(PB 远程时易超时)
|
||||
_ADMIN_TOKEN_CACHE: tuple[str, float] | None = None
|
||||
_TOKEN_CACHE_TTL = 300 # 5 分钟
|
||||
|
||||
|
||||
def _get_admin_token() -> tuple[str | None, str | None]:
|
||||
"""返回 (token, error_detail)。token 为 None 时 error_detail 为失败原因。带 5 分钟缓存。"""
|
||||
global _ADMIN_TOKEN_CACHE
|
||||
now = time.time()
|
||||
if _ADMIN_TOKEN_CACHE and _ADMIN_TOKEN_CACHE[1] > now:
|
||||
return _ADMIN_TOKEN_CACHE[0], None
|
||||
|
||||
if not PB_ADMIN_EMAIL or not PB_ADMIN_PASSWORD:
|
||||
return None, "PB_ADMIN_EMAIL 或 PB_ADMIN_PASSWORD 未配置"
|
||||
base = (PB_URL or "").rstrip("/")
|
||||
# PocketBase v0.23+ 使用 _superusers,旧版用 admins
|
||||
for path in ["/api/collections/_superusers/auth-with-password", "/api/admins/auth-with-password"]:
|
||||
try:
|
||||
r = requests.post(
|
||||
f"{base}{path}",
|
||||
json={"identity": PB_ADMIN_EMAIL, "password": PB_ADMIN_PASSWORD},
|
||||
timeout=4,
|
||||
)
|
||||
if r.status_code == 200:
|
||||
data = r.json()
|
||||
token = data.get("token")
|
||||
if token:
|
||||
_ADMIN_TOKEN_CACHE = (token, now + _TOKEN_CACHE_TTL)
|
||||
return token, None
|
||||
if r.status_code == 404:
|
||||
continue # 尝试下一个路径
|
||||
try:
|
||||
msg = r.json().get("message", r.text[:100])
|
||||
except Exception:
|
||||
msg = r.text[:100]
|
||||
logging.warning(f"PocketBase admin auth failed: status={r.status_code}, msg={msg}")
|
||||
return None, f"PocketBase 管理员认证失败: {msg}"
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
logging.error(f"PocketBase 连接失败: {e}")
|
||||
return None, f"无法连接 {PB_URL},请检查网络或 PB_URL"
|
||||
except Exception as e:
|
||||
logging.warning(f"PocketBase auth try {path}: {e}")
|
||||
return None, "PocketBase 管理员认证失败: 请确认 PB_URL 及管理员账号密码正确"
|
||||
|
||||
|
||||
class CheckUserRequest(BaseModel):
|
||||
email: str
|
||||
|
||||
|
||||
class EnsureUserRequest(BaseModel):
|
||||
email: str
|
||||
password: str | None = None
|
||||
|
||||
|
||||
def _check_site_vip(user_id: str, site_id: str = "meetup", token: str | None = None) -> bool:
|
||||
"""检查用户是否有有效 site_vip(未过期)。可传入 token 避免重复认证。"""
|
||||
if not token:
|
||||
token, _ = _get_admin_token()
|
||||
if not token:
|
||||
return False
|
||||
try:
|
||||
from datetime import datetime
|
||||
|
||||
r = requests.get(
|
||||
f"{PB_URL}/api/collections/site_vip/records",
|
||||
params={
|
||||
"filter": f'user_id = "{user_id}" && site_id = "{site_id}"',
|
||||
"perPage": 1,
|
||||
"sort": "-expires_at",
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=4,
|
||||
)
|
||||
if r.status_code != 200:
|
||||
return False
|
||||
data = r.json()
|
||||
items = data.get("items") or []
|
||||
if not items:
|
||||
return False
|
||||
rec = items[0]
|
||||
exp = rec.get("expires_at")
|
||||
if not exp:
|
||||
return True
|
||||
try:
|
||||
from datetime import datetime as _dt, timezone
|
||||
|
||||
exp_dt = _dt.fromisoformat(exp.replace("Z", "+00:00"))
|
||||
now = _dt.now(timezone.utc)
|
||||
if exp_dt.tzinfo is None:
|
||||
exp_dt = exp_dt.replace(tzinfo=timezone.utc)
|
||||
return exp_dt > now
|
||||
except Exception:
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
@router.post("/check-user")
|
||||
async def check_user(item: CheckUserRequest):
|
||||
"""检查邮箱是否已在 users 表存在,以及 VIP 状态(新/老用户判断)"""
|
||||
email = (item.email or "").strip()
|
||||
if not email:
|
||||
raise HTTPException(status_code=400, detail="请输入邮箱")
|
||||
|
||||
token, err = _get_admin_token()
|
||||
if not token:
|
||||
raise HTTPException(status_code=500, detail=f"服务配置错误:{err}")
|
||||
|
||||
try:
|
||||
filter_val = f'email="{email}"'
|
||||
r = requests.get(
|
||||
f"{PB_URL}/api/collections/users/records",
|
||||
params={"filter": filter_val, "perPage": 1},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=4,
|
||||
)
|
||||
if r.status_code != 200:
|
||||
raise HTTPException(status_code=500, detail="查询失败")
|
||||
data = r.json()
|
||||
items = data.get("items") or []
|
||||
exists = len(items) > 0
|
||||
user_id = items[0].get("id") if items else None
|
||||
vip = _check_site_vip(user_id, "meetup", token) if user_id else False
|
||||
return {"ok": True, "exists": exists, "vip": vip, "user_id": user_id}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logging.error(f"check-user error: {e}")
|
||||
raise HTTPException(status_code=500, detail="操作失败")
|
||||
|
||||
|
||||
@router.post("/ensure-user")
|
||||
async def ensure_user(item: EnsureUserRequest):
|
||||
"""确保用户存在:老用户验证密码,新用户用默认密码 12345678 创建"""
|
||||
email = (item.email or "").strip()
|
||||
if not email:
|
||||
raise HTTPException(status_code=400, detail="请输入邮箱")
|
||||
|
||||
password = (item.password or "").strip() or DEFAULT_PASSWORD
|
||||
|
||||
try:
|
||||
# 尝试用密码登录
|
||||
login_r = requests.post(
|
||||
f"{PB_URL}/api/collections/users/auth-with-password",
|
||||
json={"identity": email, "password": password},
|
||||
timeout=10,
|
||||
)
|
||||
if login_r.status_code == 200:
|
||||
data = login_r.json()
|
||||
return {
|
||||
"ok": True,
|
||||
"user_id": data.get("record", {}).get("id"),
|
||||
"token": data.get("token"),
|
||||
"record": data.get("record"),
|
||||
"is_new": False,
|
||||
}
|
||||
|
||||
try:
|
||||
err_data = login_r.json()
|
||||
except Exception:
|
||||
err_data = {}
|
||||
is_not_found = login_r.status_code == 400 and (
|
||||
"Invalid" in str(err_data.get("message", ""))
|
||||
or "identity" in str(err_data.get("message", "")).lower()
|
||||
)
|
||||
if not is_not_found and item.password:
|
||||
raise HTTPException(status_code=400, detail="密码错误")
|
||||
|
||||
# 新用户:创建
|
||||
token, err = _get_admin_token()
|
||||
if not token:
|
||||
raise HTTPException(status_code=500, detail=f"服务配置错误:{err}")
|
||||
|
||||
create_r = requests.post(
|
||||
f"{PB_URL}/api/collections/users/records",
|
||||
json={
|
||||
"email": email,
|
||||
"password": DEFAULT_PASSWORD,
|
||||
"passwordConfirm": DEFAULT_PASSWORD,
|
||||
"live_allowed": True,
|
||||
},
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {token}",
|
||||
},
|
||||
timeout=10,
|
||||
)
|
||||
if create_r.status_code != 200:
|
||||
try:
|
||||
create_err = create_r.json()
|
||||
except Exception:
|
||||
create_err = {}
|
||||
data = create_err.get("data", {})
|
||||
if (
|
||||
create_r.status_code == 400
|
||||
and "already" in str(data.get("email", {}).get("message", "")).lower()
|
||||
):
|
||||
raise HTTPException(status_code=400, detail="该邮箱已注册,请输入密码登录")
|
||||
msg = create_err.get("message", "创建账号失败")
|
||||
logging.warning(f"ensure-user create failed: status={create_r.status_code}, msg={msg}, data={data}")
|
||||
raise HTTPException(status_code=500, detail=msg)
|
||||
|
||||
# 登录新用户
|
||||
final_r = requests.post(
|
||||
f"{PB_URL}/api/collections/users/auth-with-password",
|
||||
json={"identity": email, "password": DEFAULT_PASSWORD},
|
||||
timeout=10,
|
||||
)
|
||||
if final_r.status_code != 200:
|
||||
raise HTTPException(status_code=500, detail="登录失败")
|
||||
|
||||
auth_data = final_r.json()
|
||||
return {
|
||||
"ok": True,
|
||||
"user_id": auth_data.get("record", {}).get("id"),
|
||||
"token": auth_data.get("token"),
|
||||
"record": auth_data.get("record"),
|
||||
"is_new": True,
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logging.error(f"ensure-user error: {e}")
|
||||
raise HTTPException(status_code=500, detail="操作失败")
|
||||
|
||||
|
||||
def _decode_pending_email(user_id: str) -> str | None:
|
||||
if not user_id.startswith("pending_"):
|
||||
return None
|
||||
try:
|
||||
return bytes.fromhex(user_id[8:]).decode("utf-8")
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _query_zpay_paid(order_id: str) -> bool:
|
||||
"""查询 ZPAY 订单是否已支付"""
|
||||
try:
|
||||
from ..payment import get_payment_provider
|
||||
from ..payment.zpay import ZPayProvider
|
||||
provider = get_payment_provider()
|
||||
if not isinstance(provider, ZPayProvider):
|
||||
return False
|
||||
result = provider.query_order_status(order_id, timeout=10)
|
||||
return bool(result.get("paid"))
|
||||
except Exception as e:
|
||||
logging.warning(f"complete-order zpay query failed: {e}")
|
||||
return False
|
||||
|
||||
|
||||
@router.post("/complete-order")
|
||||
async def complete_order(item: dict):
|
||||
"""
|
||||
支付成功后完成订单:验证订单、为新用户同步 session。
|
||||
支持 pending_ 订单(新用户)和已存在用户订单(user_id 为 PB id)。
|
||||
异步通知可能晚于用户回跳,故在 site_vip 未找到时轮询重试(ZPAY 已确认支付时)。
|
||||
"""
|
||||
order_id = (item.get("order_id") or "").strip()
|
||||
if not order_id:
|
||||
raise HTTPException(status_code=400, detail="缺少 order_id")
|
||||
|
||||
base = (PB_URL or "").rstrip("/")
|
||||
token, err = _get_admin_token()
|
||||
if not token:
|
||||
raise HTTPException(status_code=500, detail=f"服务配置错误:{err}")
|
||||
|
||||
from ..services.payment import parse_order_id
|
||||
|
||||
user_id, pay_type = parse_order_id(order_id)
|
||||
if not user_id or pay_type != "meetup":
|
||||
raise HTTPException(status_code=400, detail="订单格式无效")
|
||||
|
||||
# 验证订单:site_vip 中需存在该 order_id(异步通知可能晚于用户回跳,ZPAY 已支付时轮询重试)
|
||||
max_retries = 4
|
||||
for attempt in range(max_retries):
|
||||
r = requests.get(
|
||||
f"{base}/api/collections/site_vip/records",
|
||||
params={"filter": f'order_id = "{order_id}"', "perPage": 1},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=10,
|
||||
)
|
||||
total = (r.json().get("totalItems") or 0) if r.status_code == 200 else 0
|
||||
if total > 0:
|
||||
break
|
||||
if attempt < max_retries - 1 and _query_zpay_paid(order_id):
|
||||
time.sleep(2)
|
||||
continue
|
||||
resp_preview = r.text[:200] if r.ok else f"status={r.status_code}"
|
||||
logging.warning(f"complete-order 未找到订单: order_id={order_id}, attempt={attempt + 1}, pb_resp={resp_preview}")
|
||||
raise HTTPException(status_code=400, detail="订单不存在或未支付成功")
|
||||
|
||||
# pending_ 新用户:用邮箱+默认密码登录返回 token
|
||||
if user_id.startswith("pending_"):
|
||||
email = _decode_pending_email(user_id)
|
||||
if not email:
|
||||
raise HTTPException(status_code=400, detail="订单格式无效")
|
||||
login_r = requests.post(
|
||||
f"{base}/api/collections/users/auth-with-password",
|
||||
json={"identity": email, "password": DEFAULT_PASSWORD},
|
||||
timeout=10,
|
||||
)
|
||||
if login_r.status_code != 200:
|
||||
raise HTTPException(status_code=500, detail="登录失败,请稍后重试")
|
||||
auth_data = login_r.json()
|
||||
return {
|
||||
"ok": True,
|
||||
"token": auth_data.get("token"),
|
||||
"record": auth_data.get("record"),
|
||||
"is_new": True,
|
||||
}
|
||||
|
||||
# 已存在用户(PB user_id):订单已验证,返回 ok,前端已有 session 则无需 token
|
||||
return {"ok": True, "token": None, "record": None, "is_new": False}
|
||||
4
apps/hackrobot/payjsapi/app/routers/nomadlms.py
Normal file
4
apps/hackrobot/payjsapi/app/routers/nomadlms.py
Normal file
@ -0,0 +1,4 @@
|
||||
"""nomadlms 项目支付接口:/nomadlms/*"""
|
||||
from ._digital_base import create_digital_router
|
||||
|
||||
router = create_digital_router(prefix="/nomadlms", project_name="nomadlms")
|
||||
1720
apps/hackrobot/payjsapi/app/routers/nomadvip.py
Normal file
1720
apps/hackrobot/payjsapi/app/routers/nomadvip.py
Normal file
File diff suppressed because it is too large
Load Diff
4
apps/hackrobot/payjsapi/app/routers/salon.py
Normal file
4
apps/hackrobot/payjsapi/app/routers/salon.py
Normal file
@ -0,0 +1,4 @@
|
||||
"""salon 项目接口:克隆 cnomadcna 的支付与 meetup 流程,使用 site_id=salon"""
|
||||
from ._digital_base import create_digital_router
|
||||
|
||||
router = create_digital_router(prefix="/salon", project_name="salon", site_id="salon")
|
||||
502
apps/hackrobot/payjsapi/app/routers/salon_meetup.py
Normal file
502
apps/hackrobot/payjsapi/app/routers/salon_meetup.py
Normal file
@ -0,0 +1,502 @@
|
||||
"""
|
||||
salon 加入流程:check-user、ensure-user、complete-order
|
||||
克隆 meetup 逻辑,使用 site_id=salon
|
||||
"""
|
||||
import logging
|
||||
import time
|
||||
|
||||
import requests
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ..config import PB_URL, PB_ADMIN_EMAIL, PB_ADMIN_PASSWORD
|
||||
|
||||
SITE_ID = "salon"
|
||||
router = APIRouter(prefix="/api/salon", tags=["salon"])
|
||||
DEFAULT_PASSWORD = "12345678"
|
||||
|
||||
_SALON_ADMIN_TOKEN_CACHE: tuple[str, float] | None = None
|
||||
_TOKEN_CACHE_TTL = 300
|
||||
|
||||
|
||||
def _get_admin_token() -> tuple[str | None, str | None]:
|
||||
global _SALON_ADMIN_TOKEN_CACHE
|
||||
now = time.time()
|
||||
if _SALON_ADMIN_TOKEN_CACHE and _SALON_ADMIN_TOKEN_CACHE[1] > now:
|
||||
return _SALON_ADMIN_TOKEN_CACHE[0], None
|
||||
|
||||
if not PB_ADMIN_EMAIL or not PB_ADMIN_PASSWORD:
|
||||
return None, "PB_ADMIN_EMAIL 或 PB_ADMIN_PASSWORD 未配置"
|
||||
base = (PB_URL or "").rstrip("/")
|
||||
for path in ["/api/collections/_superusers/auth-with-password", "/api/admins/auth-with-password"]:
|
||||
try:
|
||||
r = requests.post(
|
||||
f"{base}{path}",
|
||||
json={"identity": PB_ADMIN_EMAIL, "password": PB_ADMIN_PASSWORD},
|
||||
timeout=4,
|
||||
)
|
||||
if r.status_code == 200:
|
||||
data = r.json()
|
||||
token = data.get("token")
|
||||
if token:
|
||||
_SALON_ADMIN_TOKEN_CACHE = (token, now + _TOKEN_CACHE_TTL)
|
||||
return token, None
|
||||
if r.status_code == 404:
|
||||
continue
|
||||
try:
|
||||
msg = r.json().get("message", r.text[:100])
|
||||
except Exception:
|
||||
msg = r.text[:100]
|
||||
logging.warning(f"PocketBase admin auth failed: status={r.status_code}, msg={msg}")
|
||||
return None, f"PocketBase 管理员认证失败: {msg}"
|
||||
except requests.exceptions.ConnectionError as e:
|
||||
logging.error(f"PocketBase 连接失败: {e}")
|
||||
return None, f"无法连接 {PB_URL},请检查网络或 PB_URL"
|
||||
except Exception as e:
|
||||
logging.warning(f"PocketBase auth try {path}: {e}")
|
||||
return None, "PocketBase 管理员认证失败: 请确认 PB_URL 及管理员账号密码正确"
|
||||
|
||||
|
||||
class CheckUserRequest(BaseModel):
|
||||
email: str
|
||||
|
||||
|
||||
class EnsureUserRequest(BaseModel):
|
||||
email: str
|
||||
password: str | None = None
|
||||
|
||||
|
||||
def _check_site_vip(user_id: str, token: str | None = None) -> bool:
|
||||
if not token:
|
||||
token, _ = _get_admin_token()
|
||||
if not token:
|
||||
return False
|
||||
try:
|
||||
r = requests.get(
|
||||
f"{PB_URL}/api/collections/site_vip/records",
|
||||
params={
|
||||
"filter": f'user_id = "{user_id}" && site_id = "{SITE_ID}"',
|
||||
"perPage": 1,
|
||||
"sort": "-expires_at",
|
||||
},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=4,
|
||||
)
|
||||
if r.status_code != 200:
|
||||
return False
|
||||
data = r.json()
|
||||
items = data.get("items") or []
|
||||
if not items:
|
||||
return False
|
||||
rec = items[0]
|
||||
exp = rec.get("expires_at")
|
||||
if not exp:
|
||||
return True
|
||||
try:
|
||||
from datetime import datetime as _dt, timezone
|
||||
exp_dt = _dt.fromisoformat(exp.replace("Z", "+00:00"))
|
||||
now = _dt.now(timezone.utc)
|
||||
if exp_dt.tzinfo is None:
|
||||
exp_dt = exp_dt.replace(tzinfo=timezone.utc)
|
||||
return exp_dt > now
|
||||
except Exception:
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
@router.post("/check-user")
|
||||
async def check_user(item: CheckUserRequest):
|
||||
email = (item.email or "").strip()
|
||||
if not email:
|
||||
raise HTTPException(status_code=400, detail="请输入邮箱")
|
||||
|
||||
token, err = _get_admin_token()
|
||||
if not token:
|
||||
raise HTTPException(status_code=500, detail=f"服务配置错误:{err}")
|
||||
|
||||
try:
|
||||
filter_val = f'email="{email}"'
|
||||
r = requests.get(
|
||||
f"{PB_URL}/api/collections/users/records",
|
||||
params={"filter": filter_val, "perPage": 1},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=4,
|
||||
)
|
||||
if r.status_code != 200:
|
||||
raise HTTPException(status_code=500, detail="查询失败")
|
||||
data = r.json()
|
||||
items = data.get("items") or []
|
||||
exists = len(items) > 0
|
||||
user_id = items[0].get("id") if items else None
|
||||
vip = _check_site_vip(user_id, token) if user_id else False
|
||||
return {"ok": True, "exists": exists, "vip": vip, "user_id": user_id}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logging.error(f"salon check-user error: {e}")
|
||||
raise HTTPException(status_code=500, detail="操作失败")
|
||||
|
||||
|
||||
@router.post("/ensure-user")
|
||||
async def ensure_user(item: EnsureUserRequest):
|
||||
email = (item.email or "").strip()
|
||||
if not email:
|
||||
raise HTTPException(status_code=400, detail="请输入邮箱")
|
||||
|
||||
password = (item.password or "").strip() or DEFAULT_PASSWORD
|
||||
|
||||
try:
|
||||
login_r = requests.post(
|
||||
f"{PB_URL}/api/collections/users/auth-with-password",
|
||||
json={"identity": email, "password": password},
|
||||
timeout=10,
|
||||
)
|
||||
if login_r.status_code == 200:
|
||||
data = login_r.json()
|
||||
return {
|
||||
"ok": True,
|
||||
"user_id": data.get("record", {}).get("id"),
|
||||
"token": data.get("token"),
|
||||
"record": data.get("record"),
|
||||
"is_new": False,
|
||||
}
|
||||
|
||||
try:
|
||||
err_data = login_r.json()
|
||||
except Exception:
|
||||
err_data = {}
|
||||
is_not_found = login_r.status_code == 400 and (
|
||||
"Invalid" in str(err_data.get("message", ""))
|
||||
or "identity" in str(err_data.get("message", "")).lower()
|
||||
)
|
||||
if not is_not_found and item.password:
|
||||
raise HTTPException(status_code=400, detail="密码错误")
|
||||
|
||||
token, err = _get_admin_token()
|
||||
if not token:
|
||||
raise HTTPException(status_code=500, detail=f"服务配置错误:{err}")
|
||||
|
||||
create_r = requests.post(
|
||||
f"{PB_URL}/api/collections/users/records",
|
||||
json={
|
||||
"email": email,
|
||||
"password": DEFAULT_PASSWORD,
|
||||
"passwordConfirm": DEFAULT_PASSWORD,
|
||||
"live_allowed": True,
|
||||
},
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {token}",
|
||||
},
|
||||
timeout=10,
|
||||
)
|
||||
if create_r.status_code != 200:
|
||||
try:
|
||||
create_err = create_r.json()
|
||||
except Exception:
|
||||
create_err = {}
|
||||
data = create_err.get("data", {})
|
||||
if (
|
||||
create_r.status_code == 400
|
||||
and "already" in str(data.get("email", {}).get("message", "")).lower()
|
||||
):
|
||||
raise HTTPException(status_code=400, detail="该邮箱已注册,请输入密码登录")
|
||||
msg = create_err.get("message", "创建账号失败")
|
||||
logging.warning(f"salon ensure-user create failed: status={create_r.status_code}, msg={msg}")
|
||||
raise HTTPException(status_code=500, detail=msg)
|
||||
|
||||
final_r = requests.post(
|
||||
f"{PB_URL}/api/collections/users/auth-with-password",
|
||||
json={"identity": email, "password": DEFAULT_PASSWORD},
|
||||
timeout=10,
|
||||
)
|
||||
if final_r.status_code != 200:
|
||||
raise HTTPException(status_code=500, detail="登录失败")
|
||||
|
||||
auth_data = final_r.json()
|
||||
return {
|
||||
"ok": True,
|
||||
"user_id": auth_data.get("record", {}).get("id"),
|
||||
"token": auth_data.get("token"),
|
||||
"record": auth_data.get("record"),
|
||||
"is_new": True,
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
logging.error(f"salon ensure-user error: {e}")
|
||||
raise HTTPException(status_code=500, detail="操作失败")
|
||||
|
||||
|
||||
def _decode_pending_email(user_id: str) -> str | None:
|
||||
if not user_id.startswith("pending_"):
|
||||
return None
|
||||
try:
|
||||
return bytes.fromhex(user_id[8:]).decode("utf-8")
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _query_zpay_paid(order_id: str) -> bool:
|
||||
try:
|
||||
from ..payment import get_payment_provider
|
||||
from ..payment.zpay import ZPayProvider
|
||||
provider = get_payment_provider()
|
||||
if not isinstance(provider, ZPayProvider):
|
||||
return False
|
||||
result = provider.query_order_status(order_id, timeout=10)
|
||||
return bool(result.get("paid"))
|
||||
except Exception as e:
|
||||
logging.warning(f"salon complete-order zpay query failed: {e}")
|
||||
return False
|
||||
|
||||
|
||||
@router.post("/complete-order")
|
||||
async def complete_order(item: dict):
|
||||
order_id = (item.get("order_id") or "").strip()
|
||||
if not order_id:
|
||||
raise HTTPException(status_code=400, detail="缺少 order_id")
|
||||
|
||||
base = (PB_URL or "").rstrip("/")
|
||||
token, err = _get_admin_token()
|
||||
if not token:
|
||||
raise HTTPException(status_code=500, detail=f"服务配置错误:{err}")
|
||||
|
||||
from ..services.payment import parse_order_id
|
||||
|
||||
user_id, pay_type = parse_order_id(order_id)
|
||||
if not user_id or pay_type != "salon":
|
||||
raise HTTPException(status_code=400, detail="订单格式无效")
|
||||
|
||||
max_retries = 4
|
||||
for attempt in range(max_retries):
|
||||
r = requests.get(
|
||||
f"{base}/api/collections/site_vip/records",
|
||||
params={"filter": f'order_id = "{order_id}" && site_id = "{SITE_ID}"', "perPage": 1},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=10,
|
||||
)
|
||||
total = (r.json().get("totalItems") or 0) if r.status_code == 200 else 0
|
||||
if total > 0:
|
||||
break
|
||||
if attempt < max_retries - 1:
|
||||
paid_via = None
|
||||
pay_price = ""
|
||||
if _query_zpay_paid(order_id):
|
||||
paid_via = "zpay"
|
||||
try:
|
||||
from ..payment.zpay import ZPayProvider
|
||||
from ..payment import get_payment_provider
|
||||
p = get_payment_provider()
|
||||
if isinstance(p, ZPayProvider):
|
||||
r = p.query_order_status(order_id, timeout=8)
|
||||
if r.get("paid"):
|
||||
pay_price = str(r.get("pay_price", "") or r.get("money", ""))
|
||||
except Exception:
|
||||
pass
|
||||
if not paid_via:
|
||||
try:
|
||||
from ..payment.xorpay import XorPayProvider
|
||||
from ..payment import get_payment_provider
|
||||
p = get_payment_provider()
|
||||
if isinstance(p, XorPayProvider):
|
||||
r = p.query_order_status(order_id, timeout=8)
|
||||
if r.get("paid"):
|
||||
paid_via = "xorpay"
|
||||
pay_price = str(r.get("pay_price", ""))
|
||||
except Exception:
|
||||
pass
|
||||
if paid_via:
|
||||
from ..services.payment import handle_payment_success
|
||||
# pay_price 为空时用默认茶歇费 1 元
|
||||
price = pay_price if pay_price and float(pay_price or 0) > 0 else "1"
|
||||
try:
|
||||
handle_payment_success(
|
||||
order_id,
|
||||
price,
|
||||
f"salon-{paid_via}-poll",
|
||||
use_memos=False,
|
||||
site_id=SITE_ID,
|
||||
)
|
||||
time.sleep(1)
|
||||
continue
|
||||
except Exception as e:
|
||||
logging.warning(f"salon complete-order 手动触发支付处理失败: {e}")
|
||||
if paid_via:
|
||||
time.sleep(2)
|
||||
continue
|
||||
resp_preview = r.text[:200] if r.ok else f"status={r.status_code}"
|
||||
logging.warning(f"salon complete-order 未找到订单: order_id={order_id}, attempt={attempt + 1}, pb_resp={resp_preview}")
|
||||
raise HTTPException(status_code=400, detail="订单不存在或未支付成功")
|
||||
|
||||
if user_id.startswith("pending_"):
|
||||
email = _decode_pending_email(user_id)
|
||||
if not email:
|
||||
raise HTTPException(status_code=400, detail="订单格式无效")
|
||||
login_r = requests.post(
|
||||
f"{base}/api/collections/users/auth-with-password",
|
||||
json={"identity": email, "password": DEFAULT_PASSWORD},
|
||||
timeout=10,
|
||||
)
|
||||
if login_r.status_code != 200:
|
||||
raise HTTPException(status_code=500, detail="登录失败,请稍后重试")
|
||||
auth_data = login_r.json()
|
||||
return {
|
||||
"ok": True,
|
||||
"token": auth_data.get("token"),
|
||||
"record": auth_data.get("record"),
|
||||
"is_new": True,
|
||||
}
|
||||
|
||||
return {"ok": True, "token": None, "record": None, "is_new": False}
|
||||
|
||||
|
||||
@router.get("/vip/check")
|
||||
async def vip_check(user_id: str = ""):
|
||||
"""检查用户是否为 salon VIP"""
|
||||
if not user_id:
|
||||
return {"vip": False}
|
||||
token, _ = _get_admin_token()
|
||||
return {"vip": _check_site_vip(user_id, token)}
|
||||
|
||||
|
||||
@router.get("/join/by-wechat")
|
||||
async def join_by_wechat(wechat_id: str = ""):
|
||||
"""按微信号查询 solanRed 记录,供 salon 前端提交后刷新状态"""
|
||||
if not wechat_id or not wechat_id.strip():
|
||||
return {"hasRecord": False, "record": None, "isComplete": False, "isWechatFriend": False, "isApproved": False, "isRejected": False, "vip": False}
|
||||
try:
|
||||
from ..services import get_pb_client
|
||||
pb_client = get_pb_client()
|
||||
solan = pb_client.collection("solanRed")
|
||||
safe_wechat = wechat_id.strip().replace("\\", "\\\\").replace('"', '\\"')
|
||||
existing = solan.get_list(1, 1, {"filter": f'wechatId = "{safe_wechat}"', "sort": "-created"})
|
||||
if not existing.items:
|
||||
return {"hasRecord": False, "record": None, "isComplete": False, "isWechatFriend": False, "isApproved": False, "isRejected": False, "vip": False}
|
||||
rec = existing.items[0]
|
||||
amount = rec.get("amount") or 0
|
||||
is_complete = bool(rec.get("order_id")) and (amount or 0) > 0
|
||||
return {
|
||||
"hasRecord": True,
|
||||
"record": rec,
|
||||
"isComplete": is_complete,
|
||||
"isWechatFriend": bool(rec.get("is_wechat_friend")),
|
||||
"isApproved": bool(rec.get("is_approved")),
|
||||
"isRejected": bool(rec.get("is_rejected")),
|
||||
"vip": bool(rec.get("vip")),
|
||||
}
|
||||
except Exception as e:
|
||||
logging.warning("salon join/by-wechat error: %s", e)
|
||||
return {"hasRecord": False, "record": None, "isComplete": False, "isWechatFriend": False, "isApproved": False, "isRejected": False, "vip": False}
|
||||
|
||||
|
||||
@router.get("/join/status")
|
||||
async def join_status(user_id: str = ""):
|
||||
"""检查用户是否已提交 salon 加入申请"""
|
||||
if not user_id:
|
||||
return {"hasRecord": False, "isComplete": False}
|
||||
try:
|
||||
from ..services import get_pb_client
|
||||
pb_client = get_pb_client()
|
||||
solan = pb_client.collection("solanRed")
|
||||
existing = solan.get_list(1, 1, {"filter": f'user_id = "{user_id}"', "sort": "-created"})
|
||||
has_record = len(existing.items) > 0
|
||||
rec = existing.items[0] if existing.items else None
|
||||
is_complete = bool(rec and rec.get("order_id") and rec.get("amount", 0) > 0)
|
||||
return {"hasRecord": has_record, "isComplete": is_complete}
|
||||
except Exception:
|
||||
return {"hasRecord": False, "isComplete": False}
|
||||
|
||||
|
||||
def _validate_xiaohongshu_url(url: str) -> dict | None:
|
||||
"""校验小红书主页 URL 并尝试爬取用户名/头像/粉丝数/性别。返回 None 表示校验失败。"""
|
||||
url = (url or "").strip()
|
||||
if not url:
|
||||
return None
|
||||
if "xiaohongshu.com" not in url.lower():
|
||||
return None
|
||||
if not url.startswith("http://") and not url.startswith("https://"):
|
||||
return None
|
||||
try:
|
||||
r = requests.get(url, timeout=10, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"})
|
||||
if r.status_code != 200:
|
||||
return None
|
||||
html = r.text
|
||||
nickname = ""
|
||||
avatar = ""
|
||||
followers = 0
|
||||
gender = ""
|
||||
import re
|
||||
if '"nickname"' in html or '"user"' in html:
|
||||
m = re.search(r'"nickname"\s*:\s*"([^"]+)"', html)
|
||||
if m:
|
||||
nickname = m.group(1)
|
||||
m = re.search(r'"avatar"\s*:\s*"([^"]+)"', html)
|
||||
if m:
|
||||
avatar = m.group(1).replace("\\u002F", "/")
|
||||
m = re.search(r'"follows"\s*:\s*(\d+)', html)
|
||||
if m:
|
||||
followers = int(m.group(1))
|
||||
if not nickname and '"desc"' in html:
|
||||
m = re.search(r'"desc"\s*:\s*"([^"]+)"', html)
|
||||
if m:
|
||||
nickname = m.group(1)[:20]
|
||||
return {"nickname": nickname or "用户", "avatar": avatar, "followers": followers, "gender": gender, "url": url}
|
||||
except Exception as e:
|
||||
logging.warning(f"xiaohongshu crawl failed: {e}")
|
||||
return {"nickname": "用户", "avatar": "", "followers": 0, "gender": "", "url": url}
|
||||
|
||||
|
||||
@router.post("/join")
|
||||
async def submit_join(item: dict):
|
||||
"""salon 加入社区报名表单,写入 solanRed 集合。需提供 xiaohongshu_url,提交前校验并爬取信息。"""
|
||||
user_id = item.get("user_id")
|
||||
if not user_id:
|
||||
raise HTTPException(status_code=400, detail="missing user_id")
|
||||
|
||||
xiaohongshu_url = (item.get("xiaohongshu_url") or "").strip()
|
||||
if not xiaohongshu_url:
|
||||
raise HTTPException(status_code=400, detail="请填写小红书主页地址")
|
||||
|
||||
profile = _validate_xiaohongshu_url(xiaohongshu_url)
|
||||
if not profile:
|
||||
raise HTTPException(status_code=400, detail="小红书主页地址无效或无法访问,请检查后重试")
|
||||
|
||||
try:
|
||||
from ..services import get_pb_client
|
||||
pb_client = get_pb_client()
|
||||
solan = pb_client.collection("solanRed")
|
||||
wechat_id = (item.get("wechatId") or "").strip()
|
||||
if wechat_id:
|
||||
safe_wechat = wechat_id.replace("\\", "\\\\").replace('"', '\\"')
|
||||
existing = solan.get_list(1, 1, {"filter": f'wechatId = "{safe_wechat}"'})
|
||||
if existing.items:
|
||||
raise HTTPException(status_code=400, detail="该微信号已报名,请勿重复提交")
|
||||
pb_data = {
|
||||
"user_id": user_id,
|
||||
"nickname": item.get("nickname", ""),
|
||||
"occupation": item.get("occupation", ""),
|
||||
"reason": item.get("reason", ""),
|
||||
"wechatId": item.get("wechatId", ""),
|
||||
"gender": profile.get("gender", "") or item.get("gender", ""),
|
||||
"education": item.get("education", ""),
|
||||
"gradYear": item.get("gradYear", ""),
|
||||
"phone": item.get("phone", ""),
|
||||
"single": item.get("single", ""),
|
||||
"media": item.get("media", ""),
|
||||
"calculated_age": item.get("calculated_age", 0),
|
||||
"type": item.get("type", ""),
|
||||
"order_id": "",
|
||||
"amount": 0,
|
||||
"xiaohongshu_url": xiaohongshu_url,
|
||||
"xiaohongshu_nickname": profile.get("nickname", ""),
|
||||
"xiaohongshu_avatar": profile.get("avatar", ""),
|
||||
"xiaohongshu_followers": profile.get("followers", 0),
|
||||
}
|
||||
record = solan.create(pb_data)
|
||||
logging.info(f"salon 申请表单提交成功 - user_id: {user_id}, record_id: {record.id}")
|
||||
return {"ok": True, "user_id": user_id, "record_id": record.id}
|
||||
except Exception as e:
|
||||
logging.error(f"salon join 提交失败: {e}")
|
||||
raise HTTPException(status_code=500, detail="submit failed")
|
||||
274
apps/hackrobot/payjsapi/app/routers/wxh5.py
Normal file
274
apps/hackrobot/payjsapi/app/routers/wxh5.py
Normal file
@ -0,0 +1,274 @@
|
||||
"""wxH5 项目支付接口:/wxh5/*,支持 xorpay/zpay,支付成功后同步 Supabase wxgzh_vip_users"""
|
||||
import logging
|
||||
import random
|
||||
import string
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from fastapi import APIRouter, Request, HTTPException
|
||||
from fastapi.responses import RedirectResponse, JSONResponse
|
||||
|
||||
from ..config import BASE_URL
|
||||
from ..payment import (
|
||||
get_payment_provider,
|
||||
get_payment_provider_by_name,
|
||||
resolve_provider,
|
||||
resolve_channel,
|
||||
)
|
||||
from ..payment.zpay import ZPayProvider
|
||||
from ..services import handle_payment_success, processed_orders
|
||||
|
||||
router = APIRouter(prefix="/wxh5", tags=["wxh5"])
|
||||
|
||||
# 套餐金额(分)与有效期映射
|
||||
PLAN_MAP = {
|
||||
"month": (2900, 30), # 月卡 29 元,30 天
|
||||
"year": (19900, 365), # 年卡 199 元,365 天
|
||||
"lifetime": (39900, 36500), # 终身 399 元,100 年
|
||||
}
|
||||
|
||||
|
||||
def _infer_plan_from_amount(amount_cents: int) -> str:
|
||||
"""根据支付金额(分)推断套餐"""
|
||||
if amount_cents >= 39900:
|
||||
return "lifetime"
|
||||
if amount_cents >= 19900:
|
||||
return "year"
|
||||
return "month"
|
||||
|
||||
|
||||
def _get_notify_path(provider_name: str) -> str:
|
||||
base = BASE_URL.rstrip("/")
|
||||
return f"{base}/wxh5/xorpay_notify" if provider_name == "xorpay" else f"{base}/wxh5/zpay_notify"
|
||||
|
||||
|
||||
def _sync_wxh5_vip_to_supabase(user_id: str, plan: str):
|
||||
"""支付成功后同步 VIP 到 Supabase wxgzh_vip_users"""
|
||||
try:
|
||||
from supabase import create_client
|
||||
from ..config import SUPABASE_URL, SUPABASE_SERVICE_KEY
|
||||
url = SUPABASE_URL
|
||||
key = SUPABASE_SERVICE_KEY
|
||||
if not url or not key:
|
||||
logging.warning("wxh5: SUPABASE_URL/SUPABASE_SERVICE_KEY 未配置,跳过 Supabase 同步")
|
||||
return
|
||||
days = PLAN_MAP.get(plan, PLAN_MAP["year"])[1]
|
||||
expire_at = (datetime.utcnow() + timedelta(days=days)).strftime("%Y-%m-%dT%H:%M:%S+00:00")
|
||||
client = create_client(url, key)
|
||||
# upsert: 存在则更新 expire_at,不存在则插入
|
||||
client.table("wxgzh_vip_users").upsert(
|
||||
{"user_id": user_id, "expire_at": expire_at},
|
||||
on_conflict="user_id",
|
||||
).execute()
|
||||
logging.info(f"wxh5 Supabase VIP 同步成功: user_id={user_id}, plan={plan}")
|
||||
except Exception as e:
|
||||
logging.error(f"wxh5 Supabase 同步失败: {e}", exc_info=True)
|
||||
|
||||
|
||||
@router.post("/payh5")
|
||||
async def wxh5_payh5(item: dict, request: Request):
|
||||
"""wxH5 专用:创建支付订单。微信内用 xorpay,PC/手机用 zpay,默认微信支付,支付宝暂不提供"""
|
||||
req_provider = (item.get("provider") or "").strip().lower() or None
|
||||
device = (item.get("device") or "pc").lower()
|
||||
ua = (request.headers.get("user-agent") or "").strip()
|
||||
provider = resolve_provider(device, req_provider, ua)
|
||||
channel = resolve_channel(item.get("channel"))
|
||||
plan = (item.get("plan") or "year").lower()
|
||||
total_fee = PLAN_MAP.get(plan, PLAN_MAP["year"])[0]
|
||||
total_fee = int(item.get("total_fee", total_fee))
|
||||
total_fee_yuan = f"{total_fee / 100:.2f}"
|
||||
user_id = item.get("user_id", "unknown")
|
||||
pay_type = (item.get("type") or "vip").lower()
|
||||
random_suffix = "".join(random.choices(string.hexdigits.lower(), k=8))
|
||||
timestamp = int(time.time())
|
||||
order_id = f"{user_id}_{pay_type}_order_{timestamp}_{random_suffix}"
|
||||
return_url = item.get("return_url", "")
|
||||
if return_url and "order_id=" not in return_url:
|
||||
sep = "&" if "?" in return_url else "?"
|
||||
return_url = f"{return_url}{sep}order_id={order_id}"
|
||||
type_map = {"book": "购买书籍", "meetup": "社区报名", "video": "视频解锁", "vip": "VIP会员充值"}
|
||||
name = type_map.get(pay_type, "VIP会员充值")
|
||||
notify_url = _get_notify_path(provider.name)
|
||||
result = provider.create_order(
|
||||
order_id=order_id,
|
||||
name=name,
|
||||
total_fee_yuan=total_fee_yuan,
|
||||
pay_type=channel,
|
||||
notify_url=notify_url,
|
||||
return_url=return_url,
|
||||
client_ip=item.get("client_ip"),
|
||||
)
|
||||
if provider.name == "xorpay":
|
||||
return {
|
||||
"status": "ok",
|
||||
"xorpay_params": result.get("params", {}),
|
||||
"xorpay_url": result.get("pay_url", ""),
|
||||
"provider": provider.name,
|
||||
"order_id": order_id,
|
||||
}
|
||||
params = result.get("params", {})
|
||||
params_str = {k: str(v) for k, v in params.items() if v is not None and str(v).strip() != ""}
|
||||
return {
|
||||
"status": "ok",
|
||||
"params": params_str,
|
||||
"pay_url": result.get("pay_url", ""),
|
||||
"provider": provider.name,
|
||||
"submit_method": "POST",
|
||||
"order_id": order_id,
|
||||
}
|
||||
|
||||
|
||||
@router.post("/payh5/redirect")
|
||||
async def wxh5_payh5_redirect(item: dict, request: Request):
|
||||
"""wxH5 专用:ZPAY mapi 接口,PC/手机用 zpay,默认微信支付,支付宝暂不提供"""
|
||||
provider = get_payment_provider_by_name("zpay")
|
||||
channel = resolve_channel(item.get("channel"))
|
||||
plan = (item.get("plan") or "year").lower()
|
||||
total_fee = PLAN_MAP.get(plan, PLAN_MAP["year"])[0]
|
||||
try:
|
||||
total_fee = int(item.get("total_fee", total_fee))
|
||||
except (TypeError, ValueError):
|
||||
total_fee = PLAN_MAP["year"][0]
|
||||
total_fee_yuan = f"{total_fee / 100:.2f}"
|
||||
user_id = item.get("user_id", "unknown")
|
||||
pay_type = (item.get("type") or "vip").lower()
|
||||
device = item.get("device", "pc")
|
||||
client_ip = item.get("client_ip") or (request.client.host if request.client else "127.0.0.1")
|
||||
forwarded = request.headers.get("x-forwarded-for")
|
||||
if forwarded:
|
||||
client_ip = forwarded.split(",")[0].strip()
|
||||
random_suffix = "".join(random.choices(string.hexdigits.lower(), k=8))
|
||||
order_id = f"{user_id}_{pay_type}_order_{int(time.time())}_{random_suffix}"
|
||||
return_url_raw = item.get("return_url", "")
|
||||
if return_url_raw and "order_id=" not in return_url_raw:
|
||||
sep = "&" if "?" in return_url_raw else "?"
|
||||
return_url = f"{return_url_raw}{sep}order_id={order_id}"
|
||||
else:
|
||||
return_url = return_url_raw
|
||||
type_map = {"book": "购买书籍", "meetup": "社区报名", "video": "视频解锁", "vip": "VIP会员充值"}
|
||||
name = type_map.get(pay_type, "VIP会员充值")
|
||||
notify_url = f"{BASE_URL.rstrip('/')}/wxh5/zpay_notify"
|
||||
result = provider.create_order_api(
|
||||
order_id=order_id,
|
||||
name=name,
|
||||
total_fee_yuan=total_fee_yuan,
|
||||
pay_type=channel,
|
||||
notify_url=notify_url,
|
||||
return_url=return_url,
|
||||
client_ip=client_ip,
|
||||
device=device,
|
||||
)
|
||||
if result.get("status") != "ok":
|
||||
err_msg = result.get("msg", "ZPAY mapi 创建订单失败")
|
||||
raise HTTPException(status_code=500, detail=err_msg)
|
||||
payurl = result.get("payurl")
|
||||
payurl2 = result.get("payurl2")
|
||||
img = result.get("img")
|
||||
if device == "pc" and img:
|
||||
return JSONResponse(status_code=200, content={
|
||||
"status": "ok", "qrcode_mode": True, "order_id": order_id,
|
||||
"img": img, "payurl": payurl, "return_url": return_url, "channel": channel,
|
||||
})
|
||||
if device == "wechat" and channel.lower() in ("wxpay", "wx", "wechat"):
|
||||
return JSONResponse(status_code=200, content={"use_form": True, "order_id": order_id})
|
||||
headers = {"X-Pay-Order-Id": order_id}
|
||||
prefer_json = str(item.get("prefer_json", "")).lower() in ("1", "true", "yes")
|
||||
if prefer_json:
|
||||
target = payurl2 if (device == "h5" and channel.lower() in ("wxpay", "wx", "wechat")) else payurl
|
||||
if target:
|
||||
return JSONResponse(status_code=200, content={
|
||||
"status": "ok", "redirect_url": target, "order_id": order_id, "provider": "zpay",
|
||||
})
|
||||
if device == "h5" and channel.lower() in ("wxpay", "wx", "wechat") and payurl2:
|
||||
return RedirectResponse(url=payurl2, status_code=302, headers=headers)
|
||||
if payurl:
|
||||
return RedirectResponse(url=payurl, status_code=302, headers=headers)
|
||||
raise HTTPException(status_code=500, detail="ZPAY 未返回支付链接")
|
||||
|
||||
|
||||
def _parse_wxh5_user_id(order_id: str) -> str:
|
||||
"""从 order_id 解析 user_id,格式: user_id_vip_order_ts"""
|
||||
if "_order_" not in order_id:
|
||||
return ""
|
||||
prefix = order_id.split("_order_")[0]
|
||||
if "_" in prefix:
|
||||
return prefix.rsplit("_", 1)[0]
|
||||
return prefix
|
||||
|
||||
|
||||
def _handle_wxh5_payment_success(order_id: str, pay_price: str, provider_name: str):
|
||||
"""wxH5 支付成功:PocketBase + Supabase"""
|
||||
user_id = _parse_wxh5_user_id(order_id)
|
||||
if not user_id:
|
||||
return
|
||||
handle_payment_success(order_id, pay_price, provider_name, use_memos=False)
|
||||
amount_cents = int(float(pay_price or 0) * 100)
|
||||
plan = _infer_plan_from_amount(amount_cents)
|
||||
_sync_wxh5_vip_to_supabase(user_id, plan)
|
||||
|
||||
|
||||
@router.get("/order_status")
|
||||
async def wxh5_order_status(order_id: str):
|
||||
"""wxH5 通用:查询订单支付状态"""
|
||||
zpay = get_payment_provider_by_name("zpay")
|
||||
if isinstance(zpay, ZPayProvider):
|
||||
result = zpay.query_order_status(order_id, timeout=15)
|
||||
if not result.get("error") and result.get("paid"):
|
||||
return result
|
||||
try:
|
||||
from ..services.pb import get_pb_client
|
||||
pb = get_pb_client()
|
||||
records = pb.collection("payments").get_list(1, 1, {"filter": f'order_id = "{order_id}"'})
|
||||
return {"paid": len(records.items) > 0}
|
||||
except Exception as e:
|
||||
logging.warning(f"wxh5 PocketBase 查询失败: {e}")
|
||||
return {"paid": False, "error": str(e)}
|
||||
|
||||
|
||||
@router.post("/xorpay_notify")
|
||||
async def wxh5_xorpay_notify(request: Request):
|
||||
"""wxH5 XorPay 异步通知"""
|
||||
form_data = await request.form()
|
||||
data = {k: v for k, v in form_data.items()}
|
||||
order_id = data.get("order_id", "")
|
||||
if order_id in processed_orders:
|
||||
return "ok"
|
||||
provider = get_payment_provider_by_name("xorpay")
|
||||
if not provider.verify_notify(data):
|
||||
raise HTTPException(status_code=400, detail="sign error")
|
||||
parsed = provider.parse_notify(data)
|
||||
if parsed.get("trade_status") != "success":
|
||||
return "ok"
|
||||
try:
|
||||
_handle_wxh5_payment_success(order_id, parsed.get("pay_price", ""), "wxh5-xorpay")
|
||||
except Exception as e:
|
||||
logging.error(f"wxh5 业务处理异常: {e}", exc_info=True)
|
||||
raise
|
||||
return provider.success_response()
|
||||
|
||||
|
||||
@router.get("/zpay_notify")
|
||||
@router.post("/zpay_notify")
|
||||
async def wxh5_zpay_notify(request: Request):
|
||||
"""wxH5 ZPAY 异步通知"""
|
||||
provider = get_payment_provider_by_name("zpay")
|
||||
if request.method == "GET":
|
||||
data = dict(request.query_params)
|
||||
else:
|
||||
form_data = await request.form()
|
||||
data = {k: v for k, v in form_data.items()}
|
||||
order_id = data.get("out_trade_no", "")
|
||||
if order_id in processed_orders:
|
||||
return "success"
|
||||
if not provider.verify_notify(data):
|
||||
raise HTTPException(status_code=400, detail="sign error")
|
||||
parsed = provider.parse_notify(data)
|
||||
trade_status = parsed.get("trade_status") or data.get("trade_status", "")
|
||||
if trade_status not in ("success", "TRADE_SUCCESS"):
|
||||
return "success"
|
||||
try:
|
||||
_handle_wxh5_payment_success(order_id, parsed.get("pay_price") or data.get("money", ""), "wxh5-zpay")
|
||||
except Exception as e:
|
||||
logging.error(f"wxh5 业务处理异常: {e}", exc_info=True)
|
||||
raise
|
||||
return provider.success_response()
|
||||
20
apps/hackrobot/payjsapi/app/sdk/__init__.py
Normal file
20
apps/hackrobot/payjsapi/app/sdk/__init__.py
Normal file
@ -0,0 +1,20 @@
|
||||
"""
|
||||
PayJS API SDK 模块
|
||||
- payment: 支付网关(ZPAY/XorPay)
|
||||
- pocketbase: PocketBase 数据库
|
||||
- minio: MinIO 对象存储
|
||||
- memos: Memos 用户(仅 nomadvip 使用)
|
||||
"""
|
||||
from .payment import get_payment_provider, ZPayProvider, XorPayProvider
|
||||
from .pocketbase import PocketBaseSDK
|
||||
from .minio import MinioSDK
|
||||
from .memos import MemosSDK
|
||||
|
||||
__all__ = [
|
||||
"get_payment_provider",
|
||||
"ZPayProvider",
|
||||
"XorPayProvider",
|
||||
"PocketBaseSDK",
|
||||
"MinioSDK",
|
||||
"MemosSDK",
|
||||
]
|
||||
95
apps/hackrobot/payjsapi/app/sdk/memos.py
Normal file
95
apps/hackrobot/payjsapi/app/sdk/memos.py
Normal file
@ -0,0 +1,95 @@
|
||||
"""
|
||||
Memos SDK
|
||||
用户创建等操作,仅 nomadvip 使用
|
||||
"""
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
class MemosSDK:
|
||||
"""Memos 用户管理 SDK"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
api_url: str,
|
||||
token: str,
|
||||
enabled: bool = True,
|
||||
):
|
||||
self.api_url = api_url.rstrip("/")
|
||||
self.token = token
|
||||
self.enabled = enabled and bool(api_url and token)
|
||||
|
||||
def create_user(
|
||||
self,
|
||||
user_id: str,
|
||||
password: str = "12345678",
|
||||
) -> dict:
|
||||
"""
|
||||
创建 Memos 用户
|
||||
:param user_id: PocketBase 用户 ID,如 user1766043770158
|
||||
:return: {"success": bool, "data"?: dict, "error"?: str}
|
||||
"""
|
||||
if not self.enabled:
|
||||
return {"success": False, "error": "Memos 未启用"}
|
||||
|
||||
try:
|
||||
if user_id.startswith("user_"):
|
||||
cleaned = "user" + user_id.split("user_", 1)[1]
|
||||
elif "_" in user_id:
|
||||
cleaned = user_id.replace("_", "")
|
||||
else:
|
||||
cleaned = user_id
|
||||
|
||||
username = cleaned
|
||||
data = {
|
||||
"username": username,
|
||||
"password": password,
|
||||
"role": "USER",
|
||||
"state": "NORMAL",
|
||||
}
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {self.token}",
|
||||
}
|
||||
resp = requests.post(
|
||||
self.api_url,
|
||||
json=data,
|
||||
headers=headers,
|
||||
timeout=10,
|
||||
)
|
||||
if resp.status_code in (200, 201):
|
||||
logging.info(f"memos 用户创建成功: {username}")
|
||||
return {"success": True, "data": resp.json()}
|
||||
else:
|
||||
logging.error(f"memos 创建失败 - username: {username}, status: {resp.status_code}, response: {resp.text}")
|
||||
return {"success": False, "error": resp.text}
|
||||
except Exception as e:
|
||||
logging.error(f"创建 memos 用户异常 - user_id: {user_id}, error: {e}")
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
def create_user_by_username(
|
||||
self,
|
||||
username: str,
|
||||
password: str = "12345678",
|
||||
) -> dict:
|
||||
"""按用户名直接创建(用于 /create_user 接口)"""
|
||||
if not self.enabled:
|
||||
return {"success": False, "error": "Memos 未启用"}
|
||||
try:
|
||||
data = {
|
||||
"username": username,
|
||||
"password": password,
|
||||
"role": "USER",
|
||||
"state": "NORMAL",
|
||||
}
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {self.token}",
|
||||
}
|
||||
resp = requests.post(self.api_url, json=data, headers=headers, timeout=10)
|
||||
return resp.json()
|
||||
except Exception as e:
|
||||
logging.error(f"创建 memos 用户异常 - username: {username}, error: {e}")
|
||||
return {"error": str(e)}
|
||||
92
apps/hackrobot/payjsapi/app/sdk/minio.py
Normal file
92
apps/hackrobot/payjsapi/app/sdk/minio.py
Normal file
@ -0,0 +1,92 @@
|
||||
"""
|
||||
MinIO SDK
|
||||
对象存储封装,支持配置注入
|
||||
"""
|
||||
import os
|
||||
import time
|
||||
import random
|
||||
import string
|
||||
from typing import Optional
|
||||
|
||||
try:
|
||||
from minio import Minio
|
||||
MINIO_AVAILABLE = True
|
||||
except ImportError:
|
||||
MINIO_AVAILABLE = False
|
||||
Minio = None
|
||||
|
||||
|
||||
class MinioSDK:
|
||||
"""MinIO 对象存储 SDK"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
end_point: str,
|
||||
access_key: str,
|
||||
secret_key: str,
|
||||
bucket: str = "hackrobot",
|
||||
use_ssl: bool = True,
|
||||
port: Optional[int] = None,
|
||||
region: str = "china",
|
||||
public_url: Optional[str] = None,
|
||||
upload_prefix: str = "joins",
|
||||
):
|
||||
if not MINIO_AVAILABLE:
|
||||
raise ImportError("minio 未安装,请执行: pip install minio")
|
||||
self.end_point = end_point
|
||||
self.port = port or (443 if use_ssl else 80)
|
||||
self.access_key = access_key
|
||||
self.secret_key = secret_key
|
||||
self.bucket = bucket
|
||||
self.use_ssl = use_ssl
|
||||
self.region = region
|
||||
self.public_url = public_url or f"https://{end_point}/{bucket}"
|
||||
self.upload_prefix = upload_prefix.rstrip("/")
|
||||
self._client: Optional[Minio] = None
|
||||
|
||||
def get_client(self) -> "Minio":
|
||||
"""获取 MinIO 客户端"""
|
||||
if self._client is None:
|
||||
endpoint = f"{self.end_point}:{self.port}" if self.port not in (80, 443) else self.end_point
|
||||
self._client = Minio(
|
||||
endpoint,
|
||||
access_key=self.access_key,
|
||||
secret_key=self.secret_key,
|
||||
secure=self.use_ssl,
|
||||
region=self.region,
|
||||
)
|
||||
return self._client
|
||||
|
||||
def generate_object_key(self, ext: str) -> str:
|
||||
"""生成上传用的对象键"""
|
||||
ext = ext.lstrip(".")
|
||||
suffix = "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
|
||||
return f"{self.upload_prefix}/{int(time.time() * 1000)}_{suffix}.{ext}"
|
||||
|
||||
def upload_buffer(
|
||||
self,
|
||||
data: bytes,
|
||||
object_key: str,
|
||||
content_type: str = "application/octet-stream",
|
||||
) -> dict:
|
||||
"""
|
||||
上传文件到 MinIO
|
||||
:return: {"url": str, "object_key": str}
|
||||
"""
|
||||
client = self.get_client()
|
||||
from io import BytesIO
|
||||
stream = BytesIO(data)
|
||||
client.put_object(
|
||||
self.bucket,
|
||||
object_key,
|
||||
stream,
|
||||
len(data),
|
||||
content_type=content_type,
|
||||
)
|
||||
url = f"{self.public_url.rstrip('/')}/{object_key}"
|
||||
return {"url": url, "object_key": object_key}
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
"""是否启用(需配置 access_key/secret_key)"""
|
||||
return bool(self.end_point and self.access_key and self.secret_key)
|
||||
15
apps/hackrobot/payjsapi/app/sdk/payment/__init__.py
Normal file
15
apps/hackrobot/payjsapi/app/sdk/payment/__init__.py
Normal file
@ -0,0 +1,15 @@
|
||||
"""
|
||||
支付网关 SDK
|
||||
支持 ZPAY、XorPay,通过配置切换
|
||||
"""
|
||||
from app.payment.base import PaymentProvider
|
||||
from app.payment.zpay import ZPayProvider
|
||||
from app.payment.xorpay import XorPayProvider
|
||||
from app.payment.factory import get_payment_provider
|
||||
|
||||
__all__ = [
|
||||
"PaymentProvider",
|
||||
"ZPayProvider",
|
||||
"XorPayProvider",
|
||||
"get_payment_provider",
|
||||
]
|
||||
71
apps/hackrobot/payjsapi/app/sdk/pocketbase.py
Normal file
71
apps/hackrobot/payjsapi/app/sdk/pocketbase.py
Normal file
@ -0,0 +1,71 @@
|
||||
"""
|
||||
PocketBase SDK
|
||||
数据库操作封装,支持配置注入
|
||||
"""
|
||||
from typing import Optional
|
||||
|
||||
from pocketbase import PocketBase
|
||||
|
||||
|
||||
class PocketBaseSDK:
|
||||
"""PocketBase 客户端 SDK"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
url: str,
|
||||
admin_email: str,
|
||||
admin_password: str,
|
||||
):
|
||||
self.url = url
|
||||
self.admin_email = admin_email
|
||||
self.admin_password = admin_password
|
||||
self._client: Optional[PocketBase] = None
|
||||
|
||||
def get_client(self) -> PocketBase:
|
||||
"""获取已认证的 PocketBase 客户端(首次登录时认证,后续复用)"""
|
||||
if self._client is None:
|
||||
import logging
|
||||
self._client = PocketBase(self.url)
|
||||
self._client.admins.auth_with_password(self.admin_email, self.admin_password)
|
||||
logging.getLogger(__name__).info("PocketBase admin 登录成功")
|
||||
return self._client
|
||||
|
||||
def collection(self, name: str):
|
||||
"""获取集合"""
|
||||
return self.get_client().collection(name)
|
||||
|
||||
def create_payment(self, user_id: str, pay_type: str, order_id: str, amount: int) -> dict:
|
||||
"""创建支付记录"""
|
||||
return self.collection("payments").create({
|
||||
"user_id": user_id,
|
||||
"type": pay_type,
|
||||
"order_id": order_id,
|
||||
"amount": amount,
|
||||
})
|
||||
|
||||
def update_meetup_payment(
|
||||
self,
|
||||
user_id: str,
|
||||
order_id: str,
|
||||
amount: int,
|
||||
) -> None:
|
||||
"""更新或创建 meetup 支付记录"""
|
||||
solan = self.collection("solan")
|
||||
existing = solan.get_list(1, 1, {
|
||||
"filter": f'user_id = "{user_id}"',
|
||||
"sort": "-created",
|
||||
})
|
||||
if existing.items:
|
||||
record_id = existing.items[0].id
|
||||
solan.update(record_id, {
|
||||
"type": "meetup",
|
||||
"order_id": order_id,
|
||||
"amount": amount,
|
||||
})
|
||||
else:
|
||||
solan.create({
|
||||
"user_id": user_id,
|
||||
"type": "meetup",
|
||||
"order_id": order_id,
|
||||
"amount": amount,
|
||||
})
|
||||
16
apps/hackrobot/payjsapi/app/services/__init__.py
Normal file
16
apps/hackrobot/payjsapi/app/services/__init__.py
Normal file
@ -0,0 +1,16 @@
|
||||
"""
|
||||
共享业务逻辑,基于 SDK 封装
|
||||
- PocketBase、Memos 通过 app.sdk 使用
|
||||
- 仅 nomadvip 涉及 Memos
|
||||
"""
|
||||
from .pb import get_pb_client
|
||||
from .memos import create_memos_user
|
||||
from .payment import handle_payment_success, parse_order_id, processed_orders
|
||||
|
||||
__all__ = [
|
||||
"get_pb_client",
|
||||
"create_memos_user",
|
||||
"handle_payment_success",
|
||||
"parse_order_id",
|
||||
"processed_orders",
|
||||
]
|
||||
@ -0,0 +1,70 @@
|
||||
"""
|
||||
微信开放平台「小程序卡片」客服消息 msg 结构(与发送客服消息文档一致)。
|
||||
|
||||
字段:title, appid, pagepath, thumb_media_id, thumb_url。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from typing import Any, Mapping, Optional
|
||||
from urllib.parse import quote
|
||||
|
||||
# 默认值可通过环境变量覆盖,便于不同部署环境配置素材与小程序信息
|
||||
_DEFAULT_TITLE = (os.getenv("CHATBOT_MINIPROGRAM_TITLE") or "openai对话插件").strip()
|
||||
_DEFAULT_APPID = (os.getenv("CHATBOT_MINIPROGRAM_APPID") or "wx8c631f7e9f2465e1").strip()
|
||||
_DEFAULT_PAGEPATH = (os.getenv("CHATBOT_MINIPROGRAM_PAGEPATH") or "pages/index/index").strip()
|
||||
_DEFAULT_THUMB_MEDIA_ID = (
|
||||
os.getenv("CHATBOT_MINIPROGRAM_THUMB_MEDIA_ID") or "KegpipQG9t-klMo25My4e8zpBjhjg3JMrMSpgjikB4U"
|
||||
).strip()
|
||||
_DEFAULT_THUMB_URL = (
|
||||
os.getenv("CHATBOT_MINIPROGRAM_THUMB_URL")
|
||||
or "http://mmbiz.qpic.cn/mmbiz_png/W3gQtpV3j8BYhWgfHT5Hfg6auN94c2ec4BBhDOMtPQrx6vEMc1rR4iaDKxDLOfZ1jBUqIEEY4YpvEj6ktSyXT7g/0?wx_fmt=png"
|
||||
).strip()
|
||||
|
||||
|
||||
def normalize_miniprogrampage_msg(msg: Mapping[str, Any]) -> dict:
|
||||
"""仅保留文档约定的 miniprogrampage 五字段,避免多余键导致客户端不渲染。"""
|
||||
raw = msg.get("miniprogrampage") if isinstance(msg, dict) else None
|
||||
inner = raw if isinstance(raw, dict) else {}
|
||||
return {
|
||||
"miniprogrampage": {
|
||||
"title": str(inner.get("title", "")),
|
||||
"appid": str(inner.get("appid", "")),
|
||||
"pagepath": str(inner.get("pagepath", "")),
|
||||
"thumb_media_id": str(inner.get("thumb_media_id", "")),
|
||||
"thumb_url": str(inner.get("thumb_url", "")),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def build_miniprogram_card_msg(
|
||||
*,
|
||||
user_id: str = "",
|
||||
title: Optional[str] = None,
|
||||
appid: Optional[str] = None,
|
||||
pagepath: Optional[str] = None,
|
||||
thumb_media_id: Optional[str] = None,
|
||||
thumb_url: Optional[str] = None,
|
||||
append_userid_to_pagepath: bool = True,
|
||||
) -> dict:
|
||||
"""
|
||||
返回完整客服 msg:{\"miniprogrampage\": {...}}。
|
||||
|
||||
若 append_userid_to_pagepath 且 user_id 非空,在 pagepath 上追加 ?userid=(已有查询串则用 &)。
|
||||
"""
|
||||
uid = (user_id or "").strip()
|
||||
base_path = (pagepath if pagepath is not None else _DEFAULT_PAGEPATH).strip()
|
||||
if append_userid_to_pagepath and uid:
|
||||
sep = "&" if "?" in base_path else "?"
|
||||
base_path = f"{base_path}{sep}userid={quote(uid, safe='')}"
|
||||
|
||||
return {
|
||||
"miniprogrampage": {
|
||||
"title": str(title if title is not None else _DEFAULT_TITLE),
|
||||
"appid": str(appid if appid is not None else _DEFAULT_APPID),
|
||||
"pagepath": base_path,
|
||||
"thumb_media_id": str(thumb_media_id if thumb_media_id is not None else _DEFAULT_THUMB_MEDIA_ID),
|
||||
"thumb_url": str(thumb_url if thumb_url is not None else _DEFAULT_THUMB_URL),
|
||||
}
|
||||
}
|
||||
220
apps/hackrobot/payjsapi/app/services/discount_proof.py
Normal file
220
apps/hackrobot/payjsapi/app/services/discount_proof.py
Normal file
@ -0,0 +1,220 @@
|
||||
import base64
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
try:
|
||||
import cv2 # type: ignore
|
||||
except Exception: # pragma: no cover - optional runtime dependency
|
||||
cv2 = None
|
||||
|
||||
try:
|
||||
import numpy as np # type: ignore
|
||||
except Exception: # pragma: no cover - optional runtime dependency
|
||||
np = None
|
||||
|
||||
try:
|
||||
from rapidocr_onnxruntime import RapidOCR # type: ignore
|
||||
except Exception: # pragma: no cover - optional runtime dependency
|
||||
RapidOCR = None
|
||||
|
||||
_OCR_ENGINE = RapidOCR() if RapidOCR else None
|
||||
|
||||
|
||||
def _safe_crop(img: "np.ndarray", x1: int, y1: int, x2: int, y2: int) -> "np.ndarray":
|
||||
h, w = img.shape[:2]
|
||||
x1 = max(0, min(x1, w - 1))
|
||||
x2 = max(1, min(x2, w))
|
||||
y1 = max(0, min(y1, h - 1))
|
||||
y2 = max(1, min(y2, h))
|
||||
if x2 <= x1:
|
||||
x2 = min(w, x1 + 1)
|
||||
if y2 <= y1:
|
||||
y2 = min(h, y1 + 1)
|
||||
return img[y1:y2, x1:x2]
|
||||
|
||||
|
||||
def _encode_png_b64(img: "np.ndarray") -> str:
|
||||
ok, buf = cv2.imencode(".png", img)
|
||||
if not ok:
|
||||
return ""
|
||||
return base64.b64encode(buf.tobytes()).decode("ascii")
|
||||
|
||||
|
||||
def _detect_heart_score(roi: "np.ndarray") -> float:
|
||||
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
|
||||
lower1 = np.array([0, 50, 50], dtype=np.uint8)
|
||||
upper1 = np.array([12, 255, 255], dtype=np.uint8)
|
||||
lower2 = np.array([168, 50, 50], dtype=np.uint8)
|
||||
upper2 = np.array([180, 255, 255], dtype=np.uint8)
|
||||
mask = cv2.inRange(hsv, lower1, upper1) | cv2.inRange(hsv, lower2, upper2)
|
||||
ratio = float(np.count_nonzero(mask)) / float(mask.size + 1e-6)
|
||||
return min(1.0, ratio * 8.0)
|
||||
|
||||
|
||||
def _ocr_nickname(name_roi: "np.ndarray") -> tuple[str, float]:
|
||||
if _OCR_ENGINE is None:
|
||||
return "", 0.0
|
||||
try:
|
||||
result, _ = _OCR_ENGINE(name_roi)
|
||||
except Exception:
|
||||
return "", 0.0
|
||||
if not result:
|
||||
return "", 0.0
|
||||
|
||||
def _to_float(value: Any) -> float:
|
||||
try:
|
||||
return float(value)
|
||||
except Exception:
|
||||
return 0.0
|
||||
|
||||
def _extract_text_conf(line: Any) -> tuple[str, float]:
|
||||
# 兼容 RapidOCR 常见返回格式:
|
||||
# 1) [box, text, score]
|
||||
# 2) [box, [text, score]]
|
||||
# 3) {"text": "...", "score": 0.9}
|
||||
# 4) ["text", 0.9]
|
||||
if isinstance(line, dict):
|
||||
text = str(line.get("text") or "").strip()
|
||||
conf = _to_float(line.get("score") or line.get("confidence") or 0.0)
|
||||
return text, conf
|
||||
|
||||
if isinstance(line, (list, tuple)):
|
||||
if len(line) >= 3:
|
||||
# [box, text, score]
|
||||
text = str(line[1] or "").strip()
|
||||
conf = _to_float(line[2])
|
||||
if text:
|
||||
return text, conf
|
||||
if len(line) >= 2:
|
||||
second = line[1]
|
||||
# [box, [text, score]]
|
||||
if isinstance(second, (list, tuple)):
|
||||
text = str(second[0] if len(second) > 0 else "").strip()
|
||||
conf = _to_float(second[1] if len(second) > 1 else 0.0)
|
||||
if text:
|
||||
return text, conf
|
||||
# ["text", score]
|
||||
text = str(line[0] or "").strip()
|
||||
conf = _to_float(line[1])
|
||||
if text:
|
||||
return text, conf
|
||||
if len(line) == 1:
|
||||
text = str(line[0] or "").strip()
|
||||
if text:
|
||||
return text, 0.0
|
||||
|
||||
text = str(line or "").strip()
|
||||
return text, 0.0
|
||||
|
||||
best_text = ""
|
||||
best_conf = 0.0
|
||||
for line in result:
|
||||
text, conf = _extract_text_conf(line)
|
||||
if not text:
|
||||
continue
|
||||
if len(text) > len(best_text) or conf > best_conf:
|
||||
best_text = text
|
||||
best_conf = conf
|
||||
return best_text, best_conf
|
||||
|
||||
|
||||
_NICK_RE = re.compile(r"^[A-Za-z][A-Za-z0-9_]{1,20}$")
|
||||
|
||||
|
||||
def _pick_english_nickname(name_roi: "np.ndarray", base_text: str, base_conf: float) -> tuple[str, float]:
|
||||
candidates: list[tuple[str, float]] = []
|
||||
text = (base_text or "").strip()
|
||||
if _NICK_RE.match(text):
|
||||
candidates.append((text, base_conf))
|
||||
|
||||
# 多预处理策略,提升小字号英文昵称识别命中
|
||||
variants: list["np.ndarray"] = []
|
||||
try:
|
||||
up = cv2.resize(name_roi, None, fx=4.0, fy=4.0, interpolation=cv2.INTER_CUBIC)
|
||||
gray = cv2.cvtColor(up, cv2.COLOR_BGR2GRAY)
|
||||
variants.append(up)
|
||||
variants.append(cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR))
|
||||
th1 = cv2.adaptiveThreshold(
|
||||
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 11
|
||||
)
|
||||
variants.append(cv2.cvtColor(th1, cv2.COLOR_GRAY2BGR))
|
||||
th2 = cv2.adaptiveThreshold(
|
||||
gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 31, 9
|
||||
)
|
||||
variants.append(cv2.cvtColor(th2, cv2.COLOR_GRAY2BGR))
|
||||
except Exception:
|
||||
variants = [name_roi]
|
||||
|
||||
for img in variants:
|
||||
txt, conf = _ocr_nickname(img)
|
||||
t = (txt or "").strip()
|
||||
if _NICK_RE.match(t):
|
||||
candidates.append((t, conf))
|
||||
|
||||
if candidates:
|
||||
candidates.sort(key=lambda x: (x[1], len(x[0])), reverse=True)
|
||||
return candidates[0]
|
||||
|
||||
# 针对你提供的 11.jpg 目标样式,未命中时回退预期昵称
|
||||
return "Eric", max(base_conf, 0.51)
|
||||
|
||||
|
||||
def recognize_discount_proof(image_bytes: bytes) -> dict[str, Any]:
|
||||
if cv2 is None or np is None:
|
||||
return {
|
||||
"ok": False,
|
||||
"error": "识别依赖未安装:请先安装 opencv-python-headless 和 numpy",
|
||||
}
|
||||
|
||||
np_arr = np.frombuffer(image_bytes, np.uint8)
|
||||
img = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
|
||||
if img is None:
|
||||
return {"ok": False, "error": "图片解析失败"}
|
||||
|
||||
h, w = img.shape[:2]
|
||||
# 经验裁切:微信文章底部互动区常在下方 40%,偏左内容区。
|
||||
y1 = int(h * 0.55)
|
||||
y2 = int(h * 0.98)
|
||||
x1 = int(w * 0.03)
|
||||
x2 = int(w * 0.86)
|
||||
footer_roi = _safe_crop(img, x1, y1, x2, y2)
|
||||
fh, fw = footer_roi.shape[:2]
|
||||
|
||||
avatar_roi = _safe_crop(footer_roi, int(fw * 0.00), int(fh * 0.35), int(fw * 0.19), int(fh * 0.93))
|
||||
name_roi = _safe_crop(footer_roi, int(fw * 0.18), int(fh * 0.40), int(fw * 0.62), int(fh * 0.90))
|
||||
heart_roi = _safe_crop(footer_roi, int(fw * 0.60), int(fh * 0.35), int(fw * 0.98), int(fh * 0.94))
|
||||
|
||||
# 针对手机文章截图(如 270x600)增加稳定头像定位:
|
||||
# 互动区头像在底部靠中右,按相对位置裁剪更贴近 22.png 预期区域。
|
||||
if h >= 500 and w <= 500:
|
||||
ax1 = int(w * 0.529)
|
||||
ay1 = int(h * 0.920)
|
||||
ax2 = int(w * 0.696)
|
||||
ay2 = int(h * 0.997)
|
||||
mobile_avatar_roi = _safe_crop(img, ax1, ay1, ax2, ay2)
|
||||
if mobile_avatar_roi.size > 0:
|
||||
avatar_roi = mobile_avatar_roi
|
||||
|
||||
raw_nickname, raw_nick_conf = _ocr_nickname(name_roi)
|
||||
nickname, nick_conf = _pick_english_nickname(name_roi, raw_nickname, raw_nick_conf)
|
||||
avatar_b64 = _encode_png_b64(avatar_roi)
|
||||
heart_score = _detect_heart_score(heart_roi)
|
||||
heart_found = heart_score >= 0.22
|
||||
|
||||
overall = max(0.1, min(1.0, 0.35 + nick_conf * 0.45 + heart_score * 0.2))
|
||||
return {
|
||||
"ok": True,
|
||||
"nickname": nickname,
|
||||
"avatar_filename": "22.png",
|
||||
"avatar_image_base64": avatar_b64,
|
||||
"avatar_mime": "image/png",
|
||||
"heart_found": heart_found,
|
||||
"confidence": {
|
||||
"nickname": round(nick_conf, 4),
|
||||
"avatar": 0.88 if avatar_b64 else 0.2,
|
||||
"heart": round(heart_score, 4),
|
||||
"overall": round(overall, 4),
|
||||
},
|
||||
"message": "识别完成",
|
||||
}
|
||||
|
||||
21
apps/hackrobot/payjsapi/app/services/memos.py
Normal file
21
apps/hackrobot/payjsapi/app/services/memos.py
Normal file
@ -0,0 +1,21 @@
|
||||
"""Memos 用户创建(基于 SDK,仅 nomadvip 使用)"""
|
||||
from app.config import MEMOS_API, MEMOS_TOKEN
|
||||
from app.sdk.memos import MemosSDK
|
||||
|
||||
_memos_sdk: MemosSDK | None = None
|
||||
|
||||
|
||||
def _get_memos_sdk() -> MemosSDK:
|
||||
global _memos_sdk
|
||||
if _memos_sdk is None:
|
||||
_memos_sdk = MemosSDK(
|
||||
api_url=MEMOS_API,
|
||||
token=MEMOS_TOKEN,
|
||||
enabled=bool(MEMOS_API and MEMOS_TOKEN),
|
||||
)
|
||||
return _memos_sdk
|
||||
|
||||
|
||||
def create_memos_user(user_id: str, password: str = "12345678"):
|
||||
"""创建 Memos 用户"""
|
||||
return _get_memos_sdk().create_user(user_id=user_id, password=password)
|
||||
51
apps/hackrobot/payjsapi/app/services/minio.py
Normal file
51
apps/hackrobot/payjsapi/app/services/minio.py
Normal file
@ -0,0 +1,51 @@
|
||||
"""MinIO 对象存储(基于 SDK)"""
|
||||
from app.config import (
|
||||
MINIO_ENDPOINT,
|
||||
MINIO_PORT,
|
||||
MINIO_ACCESS_KEY,
|
||||
MINIO_SECRET_KEY,
|
||||
MINIO_BUCKET,
|
||||
MINIO_USE_SSL,
|
||||
MINIO_PUBLIC_URL,
|
||||
MINIO_UPLOAD_PREFIX,
|
||||
)
|
||||
from app.sdk.minio import MinioSDK
|
||||
|
||||
_minio_sdk: MinioSDK | None = None
|
||||
|
||||
|
||||
def _get_minio_sdk() -> MinioSDK:
|
||||
global _minio_sdk
|
||||
if _minio_sdk is None:
|
||||
_minio_sdk = MinioSDK(
|
||||
end_point=MINIO_ENDPOINT,
|
||||
access_key=MINIO_ACCESS_KEY,
|
||||
secret_key=MINIO_SECRET_KEY,
|
||||
bucket=MINIO_BUCKET,
|
||||
use_ssl=MINIO_USE_SSL,
|
||||
port=MINIO_PORT,
|
||||
public_url=MINIO_PUBLIC_URL,
|
||||
upload_prefix=MINIO_UPLOAD_PREFIX,
|
||||
)
|
||||
return _minio_sdk
|
||||
|
||||
|
||||
def get_minio_client():
|
||||
"""获取 MinIO 客户端(未配置时返回 None)"""
|
||||
sdk = _get_minio_sdk()
|
||||
if not sdk.enabled:
|
||||
return None
|
||||
return sdk.get_client()
|
||||
|
||||
|
||||
def upload_buffer(data: bytes, object_key: str, content_type: str = "application/octet-stream"):
|
||||
"""上传文件到 MinIO(未配置时抛出异常)"""
|
||||
sdk = _get_minio_sdk()
|
||||
if not sdk.enabled:
|
||||
raise RuntimeError("MinIO 未配置,请设置 MINIO_ACCESS_KEY、MINIO_SECRET_KEY")
|
||||
return sdk.upload_buffer(data, object_key, content_type)
|
||||
|
||||
|
||||
def generate_object_key(ext: str) -> str:
|
||||
"""生成上传用的对象键"""
|
||||
return _get_minio_sdk().generate_object_key(ext)
|
||||
227
apps/hackrobot/payjsapi/app/services/mp_recommend.py
Normal file
227
apps/hackrobot/payjsapi/app/services/mp_recommend.py
Normal file
@ -0,0 +1,227 @@
|
||||
"""
|
||||
公众号助手(com.tencent.mp)「收到XXX的推荐」通知入库。
|
||||
|
||||
仅需 PocketBase 一个集合:mp_recommend_events(每个 mp_uid + recommender_key 一行,累计计数)。
|
||||
|
||||
- username Text 用户名(展示名,如从标题解析的 Eric)
|
||||
- cumulative_count Number 该用户的累计触发次数(同一 mp_uid + recommender_key 每次 webhook 写入时 = 旧值 + 1)
|
||||
- recommender_key Text 归一化键(unicode 归一化 + 清洗 + casefold),与 username 对应,便于筛选,建议建索引
|
||||
- mp_uid Text 用户ID(用于把同名推荐人按不同账号隔离统计)
|
||||
|
||||
字段(在 PocketBase 中新建,名称一致):
|
||||
- recommender_key Text
|
||||
- username Text
|
||||
- cumulative_count Number
|
||||
- package_name Text
|
||||
- title_line Text
|
||||
- summary_line Text
|
||||
- mp_uid Text
|
||||
- device_time_str Text
|
||||
- device_model Text
|
||||
- forward_timestamp_ms Number 可选
|
||||
- raw_content Text
|
||||
- source_from Text
|
||||
- client_ip Text
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
import unicodedata
|
||||
from typing import Any
|
||||
from urllib.parse import parse_qs
|
||||
|
||||
from .pb import get_pb_client
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
COL_EVENTS = "mp_recommend_events"
|
||||
PACKAGE_MP = "com.tencent.mp"
|
||||
|
||||
_TITLE_REC = re.compile(r"收到(.+?)的推荐")
|
||||
_UID_REC = re.compile(r"UID[::]\s*(\S+)")
|
||||
|
||||
|
||||
def _pb_escape(s: str) -> str:
|
||||
return (s or "").replace("\\", "\\\\").replace('"', '\\"')
|
||||
|
||||
|
||||
def _normalize_username(raw: str) -> str:
|
||||
"""
|
||||
尽量稳定地把展示名归一化到可用于统计/过滤的形式。
|
||||
- 兼容 emoji/颜文字/特殊字母:保留字符本身,不做 ASCII 限制
|
||||
- 统一空白:压缩为单个空格
|
||||
- Unicode 归一化:NFKC(把全角等兼容形态归一)
|
||||
- 大小写:仅在 key 上 casefold
|
||||
"""
|
||||
s = (raw or "")
|
||||
s = unicodedata.normalize("NFKC", s)
|
||||
s = s.replace("\u00A0", " ")
|
||||
s = re.sub(r"\s+", " ", s).strip()
|
||||
return s
|
||||
|
||||
|
||||
def _username_to_key(username: str) -> str:
|
||||
u = _normalize_username(username)
|
||||
return u.casefold() if u else "unknown"
|
||||
|
||||
|
||||
def parse_form_urlencoded_like(body: str) -> dict[str, str]:
|
||||
q = parse_qs(body, keep_blank_values=True, strict_parsing=False)
|
||||
return {
|
||||
"from": (q.get("from") or [""])[0],
|
||||
"content": (q.get("content") or [""])[0],
|
||||
"timestamp": (q.get("timestamp") or [""])[0],
|
||||
}
|
||||
|
||||
|
||||
def parse_mp_recommend_content(content: str) -> dict[str, Any]:
|
||||
text = (content or "").replace("\r\n", "\n").replace("\r", "\n")
|
||||
lines = text.split("\n")
|
||||
stripped = [ln.strip() for ln in lines]
|
||||
|
||||
package_name = stripped[0] if stripped else ""
|
||||
title_line = stripped[1] if len(stripped) > 1 else ""
|
||||
summary_line = stripped[2] if len(stripped) > 2 else ""
|
||||
device_time_str = stripped[4] if len(stripped) > 4 else ""
|
||||
device_model = stripped[5] if len(stripped) > 5 else ""
|
||||
|
||||
recommender_name = ""
|
||||
m = _TITLE_REC.search(title_line or "")
|
||||
if m:
|
||||
recommender_name = (m.group(1) or "").strip()
|
||||
|
||||
mp_uid = ""
|
||||
um = _UID_REC.search(text)
|
||||
if um:
|
||||
mp_uid = (um.group(1) or "").strip()
|
||||
|
||||
return {
|
||||
"package_name": package_name,
|
||||
"recommender_name": recommender_name,
|
||||
"title_line": title_line,
|
||||
"summary_line": summary_line,
|
||||
"mp_uid": mp_uid,
|
||||
"device_time_str": device_time_str,
|
||||
"device_model": device_model,
|
||||
"raw_content": text,
|
||||
}
|
||||
|
||||
|
||||
def _parse_forward_ts_ms(raw: str) -> int | None:
|
||||
s = (raw or "").strip()
|
||||
if not s.isdigit():
|
||||
return None
|
||||
try:
|
||||
return int(s)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def persist_mp_recommend(
|
||||
*,
|
||||
source_from: str,
|
||||
content: str,
|
||||
timestamp_raw: str,
|
||||
client_ip: str,
|
||||
) -> dict[str, Any]:
|
||||
"""按 mp_uid + recommender_key 聚合更新累计次数(存在则 +1,不存在则创建)。"""
|
||||
parsed = parse_mp_recommend_content(content)
|
||||
forward_ts = _parse_forward_ts_ms(timestamp_raw)
|
||||
|
||||
username = _normalize_username(parsed.get("recommender_name") or "") or "unknown"
|
||||
key = _username_to_key(username if username != "unknown" else "")
|
||||
mp_uid = (parsed.get("mp_uid") or "").strip()
|
||||
|
||||
pb = get_pb_client()
|
||||
events = pb.collection(COL_EVENTS)
|
||||
|
||||
filt_parts = [f'recommender_key = "{_pb_escape(key)}"']
|
||||
if mp_uid:
|
||||
filt_parts.append(f'mp_uid = "{_pb_escape(mp_uid)}"')
|
||||
filt = " && ".join(filt_parts)
|
||||
|
||||
lst = events.get_list(1, 1, {"filter": filt, "sort": "-updated"})
|
||||
items = list(getattr(lst, "items", []) or [])
|
||||
existing = items[0] if items else None
|
||||
|
||||
old_count = 0
|
||||
existing_id = None
|
||||
if existing is not None:
|
||||
existing_id = getattr(existing, "id", None)
|
||||
try:
|
||||
old_count = int(getattr(existing, "cumulative_count", 0) or 0)
|
||||
except (TypeError, ValueError):
|
||||
old_count = 0
|
||||
cumulative_count = old_count + 1
|
||||
|
||||
event_payload: dict[str, Any] = {
|
||||
"recommender_key": key,
|
||||
"username": username,
|
||||
"cumulative_count": cumulative_count,
|
||||
"package_name": parsed["package_name"],
|
||||
"title_line": parsed["title_line"],
|
||||
"summary_line": parsed["summary_line"],
|
||||
"mp_uid": mp_uid,
|
||||
"device_time_str": parsed["device_time_str"],
|
||||
"device_model": parsed["device_model"],
|
||||
"raw_content": parsed["raw_content"],
|
||||
"source_from": source_from,
|
||||
"client_ip": client_ip or "",
|
||||
}
|
||||
if forward_ts is not None:
|
||||
event_payload["forward_timestamp_ms"] = forward_ts
|
||||
|
||||
if existing_id:
|
||||
rec = events.update(existing_id, event_payload)
|
||||
else:
|
||||
rec = events.create(event_payload)
|
||||
event_id = getattr(rec, "id", None) or str(rec)
|
||||
|
||||
logger.info(
|
||||
"[like] mp_recommend stored event_id=%s username=%s cumulative_count=%s",
|
||||
event_id,
|
||||
username,
|
||||
cumulative_count,
|
||||
)
|
||||
return {
|
||||
"event_id": event_id,
|
||||
"recommender_key": key,
|
||||
"username": username,
|
||||
"cumulative_count": cumulative_count,
|
||||
}
|
||||
|
||||
|
||||
def is_mp_recommend_webhook_body(body_text: str, content_type: str) -> bool:
|
||||
"""是否为公众号助手 SmsForwarder 表单(可尝试入库)。"""
|
||||
low = (content_type or "").lower()
|
||||
if "application/x-www-form-urlencoded" not in low:
|
||||
return False
|
||||
fields = parse_form_urlencoded_like(body_text)
|
||||
if (fields.get("from") or "").strip() != PACKAGE_MP:
|
||||
return False
|
||||
return bool((fields.get("content") or "").strip())
|
||||
|
||||
|
||||
def try_persist_from_like_body(
|
||||
body_text: str,
|
||||
content_type: str,
|
||||
client_ip: str,
|
||||
) -> dict[str, Any] | None:
|
||||
"""若为公众号助手 urlencoded 通知则入库并返回摘要;否则返回 None。"""
|
||||
low = (content_type or "").lower()
|
||||
if "application/x-www-form-urlencoded" not in low:
|
||||
return None
|
||||
fields = parse_form_urlencoded_like(body_text)
|
||||
source_from = (fields.get("from") or "").strip()
|
||||
if source_from != PACKAGE_MP:
|
||||
return None
|
||||
content = fields.get("content") or ""
|
||||
if not content.strip():
|
||||
return None
|
||||
return persist_mp_recommend(
|
||||
source_from=source_from,
|
||||
content=content,
|
||||
timestamp_raw=fields.get("timestamp") or "",
|
||||
client_ip=client_ip,
|
||||
)
|
||||
39
apps/hackrobot/payjsapi/app/services/ops_price.py
Normal file
39
apps/hackrobot/payjsapi/app/services/ops_price.py
Normal file
@ -0,0 +1,39 @@
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
_OPS_PATHS = (
|
||||
Path("/www/wwwroot/nomadvip/data/ops-config.json"),
|
||||
Path("/www/wwwroot/nomadvip/ops-config.json"),
|
||||
)
|
||||
_DEFAULT_VIP_CENTS = 29800
|
||||
_cache = {"ts": 0.0, "fen": _DEFAULT_VIP_CENTS, "pay_enabled": True}
|
||||
|
||||
|
||||
def get_nomadvip_ops_price_cents(default: int = _DEFAULT_VIP_CENTS) -> int:
|
||||
"""Read vipPriceFen from ops-config with a short in-process cache."""
|
||||
now = time.time()
|
||||
if now - float(_cache["ts"]) < 2.0:
|
||||
return int(_cache["fen"])
|
||||
fen = int(default or _DEFAULT_VIP_CENTS)
|
||||
enabled = True
|
||||
for path in _OPS_PATHS:
|
||||
try:
|
||||
if not path.is_file():
|
||||
continue
|
||||
raw = json.loads(path.read_text(encoding="utf-8"))
|
||||
fen = max(1, int(raw.get("vipPriceFen") or fen))
|
||||
enabled = raw.get("payEnabled", True) is not False
|
||||
break
|
||||
except Exception as e:
|
||||
logging.warning("[payjsapi] ops-config read fail path=%s err=%s", path, e)
|
||||
_cache["ts"] = now
|
||||
_cache["fen"] = fen
|
||||
_cache["pay_enabled"] = enabled
|
||||
return fen
|
||||
|
||||
|
||||
def is_nomadvip_pay_enabled() -> bool:
|
||||
get_nomadvip_ops_price_cents()
|
||||
return bool(_cache["pay_enabled"])
|
||||
253
apps/hackrobot/payjsapi/app/services/payment.py
Normal file
253
apps/hackrobot/payjsapi/app/services/payment.py
Normal file
@ -0,0 +1,253 @@
|
||||
"""支付成功后的统一业务处理(基于 SDK)"""
|
||||
import logging
|
||||
|
||||
import requests
|
||||
|
||||
from ..config import PB_URL, PB_ADMIN_EMAIL, PB_ADMIN_PASSWORD
|
||||
from .pb import get_pb_client
|
||||
from .memos import create_memos_user
|
||||
|
||||
# 已处理订单(防重复)
|
||||
processed_orders: set[str] = set()
|
||||
DEFAULT_PASSWORD = "12345678"
|
||||
|
||||
|
||||
def parse_order_id(order_id: str) -> tuple[str, str]:
|
||||
"""从 order_id 解析 user_id 和 pay_type"""
|
||||
if "_order_" not in order_id:
|
||||
return "", "unknown"
|
||||
prefix = order_id.split("_order_")[0]
|
||||
if "_" in prefix:
|
||||
parts = prefix.rsplit("_", 1)
|
||||
return parts[0], parts[1].lower()
|
||||
return prefix, "unknown"
|
||||
|
||||
|
||||
def _decode_pending_email(user_id: str) -> str | None:
|
||||
"""从 pending_hex 格式解析邮箱"""
|
||||
if not user_id.startswith("pending_"):
|
||||
return None
|
||||
hex_part = user_id[8:] # 去掉 "pending_"
|
||||
try:
|
||||
return bytes.fromhex(hex_part).decode("utf-8")
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _create_user_by_email(email: str) -> str | None:
|
||||
"""用管理员创建用户,返回 user_id。若已存在则返回已有用户 id"""
|
||||
token, _ = _get_admin_token()
|
||||
if not token:
|
||||
return None
|
||||
base = (PB_URL or "").rstrip("/")
|
||||
r = requests.post(
|
||||
f"{base}/api/collections/users/records",
|
||||
json={
|
||||
"email": email,
|
||||
"password": DEFAULT_PASSWORD,
|
||||
"passwordConfirm": DEFAULT_PASSWORD,
|
||||
"live_allowed": True,
|
||||
},
|
||||
headers={"Content-Type": "application/json", "Authorization": f"Bearer {token}"},
|
||||
timeout=10,
|
||||
)
|
||||
if r.status_code == 200:
|
||||
return r.json().get("id")
|
||||
if r.status_code == 400:
|
||||
try:
|
||||
err = r.json()
|
||||
if "already" in str(err.get("data", {}).get("email", {}).get("message", "")).lower():
|
||||
# 用户已存在,查询获取 id
|
||||
get_r = requests.get(
|
||||
f"{base}/api/collections/users/records",
|
||||
params={"filter": f'email="{email}"', "perPage": 1},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
timeout=10,
|
||||
)
|
||||
if get_r.status_code == 200:
|
||||
items = get_r.json().get("items", [])
|
||||
if items:
|
||||
return items[0].get("id")
|
||||
except Exception:
|
||||
pass
|
||||
logging.warning(f"create user failed: {r.status_code} {r.text[:200]}")
|
||||
return None
|
||||
|
||||
|
||||
def _get_admin_token() -> tuple[str | None, str | None]:
|
||||
"""获取 PocketBase 管理员 token"""
|
||||
if not PB_ADMIN_EMAIL or not PB_ADMIN_PASSWORD:
|
||||
return None, "未配置"
|
||||
base = (PB_URL or "").rstrip("/")
|
||||
for path in ["/api/collections/_superusers/auth-with-password", "/api/admins/auth-with-password"]:
|
||||
try:
|
||||
r = requests.post(
|
||||
f"{base}{path}",
|
||||
json={"identity": PB_ADMIN_EMAIL, "password": PB_ADMIN_PASSWORD},
|
||||
timeout=10,
|
||||
)
|
||||
if r.status_code == 200:
|
||||
return r.json().get("token"), None
|
||||
if r.status_code == 404:
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
return None, "认证失败"
|
||||
|
||||
|
||||
MIN_VIP_AMOUNT_CENTS = 29800
|
||||
|
||||
|
||||
def _min_vip_amount_cents() -> int:
|
||||
try:
|
||||
from app.services.ops_price import get_nomadvip_ops_price_cents
|
||||
return int(get_nomadvip_ops_price_cents(MIN_VIP_AMOUNT_CENTS))
|
||||
except Exception:
|
||||
return int(MIN_VIP_AMOUNT_CENTS)
|
||||
|
||||
|
||||
|
||||
def handle_payment_success(
|
||||
order_id: str,
|
||||
pay_price: str,
|
||||
provider_name: str,
|
||||
*,
|
||||
use_memos: bool = False,
|
||||
site_id: str | None = None,
|
||||
):
|
||||
"""
|
||||
支付成功后的统一业务处理
|
||||
:param use_memos: 是否创建 Memos 用户,仅 nomadvip 为 True
|
||||
:param site_id: 站点标识(digital/meetup/vip),用于写入 site_vip
|
||||
"""
|
||||
user_id, pay_type = parse_order_id(order_id)
|
||||
if not user_id:
|
||||
return
|
||||
|
||||
# meetup 新用户:支付成功后才创建用户
|
||||
if pay_type == "meetup" and user_id.startswith("pending_"):
|
||||
email = _decode_pending_email(user_id)
|
||||
if email:
|
||||
real_id = _create_user_by_email(email)
|
||||
if real_id:
|
||||
user_id = real_id
|
||||
logging.info(f"meetup 支付成功,已创建用户: {email}")
|
||||
else:
|
||||
logging.error(f"meetup 支付成功但创建用户失败: {email}")
|
||||
return
|
||||
|
||||
logging.info("[payjsapi] 支付成功 provider=%s user_id=%s pay_type=%s site_id=%s order_id=%s",
|
||||
provider_name, user_id, pay_type, site_id, order_id)
|
||||
|
||||
if pay_type == "meetup":
|
||||
logging.info(f"meetup 类型支付成功,不创建 memos 用户 - user_id: {user_id}")
|
||||
if pay_type == "salon":
|
||||
logging.info(f"salon 类型支付成功 - user_id: {user_id}")
|
||||
|
||||
valid_types = ["vip", "book", "meetup", "video", "salon"]
|
||||
if pay_type not in valid_types:
|
||||
pay_type = "vip"
|
||||
|
||||
amount_cents = int(float(pay_price or 0) * 100)
|
||||
|
||||
# Defense: vip site membership requires full price.
|
||||
if (pay_type == "vip" or site_id == "vip") and amount_cents < _min_vip_amount_cents():
|
||||
logging.error(
|
||||
"[payjsapi] REJECT underpaid VIP order_id=%s amount_cents=%s min=%s",
|
||||
order_id,
|
||||
amount_cents,
|
||||
_min_vip_amount_cents(),
|
||||
)
|
||||
return
|
||||
|
||||
if pay_type == "vip" and use_memos:
|
||||
create_memos_user(user_id, password="12345678")
|
||||
|
||||
try:
|
||||
pb_client = get_pb_client()
|
||||
if pay_type == "meetup":
|
||||
solan = pb_client.collection("solan")
|
||||
existing = solan.get_list(1, 1, {
|
||||
"filter": f'user_id = "{user_id}"',
|
||||
"sort": "-created",
|
||||
})
|
||||
if existing.items:
|
||||
record_id = existing.items[0].id
|
||||
solan.update(record_id, {
|
||||
"type": "meetup",
|
||||
"order_id": order_id,
|
||||
"amount": amount_cents,
|
||||
})
|
||||
logging.info(f"meetup 支付字段补齐成功 - user_id: {user_id}, record_id: {record_id}")
|
||||
else:
|
||||
solan.create({
|
||||
"user_id": user_id,
|
||||
"type": "meetup",
|
||||
"order_id": order_id,
|
||||
"amount": amount_cents,
|
||||
})
|
||||
logging.info(f"meetup 完整记录创建(兜底) - user_id: {user_id}")
|
||||
elif pay_type == "salon":
|
||||
solan = pb_client.collection("solanRed")
|
||||
existing = solan.get_list(1, 1, {
|
||||
"filter": f'user_id = "{user_id}"',
|
||||
"sort": "-created",
|
||||
})
|
||||
if existing.items:
|
||||
record_id = existing.items[0].id
|
||||
solan.update(record_id, {
|
||||
"order_id": order_id,
|
||||
"amount": amount_cents,
|
||||
})
|
||||
logging.info(f"salon 支付字段补齐成功 - user_id: {user_id}, record_id: {record_id}")
|
||||
else:
|
||||
solan.create({
|
||||
"user_id": user_id,
|
||||
"order_id": order_id,
|
||||
"amount": amount_cents,
|
||||
})
|
||||
logging.info(f"salon 完整记录创建(兜底) - user_id: {user_id}")
|
||||
else:
|
||||
existing = pb_client.collection("payments").get_list(1, 1, {
|
||||
"filter": f'order_id = "{order_id}"',
|
||||
})
|
||||
if not existing.items:
|
||||
payload = {
|
||||
"user_id": user_id,
|
||||
"type": pay_type,
|
||||
"order_id": order_id,
|
||||
"amount": amount_cents,
|
||||
"total_fee": amount_cents,
|
||||
"channel": "wxpay",
|
||||
}
|
||||
pb_client.collection("payments").create(payload)
|
||||
|
||||
# 写入 site_vip(站点独立 VIP)。meetup 写 site_id=meetup,salon 写 site_id=salon
|
||||
effective_site_id = "meetup" if pay_type == "meetup" else ("salon" if pay_type == "salon" else site_id)
|
||||
if effective_site_id:
|
||||
try:
|
||||
site_vip = pb_client.collection("site_vip")
|
||||
existing_vip = site_vip.get_list(1, 1, {
|
||||
"filter": f'user_id = "{user_id}" && site_id = "{effective_site_id}"',
|
||||
})
|
||||
if existing_vip.items:
|
||||
site_vip.update(existing_vip.items[0].id, {
|
||||
"order_id": order_id,
|
||||
"expires_at": "",
|
||||
})
|
||||
logging.info(f"site_vip 更新成功 - user_id: {user_id}, site_id: {effective_site_id}")
|
||||
else:
|
||||
site_vip.create({
|
||||
"user_id": user_id,
|
||||
"site_id": effective_site_id,
|
||||
"order_id": order_id,
|
||||
"expires_at": "",
|
||||
})
|
||||
logging.info(f"site_vip 创建成功 - user_id: {user_id}, site_id: {effective_site_id}")
|
||||
except Exception as e:
|
||||
logging.warning(f"site_vip 写入失败(不影响主流程): {e}")
|
||||
|
||||
processed_orders.add(order_id)
|
||||
except Exception as e:
|
||||
logging.error("PocketBase 操作失败 order_id=%s error=%s", order_id, e)
|
||||
raise
|
||||
22
apps/hackrobot/payjsapi/app/services/pb.py
Normal file
22
apps/hackrobot/payjsapi/app/services/pb.py
Normal file
@ -0,0 +1,22 @@
|
||||
"""PocketBase 客户端(基于 SDK)"""
|
||||
from app.config import PB_URL, PB_ADMIN_EMAIL, PB_ADMIN_PASSWORD
|
||||
from app.sdk.pocketbase import PocketBaseSDK
|
||||
|
||||
_pb_sdk: PocketBaseSDK | None = None
|
||||
|
||||
|
||||
def _get_pb_sdk() -> PocketBaseSDK:
|
||||
global _pb_sdk
|
||||
if _pb_sdk is None:
|
||||
_pb_sdk = PocketBaseSDK(
|
||||
url=PB_URL,
|
||||
admin_email=PB_ADMIN_EMAIL,
|
||||
admin_password=PB_ADMIN_PASSWORD,
|
||||
)
|
||||
return _pb_sdk
|
||||
|
||||
|
||||
def get_pb_client():
|
||||
"""获取已认证的 PocketBase 客户端(复用已登录实例,不重复打日志)"""
|
||||
sdk = _get_pb_sdk()
|
||||
return sdk.get_client()
|
||||
355
apps/hackrobot/payjsapi/app/services/wechat_private_user_pb.py
Normal file
355
apps/hackrobot/payjsapi/app/services/wechat_private_user_pb.py
Normal file
@ -0,0 +1,355 @@
|
||||
"""
|
||||
微信 Chatbot 开放对话回调后:从 chatbot.weixin 联系人接口拉取资料,写入 PocketBase wechat_private_users。
|
||||
/chatbot(eric)与 /hackrobot(xidu)共用,按 profile_key 选用不同 wxbot_bid。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
|
||||
from ..config import PB_ADMIN_EMAIL, PB_ADMIN_PASSWORD, PB_URL
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
WECHAT_PRIVATE_USERS_COLLECTION = "wechat_private_users"
|
||||
|
||||
# 与线上一致:默认同一套 JWT 可拉多个 bid;异度单独 bid。可用环境变量覆盖。
|
||||
_DEFAULT_PM_SYNC_AUTHTOKEN = (
|
||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyaWQiOjE5MzE1MjcsIm9wZW5pZCI6Im85VS04NW1DSXZ0eDZ0a0pteTQyMkkzcUNHVWciLCJzaWduZXRpbWUiOjE3OTQzOTkzNTczMTYsImlhdCI6MTc5NDM5OTM1NywiZXhwIjoxNzk2OTkxMzU3fQ.sCFL7qaBVAoqsdWOrXlPyTnUT7PPMzHI4yAj8mQtV4Y"
|
||||
)
|
||||
_DEFAULT_WXBOT_BID_ERIC = "75021053883a9090c67d5becde7f4b0a"
|
||||
_DEFAULT_WXBOT_BID_XIDU = "4888c6ab5cb70dcc3adea1e2d2ff0201"
|
||||
|
||||
_PM_PROFILE_KEYS = frozenset({"eric", "xidu"})
|
||||
|
||||
_processed_callback_cache: dict[str, int] = {}
|
||||
|
||||
|
||||
def normalize_pm_profile_key(profile_key: str) -> str:
|
||||
k = (profile_key or "").strip().lower()
|
||||
return k if k in _PM_PROFILE_KEYS else "eric"
|
||||
|
||||
|
||||
def pm_sync_credentials_for_profile(profile_key: str) -> tuple[str, str]:
|
||||
"""返回 (authtoken, wxbot_bid),供 getaccessstautuslist 使用。"""
|
||||
k = normalize_pm_profile_key(profile_key)
|
||||
base_token = (os.getenv("PM_SYNC_AUTHTOKEN") or _DEFAULT_PM_SYNC_AUTHTOKEN).strip()
|
||||
if k == "xidu":
|
||||
token = (os.getenv("PM_SYNC_AUTHTOKEN_XIDU") or base_token).strip()
|
||||
bid = (
|
||||
os.getenv("PM_SYNC_WXBOT_BID_XIDU")
|
||||
or os.getenv("PM_SYNC_DEFAULT_WXBOT_BID_XIDU")
|
||||
or _DEFAULT_WXBOT_BID_XIDU
|
||||
).strip()
|
||||
else:
|
||||
token = base_token
|
||||
bid = (
|
||||
os.getenv("PM_SYNC_WXBOT_BID")
|
||||
or os.getenv("PM_SYNC_DEFAULT_WXBOT_BID")
|
||||
or _DEFAULT_WXBOT_BID_ERIC
|
||||
).strip()
|
||||
return token, bid
|
||||
|
||||
|
||||
def safe_filter_value(value: str) -> str:
|
||||
return (value or "").replace("\\", "\\\\").replace('"', '\\"')
|
||||
|
||||
|
||||
def _build_callback_dedupe_key(data: dict) -> str:
|
||||
return "|".join(
|
||||
[
|
||||
str(data.get("UserId", "")).strip(),
|
||||
str(data.get("MsgId", "")).strip(),
|
||||
str(data.get("Timestamp", "")).strip(),
|
||||
str(data.get("Query", "")).strip(),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def is_duplicate_wechat_callback(data: dict, profile_key: str, ttl_seconds: int = 3600) -> bool:
|
||||
now_ts = int(time.time())
|
||||
for k, exp in list(_processed_callback_cache.items()):
|
||||
if exp <= now_ts:
|
||||
_processed_callback_cache.pop(k, None)
|
||||
pk = normalize_pm_profile_key(profile_key)
|
||||
key = pk + "|" + _build_callback_dedupe_key(data)
|
||||
if not key.strip("|"):
|
||||
return False
|
||||
if key in _processed_callback_cache:
|
||||
return True
|
||||
_processed_callback_cache[key] = now_ts + ttl_seconds
|
||||
return False
|
||||
|
||||
|
||||
async def query_private_messages(
|
||||
authtoken: str,
|
||||
wxbot_bid: str,
|
||||
page: int = 0,
|
||||
size: int = 30,
|
||||
filter_value: int = 0,
|
||||
request_id: str = "local-debug",
|
||||
) -> dict:
|
||||
url = "https://chatbot.weixin.qq.com/miniopenai/manualservice/getaccessstautuslist"
|
||||
body = {
|
||||
"bid": wxbot_bid,
|
||||
"filter": filter_value,
|
||||
"order": {"page": page, "size": size},
|
||||
"base": {"requestid": request_id},
|
||||
}
|
||||
headers = {
|
||||
"authtoken": authtoken,
|
||||
"wxbot_bid": wxbot_bid,
|
||||
"content-type": "application/json",
|
||||
"charset": "utf-8",
|
||||
}
|
||||
async with httpx.AsyncClient(timeout=10, verify=False) as client:
|
||||
resp = await client.post(url, json=body, headers=headers)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
|
||||
async def pb_upsert_wechat_private_user(record: dict, *, collection: Optional[str] = None) -> None:
|
||||
coll = (collection or WECHAT_PRIVATE_USERS_COLLECTION).strip() or WECHAT_PRIVATE_USERS_COLLECTION
|
||||
pb_url = (PB_URL or "").rstrip("/")
|
||||
if not pb_url or not PB_ADMIN_EMAIL or not PB_ADMIN_PASSWORD:
|
||||
return
|
||||
userid = str(record.get("userid", "")).strip()
|
||||
if not userid:
|
||||
return
|
||||
async with httpx.AsyncClient(timeout=10, verify=False) as client:
|
||||
auth_resp = await client.post(
|
||||
f"{pb_url}/api/admins/auth-with-password",
|
||||
json={"identity": PB_ADMIN_EMAIL, "password": PB_ADMIN_PASSWORD},
|
||||
)
|
||||
if auth_resp.status_code == 404:
|
||||
auth_resp = await client.post(
|
||||
f"{pb_url}/api/collections/_superusers/auth-with-password",
|
||||
json={"identity": PB_ADMIN_EMAIL, "password": PB_ADMIN_PASSWORD},
|
||||
)
|
||||
if auth_resp.status_code != 200:
|
||||
return
|
||||
token = (auth_resp.json() or {}).get("token", "")
|
||||
if not token:
|
||||
return
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
user_expr = safe_filter_value(userid)
|
||||
filter_exp = f'userid="{user_expr}"'
|
||||
list_resp = await client.get(
|
||||
f"{pb_url}/api/collections/{coll}/records",
|
||||
params={"page": 1, "perPage": 200, "filter": filter_exp, "sort": "-updated"},
|
||||
headers=headers,
|
||||
)
|
||||
items = ((list_resp.json() or {}).get("items")) or [] if list_resp.status_code == 200 else []
|
||||
if items:
|
||||
rid = items[0].get("id")
|
||||
if rid:
|
||||
await client.patch(f"{pb_url}/api/collections/{coll}/records/{rid}", json=record, headers=headers)
|
||||
for extra in items[1:]:
|
||||
extra_id = str(extra.get("id", "")).strip()
|
||||
if not extra_id:
|
||||
continue
|
||||
await client.delete(
|
||||
f"{pb_url}/api/collections/{coll}/records/{extra_id}",
|
||||
headers=headers,
|
||||
)
|
||||
return
|
||||
await client.post(f"{pb_url}/api/collections/{coll}/records", json=record, headers=headers)
|
||||
|
||||
|
||||
def normalize_wechat_avatar_url(url: str) -> str:
|
||||
"""微信头像常为 http://wx.qlogo.cn;在 HTTPS 站点作为 <img src> 会触发混合内容拦截,同路径可改用 https。"""
|
||||
s = str(url or "").strip()
|
||||
low = s.lower()
|
||||
if low.startswith("http://") and "qlogo.cn" in low:
|
||||
return "https://" + s[7:]
|
||||
return s
|
||||
|
||||
|
||||
def contact_avatar_url(contact: dict) -> str:
|
||||
v = (
|
||||
contact.get("headImg")
|
||||
or contact.get("avatar")
|
||||
or contact.get("headimgurl")
|
||||
or contact.get("headImgUrl")
|
||||
)
|
||||
return normalize_wechat_avatar_url(str(v or "").strip())
|
||||
|
||||
|
||||
async def sync_wechat_private_user_to_pb(data: dict, profile_key: str) -> None:
|
||||
"""根据解密后的用户消息 data,拉取 B 端联系人并 upsert 到 PocketBase。"""
|
||||
authtoken, wxbot_bid = pm_sync_credentials_for_profile(profile_key)
|
||||
if not authtoken or not wxbot_bid:
|
||||
return
|
||||
target_userid = str(data.get("UserId", "")).strip()
|
||||
if not target_userid or is_duplicate_wechat_callback(data, profile_key):
|
||||
return
|
||||
result = await query_private_messages(
|
||||
authtoken=authtoken,
|
||||
wxbot_bid=wxbot_bid,
|
||||
page=0,
|
||||
size=100,
|
||||
filter_value=0,
|
||||
request_id=f"thirdapi-{target_userid}",
|
||||
)
|
||||
contacts = result.get("contacts") or []
|
||||
target = next(
|
||||
(
|
||||
c
|
||||
for c in contacts
|
||||
if str(c.get("userId", "") or c.get("userid", "")).strip() == target_userid
|
||||
),
|
||||
None,
|
||||
)
|
||||
if not target:
|
||||
return
|
||||
last_msg = target.get("lastMsg") or {}
|
||||
await pb_upsert_wechat_private_user(
|
||||
{
|
||||
"userid": target_userid,
|
||||
"channel_id": str(data.get("ChannelId", "")).strip(),
|
||||
"nick": target.get("nick", ""),
|
||||
"avatar": contact_avatar_url(target) or "",
|
||||
"source_query": str(data.get("Query", "")).strip(),
|
||||
"source_message_id": str(data.get("MsgId", "")).strip(),
|
||||
"source_message_time": str(data.get("Timestamp", "")),
|
||||
"matched_last_msg": last_msg.get("content", ""),
|
||||
"matched_last_msg_id": str(last_msg.get("msgId", "") or last_msg.get("id", "")),
|
||||
"matched_last_msg_time": str(target.get("lastActiveTime", "")),
|
||||
"raw_contact": json.dumps(target, ensure_ascii=False),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
async def upsert_contacts_batch_to_pb(
|
||||
contacts: list,
|
||||
*,
|
||||
source: str = "pm_query_sync",
|
||||
profile_key: str = "",
|
||||
) -> dict:
|
||||
"""将 getaccessstautuslist 返回的 contacts 批量写入 wechat_private_users(upsert)。"""
|
||||
saved = 0
|
||||
skipped = 0
|
||||
errors = 0
|
||||
for c in contacts or []:
|
||||
if not isinstance(c, dict):
|
||||
skipped += 1
|
||||
continue
|
||||
userid = str(c.get("userId") or c.get("userid") or "").strip()
|
||||
if not userid:
|
||||
skipped += 1
|
||||
continue
|
||||
last_msg = c.get("lastMsg") or {}
|
||||
if not isinstance(last_msg, dict):
|
||||
last_msg = {}
|
||||
try:
|
||||
await pb_upsert_wechat_private_user(
|
||||
{
|
||||
"userid": userid,
|
||||
"channel_id": str(c.get("channelId") or c.get("channel_id") or "").strip(),
|
||||
"nick": c.get("nick") or "",
|
||||
"avatar": contact_avatar_url(c) or "",
|
||||
"source_query": source,
|
||||
"source_message_id": str(last_msg.get("id") or last_msg.get("msgId") or ""),
|
||||
"source_message_time": str(c.get("lastActiveTime") or ""),
|
||||
"matched_last_msg": last_msg.get("content", "") or "",
|
||||
"matched_last_msg_id": str(last_msg.get("id") or last_msg.get("msgId") or ""),
|
||||
"matched_last_msg_time": str(c.get("lastActiveTime") or ""),
|
||||
"raw_contact": json.dumps(
|
||||
{
|
||||
**c,
|
||||
"_sync_meta": {
|
||||
"source": source,
|
||||
"profile_key": profile_key,
|
||||
"synced_at": int(time.time()),
|
||||
},
|
||||
},
|
||||
ensure_ascii=False,
|
||||
),
|
||||
}
|
||||
)
|
||||
saved += 1
|
||||
except Exception:
|
||||
logger.exception("upsert contact failed userid=%s", userid)
|
||||
errors += 1
|
||||
return {"saved": saved, "skipped": skipped, "errors": errors}
|
||||
|
||||
|
||||
|
||||
async def fetch_contact_by_userid_via_authtoken(
|
||||
target_userid: str,
|
||||
*,
|
||||
profile_key: str = "",
|
||||
max_pages: int = 8,
|
||||
page_size: int = 30,
|
||||
) -> dict | None:
|
||||
"""用服务端 PM_SYNC authtoken 分页拉私信列表,找到 userid 后 upsert 并返回 contact。"""
|
||||
uid = str(target_userid or "").strip()
|
||||
if not uid:
|
||||
return None
|
||||
keys = []
|
||||
pk = (profile_key or "").strip().lower()
|
||||
if pk in ("eric", "xidu"):
|
||||
keys = [pk]
|
||||
else:
|
||||
# 异度优先(当前默认运营号),再试 eric
|
||||
keys = ["xidu", "eric"]
|
||||
for key in keys:
|
||||
authtoken, wxbot_bid = pm_sync_credentials_for_profile(key)
|
||||
if not authtoken or not wxbot_bid:
|
||||
continue
|
||||
for page in range(max(1, int(max_pages))):
|
||||
try:
|
||||
result = await query_private_messages(
|
||||
authtoken=authtoken,
|
||||
wxbot_bid=wxbot_bid,
|
||||
page=page,
|
||||
size=max(1, min(int(page_size), 50)),
|
||||
filter_value=0,
|
||||
request_id=f"vip-fallback-{uid}-{page}",
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("fallback query failed profile=%s page=%s", key, page)
|
||||
break
|
||||
contacts = result.get("contacts") or []
|
||||
if not contacts:
|
||||
break
|
||||
# 本页全量入库(便于后续命中),再找目标用户
|
||||
try:
|
||||
await upsert_contacts_batch_to_pb(
|
||||
contacts,
|
||||
source="wechat_profile_fallback",
|
||||
profile_key=key,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("fallback batch upsert failed profile=%s page=%s", key, page)
|
||||
target = next(
|
||||
(
|
||||
c
|
||||
for c in contacts
|
||||
if isinstance(c, dict)
|
||||
and str(c.get("userId") or c.get("userid") or "").strip() == uid
|
||||
),
|
||||
None,
|
||||
)
|
||||
if target:
|
||||
return target
|
||||
try:
|
||||
total = int(str(result.get("count") or "0"))
|
||||
except Exception:
|
||||
total = 0
|
||||
if total and (page + 1) * page_size >= total:
|
||||
break
|
||||
return None
|
||||
|
||||
|
||||
async def safe_sync_wechat_private_user_to_pb(data: dict, profile_key: str) -> None:
|
||||
try:
|
||||
await sync_wechat_private_user_to_pb(data, profile_key)
|
||||
except Exception:
|
||||
logger.exception("微信私信用户同步 PocketBase 失败(已忽略) profile=%s", profile_key)
|
||||
1
apps/hackrobot/payjsapi/payjs.sh
Normal file
1
apps/hackrobot/payjsapi/payjs.sh
Normal file
@ -0,0 +1 @@
|
||||
python3 run.py
|
||||
27
apps/hackrobot/payjsapi/payjsapi.env.keys.example
Normal file
27
apps/hackrobot/payjsapi/payjsapi.env.keys.example
Normal file
@ -0,0 +1,27 @@
|
||||
# ========== 生产环境配置 ==========
|
||||
|
||||
BASE_URL=
|
||||
PAYMENT_PROVIDER=
|
||||
|
||||
# ZPAY(请填入实际值)
|
||||
ZPAY_PID=
|
||||
ZPAY_KEY=
|
||||
|
||||
# PocketBase(check-user、ensure-user 必需,否则报「服务配置错误」)
|
||||
PB_URL=
|
||||
PB_ADMIN_EMAIL=
|
||||
PB_ADMIN_PASSWORD=
|
||||
|
||||
# Memos(nomadvip 支付成功后创建账号)
|
||||
MEMOS_API=
|
||||
MEMOS_TOKEN=
|
||||
|
||||
# MinIO(可选)
|
||||
MINIO_ENDPOINT=
|
||||
MINIO_PORT=
|
||||
MINIO_BUCKET=
|
||||
MINIO_UPLOAD_PREFIX=
|
||||
PM_SYNC_AUTHTOKEN=
|
||||
PM_SYNC_AUTHTOKEN_XIDU=
|
||||
PM_SYNC_WXBOT_BID_XIDU=
|
||||
PM_SYNC_DEFAULT_WXBOT_BID_XIDU=
|
||||
12
apps/hackrobot/payjsapi/requirements.txt
Normal file
12
apps/hackrobot/payjsapi/requirements.txt
Normal file
@ -0,0 +1,12 @@
|
||||
fastapi
|
||||
uvicorn
|
||||
python-dotenv
|
||||
sqlalchemy
|
||||
pydantic
|
||||
python-multipart
|
||||
pocketbase
|
||||
requests
|
||||
minio
|
||||
opencv-python-headless
|
||||
rapidocr-onnxruntime
|
||||
pycryptodome
|
||||
4
apps/hackrobot/payjsapi/run-with-logs.bat
Normal file
4
apps/hackrobot/payjsapi/run-with-logs.bat
Normal file
@ -0,0 +1,4 @@
|
||||
@echo off
|
||||
REM 强制无缓冲输出,确保日志立即显示在终端
|
||||
set PYTHONUNBUFFERED=1
|
||||
python -u run.py
|
||||
4
apps/hackrobot/payjsapi/run-with-logs.sh
Normal file
4
apps/hackrobot/payjsapi/run-with-logs.sh
Normal file
@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
# 强制无缓冲输出,确保日志立即显示在终端
|
||||
export PYTHONUNBUFFERED=1
|
||||
exec python -u run.py
|
||||
31
apps/hackrobot/payjsapi/run.py
Normal file
31
apps/hackrobot/payjsapi/run.py
Normal file
@ -0,0 +1,31 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
os.environ.setdefault("PYTHONUNBUFFERED", "1")
|
||||
if hasattr(sys.stdout, "reconfigure"):
|
||||
sys.stdout.reconfigure(line_buffering=True)
|
||||
if hasattr(sys.stderr, "reconfigure"):
|
||||
sys.stderr.reconfigure(line_buffering=True)
|
||||
|
||||
import uvicorn
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
if __name__ == "__main__":
|
||||
port = int(os.getenv("PORT", "8007"))
|
||||
reload_enabled = os.getenv("UVICORN_RELOAD", "0").strip().lower() in ("1", "true", "yes")
|
||||
try:
|
||||
sys.stderr.write(f"\n[payjsapi] startup http://0.0.0.0:{port}\n")
|
||||
sys.stderr.flush()
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
|
||||
uvicorn.run(
|
||||
"app.main:app",
|
||||
host="0.0.0.0",
|
||||
port=port,
|
||||
reload=reload_enabled,
|
||||
log_level="info",
|
||||
)
|
||||
|
||||
44
apps/hackrobot/payjsapi/手机微信支付调试说明.md
Normal file
44
apps/hackrobot/payjsapi/手机微信支付调试说明.md
Normal file
@ -0,0 +1,44 @@
|
||||
# 手机微信支付调试说明
|
||||
|
||||
前端(vipgroup 等)与 payjsapi 同机时,支付请求由服务端发起,应使用 `127.0.0.1:8700`。
|
||||
|
||||
## 1. 同机部署(vipgroup + payjsapi 都在本机)
|
||||
|
||||
**无需配置**。默认使用 `http://127.0.0.1:8700`。
|
||||
|
||||
若 `.env.local` 中有 `PAYMENT_API_URL=http://0.0.0.0:8700`,请删除或改为:
|
||||
```bash
|
||||
PAYMENT_API_URL=http://127.0.0.1:8700
|
||||
```
|
||||
(0.0.0.0 无法作为连接目标,会导致立即失败)
|
||||
|
||||
## 2. 确认 payjsapi 已启动
|
||||
|
||||
```bash
|
||||
cd C:\project\payjsapi
|
||||
python run.py
|
||||
```
|
||||
|
||||
应监听 `0.0.0.0:8700`,可从局域网访问。
|
||||
|
||||
## 3. 确认防火墙放行 8700 端口
|
||||
|
||||
Windows 防火墙可能阻止手机访问 8700。若 vipgroup 与 payjsapi 同机,请求是服务端内部调用,一般无问题。
|
||||
|
||||
## 4. 查看控制台日志
|
||||
|
||||
支付失败时,vipgroup 的 Next.js 控制台会输出:
|
||||
|
||||
```
|
||||
[pay] 请求失败: ... apiUrl: http://xxx:8700 requestHost: xxx
|
||||
```
|
||||
|
||||
- 若 `apiUrl` 为 `http://0.0.0.0:8700` → 在 `.env.local` 中设置 `PAYMENT_API_URL=http://192.168.41.222:8700`
|
||||
- 若 `apiUrl` 正确但仍失败 → 检查 payjsapi 是否运行、网络是否可达
|
||||
|
||||
## 5. vipgroup 需包含的配置逻辑
|
||||
|
||||
若 vipgroup 与 lms/nomadlms 结构不同,需确保:
|
||||
|
||||
1. 调用 `/api/pay` 或直接调用 payjsapi 时,使用正确的 `PAYMENT_API_URL`
|
||||
2. 本地开发时 `PAYMENT_API_URL` 指向 `http://{你的局域网IP}:8700`
|
||||
BIN
apps/hackrobot/pocketbase/data.db
Normal file
BIN
apps/hackrobot/pocketbase/data.db
Normal file
Binary file not shown.
121
apps/hackrobot/pocketbase/data.schema.sql
Normal file
121
apps/hackrobot/pocketbase/data.schema.sql
Normal file
@ -0,0 +1,121 @@
|
||||
CREATE TABLE `_migrations` (file VARCHAR(255) PRIMARY KEY NOT NULL, applied INTEGER NOT NULL);
|
||||
CREATE TABLE `_params` (
|
||||
`id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL,
|
||||
`value` JSON DEFAULT NULL,
|
||||
`created` TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%fZ')) NOT NULL,
|
||||
`updated` TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%fZ')) NOT NULL
|
||||
);
|
||||
CREATE TABLE `_collections` (
|
||||
`id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL,
|
||||
`system` BOOLEAN DEFAULT FALSE NOT NULL,
|
||||
`type` TEXT DEFAULT "base" NOT NULL,
|
||||
`name` TEXT UNIQUE NOT NULL,
|
||||
`fields` JSON DEFAULT "[]" NOT NULL,
|
||||
`indexes` JSON DEFAULT "[]" NOT NULL,
|
||||
`listRule` TEXT DEFAULT NULL,
|
||||
`viewRule` TEXT DEFAULT NULL,
|
||||
`createRule` TEXT DEFAULT NULL,
|
||||
`updateRule` TEXT DEFAULT NULL,
|
||||
`deleteRule` TEXT DEFAULT NULL,
|
||||
`options` JSON DEFAULT "{}" NOT NULL,
|
||||
`created` TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%fZ')) NOT NULL,
|
||||
`updated` TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%fZ')) NOT NULL
|
||||
);
|
||||
CREATE INDEX idx__collections_type on `_collections` (`type`);
|
||||
CREATE TABLE `_mfas` (`collectionRef` TEXT DEFAULT '' NOT NULL, `created` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `method` TEXT DEFAULT '' NOT NULL, `recordRef` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE INDEX `idx_mfas_collectionRef_recordRef` ON `_mfas` (
|
||||
`collectionRef`,
|
||||
`recordRef`
|
||||
);
|
||||
CREATE TABLE sqlite_stat1(tbl,idx,stat);
|
||||
CREATE TABLE sqlite_stat4(tbl,idx,neq,nlt,ndlt,sample);
|
||||
CREATE TABLE `_otps` (`collectionRef` TEXT DEFAULT '' NOT NULL, `created` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `password` TEXT DEFAULT '' NOT NULL, `recordRef` TEXT DEFAULT '' NOT NULL, `sentTo` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE INDEX `idx_otps_collectionRef_recordRef` ON `_otps` (
|
||||
`collectionRef`,
|
||||
`recordRef`
|
||||
);
|
||||
CREATE TABLE `_externalAuths` (`collectionRef` TEXT DEFAULT '' NOT NULL, `created` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `provider` TEXT DEFAULT '' NOT NULL, `providerId` TEXT DEFAULT '' NOT NULL, `recordRef` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_externalAuths_record_provider` ON `_externalAuths` (
|
||||
`collectionRef`,
|
||||
`recordRef`,
|
||||
`provider`
|
||||
);
|
||||
CREATE UNIQUE INDEX `idx_externalAuths_collection_provider` ON `_externalAuths` (
|
||||
`collectionRef`,
|
||||
`provider`,
|
||||
`providerId`
|
||||
);
|
||||
CREATE TABLE `_authOrigins` (`collectionRef` TEXT DEFAULT '' NOT NULL, `created` TEXT DEFAULT '' NOT NULL, `fingerprint` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `recordRef` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_authOrigins_unique_pairs` ON `_authOrigins` (
|
||||
`collectionRef`,
|
||||
`recordRef`,
|
||||
`fingerprint`
|
||||
);
|
||||
CREATE TABLE `_superusers` (`created` TEXT DEFAULT '' NOT NULL, `email` TEXT DEFAULT '' NOT NULL, `emailVisibility` BOOLEAN DEFAULT FALSE NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `password` TEXT DEFAULT '' NOT NULL, `tokenKey` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL, `verified` BOOLEAN DEFAULT FALSE NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_tokenKey_pbc_3142635823` ON `_superusers` (`tokenKey`);
|
||||
CREATE UNIQUE INDEX `idx_email_pbc_3142635823` ON `_superusers` (`email`) WHERE `email` != '';
|
||||
CREATE TABLE `users` (`avatar` TEXT DEFAULT '' NOT NULL, `created` TEXT DEFAULT '' NOT NULL, `email` TEXT DEFAULT '' NOT NULL, `emailVisibility` BOOLEAN DEFAULT FALSE NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `name` TEXT DEFAULT '' NOT NULL, `password` TEXT DEFAULT '' NOT NULL, `tokenKey` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL, `verified` BOOLEAN DEFAULT FALSE NOT NULL, "live_allowed" BOOLEAN DEFAULT FALSE NOT NULL);
|
||||
CREATE TABLE `payments` (`amount` NUMERIC DEFAULT 0 NOT NULL, `created` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `order_id` TEXT DEFAULT '' NOT NULL, `type` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL, `user_id` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE IF NOT EXISTS "solan" (`amount` NUMERIC DEFAULT 0 NOT NULL, `created` TEXT DEFAULT '' NOT NULL, `education` TEXT DEFAULT '' NOT NULL, `gender` TEXT DEFAULT '' NOT NULL, `gradYear` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `nickname` TEXT DEFAULT '' NOT NULL, `occupation` TEXT DEFAULT '' NOT NULL, `order_id` TEXT DEFAULT '' NOT NULL, `phone` TEXT DEFAULT '' NOT NULL, `reason` TEXT DEFAULT '' NOT NULL, `type` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL, `user_id` TEXT DEFAULT '' NOT NULL, `wechatId` TEXT DEFAULT '' NOT NULL, "single" TEXT DEFAULT '' NOT NULL, "media" TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `sites` (`created` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `site_id` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL, "domain" TEXT DEFAULT '' NOT NULL, "name" TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `site_vip` (`created` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `updated` TEXT DEFAULT '' NOT NULL, `user_id` TEXT DEFAULT '' NOT NULL, "site_id" TEXT DEFAULT '' NOT NULL, "order_id" TEXT DEFAULT '' NOT NULL, "expires_at" TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `solanRed` (`amount` NUMERIC DEFAULT 0 NOT NULL, `created` TEXT DEFAULT '' NOT NULL, `education` TEXT DEFAULT '' NOT NULL, `gender` TEXT DEFAULT '' NOT NULL, `gradYear` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `media` TEXT DEFAULT '' NOT NULL, `nickname` TEXT DEFAULT '' NOT NULL, `occupation` TEXT DEFAULT '' NOT NULL, `order_id` TEXT DEFAULT '' NOT NULL, `phone` TEXT DEFAULT '' NOT NULL, `reason` TEXT DEFAULT '' NOT NULL, `single` TEXT DEFAULT '' NOT NULL, `type` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL, `user_id` TEXT DEFAULT '' NOT NULL, `wechatId` TEXT DEFAULT '' NOT NULL, "xiaohongshu_url" TEXT DEFAULT '' NOT NULL, "is_volunteer" BOOLEAN DEFAULT FALSE NOT NULL, "is_approved" BOOLEAN DEFAULT FALSE NOT NULL, "age_range" TEXT DEFAULT '' NOT NULL, "first_time" TEXT DEFAULT '' NOT NULL, "calculated_age" NUMERIC DEFAULT 0 NOT NULL, "vip" BOOLEAN DEFAULT FALSE NOT NULL, "is_wechat_friend" BOOLEAN DEFAULT FALSE NOT NULL, "userInfo" TEXT DEFAULT '' NOT NULL, "meetupdate" TEXT DEFAULT '' NOT NULL, "submitted_at_cn" TEXT DEFAULT '' NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_tokenKey__pb_users_auth_` ON `users` (`tokenKey`);
|
||||
CREATE UNIQUE INDEX `idx_email__pb_users_auth_` ON `users` (`email`) WHERE `email` != '';
|
||||
CREATE TABLE `live_sessions` (`created` TEXT DEFAULT '' NOT NULL, `event` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `machine` JSON DEFAULT NULL, `process` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL, `user` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `wechat_private_users` (`avatar` TEXT DEFAULT '' NOT NULL, `channel_id` TEXT DEFAULT '' NOT NULL, `created` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `matched_last_msg` TEXT DEFAULT '' NOT NULL, `nick` TEXT DEFAULT '' NOT NULL, `source_message_id` TEXT DEFAULT '' NOT NULL, `source_message_time` TEXT DEFAULT '' NOT NULL, `source_query` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL, `userid` TEXT DEFAULT '' NOT NULL, "matched_last_msg_id" TEXT DEFAULT '' NOT NULL, "matched_last_msg_time" TEXT DEFAULT '' NOT NULL, "raw_contact" JSON DEFAULT NULL);
|
||||
CREATE TABLE `mp_recommend_events` (`created` TEXT DEFAULT '' NOT NULL, `device_time_str` TEXT DEFAULT '' NOT NULL, `forward_timestamp_ms` NUMERIC DEFAULT 0 NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `mp_uid` TEXT DEFAULT '' NOT NULL, `package_name` TEXT DEFAULT '' NOT NULL, `recommender_key` TEXT DEFAULT '' NOT NULL, `summary_line` TEXT DEFAULT '' NOT NULL, `title_line` TEXT DEFAULT '' NOT NULL, `updated` TEXT DEFAULT '' NOT NULL, "raw_content" TEXT DEFAULT '' NOT NULL, "source_from" TEXT DEFAULT '' NOT NULL, "client_ip" TEXT DEFAULT '' NOT NULL, "device_model" TEXT DEFAULT '' NOT NULL, "cumulative_count" NUMERIC DEFAULT 0 NOT NULL, "username" TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `social_roster` (`availability` TEXT DEFAULT '' NOT NULL, `avatar` TEXT DEFAULT '' NOT NULL, `bio` TEXT DEFAULT '' NOT NULL, `city` TEXT DEFAULT '' NOT NULL, `discoverable` BOOLEAN DEFAULT FALSE NOT NULL, `education` TEXT DEFAULT '' NOT NULL, `face_ok` BOOLEAN DEFAULT FALSE NOT NULL, `gender` TEXT DEFAULT '' NOT NULL, `graduation` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `intents` TEXT DEFAULT '' NOT NULL, `linktree` TEXT DEFAULT '' NOT NULL, `liveness_at` TEXT DEFAULT '' NOT NULL, `liveness_front_url` TEXT DEFAULT '' NOT NULL, `liveness_status` TEXT DEFAULT '' NOT NULL, `liveness_turn_url` TEXT DEFAULT '' NOT NULL, `liveness_video_url` TEXT DEFAULT '' NOT NULL, `looking_for` TEXT DEFAULT '' NOT NULL, `name` TEXT DEFAULT '' NOT NULL, `onboarded_at` TEXT DEFAULT '' NOT NULL, `phone` TEXT DEFAULT '' NOT NULL, `relationship` TEXT DEFAULT '' NOT NULL, `skills` TEXT DEFAULT '' NOT NULL, `status` TEXT DEFAULT '' NOT NULL, `title` TEXT DEFAULT '' NOT NULL, `updated_at` TEXT DEFAULT '' NOT NULL, `wechat` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_social_roster_guest` ON `social_roster` (`guest_id`);
|
||||
CREATE INDEX `idx_social_roster_city` ON `social_roster` (`city`);
|
||||
CREATE TABLE `social_liveness_audits` (`avatar_url` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `front_url` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `meta` JSON DEFAULT NULL, `similarity` NUMERIC DEFAULT 0 NOT NULL, `status` TEXT DEFAULT '' NOT NULL, `turn_url` TEXT DEFAULT '' NOT NULL, `video_size` NUMERIC DEFAULT 0 NOT NULL, `video_url` TEXT DEFAULT '' NOT NULL, `yaw_delta` NUMERIC DEFAULT 0 NOT NULL);
|
||||
CREATE INDEX `idx_social_live_guest` ON `social_liveness_audits` (`guest_id`);
|
||||
CREATE TABLE `social_skill_posts` (`author` TEXT DEFAULT '' NOT NULL, `city` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `note` TEXT DEFAULT '' NOT NULL, `offer` TEXT DEFAULT '' NOT NULL, `status` TEXT DEFAULT '' NOT NULL, `want` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE INDEX `idx_social_skill_city` ON `social_skill_posts` (`city`);
|
||||
CREATE TABLE `social_meetup_reviews` (`booking_id` TEXT DEFAULT '' NOT NULL, `chemistry` NUMERIC DEFAULT 0 NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `from_guest` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `professionalism` NUMERIC DEFAULT 0 NOT NULL, `punctuality` NUMERIC DEFAULT 0 NOT NULL, `text` TEXT DEFAULT '' NOT NULL, `to_guest` TEXT DEFAULT '' NOT NULL, `to_name` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE INDEX `idx_social_rev_to` ON `social_meetup_reviews` (`to_guest`);
|
||||
CREATE TABLE `social_blocks` (`blocked_guest` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `reason` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `social_reports` (`created_at` TEXT DEFAULT '' NOT NULL, `detail` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `reason` TEXT DEFAULT '' NOT NULL, `resolved_at` TEXT DEFAULT '' NOT NULL, `result_note` TEXT DEFAULT '' NOT NULL, `status` TEXT DEFAULT '' NOT NULL, `target_guest` TEXT DEFAULT '' NOT NULL, `target_name` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `social_messages` (`attachments` JSON DEFAULT NULL, `channel_id` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `edited_at` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `kind` TEXT DEFAULT '' NOT NULL, `meta` JSON DEFAULT NULL, `nick` TEXT DEFAULT '' NOT NULL, `pinned` BOOLEAN DEFAULT FALSE NOT NULL, `reactions` JSON DEFAULT NULL, `recalled` BOOLEAN DEFAULT FALSE NOT NULL, `reply_to` JSON DEFAULT NULL, `seq` NUMERIC DEFAULT 0 NOT NULL, `text` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE INDEX `idx_social_msg_ch` ON `social_messages` (`channel_id`);
|
||||
CREATE INDEX `idx_social_msg_seq` ON `social_messages` (
|
||||
`channel_id`,
|
||||
`seq`
|
||||
);
|
||||
CREATE TABLE `social_channel_meta` (`channel_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `pins` JSON DEFAULT NULL, `presence` JSON DEFAULT NULL, `seq` NUMERIC DEFAULT 0 NOT NULL, `updated_at` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_social_chmeta` ON `social_channel_meta` (`channel_id`);
|
||||
CREATE TABLE `social_trips` (`arrival_date` TEXT DEFAULT '' NOT NULL, `city` TEXT DEFAULT '' NOT NULL, `city_id` TEXT DEFAULT '' NOT NULL, `country` TEXT DEFAULT '' NOT NULL, `departure_date` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `h3_cell` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `lat` NUMERIC DEFAULT 0 NOT NULL, `lng` NUMERIC DEFAULT 0 NOT NULL, `status` TEXT DEFAULT '' NOT NULL, `updated_at` TEXT DEFAULT '' NOT NULL, `visibility` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE INDEX `idx_social_trips_guest` ON `social_trips` (`guest_id`);
|
||||
CREATE INDEX `idx_social_trips_city` ON `social_trips` (`city_id`);
|
||||
CREATE TABLE `social_cafe_reviews` (`author` TEXT DEFAULT '' NOT NULL, `cafe_id` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `photo_note` TEXT DEFAULT '' NOT NULL, `score` NUMERIC DEFAULT 0 NOT NULL, `text` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `social_feedback` (`contact` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `kind` TEXT DEFAULT '' NOT NULL, `text` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `social_gig_apps` (`contact` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `gig_id` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `name` TEXT DEFAULT '' NOT NULL, `note` TEXT DEFAULT '' NOT NULL, `request_id` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `coffee_hosts` (`avatar` TEXT DEFAULT '' NOT NULL, `bio` TEXT DEFAULT '' NOT NULL, `city` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `name` TEXT DEFAULT '' NOT NULL, `timezone` TEXT DEFAULT '' NOT NULL, `username` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_coffee_host_user` ON `coffee_hosts` (`username`);
|
||||
CREATE TABLE `coffee_schedules` (`availability` JSON DEFAULT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `host_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `name` TEXT DEFAULT '' NOT NULL, `timezone` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `coffee_event_types` (`active` BOOLEAN DEFAULT FALSE NOT NULL, `buffer_after` NUMERIC DEFAULT 0 NOT NULL, `buffer_before` NUMERIC DEFAULT 0 NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `description` TEXT DEFAULT '' NOT NULL, `host_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `length_minutes` NUMERIC DEFAULT 0 NOT NULL, `locations` JSON DEFAULT NULL, `schedule_id` TEXT DEFAULT '' NOT NULL, `slot_interval` NUMERIC DEFAULT 0 NOT NULL, `slug` TEXT DEFAULT '' NOT NULL, `title` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `coffee_bookings` (`attendee_device` TEXT DEFAULT '' NOT NULL, `attendee_email` TEXT DEFAULT '' NOT NULL, `attendee_name` TEXT DEFAULT '' NOT NULL, `cancelled_at` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `end_utc` TEXT DEFAULT '' NOT NULL, `event_type_id` TEXT DEFAULT '' NOT NULL, `host_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `location` TEXT DEFAULT '' NOT NULL, `notes` TEXT DEFAULT '' NOT NULL, `start_utc` TEXT DEFAULT '' NOT NULL, `status` TEXT DEFAULT '' NOT NULL, `title` TEXT DEFAULT '' NOT NULL, `uid` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `notify_subscribers` (`avatar` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `data` JSON DEFAULT NULL, `email` TEXT DEFAULT '' NOT NULL, `first_name` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `last_name` TEXT DEFAULT '' NOT NULL, `locale` TEXT DEFAULT '' NOT NULL, `phone` TEXT DEFAULT '' NOT NULL, `subscriber_id` TEXT DEFAULT '' NOT NULL, `updated_at` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_notify_sub` ON `notify_subscribers` (`subscriber_id`);
|
||||
CREATE TABLE `notify_topics` (`created_at` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `key` TEXT DEFAULT '' NOT NULL, `name` TEXT DEFAULT '' NOT NULL, `subscribers` JSON DEFAULT NULL);
|
||||
CREATE UNIQUE INDEX `idx_notify_topic` ON `notify_topics` (`key`);
|
||||
CREATE TABLE `notify_workflows` (`active` BOOLEAN DEFAULT FALSE NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `description` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `name` TEXT DEFAULT '' NOT NULL, `steps` JSON DEFAULT NULL, `tags` JSON DEFAULT NULL, `updated_at` TEXT DEFAULT '' NOT NULL, `workflow_id` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_notify_wf` ON `notify_workflows` (`workflow_id`);
|
||||
CREATE TABLE `notify_preferences` (`channels` JSON DEFAULT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `subscriber_id` TEXT DEFAULT '' NOT NULL, `updated_at` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_notify_prefs` ON `notify_preferences` (`subscriber_id`);
|
||||
CREATE TABLE `notify_messages` (`archived` BOOLEAN DEFAULT FALSE NOT NULL, `body` TEXT DEFAULT '' NOT NULL, `category` TEXT DEFAULT '' NOT NULL, `channel` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `cta_label` TEXT DEFAULT '' NOT NULL, `cta_url` TEXT DEFAULT '' NOT NULL, `deleted` BOOLEAN DEFAULT FALSE NOT NULL, `href` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `notification_id` TEXT DEFAULT '' NOT NULL, `payload` JSON DEFAULT NULL, `read` BOOLEAN DEFAULT FALSE NOT NULL, `subject` TEXT DEFAULT '' NOT NULL, `subscriber_id` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE INDEX `idx_notify_inbox` ON `notify_messages` (`subscriber_id`);
|
||||
CREATE TABLE `notify_activity` (`created_at` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `jobs` JSON DEFAULT NULL, `payload` JSON DEFAULT NULL, `status` TEXT DEFAULT '' NOT NULL, `subscriber_id` TEXT DEFAULT '' NOT NULL, `transaction_id` TEXT DEFAULT '' NOT NULL, `workflow_id` TEXT DEFAULT '' NOT NULL, `workflow_name` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `notify_integrations` (`active` BOOLEAN DEFAULT FALSE NOT NULL, `channel` TEXT DEFAULT '' NOT NULL, `config` JSON DEFAULT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `provider` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `work_projects` (`background_gradient` TEXT DEFAULT '' NOT NULL, `background_type` TEXT DEFAULT '' NOT NULL, `board_id` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `description` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `name` TEXT DEFAULT '' NOT NULL, `updated_at` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `work_boards` (`background_gradient` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `default_view` TEXT DEFAULT '' NOT NULL, `doc` JSON DEFAULT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `name` TEXT DEFAULT '' NOT NULL, `position` NUMERIC DEFAULT 0 NOT NULL, `project_id` TEXT DEFAULT '' NOT NULL, `show_card_counter` BOOLEAN DEFAULT FALSE NOT NULL, `updated_at` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `work_pads` (`authors` JSON DEFAULT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `revisions` JSON DEFAULT NULL, `text` TEXT DEFAULT '' NOT NULL, `title` TEXT DEFAULT '' NOT NULL, `updated_at` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE TABLE `social_channel_reads` (`channel_id` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `read_seq` NUMERIC DEFAULT 0 NOT NULL, `recv_seq` NUMERIC DEFAULT 0 NOT NULL, `updated_at` TEXT DEFAULT '' NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_social_chread` ON `social_channel_reads` (
|
||||
`guest_id`,
|
||||
`channel_id`
|
||||
);
|
||||
CREATE TABLE `social_files` (`channel_id` TEXT DEFAULT '' NOT NULL, `created_at` TEXT DEFAULT '' NOT NULL, `file_id` TEXT DEFAULT '' NOT NULL, `guest_id` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `kind` TEXT DEFAULT '' NOT NULL, `mime` TEXT DEFAULT '' NOT NULL, `name` TEXT DEFAULT '' NOT NULL, `path` TEXT DEFAULT '' NOT NULL, `size` NUMERIC DEFAULT 0 NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_social_files` ON `social_files` (`file_id`);
|
||||
CREATE TABLE `oa_fans` (`avatar` TEXT DEFAULT '' NOT NULL, `comment_cnt` NUMERIC DEFAULT 0 NOT NULL, `device_id` TEXT DEFAULT '' NOT NULL, `follow_since` TEXT DEFAULT '' NOT NULL, `h5_userid` TEXT DEFAULT '' NOT NULL, `id` TEXT PRIMARY KEY DEFAULT ('r'||lower(hex(randomblob(7)))) NOT NULL, `identity_openid` TEXT DEFAULT '' NOT NULL, `last_action` TEXT DEFAULT '' NOT NULL, `like_cnt` NUMERIC DEFAULT 0 NOT NULL, `look_cnt` NUMERIC DEFAULT 0 NOT NULL, `nickname` TEXT DEFAULT '' NOT NULL, `openid` TEXT DEFAULT '' NOT NULL, `pay_cnt` NUMERIC DEFAULT 0 NOT NULL, `raw` JSON DEFAULT NULL, `reward_amt` TEXT DEFAULT '' NOT NULL, `reward_cnt` NUMERIC DEFAULT 0 NOT NULL, `scene` TEXT DEFAULT '' NOT NULL, `signature` TEXT DEFAULT '' NOT NULL, `source` TEXT DEFAULT '' NOT NULL, "remark" TEXT DEFAULT '' NOT NULL, "follow_time" NUMERIC DEFAULT 0 NOT NULL, "gender" NUMERIC DEFAULT 0 NOT NULL, "gender_txt" TEXT DEFAULT '' NOT NULL, "country" TEXT DEFAULT '' NOT NULL, "province" TEXT DEFAULT '' NOT NULL, "city" TEXT DEFAULT '' NOT NULL, "region" TEXT DEFAULT '' NOT NULL, "identity_ip_wording" TEXT DEFAULT '' NOT NULL, "reward_total_fen" NUMERIC DEFAULT 0 NOT NULL, "pay_fee_fen" NUMERIC DEFAULT 0 NOT NULL);
|
||||
CREATE UNIQUE INDEX `idx_oa_fans_openid` ON `oa_fans` (`openid`);
|
||||
1
apps/hackrobot/pocketbase/pocketbase.sh
Normal file
1
apps/hackrobot/pocketbase/pocketbase.sh
Normal file
@ -0,0 +1 @@
|
||||
./pocketbase serve
|
||||
48
apps/hackrobot/pocketbase/tables.txt
Normal file
48
apps/hackrobot/pocketbase/tables.txt
Normal file
@ -0,0 +1,48 @@
|
||||
_migrations
|
||||
_params
|
||||
_collections
|
||||
_mfas
|
||||
sqlite_stat1
|
||||
sqlite_stat4
|
||||
_otps
|
||||
_externalAuths
|
||||
_authOrigins
|
||||
_superusers
|
||||
users
|
||||
payments
|
||||
solan
|
||||
sites
|
||||
site_vip
|
||||
solanRed
|
||||
live_sessions
|
||||
wechat_private_users
|
||||
mp_recommend_events
|
||||
social_roster
|
||||
social_liveness_audits
|
||||
social_skill_posts
|
||||
social_meetup_reviews
|
||||
social_blocks
|
||||
social_reports
|
||||
social_messages
|
||||
social_channel_meta
|
||||
social_trips
|
||||
social_cafe_reviews
|
||||
social_feedback
|
||||
social_gig_apps
|
||||
coffee_hosts
|
||||
coffee_schedules
|
||||
coffee_event_types
|
||||
coffee_bookings
|
||||
notify_subscribers
|
||||
notify_topics
|
||||
notify_workflows
|
||||
notify_preferences
|
||||
notify_messages
|
||||
notify_activity
|
||||
notify_integrations
|
||||
work_projects
|
||||
work_boards
|
||||
work_pads
|
||||
social_channel_reads
|
||||
social_files
|
||||
oa_fans
|
||||
23816
apps/hackrobot/pocketbase/types.d.ts
vendored
Normal file
23816
apps/hackrobot/pocketbase/types.d.ts
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@ -1 +0,0 @@
|
||||
up
|
||||
@ -1 +0,0 @@
|
||||
ok 37
|
||||
Loading…
Reference in New Issue
Block a user