This commit is contained in:
aixianling
2023-03-23 18:19:52 +08:00
parent d4f1c04f07
commit 3a01fa9011
2 changed files with 39 additions and 10 deletions

View File

@@ -1,7 +1,12 @@
import request from "./request";
/**
* Area类用于处理地区相关信息
*/
export default class Area {
/**
* 构造函数
* @param {string} code 地区编码
* @param {Object} hash 哈希表
*/
constructor(code, hash = {}) {
this.id = code
this.level = Area.getLevelByAreaId(code)
@@ -13,7 +18,7 @@ export default class Area {
/**
* 获取地区的行政等级
* @param code 地区编码
* @param {string} code 地区编码
* @returns {number}
*/
static getLevelByAreaId(code) {
@@ -28,8 +33,8 @@ export default class Area {
/**
* 根据地区编码获取指定等级的地区编码
* @param value 地区编码
* @param level 指定等级
* @param {string} value 地区编码
* @param {number} level 指定等级
* @returns {string|null|*}
*/
static getAreaCodeByLevel(value, level) {
@@ -50,10 +55,22 @@ export default class Area {
} 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);
@@ -63,21 +80,30 @@ export default class Area {
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.nameMap = names?.map(e => e?.name) || []
this.name = names?.slice(-1)?.[0]?.name
this.fullname = this.nameMap.join('')
}
/**
* 异步从数据库中获取地区信息
* @returns {Promise<void>}
* @param {string} code 地区编码
* @returns {Promise<Area>} Area实例
*/
static async init(code) {
const names = await Area.createByAction(code),