封装实时接口,请求,共多个页面调用
This commit is contained in:
@ -2,15 +2,67 @@
|
||||
* 生成短ID (16位字符)
|
||||
*/
|
||||
export const generateShortId = () => {
|
||||
const crypto = window.crypto || window.msCrypto;
|
||||
|
||||
if (crypto?.getRandomValues) {
|
||||
return Array.from(crypto.getRandomValues(new Uint32Array(3)))
|
||||
.map(n => n.toString(36))
|
||||
.join('')
|
||||
.slice(0, 16);
|
||||
}
|
||||
|
||||
return Date.now().toString(36) + Math.random().toString(36).substr(2, 8);
|
||||
const crypto = window.crypto || window.msCrypto;
|
||||
|
||||
if (crypto?.getRandomValues) {
|
||||
return Array.from(crypto.getRandomValues(new Uint32Array(3)))
|
||||
.map(n => n.toString(36))
|
||||
.join('')
|
||||
.slice(0, 16);
|
||||
}
|
||||
|
||||
return Date.now().toString(36) + Math.random().toString(36).substr(2, 8);
|
||||
};
|
||||
export default generateShortId;
|
||||
export default generateShortId;
|
||||
|
||||
// 获取设备实时状态的
|
||||
/**
|
||||
* 获取设备状态(带自动轮询)
|
||||
* @param {Object} options - 配置对象
|
||||
* @param {number} options.functionMode - 功能模式
|
||||
* @param {string} options.batchId - 批次ID
|
||||
* @param {Object} options.sendInfo - 设备信息
|
||||
* @param {string} options.sendInfo.typeName - 类型名称
|
||||
* @param {string} options.sendInfo.deviceImei - 设备IMEI
|
||||
* @param {number} [options.interval=500] - 轮询间隔(毫秒)
|
||||
* @param {Function} apiClient - 接口调用函数
|
||||
*/
|
||||
export async function getdeviceSTatus({
|
||||
functionMode,
|
||||
batchId,
|
||||
typeName,
|
||||
deviceImei,
|
||||
interval
|
||||
}, apiClient) {
|
||||
const checkStatus = async () => {
|
||||
try {
|
||||
const res = await apiClient({
|
||||
functionMode,
|
||||
batchId,
|
||||
typeName,
|
||||
deviceImei
|
||||
});
|
||||
if (res.code !== 200) {
|
||||
throw new Error(res.msg || '请求失败');
|
||||
}
|
||||
switch (res.data.functionAccess) {
|
||||
case 'OK':
|
||||
return res;
|
||||
case 'ACTIVE':
|
||||
await new Promise(r => setTimeout(r, interval));
|
||||
return checkStatus();
|
||||
case 'FAILED':
|
||||
throw new Error('设备操作失败');
|
||||
case 'TIMEOUT':
|
||||
throw new Error('设备响应超时');
|
||||
default:
|
||||
throw new Error('未知状态');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('设备状态轮询错误:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
return checkStatus();
|
||||
}
|
Reference in New Issue
Block a user