import json, subprocess, tempfile, os from urllib.parse import quote def q(text): return quote(text, encoding='utf-8') def kw(name, value): # 方括号必须百分号编码(前端 tansParams 就是 encodeURIComponent(键)) return 'params%5B' + name + '%5D=' + q(value) def api(method, url, body=None): cmd = ['sh', '.tmp_api.sh', method, url] tmp = None if body is not None: fd, tmp = tempfile.mkstemp(suffix='.json') with os.fdopen(fd, 'w', encoding='utf-8') as f: json.dump(body, f, ensure_ascii=False) cmd.append(tmp) out = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8').stdout.strip() if tmp: os.remove(tmp) try: return json.loads(out) except Exception: return {'raw': out[:300]} def res(tag, r): print(f'{tag}: code={r.get("code", "?")} msg={r.get("msg", str(r)[:200])}') print('=== D. 档案列表与筛选 ===') r = api('GET', '/account/user/company/list?pageNum=1&pageSize=50') print('D1 全部 total =', r.get('total'), '(期望 7)') for row in r.get('rows', []): print(' ', row['id'], row.get('province'), '/', row.get('cityName'), '|', row['companyName'], '| user', row['userId'], '| 联系人', row['contactName'], '| status', row['status']) print('D2 含关联会员非 L4 的 33140005 =', any(x['id'] == 33140005 for x in r['rows']), '(LEFT JOIN 生效则必须为 True)') r = api('GET', '/account/user/company/list?cityId=113') print('D3 cityId=113 total =', r.get('total'), [x['id'] for x in r.get('rows', [])]) r = api('GET', '/account/user/company/list?' + kw('companyName', '晨禾')) print('D4 companyName=晨禾 total =', r.get('total'), [x['id'] for x in r.get('rows', [])]) r = api('GET', '/account/user/company/list?status=1') print('D5 status=1 total =', r.get('total'), [x['id'] for x in r.get('rows', [])]) print() print('=== E. 档案详情 ===') d = api('GET', '/account/user/company/33140005')['data'] print('E1 33140005(会员已被降级为 L1):', d['province'], '/', d['cityName'], '| phone', d.get('phone'), '| level', d.get('systemLevel'), '| memberName', d.get('memberName')) d = api('GET', '/account/user/company/33140006')['data'] print('E2 33140006(城市已禁用):', d['province'], '/', d['cityName'], '| phone', d.get('phone'), '| level', d.get('systemLevel'), '| memberName', d.get('memberName')) d = api('GET', '/account/user/company/33140001')['data'] print('E3 33140001(会员已注销):', d.get('systemLevel'), d.get('phone'), d.get('memberName')) r = api('GET', '/account/user/company/999999') print('E4 不存在的档案:', r.get('code'), r.get('data')) print() print('=== F. 新增/修改校验 ===') base = {'cityId': 125, 'companyName': '郑州测试商贸有限公司', 'userId': 123, 'contactName': '陈静', 'contactPhone': '13800000123'} res('F1 城市已有档案(113 杭州)', api('POST', '/account/user/company', dict(base, cityId=113, companyName='重复城市测试'))) res('F2 关联用户非 L4(101 李娜 L1)', api('POST', '/account/user/company', dict(base, userId=101))) res('F3 关联用户不存在', api('POST', '/account/user/company', dict(base, userId=999999))) res('F4 城市已禁用(127 西宁)', api('POST', '/account/user/company', dict(base, cityId=127))) res('F5 城市不存在', api('POST', '/account/user/company', dict(base, cityId=999999))) res('F6 缺公司名称', api('POST', '/account/user/company', dict(base, companyName=''))) res('F7 缺联系人', api('POST', '/account/user/company', dict(base, contactName=''))) res('F8 缺联系电话', api('POST', '/account/user/company', dict(base, contactPhone=''))) res('F9 缺关联用户', api('POST', '/account/user/company', dict(base, userId=None))) r = api('POST', '/account/user/company', base) res('F10 新增郑州档案', r) r = api('GET', '/account/user/company/list?cityId=125') new_id = r['rows'][0]['id'] if r.get('rows') else None print('F11 新档案 id =', new_id) print() print('=== G. 修改 / 状态 ===') res('G1 无 id', api('PUT', '/account/user/company', dict(base, companyName='改名无id'))) res('G2 不存在的 id', api('PUT', '/account/user/company', dict(base, id=999999))) res('G3 改成已有档案的城市(113)', api('PUT', '/account/user/company', dict(base, id=new_id, cityId=113))) res('G4 非法状态', api('PUT', '/account/user/company/changeStatus', {'id': new_id, 'status': '9'})) res('G5 正常修改(改公司名与联系人)', api('PUT', '/account/user/company', dict(base, id=new_id, companyName='郑州测试商贸集团有限公司', contactName='陈静改'))) d = api('GET', '/account/user/company/%s' % new_id)['data'] print('G6 修改后:', d['companyName'], '|', d['contactName'], '| user', d['userId']) res('G7 禁用', api('PUT', '/account/user/company/changeStatus', {'id': new_id, 'status': '1'})) r = api('GET', '/account/user/company/list?status=1') print('G8 status=1 是否含新档案 =', any(x['id'] == new_id for x in r.get('rows', [])), '| total =', r.get('total')) res('G9 启用', api('PUT', '/account/user/company/changeStatus', {'id': new_id, 'status': '0'})) print() print('=== H. 关联用户搜索(限 L4) ===') for k in ['13800000104', '13800000101', '138', '101', '104', '施婷', '晨', '']: r = api('GET', '/account/user/company/userSearch?keyword=' + q(k)) data = r.get('data') if isinstance(data, list): print(f'H keyword={k!r:14} -> {len(data)} 条 {[(x["userId"], x.get("memberName"), x.get("systemLevel")) for x in data]}') else: print(f'H keyword={k!r:14} -> {r.get("code")} {r.get("msg")}') print() print('===== 待清理的新档案 id =====', new_id)