2026-06-25 11:00:20 +08:00
|
|
|
|
import axios from 'axios'
|
2026-06-25 11:44:01 +08:00
|
|
|
|
import { Message } from 'element-ui'
|
2026-06-25 11:00:20 +08:00
|
|
|
|
|
2026-06-25 11:33:22 +08:00
|
|
|
|
const WORKERMAN_URL = process.env.VUE_APP_WORKERMAN_URL || 'http://127.0.0.1:34351'
|
2026-06-25 12:38:34 +08:00
|
|
|
|
const WORKERMAN_API = process.env.VUE_APP_WORKERMAN_API || WORKERMAN_URL
|
2026-06-25 11:33:22 +08:00
|
|
|
|
|
2026-06-25 11:00:20 +08:00
|
|
|
|
const workerman = axios.create({
|
|
|
|
|
|
timeout: 10000,
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
function normalizePayload (data = {}) {
|
|
|
|
|
|
return data.sendData || data
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-25 11:44:01 +08:00
|
|
|
|
function readBusinessError (data) {
|
|
|
|
|
|
if (!data || typeof data !== 'object') return ''
|
|
|
|
|
|
const code = data.code ?? data.errcode
|
|
|
|
|
|
if (code === undefined || Number(code) === 0) return ''
|
|
|
|
|
|
return data.errmsg || data.msg || data.message || 'Workerman 接口返回失败'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function buildRequestErrorMessage (error) {
|
|
|
|
|
|
if (error.response) {
|
|
|
|
|
|
return `Workerman 接口请求失败:HTTP ${error.response.status}`
|
|
|
|
|
|
}
|
|
|
|
|
|
if (error.code === 'ECONNABORTED') {
|
|
|
|
|
|
return `Workerman 接口请求超时,请确认 ${WORKERMAN_URL} 是否可访问`
|
|
|
|
|
|
}
|
|
|
|
|
|
if (error.request) {
|
|
|
|
|
|
return `Workerman 接口无响应,请确认浏览器可以访问 ${WORKERMAN_URL}`
|
|
|
|
|
|
}
|
|
|
|
|
|
return error.message || 'Workerman 接口请求失败'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-25 11:00:20 +08:00
|
|
|
|
export function sendWorkerman (data) {
|
2026-06-25 12:38:34 +08:00
|
|
|
|
return workerman.post(WORKERMAN_API, normalizePayload(data)).then(response => {
|
2026-06-25 11:44:01 +08:00
|
|
|
|
const result = response.data
|
|
|
|
|
|
const businessError = readBusinessError(result)
|
|
|
|
|
|
if (businessError) {
|
|
|
|
|
|
Message.error(businessError)
|
|
|
|
|
|
const error = new Error(businessError)
|
|
|
|
|
|
error.__workermanHandled = true
|
|
|
|
|
|
throw error
|
|
|
|
|
|
}
|
|
|
|
|
|
if (result === undefined || result === null || result === '') {
|
|
|
|
|
|
const message = 'Workerman 接口未返回有效数据'
|
|
|
|
|
|
Message.error(message)
|
|
|
|
|
|
const error = new Error(message)
|
|
|
|
|
|
error.__workermanHandled = true
|
|
|
|
|
|
throw error
|
|
|
|
|
|
}
|
|
|
|
|
|
return result
|
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
|
if (error.__workermanHandled) throw error
|
|
|
|
|
|
const message = buildRequestErrorMessage(error)
|
|
|
|
|
|
Message.error(message)
|
|
|
|
|
|
throw error
|
|
|
|
|
|
})
|
2026-06-25 11:00:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function trayLogin (param) {
|
|
|
|
|
|
return sendWorkerman({
|
|
|
|
|
|
action: 'set_tray_login',
|
|
|
|
|
|
param
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function trayUnbinding (tray) {
|
|
|
|
|
|
return sendWorkerman({
|
|
|
|
|
|
action: 'set_tray_unbinding',
|
|
|
|
|
|
param: { tray }
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-25 14:58:37 +08:00
|
|
|
|
export function trayInactivity (tray) {
|
2026-06-25 11:00:20 +08:00
|
|
|
|
return sendWorkerman({
|
|
|
|
|
|
action: 'set_tray_inactivity',
|
2026-06-25 14:58:37 +08:00
|
|
|
|
param: { tray }
|
2026-06-25 11:00:20 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function batteryInactivity (data) {
|
|
|
|
|
|
return sendWorkerman({
|
|
|
|
|
|
action: 'set_battery_inactivity',
|
|
|
|
|
|
param: { data }
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|