| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- import { DataModel } from '@imengyu/js-request-transform';
- import { MengyuServerRequestModule } from '../RequestModules';
- export interface CityItem {
- first_letter: string;
- code: string;
- name: string;
- /**
- * 0:省
- * 1:市
- * 2:区
- */
- type: number;
- children?: CityItem[];
- }
- /** 行政区划层级(与服务端 MapCityData.level 一致) */
- export type MapCityDataLevel = 'province' | 'city' | 'district';
- /**
- * 省市区数据模型
- */
- export class MapCityData extends DataModel<MapCityData> {
- constructor() {
- super(MapCityData, '省市区数据');
- }
- adcode = '';
- parentAdCode: string | null = null;
- citycode = '';
- name = '';
- center = '';
- polyline = '';
- pinyin = '';
- level = '' as MapCityDataLevel;
- }
- export class MapApi extends MengyuServerRequestModule<MapCityData> {
- /**
- * 快速获取地区编码
- * @param name 地区名称
- * @param city 城市名称(可选,用于消歧)
- */
- simpleGetRegionCode(name: string, city?: string) {
- return this.get<string>(
- `/content/map/simpleGetRegionCode`,
- '快速获取地区编码',
- { name, city },
- )
- .then(res => res.requireData() as string)
- .catch(e => { throw e });
- }
- /**
- * 简单获取地区(扁平地区信息)
- * @param name 地区名称
- * @param city 城市名称(可选,用于消歧)
- */
- simpleGetRegion(name: string, city?: string) {
- return this.get<MapCityData>(
- `/content/map/simpleGetRegion`,
- '简单获取地区',
- { name, city },
- MapCityData,
- );
- }
- /**
- * 按编码获取地区详情
- * @param adcode 地区编码
- */
- getRegionInfo(adcode: string) {
- return this.get<MapCityData>(
- `/content/map/getRegionInfo`,
- '按编码获取地区详情',
- { adcode },
- );
- }
- /**
- * 获取子级地区列表
- * @param adcode 地区编码(可选;不传则返回省级列表)
- */
- getRegionChildren(adcode?: string) {
- return this.get<MapCityData[]>(
- `/content/map/getRegionChildren`,
- '获取子级地区列表',
- { adcode },
- );
- }
- loadCityData(full = false) : Promise<CityItem[]> {
- return new Promise((resolve, reject) => {
- uni.request({
- url: `https://mn.wenlvti.net/app_static/xiangyuan/data/ChinaCityData${full ? '' : '.slim'}.json`,
- method: 'GET',
- success(result) {
- if (result.statusCode === 200) {
- resolve(result.data as CityItem[]);
- } else {
- reject(new Error(`请求失败,状态码:${result.statusCode}`));
- }
- },
- fail(error) {
- reject(error);
- }
- })
- });
- }
- }
- export default new MapApi();
|