账户信息
@@ -718,26 +718,21 @@ export default {
try {
console.log('加载账户信息,设备SN:', this.selectedDeviceSn)
- // 根据 sn_code 查询账户信息
- const res = await PlaAccountServer.page({
- seachOption: {
- key: 'sn_code',
- value: this.selectedDeviceSn
- },
- pageOption: {
- page: 1,
- pageSize: 1
- }
- })
+ // 通过 sn_code 直接获取账户信息
+ const res = await PlaAccountServer.getBySnCode(this.selectedDeviceSn)
console.log('账户信息接口返回:', res)
- // 支持多种数据格式
- const accountList = res.data?.rows || res.data?.list || res.data?.data || []
-
- if (accountList.length > 0) {
- const accountData = accountList[0]
- console.log('找到账户信息:', accountData)
+ if (res.code === 0 && res.data) {
+ const accountData = res.data
+ console.log('找到账户信息原始数据:', accountData)
+
+ // 确保 id 字段存在,支持多种字段名
+ if (!accountData.id) {
+ accountData.id = accountData.account_id || accountData.accountId || accountData.ID || null
+ }
+
+ console.log('账户ID:', accountData.id)
// 如果接口没有返回设备状态,则单独查询
if (accountData.is_online === undefined || accountData.is_logged_in === undefined) {
@@ -758,16 +753,18 @@ export default {
}
}
- // 确保布尔值字段正确初始化
- accountData.is_enabled = accountData.is_enabled !== undefined ? accountData.is_enabled : 1
- accountData.auto_deliver = accountData.auto_deliver !== undefined ? accountData.auto_deliver : 0
- accountData.auto_chat = accountData.auto_chat !== undefined ? accountData.auto_chat : 0
- accountData.auto_active = accountData.auto_active !== undefined ? accountData.auto_active : 0
+ // 确保布尔值字段正确初始化,转换为数字类型(0 或 1)
+ accountData.is_enabled = accountData.is_enabled !== undefined ? Number(accountData.is_enabled) : 1
+ accountData.auto_deliver = accountData.auto_deliver !== undefined ? Number(accountData.auto_deliver) : 0
+ accountData.auto_chat = accountData.auto_chat !== undefined ? Number(accountData.auto_chat) : 0
+ accountData.auto_active = accountData.auto_active !== undefined ? Number(accountData.auto_active) : 0
this.accountInfo = accountData
console.log('设置账户信息:', this.accountInfo)
+ console.log('accountInfo.id 值:', this.accountInfo.id)
+ console.log('v-if 判断结果:', !!this.accountInfo.id)
} else {
- console.warn('未找到账户信息,设备SN:', this.selectedDeviceSn)
+ console.warn('未找到账户信息,设备SN:', this.selectedDeviceSn, '响应:', res)
this.accountInfo = {}
}
} catch (error) {
@@ -778,7 +775,7 @@ export default {
// 处理开关变化
async handleSwitchChange(field, value) {
- if (!this.accountInfo.id) {
+ if (!this.accountInfo || !this.accountInfo.id) {
this.$Message.warning('账户信息不存在')
return
}
@@ -801,8 +798,8 @@ export default {
this.switchLoading[field] = true
try {
- // 确保 value 是数字类型(0 或 1)
- const numericValue = value ? 1 : 0
+ // i-switch 的 on-change 事件已经返回了正确的值(1 或 0),直接使用
+ const numericValue = Number(value)
// 构建更新数据,确保是纯对象,只包含基本类型
const updateData = {
@@ -822,19 +819,8 @@ export default {
const errorMsg = error.response?.data?.message || error.message || '请稍后重试'
this.$Message.error('更新失败:' + errorMsg)
- // 恢复原值
- this.$nextTick(() => {
- const oldValue = this.accountInfo[field]
- if (field === 'is_enabled') {
- this.accountInfo.is_enabled = oldValue === 1 ? 0 : 1
- } else if (field === 'auto_deliver') {
- this.accountInfo.auto_deliver = oldValue === 1 ? 0 : 1
- } else if (field === 'auto_chat') {
- this.accountInfo.auto_chat = oldValue === 1 ? 0 : 1
- } else if (field === 'auto_active') {
- this.accountInfo.auto_active = oldValue === 1 ? 0 : 1
- }
- })
+ // 恢复原值(从数据库重新加载)
+ await this.loadAccountInfo()
} finally {
this.switchLoading[field] = false
}
@@ -853,8 +839,8 @@ export default {
// 获取平台颜色
getPlatformColor(platformType) {
const colorMap = {
- '1': 'blue',
- '2': 'green'
+ 'boss': 'blue',
+ 'liepin': 'green'
}
return colorMap[platformType] || 'default'
},
@@ -862,8 +848,8 @@ export default {
// 获取平台文本
getPlatformText(platformType) {
const textMap = {
- '1': 'Boss直聘',
- '2': '猎聘'
+ 'boss': 'Boss直聘',
+ 'liepin': '猎聘'
}
return textMap[platformType] || platformType || '-'
}
diff --git a/api/controller_admin/pla_account.js b/api/controller_admin/pla_account.js
index cec0389..94d2eec 100644
--- a/api/controller_admin/pla_account.js
+++ b/api/controller_admin/pla_account.js
@@ -18,6 +18,35 @@ module.exports = {
const accountData = await plaAccountService.getAccountById(id);
return ctx.success(accountData);
},
+
+ /**
+ * @swagger
+ * /admin_api/account/getBySnCode:
+ * post:
+ * summary: 通过设备SN码获取账号信息
+ * description: 根据设备SN码直接获取对应的账号信息
+ * tags: [后台-账号管理]
+ * requestBody:
+ * required: true
+ * content:
+ * application/json:
+ * schema:
+ * type: object
+ * required:
+ * - sn_code
+ * properties:
+ * sn_code:
+ * type: string
+ * description: 设备SN码
+ * responses:
+ * 200:
+ * description: 获取成功
+ */
+ 'POST /account/getBySnCode': async (ctx) => {
+ const { sn_code } = ctx.getBody();
+ const accountData = await plaAccountService.getAccountBySnCode(sn_code);
+ return ctx.success(accountData);
+ },
/**
* @swagger
* /admin_api/account/list:
diff --git a/api/middleware/mqtt/mqttDispatcher.js b/api/middleware/mqtt/mqttDispatcher.js
index 96e433c..0c93229 100644
--- a/api/middleware/mqtt/mqttDispatcher.js
+++ b/api/middleware/mqtt/mqttDispatcher.js
@@ -1,4 +1,5 @@
const db = require('../dbProxy.js');
+const logProxy = require('../logProxy.js');
const deviceManager = require('../schedule/deviceManager.js');
/**
@@ -192,7 +193,7 @@ class MqttDispatcher {
heartbeatData = JSON.parse(message);
}
- const { sn_code, timestamp, status, memory, platform_status, platform_login_status } = heartbeatData;
+ const { sn_code, timestamp, status, memory, platform_status, platform_login_status,clientId } = heartbeatData;
if (!sn_code) {
console.warn('[MQTT心跳] 心跳消息中未找到设备SN码');
@@ -231,6 +232,7 @@ class MqttDispatcher {
loggedInPlatform = platform_login_status.platform || null;
loggedInUsername = platform_login_status.username || '';
loggedInUserId = platform_login_status.user_id || null;
+
if (platform_login_status.timestamp) {
loginTime = new Date(platform_login_status.timestamp);
@@ -266,19 +268,11 @@ class MqttDispatcher {
if (device) {
await device_status.update(updateData, { where: { sn_code } });
console.log(`[MQTT心跳] 设备 ${sn_code} 状态已更新 - 在线: true, 登录: ${updateData.isLoggedIn}`);
- } else {
- // 创建新设备记录
- await device_status.create({
- sn_code,
- deviceName: `设备_${sn_code}`,
- deviceType: 'node_mqtt_client',
- ...updateData,
- isRunning: false,
- taskStatus: 'idle',
- healthStatus: 'unknown',
- healthScore: 0
- });
- console.log(`[MQTT心跳] 设备 ${sn_code} 记录已创建 - 在线: true, 登录: ${updateData.isLoggedIn}`);
+ }
+ else
+ {
+ logProxy.error('[MQTT心跳] 设备 ${sn_code} 不存在', { sn_code });
+ return;
}
// 记录心跳到设备管理器
diff --git a/api/services/pla_account_service.js b/api/services/pla_account_service.js
index 2f00874..69377fd 100644
--- a/api/services/pla_account_service.js
+++ b/api/services/pla_account_service.js
@@ -37,6 +37,46 @@ class PlaAccountService {
return accountData;
}
+ /**
+ * 根据设备SN码获取账号信息
+ * @param {string} sn_code - 设备SN码
+ * @returns {Promise