修改政府端前端,银行端小程序和后端接口
This commit is contained in:
114
government-backend/scripts/initEpidemicAgencyTable.js
Normal file
114
government-backend/scripts/initEpidemicAgencyTable.js
Normal file
@@ -0,0 +1,114 @@
|
||||
const sequelize = require('../config/database');
|
||||
const EpidemicAgency = require('../models/EpidemicAgency');
|
||||
|
||||
async function initEpidemicAgencyTable() {
|
||||
try {
|
||||
console.log('开始初始化防疫机构表...');
|
||||
|
||||
// 测试数据库连接
|
||||
await sequelize.authenticate();
|
||||
console.log('✅ 数据库连接成功');
|
||||
|
||||
// 同步模型到数据库(创建表)
|
||||
await EpidemicAgency.sync({
|
||||
alter: true // 如有必要,修改表结构
|
||||
});
|
||||
console.log('✅ 防疫机构表创建/更新成功');
|
||||
|
||||
// 清除现有数据
|
||||
await EpidemicAgency.destroy({
|
||||
where: {},
|
||||
truncate: true
|
||||
});
|
||||
console.log('✅ 现有数据已清空');
|
||||
|
||||
// 插入测试数据
|
||||
const testData = [
|
||||
{
|
||||
name: '中心动物防疫站',
|
||||
director: '张三',
|
||||
phone: '13800138001',
|
||||
address: '市南区健康路100号',
|
||||
email: 'center@animalhealth.gov.cn',
|
||||
type: 'center',
|
||||
status: 'active',
|
||||
establishmentDate: '2010-01-15',
|
||||
description: '负责全市动物防疫工作的统筹管理和技术指导'
|
||||
},
|
||||
{
|
||||
name: '东区动物防疫分站',
|
||||
director: '李四',
|
||||
phone: '13800138002',
|
||||
address: '市东区防疫路50号',
|
||||
email: 'east@animalhealth.gov.cn',
|
||||
type: 'branch',
|
||||
status: 'active',
|
||||
establishmentDate: '2012-05-20',
|
||||
description: '负责东区范围内的动物防疫工作'
|
||||
},
|
||||
{
|
||||
name: '西区动物防疫分站',
|
||||
director: '王五',
|
||||
phone: '13800138003',
|
||||
address: '市西区健康大道200号',
|
||||
email: 'west@animalhealth.gov.cn',
|
||||
type: 'branch',
|
||||
status: 'active',
|
||||
establishmentDate: '2013-03-10',
|
||||
description: '负责西区范围内的动物防疫工作'
|
||||
},
|
||||
{
|
||||
name: '北区动物防疫分站',
|
||||
director: '赵六',
|
||||
phone: '13800138004',
|
||||
address: '市北区安全路88号',
|
||||
email: 'north@animalhealth.gov.cn',
|
||||
type: 'branch',
|
||||
status: 'active',
|
||||
establishmentDate: '2014-07-05',
|
||||
description: '负责北区范围内的动物防疫工作'
|
||||
},
|
||||
{
|
||||
name: '南区动物防疫分站',
|
||||
director: '钱七',
|
||||
phone: '13800138005',
|
||||
address: '市南区健康路66号',
|
||||
email: 'south@animalhealth.gov.cn',
|
||||
type: 'branch',
|
||||
status: 'active',
|
||||
establishmentDate: '2015-02-28',
|
||||
description: '负责南区范围内的动物防疫工作'
|
||||
},
|
||||
{
|
||||
name: '流动防疫队',
|
||||
director: '孙八',
|
||||
phone: '13800138006',
|
||||
address: '市中区应急中心',
|
||||
email: 'mobile@animalhealth.gov.cn',
|
||||
type: 'mobile',
|
||||
status: 'active',
|
||||
establishmentDate: '2016-09-15',
|
||||
description: '负责偏远地区和突发事件的动物防疫工作'
|
||||
}
|
||||
];
|
||||
|
||||
await EpidemicAgency.bulkCreate(testData);
|
||||
console.log('✅ 测试数据插入成功');
|
||||
|
||||
// 验证数据是否插入成功
|
||||
const agencies = await EpidemicAgency.findAll();
|
||||
console.log(`✅ 共插入 ${agencies.length} 条数据`);
|
||||
|
||||
console.log('🎉 防疫机构表初始化完成');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 初始化防疫机构表失败:', error);
|
||||
} finally {
|
||||
// 关闭数据库连接
|
||||
await sequelize.close();
|
||||
console.log('🔌 数据库连接已关闭');
|
||||
}
|
||||
}
|
||||
|
||||
// 执行初始化函数
|
||||
initEpidemicAgencyTable();
|
||||
92
government-backend/scripts/initSmartCollarTable.js
Normal file
92
government-backend/scripts/initSmartCollarTable.js
Normal file
@@ -0,0 +1,92 @@
|
||||
const sequelize = require('../config/database');
|
||||
const SmartCollar = require('../models/SmartCollar');
|
||||
|
||||
/**
|
||||
* 初始化智能项圈表并添加测试数据
|
||||
*/
|
||||
async function initSmartCollarTable() {
|
||||
try {
|
||||
console.log('开始初始化智能项圈表...');
|
||||
|
||||
// 同步模型到数据库(创建表)
|
||||
await SmartCollar.sync({
|
||||
alter: true, // 这会修改已存在的表结构以匹配模型定义
|
||||
force: false // 设为 true 会删除现有表并重新创建,但会丢失现有数据
|
||||
});
|
||||
|
||||
console.log('智能项圈表同步成功');
|
||||
|
||||
// 检查是否已有测试数据
|
||||
const existingCount = await SmartCollar.count();
|
||||
if (existingCount === 0) {
|
||||
console.log('开始添加测试数据...');
|
||||
|
||||
// 测试数据
|
||||
const testData = [
|
||||
{
|
||||
collar_id: 'CL001',
|
||||
name: '智能项圈001',
|
||||
status: 'active',
|
||||
battery: 85,
|
||||
remark: '用于示范的项圈',
|
||||
created_at: new Date('2023-09-15 10:00:00'),
|
||||
updated_at: new Date('2023-09-20 14:30:00')
|
||||
},
|
||||
{
|
||||
collar_id: 'CL002',
|
||||
name: '智能项圈002',
|
||||
status: 'inactive',
|
||||
battery: 100,
|
||||
remark: '',
|
||||
created_at: new Date('2023-09-16 11:20:00'),
|
||||
updated_at: new Date('2023-09-16 11:20:00')
|
||||
},
|
||||
{
|
||||
collar_id: 'CL003',
|
||||
name: '智能项圈003',
|
||||
status: 'maintenance',
|
||||
battery: 20,
|
||||
remark: '电池需要更换',
|
||||
created_at: new Date('2023-09-10 09:15:00'),
|
||||
updated_at: new Date('2023-09-21 16:45:00')
|
||||
},
|
||||
{
|
||||
collar_id: 'CL004',
|
||||
name: '智能项圈004',
|
||||
status: 'active',
|
||||
battery: 90,
|
||||
remark: '养殖场A区',
|
||||
created_at: new Date('2023-09-05 14:20:00'),
|
||||
updated_at: new Date('2023-09-18 09:30:00')
|
||||
},
|
||||
{
|
||||
collar_id: 'CL005',
|
||||
name: '智能项圈005',
|
||||
status: 'active',
|
||||
battery: 75,
|
||||
remark: '养殖场B区',
|
||||
created_at: new Date('2023-09-08 16:10:00'),
|
||||
updated_at: new Date('2023-09-19 11:45:00')
|
||||
}
|
||||
];
|
||||
|
||||
// 批量创建测试数据
|
||||
await SmartCollar.bulkCreate(testData);
|
||||
|
||||
console.log('测试数据添加成功');
|
||||
} else {
|
||||
console.log(`智能项圈表已有 ${existingCount} 条数据,跳过添加测试数据`);
|
||||
}
|
||||
|
||||
console.log('智能项圈表初始化完成');
|
||||
} catch (error) {
|
||||
console.error('智能项圈表初始化失败:', error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
// 关闭数据库连接
|
||||
await sequelize.close();
|
||||
}
|
||||
}
|
||||
|
||||
// 执行初始化
|
||||
initSmartCollarTable();
|
||||
62
government-backend/scripts/initSmartEarmarkTable.js
Normal file
62
government-backend/scripts/initSmartEarmarkTable.js
Normal file
@@ -0,0 +1,62 @@
|
||||
// 初始化智能耳标表并添加测试数据
|
||||
const sequelize = require('../config/database');
|
||||
const SmartEarmark = require('../models/SmartEarmark');
|
||||
|
||||
async function initSmartEarmarkTable() {
|
||||
try {
|
||||
// 同步模型到数据库,创建表
|
||||
await SmartEarmark.sync({
|
||||
force: false, // 设置为true会删除已存在的表并重新创建
|
||||
alter: true // 允许修改表结构以匹配模型
|
||||
});
|
||||
|
||||
console.log('智能耳标表同步成功');
|
||||
|
||||
// 检查是否已经有数据
|
||||
const existingCount = await SmartEarmark.count();
|
||||
if (existingCount === 0) {
|
||||
// 添加测试数据
|
||||
const testData = [
|
||||
{
|
||||
earmark_id: 'EM001',
|
||||
name: '智能耳标001',
|
||||
status: 'active',
|
||||
battery: 90,
|
||||
remark: '用于示范的耳标'
|
||||
},
|
||||
{
|
||||
earmark_id: 'EM002',
|
||||
name: '智能耳标002',
|
||||
status: 'inactive',
|
||||
battery: 100,
|
||||
remark: ''
|
||||
},
|
||||
{
|
||||
earmark_id: 'EM003',
|
||||
name: '智能耳标003',
|
||||
status: 'maintenance',
|
||||
battery: 15,
|
||||
remark: '电池需要更换'
|
||||
}
|
||||
];
|
||||
|
||||
await SmartEarmark.bulkCreate(testData);
|
||||
console.log(`已添加 ${testData.length} 条测试数据到智能耳标表`);
|
||||
} else {
|
||||
console.log(`智能耳标表已有 ${existingCount} 条数据,不重复添加测试数据`);
|
||||
}
|
||||
|
||||
// 关闭数据库连接
|
||||
await sequelize.close();
|
||||
} catch (error) {
|
||||
console.error('初始化智能耳标表失败:', error);
|
||||
// 确保出错时也关闭数据库连接
|
||||
await sequelize.close().catch(closeError => {
|
||||
console.error('关闭数据库连接时出错:', closeError);
|
||||
});
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// 执行初始化函数
|
||||
initSmartEarmarkTable();
|
||||
74
government-backend/scripts/test-data.js
Normal file
74
government-backend/scripts/test-data.js
Normal file
@@ -0,0 +1,74 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const sequelize = require('../config/database');
|
||||
const SmartHost = require('../models/SmartHost');
|
||||
|
||||
// 测试数据
|
||||
const testData = [
|
||||
{
|
||||
hostId: 'HOST-2023-001',
|
||||
name: '智能主机001',
|
||||
ipAddress: '192.168.1.101',
|
||||
status: 'active',
|
||||
remark: '一号仓库智能主机'
|
||||
},
|
||||
{
|
||||
hostId: 'HOST-2023-002',
|
||||
name: '智能主机002',
|
||||
ipAddress: '192.168.1.102',
|
||||
status: 'active',
|
||||
remark: '二号仓库智能主机'
|
||||
},
|
||||
{
|
||||
hostId: 'HOST-2023-003',
|
||||
name: '智能主机003',
|
||||
ipAddress: '192.168.1.103',
|
||||
status: 'maintenance',
|
||||
remark: '三号仓库智能主机(维护中)'
|
||||
},
|
||||
{
|
||||
hostId: 'HOST-2023-004',
|
||||
name: '智能主机004',
|
||||
ipAddress: '192.168.1.104',
|
||||
status: 'inactive',
|
||||
remark: '四号仓库智能主机(未使用)'
|
||||
},
|
||||
{
|
||||
hostId: 'HOST-2023-005',
|
||||
name: '智能主机005',
|
||||
ipAddress: '192.168.1.105',
|
||||
status: 'active',
|
||||
remark: '五号仓库智能主机'
|
||||
}
|
||||
];
|
||||
|
||||
// 重置表并添加测试数据
|
||||
const resetAndAddTestData = async () => {
|
||||
try {
|
||||
// 确保表存在
|
||||
await SmartHost.sync({
|
||||
force: true // 这将删除表(如果存在)并重新创建
|
||||
});
|
||||
|
||||
console.log('SmartHost表已创建或重置');
|
||||
|
||||
// 插入测试数据
|
||||
const createdHosts = await SmartHost.bulkCreate(testData.map(item => ({
|
||||
host_id: item.hostId,
|
||||
name: item.name,
|
||||
ip_address: item.ipAddress,
|
||||
status: item.status,
|
||||
remark: item.remark
|
||||
})));
|
||||
|
||||
console.log(`已成功添加 ${createdHosts.length} 条智能主机测试数据`);
|
||||
|
||||
// 关闭数据库连接
|
||||
await sequelize.close();
|
||||
} catch (error) {
|
||||
console.error('添加测试数据失败:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
// 执行脚本
|
||||
resetAndAddTestData();
|
||||
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