xx-worker-applets/src/core/models/system.js

86 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Vue from "vue"
let prototype = Vue.prototype;
export default {
getCity() {
const citys = require("@/static/temp/district.json");
let res = [];
if (citys.length) {
// 递归生成
res = this.handleTree(citys);
}
return res;
},
handleTree(data, parent_code = null) {
let res = [];
let keys = {
id: "code",
pid: "parent_code",
children: "children",
text: "name",
value: "code",
};
let oneItemDEMO = {
text: "",
value: "",
children: [],
};
let oneItem = {};
// 循环
for (let index in data) {
// 判断
if (parent_code === null) {
// 顶级菜单 - 省
if (!data[index].hasOwnProperty(keys.pid) || data[index][keys.pid] == parent_code) {
// 不存在parent_code或者已匹配
oneItem = JSON.parse(JSON.stringify(oneItemDEMO));
oneItem.text = data[index][keys.text];
oneItem.value = data[index][keys.value];
// 递归下去
oneItem.children = this.handleTree(data, data[index][keys.id]);
res.push(oneItem);
} else {
// 匹配不到,跳过
}
} else {
// 非顶级菜单 - 市、区、街道
if (data[index].hasOwnProperty(keys.pid) && data[index][keys.pid] == parent_code) {
// 已匹配
oneItem = JSON.parse(JSON.stringify(oneItemDEMO));
oneItem.text = data[index][keys.text];
oneItem.value = data[index][keys.value];
// 递归下去
// oneItem.children = this.handleTree(data, data[index][keys.id]);
oneItem.children = [];
res.push(oneItem);
} else {
// 匹配不到,跳过
}
}
}
return res;
},
/**
* 上传文件
*/
upload(path) {
return new Promise((resolve, reject) => {
prototype.$upload({
api: "system.uploadFile",
path: path
}).then(response => {
if (response.code == 1) {
return resolve(response.data);
}
return reject(response.msg);
});
});
},
}