对接大部分接口

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
}
}