class WvpService { constructor() { this.baseURL = import.meta.env.VITE_APP_WVP_API; this.token = ''; this.login() } login() { return fetch(`${this.baseURL}/api/user/login?username=admin&password=21232f297a57a5a743894a0e4a801fc3`) .then(response => response.json()) .then(({ data }) => { this.token = data.accessToken; }) .catch(error => { console.error('Error:', error); }); } async deviceList() { if (!this.token) { await this.login(); } return fetch(`${this.baseURL}/api/device/query/devices?page=1&count=100`, { headers: { 'Access-Token': `${this.token}` } }) .then(response => response.json()) .then(data => { if (data.code === 401) { // this.login(); } else { return data.data.list; } }) .catch(error => { console.error('Error:', error); }); } getTree(deviceId, parentId, onlyCatalog, callback, endCallback, errorCallback) { let currentPage = 1; let count = 100; let catalogList = [] this.getTreeIteration(deviceId, parentId, onlyCatalog, catalogList, currentPage, count, callback, endCallback, errorCallback) } getTreeIteration(deviceId, parentId, onlyCatalog, catalogList, currentPage, count, callback, endCallback, errorCallback) { this.getTreeInfo(deviceId, parentId, onlyCatalog, currentPage, count, (data) => { console.log(data) if (data.code === 0 && data.data.list) { if (typeof (callback) == "function") callback(data.data.list) catalogList = catalogList.concat(data.data.list); if (catalogList.length < data.data.total) { currentPage++ this.getTreeIteration(deviceId, parentId, onlyCatalog, catalogList, currentPage, count, callback, endCallback, errorCallback) } else { if (typeof (endCallback) == "function") endCallback(catalogList) } } }, errorCallback) } getTreeInfo(deviceId, parentId, onlyCatalog, currentPage, count, callback, errorCallback) { if (onlyCatalog == null || typeof onlyCatalog === "undefined") { onlyCatalog = false; } return fetch(`${this.baseURL}/api/device/query/tree/${deviceId}?page=${currentPage}&count=${count}&parentId=${parentId}&onlyCatalog=${onlyCatalog}`, { headers: { 'Access-Token': `${this.token}` } }) .then(response => response.json()) .then((res) => { if (typeof (callback) == "function") callback(res) }).catch(errorCallback); } startPlay(deviceId, channelId) { return fetch(`${this.baseURL}/api/play/start/${deviceId}/${channelId}`, { headers: { 'Access-Token': `${this.token}` } }) .then(response => response.json()) } } export default new WvpService();