完成接口调用

This commit is contained in:
xiaozhang 2024-02-22 08:42:07 +08:00
parent 02ae71d10b
commit b0e980235d
4 changed files with 102 additions and 18 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ pnpm-debug.log*
lerna-debug.log*
node_modules
.vscode
.DS_Store
dist
dist-ssr

View File

@ -1,8 +1,18 @@
<template>
<div>
<p style="color: blue; font-size: 24px;">贵州智信云科技有限公司</p>
<div v-if="jsonData">
<pre>{{ jsonData }}</pre>
<div v-for="(field, fieldName) in jsonData.root.kczy.row[0]" :key="fieldName">
<p v-if="getFieldLabel(fieldName)">{{ getFieldLabel(fieldName) }}: {{ field['#text'] }}</p>
</div>
</div>
<!-- <div>
<div v-if="jsonData">
<div v-for="(field, fieldName) in jsonData.root.kczy.row[0]" :key="fieldName">
<p>{{ fieldName }}: {{ field['#text'] }}</p>
</div>
</div>
</div> -->
</div>
</template>
@ -12,7 +22,7 @@ import axios from 'axios';
import { fetchToken } from '../utils/getToken'; // fetchToken
const token = ref('');
const data = ref('')
const data = ref('');
const jsonData = ref(null);
// fetchToken
@ -23,7 +33,7 @@ fetchToken().then((response) => {
}
});
// token
// token
const fetchData = async (token) => {
try {
const response = await axios.get(`api/seeyon/rest/dee/task/getMineral?token=${token.id}`);
@ -31,7 +41,8 @@ const fetchData = async (token) => {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(response.data, 'text/xml');
const json = xmlToJson(xmlDoc);
jsonData.value = JSON.stringify(json, null, 2);
jsonData.value = json;
console.log(jsonData.value);
} catch (error) {
console.error(error);
}
@ -74,4 +85,19 @@ function xmlToJson(xml) {
return obj;
}
//
function getFieldLabel(fieldName) {
const fieldLabels = {
FIELD0033: '地址',
FIELD0013: '规划 ',
FIELD0035: '联系人',
FIELD0037: '电话',
FIELD0006: '建设条件',
FIELD0007: '产业条件',
ID: 'id',
//
};
return fieldLabels[fieldName] || '';
}
</script>

11
src/views/show.vue Normal file
View File

@ -0,0 +1,11 @@
<template>
</template>
<script setup>
</script>
<style scoped>
</style>

View File

@ -1,17 +1,29 @@
<template>
<div>
<div v-if="jsonData">{{ jsonData }}</div>
<div v-if="jsonData">
<div v-for="(field, fieldName) in jsonData.root.kczy.row[0]" :key="fieldName">
<p v-if="fieldLabels[fieldName]">{{ fieldLabels[fieldName] }}: {{ field['#text'] }}</p>
</div>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue';
import axios from 'axios';
import xml2js from 'xml2js';
import { fetchToken } from '../utils/getToken'; // fetchToken
const token = ref('');
const jsonData = ref('');
const data = ref('');
const jsonData = ref(null);
//
const fieldLabels = {
FIELD0013: '计划',
FIELD0033: '地址',
FIELD0006: '建设条件',
FIELD0007: '产业条件'
//
};
// fetchToken
fetchToken().then((response) => {
@ -21,22 +33,56 @@ fetchToken().then((response) => {
}
});
// token
// token
const fetchData = async (token) => {
try {
const response = await axios.get(`api/seeyon/rest/dee/task/getMineral?token=${token.id}`);
const xmlData = response.data;
xml2js.parseString(xmlData, (err, result) => {
if (err) {
console.error(err);
return;
} else {
jsonData.value = JSON.stringify(result);
}
});
data.value = response.data;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(response.data, 'text/xml');
const json = xmlToJson(xmlDoc);
jsonData.value = json;
console.log(jsonData.value); // jsonData.value
} 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>