const BASE = 'http://localhost:5246' let token = '' const h = (json = false) => ({ Authorization: `Bearer ${token}`, ...(json ? { 'Content-Type': 'application/json' } : {}) }) const api = async (path, opts = {}) => { const res = await fetch(BASE + path, opts) if (!res.ok) { const text = await res.text() throw new Error(`${res.status} ${path} => ${text.slice(0, 200)}`) } return res.json() } token = (await api('/api/auth/login', { method: 'POST', headers: h(true), body: JSON.stringify({ username: 'admin', password: '123456' }) })).token const enc = encodeURIComponent console.log('== /farmers/all 新字段(含老数据 NULL 兼容) ==') const all = await api('/api/farmers/all?limit=6', { headers: h() }) all.forEach((f) => console.log(`id=${f.id} ${f.name} | province=${f.province || ''} county=${f.county || ''} township=${f.township || ''} village=${f.village} groupName=${f.groupName || ''}`)) console.log('== 村列表(锦江区/春熙路街道) ==') console.log((await api(`/api/regions/villages?county=${enc('锦江区')}&township=${enc('春熙路街道')}`, { headers: h() })).join(', ')) console.log('== 组列表(锦江区/春熙路街道/水井坊村) ==') console.log((await api(`/api/regions/groups?county=${enc('锦江区')}&township=${enc('春熙路街道')}&village=${enc('水井坊村')}`, { headers: h() })).join(', ')) console.log('== 乡镇列表(锦江区, 公开数据+农户补充) ==') console.log((await api(`/api/regions/townships?county=${enc('锦江区')}`, { headers: h() })).join(', ')) console.log('== 列表按县筛选(平南县) ==') const list = await api(`/api/farmers?county=${enc('平南县')}&page=1&pageSize=10`, { headers: h() }) console.log(`total=${list.total}`) list.items.forEach((f) => console.log(`${f.name} | ${f.province}/${f.county}/${f.township}/${f.village}/${f.groupName} | ${f.phone} | ${f.bankAccount}`)) console.log('== 关键字搜索“水井坊” ==') const search = await api(`/api/farmers?keyword=${enc('水井坊')}&page=1&pageSize=10`, { headers: h() }) console.log(`total=${search.total}`) search.items.forEach((f) => console.log(`${f.name} | ${f.county}/${f.township}/${f.village}/${f.groupName}`)) console.log('== 全量列表分页(page=1,pageSize=3,验证NULL兼容) ==') const p = await api('/api/farmers?page=1&pageSize=3', { headers: h() }) console.log(`total=${p.total}`) p.items.forEach((f) => console.log(`id=${f.id} ${f.name} | province=${f.province || ''} | phone=${f.phone}`))