78 lines
1.9 KiB
Vue
78 lines
1.9 KiB
Vue
<template>
|
|
<div>
|
|
<div v-if="jsonData">
|
|
<pre>{{ jsonData }}</pre>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref } from 'vue';
|
|
import axios from 'axios';
|
|
import { fetchToken } from '../utils/getToken'; // 导入 fetchToken 函数
|
|
|
|
const token = ref('');
|
|
const data = ref('')
|
|
const jsonData = ref(null);
|
|
|
|
// 在组件加载时调用 fetchToken 函数
|
|
fetchToken().then((response) => {
|
|
if (response) {
|
|
token.value = response;
|
|
fetchData(response); // 获取 token 后调用 fetchData 函数
|
|
}
|
|
});
|
|
|
|
// 加入token获取数据
|
|
const fetchData = async (token) => {
|
|
try {
|
|
const response = await axios.get(`api/seeyon/rest/dee/task/getMineral?token=${token.id}`);
|
|
data.value = response.data;
|
|
const parser = new DOMParser();
|
|
const xmlDoc = parser.parseFromString(response.data, 'text/xml');
|
|
const json = xmlToJson(xmlDoc);
|
|
jsonData.value = JSON.stringify(json, null, 2);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
};
|
|
|
|
// 将 XML 转换为 JSON
|
|
function xmlToJson(xml) {
|
|
let obj = {};
|
|
|
|
if (xml.nodeType === 1) { // element
|
|
// do attributes
|
|
if (xml.attributes.length > 0) {
|
|
obj['@attributes'] = {};
|
|
for (let j = 0; j < xml.attributes.length; j++) {
|
|
const attribute = xml.attributes.item(j);
|
|
obj['@attributes'][attribute.nodeName] = attribute.nodeValue;
|
|
}
|
|
}
|
|
} else if (xml.nodeType === 3) { // text
|
|
obj = xml.nodeValue;
|
|
}
|
|
|
|
// do children
|
|
if (xml.hasChildNodes()) {
|
|
for (let i = 0; i < xml.childNodes.length; i++) {
|
|
const item = xml.childNodes.item(i);
|
|
const nodeName = item.nodeName;
|
|
if (typeof obj[nodeName] === 'undefined') {
|
|
obj[nodeName] = xmlToJson(item);
|
|
} else {
|
|
if (typeof obj[nodeName].push === 'undefined') {
|
|
const old = obj[nodeName];
|
|
obj[nodeName] = [];
|
|
obj[nodeName].push(old);
|
|
}
|
|
obj[nodeName].push(xmlToJson(item));
|
|
}
|
|
}
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
</script>
|