对接大部分接口

This commit is contained in:
aixianling
2023-10-23 10:48:30 +08:00
parent 189e7621ad
commit 8c07c9a6cb
5 changed files with 232 additions and 139 deletions

View File

@@ -2,113 +2,113 @@
* Area类用于处理地区相关信息
*/
export default class Area {
/**
* 构造函数
* @param {string} code 地区编码
* @param {Object} hash 哈希表
*/
constructor(code, hash = {}) {
this.id = code
this.level = Area.getLevelByAreaId(code)
this.areaMap = Object.values(this.getAreaInfo(code))
if (Object.keys(hash).length > 0) {
this.getName(this.areaMap.map(id => hash[id]))
/**
* 构造函数
* @param {string} code 地区编码
* @param {Object} hash 哈希表
*/
constructor(code, hash = {}) {
this.id = code
this.level = Area.getLevelByAreaId(code)
this.areaMap = Object.values(this.getAreaInfo(code))
if (Object.keys(hash).length > 0) {
this.getName(this.areaMap.map(id => hash[id]))
}
}
}
/**
* 获取地区的行政等级
* @param {string} code 地区编码
* @returns {number}
*/
static getLevelByAreaId(code) {
if (code) {
if (code.length === 2 || /0{10}$/.test(code)) return 0;
else if (/0{8}$/.test(code)) return 1;
else if (/0{6}$/.test(code)) return 2;
else if (/0{3}$/.test(code)) return 3;
else return 4
} else return -1
}
/**
* 根据地区编码获取指定等级的地区编码
* @param {string} value 地区编码
* @param {number} level 指定等级
* @returns {string|null|*}
*/
static getAreaCodeByLevel(value, level) {
if (value) {
const areaNumber = value.toString();
switch (level) {
case 0:
return areaNumber.substring(0, 2) + '0000000000';
case 1:
return areaNumber.substring(0, 4) + '00000000';
case 2:
return areaNumber.substring(0, 6) + '000000';
case 3:
return areaNumber.substring(0, 9) + '000';
case 4:
return areaNumber
}
} else return null
}
/**
* 根据地区id获取所有父级地区id
* @param {string} areaId 地区id
* @param {Object} ins 请求实例
* @param {string} action 请求地址
* @returns {Promise<Array>} 所有父级地区id
*/
static createByAction(areaId, ins = request, action = "/admin/area/getAllParentAreaId") {
return ins.post(action, null, {params: {areaId}, withoutToken: 1}).then(res => res?.data?.reverse() || [])
}
/**
* 获取地区信息
* @param {string} id 地区id
* @returns {Object} 地区信息
*/
getAreaInfo(id) {
let info = {}
const currentLevel = Area.getLevelByAreaId(id);
for (let i = 0; i <= currentLevel; i++) {
info[i] = Area.getAreaCodeByLevel(id, i);
/**
* 获取地区的行政等级
* @param {string} code 地区编码
* @returns {number}
*/
static getLevelByAreaId(code) {
if (code) {
if (code.length === 2 || /0{10}$/.test(code)) return 0;
else if (/0{8}$/.test(code)) return 1;
else if (/0{6}$/.test(code)) return 2;
else if (/0{3}$/.test(code)) return 3;
else return 4
} else return -1
}
return info
}
/**
* 异步获取地区名称
* @returns {Promise<void>}
*/
async getAreaName() {
const names = await Area.createByAction(this.id);
this.getName(names)
}
/**
* 根据地区编码获取指定等级的地区编码
* @param {string} value 地区编码
* @param {number} level 指定等级
* @returns {string|null|*}
*/
static getAreaCodeByLevel(value, level) {
if (value) {
const areaNumber = value.toString();
switch (level) {
case 0:
return areaNumber.substring(0, 2) + '0000000000';
case 1:
return areaNumber.substring(0, 4) + '00000000';
case 2:
return areaNumber.substring(0, 6) + '000000';
case 3:
return areaNumber.substring(0, 9) + '000';
case 4:
return areaNumber
}
} else return null
}
/**
* 获取地区名称
* @param {Array} names 地区名称数组
*/
getName(names) {
this.meta = names
this.nameMap = names?.map(e => e?.name) || []
this.name = names?.slice(-1)?.[0]?.name
this.fullname = this.nameMap.join('')
}
/**
* 根据地区id获取所有父级地区id
* @param {string} areaId 地区id
* @param {Object} ins 请求实例
* @param {string} action 请求地址
* @returns {Promise<Array>} 所有父级地区id
*/
static createByAction(areaId, ins = request, action = "/admin/area/getAllParentAreaId") {
return ins.post(action, null, {params: {areaId}, withoutToken: 1}).then(res => res?.data?.reverse() || [])
}
/**
* 异步从数据库中获取地区信息
* @param {string} code 地区编码
* @returns {Promise<Area>} Area实例
*/
static async init(code) {
const names = await Area.createByAction(code),
area = new Area(code)
area.getName(names)
return area
}
/**
* 获取地区信息
* @param {string} id 地区id
* @returns {Object} 地区信息
*/
getAreaInfo(id) {
let info = {}
const currentLevel = Area.getLevelByAreaId(id);
for (let i = 0; i <= currentLevel; i++) {
info[i] = Area.getAreaCodeByLevel(id, i);
}
return info
}
/**
* 异步获取地区名称
* @returns {Promise<void>}
*/
async getAreaName() {
const names = await Area.createByAction(this.id);
this.getName(names)
}
/**
* 获取地区名称
* @param {Array} names 地区名称数组
*/
getName(names) {
this.meta = names
this.nameMap = names?.map(e => e?.name) || []
this.name = names?.slice(-1)?.[0]?.name || "无"
this.fullname = this.nameMap.join('')
}
/**
* 异步从数据库中获取地区信息
* @param {string} code 地区编码
* @returns {Promise<Area>} Area实例
*/
static async init(code) {
const names = await Area.createByAction(code),
area = new Area(code)
area.getName(names)
return area
}
}

View File

@@ -12,14 +12,14 @@
</div>
<el-button v-else-if="!customClicker&&!$scopedSlots.default" class="area-btn" type="primary" size="mini"
@click="chooseArea">
{{ btnShowArea ? selectedName : "切换地区" }}
{{ btnShowArea ? selectedArea.name : "切换地区" }}
</el-button>
<a class="custom-clicker" v-else @click="chooseArea">
<slot :areaname="selectedName" :fullname="fullName" :id="selected"/>
<slot :areaname="selectedArea.name" :fullname="fullName" :id="selected"/>
</a>
<ai-dialog :visible.sync="dialog" title="选择地区" width="60%" @onConfirm="confirmArea" :modal="mask"
@open="selected=(value||'')">
<ai-highlight content="您当前选择&nbsp;@v" :value="selectedName" color="#333" bold/>
<ai-highlight content="您当前选择&nbsp;@v" :value="selectedArea.name" color="#333" bold/>
<div class="area_edge">
<div class="area-box" v-for="ops in showOps">
<h2 v-text="ops.header"/>
@@ -85,7 +85,6 @@ export default {
startLevel: v => Math.max(Number(v.hideLevel), 0, v.rootArea.level),//地区最高可选行政地区等级
endLevel: v => Number(v.areaLevel) || 0,//地区最低可选行政地区等级
selectedArea: v => new Area(v.currentArea, v.hashMap),
selectedName: v => v.selectedArea.name || "无",
selectedMap: v => v.selectedArea.areaMap,
validateState: v => ['', 'success'].includes(v.elFormItem?.validateState),
hashMap() {
@@ -230,7 +229,7 @@ export default {
Area.createByAction(this.currentArea, this.instance).then(names => {
this.selectedArea.getName(names)
this.fullName = this.selectedArea.nameMap.join(this.separator)
this.$emit("update:name", this.selectedName)
this.$emit("update:name", this.selectedArea.name)
this.$emit("fullname", this.fullName)
})
}