refactor: 通过文件形式下载分类结果

This commit is contained in:
马琦钧 2021-08-08 01:28:57 +08:00
parent 4bb5838795
commit e48a7653c7
2 changed files with 28 additions and 8 deletions

View File

@ -102,29 +102,39 @@
<script> <script>
import { getColor } from '../../js/color.js' import { getColor } from '../../js/color.js'
import { saveAsFile } from '../../js/utils.js'
// //
const isLocal = false const isLocal = false
function get (url, cb) { function get (url, cb, config={}) {
query('GET', url, '', cb) query('GET', url, '', cb, config)
} }
function post (url, data, cb) { function post (url, data, cb, config={}) {
query('POST', url, data, cb) query('POST', url, data, cb, config)
} }
function query (method, url, data = '', cb, tryTimes = 0) { function query (method, url, data = '', cb, config = {}) {
console.log('config',config);
config.tryTimes = config.tryTimes || 0
config.isDirect = config.isDirect || false
var xhr = new XMLHttpRequest() var xhr = new XMLHttpRequest()
xhr.open(method, url) xhr.open(method, url)
xhr.setRequestHeader('content-type', 'application/json') xhr.setRequestHeader('content-type', 'application/json')
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) { if (xhr.readyState === 4 && xhr.status === 200) {
if (config.isDirect) {
return cb(xhr.responseText) //
}
const result = JSON.parse(xhr.responseText) const result = JSON.parse(xhr.responseText)
console.log('result',result);
if (result.errCode !== 0) { if (result.errCode !== 0) {
if (tryTimes >= 2) { if (config.tryTimes >= 2) {
alert(result.errMsg) alert(result.errMsg)
} else { } else {
setTimeout(() => { setTimeout(() => {
query(method, url, data = '', cb, tryTimes + 1) config.tryTimes += 1;
query(method, url, data = '', cb, config)
}, 200) }, 200)
} }
} else { } else {
@ -527,7 +537,9 @@ export default {
if (this.projectType === '命名实体识别') if (this.projectType === '命名实体识别')
window.open(`/v1/files/get_json?projectName=${this.projectName}`, '_self') window.open(`/v1/files/get_json?projectName=${this.projectName}`, '_self')
if (this.projectType === '文本分类') if (this.projectType === '文本分类')
window.open(`/v1/files/get_labels?projectName=${this.projectName}`, '_self') get(`/v1/files/get_labels?projectName=${this.projectName}`, function(text){
saveAsFile(text, 'labels.json');
}, {isDirect : true})
return true return true
} }
this.nersCache[this.nowFile] = this.ners this.nersCache[this.nowFile] = this.ners

8
fe/src/js/utils.js Normal file
View File

@ -0,0 +1,8 @@
export function saveAsFile(data, name) {
var urlObject = window.URL || window.webkitURL || window;
var export_blob = new Blob([data]);
var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
save_link.href = urlObject.createObjectURL(export_blob);
save_link.download = name;
save_link.click();
}