修改政府端前端,银行端小程序和后端接口
This commit is contained in:
129
government-backend/scripts/testSmartEarmarkAPI.js
Normal file
129
government-backend/scripts/testSmartEarmarkAPI.js
Normal file
@@ -0,0 +1,129 @@
|
||||
// 测试智能耳标API
|
||||
const http = require('http');
|
||||
const querystring = require('querystring');
|
||||
|
||||
// 配置
|
||||
const baseUrl = 'localhost';
|
||||
const port = 5352;
|
||||
const apiPath = '/api/smart-earmark';
|
||||
|
||||
// 发送HTTP请求的函数
|
||||
function sendRequest(method, path, data = null, headers = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = {
|
||||
hostname: baseUrl,
|
||||
port: port,
|
||||
path: path,
|
||||
method: method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...headers
|
||||
}
|
||||
};
|
||||
|
||||
const req = http.request(options, (res) => {
|
||||
let data = '';
|
||||
|
||||
res.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
res.on('end', () => {
|
||||
resolve({
|
||||
statusCode: res.statusCode,
|
||||
headers: res.headers,
|
||||
data: data ? JSON.parse(data) : null
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (error) => {
|
||||
reject(error);
|
||||
});
|
||||
|
||||
if (data) {
|
||||
req.write(JSON.stringify(data));
|
||||
}
|
||||
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
// 测试函数
|
||||
async function runTests() {
|
||||
console.log('开始测试智能耳标API...');
|
||||
|
||||
try {
|
||||
// 1. 先登录获取token
|
||||
console.log('\n1. 登录获取token...');
|
||||
const loginResponse = await sendRequest('POST', '/api/auth/login', {
|
||||
username: 'admin',
|
||||
password: '123456'
|
||||
});
|
||||
|
||||
console.log('登录响应状态码:', loginResponse.statusCode);
|
||||
console.log('登录响应数据:', loginResponse.data);
|
||||
|
||||
if (loginResponse.statusCode !== 200) {
|
||||
console.error('登录失败:', loginResponse);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const token = loginResponse.data.data.token;
|
||||
console.log('登录成功,获取到token:', token);
|
||||
|
||||
// 设置Authorization头
|
||||
const authHeaders = {
|
||||
'Authorization': `Bearer ${token}`
|
||||
};
|
||||
|
||||
// 2. 获取智能耳标列表
|
||||
console.log('\n2. 获取智能耳标列表...');
|
||||
const listResponse = await sendRequest('GET', `${apiPath}?page=1&pageSize=10`, null, authHeaders);
|
||||
console.log('获取列表结果:', listResponse.statusCode);
|
||||
console.log('列表数据:', listResponse.data);
|
||||
|
||||
// 3. 创建新的智能耳标
|
||||
console.log('\n3. 创建新的智能耳标...');
|
||||
const newEarmark = {
|
||||
earmarkId: `EM${Date.now().toString().slice(-4)}`,
|
||||
name: '测试智能耳标',
|
||||
status: 'inactive',
|
||||
battery: 95,
|
||||
remark: '这是一个测试耳标'
|
||||
};
|
||||
|
||||
const createResponse = await sendRequest('POST', apiPath, newEarmark, authHeaders);
|
||||
console.log('创建结果:', createResponse.statusCode);
|
||||
console.log('创建的数据:', createResponse.data);
|
||||
|
||||
if (createResponse.statusCode === 201 && createResponse.data?.data?.id) {
|
||||
const earmarkId = createResponse.data.data.id;
|
||||
|
||||
// 4. 更新刚刚创建的智能耳标
|
||||
console.log('\n4. 更新智能耳标...');
|
||||
const updateData = {
|
||||
...newEarmark,
|
||||
name: '更新后的测试耳标',
|
||||
status: 'active'
|
||||
};
|
||||
|
||||
const updateResponse = await sendRequest('PUT', `${apiPath}/${earmarkId}`, updateData, authHeaders);
|
||||
console.log('更新结果:', updateResponse.statusCode);
|
||||
console.log('更新的数据:', updateResponse.data);
|
||||
|
||||
// 5. 删除智能耳标
|
||||
console.log('\n5. 删除智能耳标...');
|
||||
const deleteResponse = await sendRequest('DELETE', `${apiPath}/${earmarkId}`, null, authHeaders);
|
||||
console.log('删除结果:', deleteResponse.statusCode);
|
||||
console.log('删除响应:', deleteResponse.data);
|
||||
}
|
||||
|
||||
console.log('\n测试完成');
|
||||
} catch (error) {
|
||||
console.error('测试过程中出错:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 运行测试
|
||||
runTests();
|
||||
Reference in New Issue
Block a user