2021-09-23 00:16:04 +08:00
|
|
|
import { Plugin } from '@nocobase/server';
|
2022-02-12 12:38:57 +08:00
|
|
|
import { resolve } from 'path';
|
2021-02-22 15:08:35 +08:00
|
|
|
|
2023-08-02 00:07:52 +08:00
|
|
|
function getChinaDivisionData(key: string) {
|
|
|
|
try {
|
|
|
|
require.resolve(`../china-division/${key}.json`);
|
|
|
|
return require(`../china-division/${key}.json`);
|
|
|
|
} catch (error) {
|
|
|
|
return require(`china-division/dist/${key}.json`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 21:19:57 +08:00
|
|
|
export class PluginChinaRegion extends Plugin {
|
2022-02-28 21:49:50 +08:00
|
|
|
async install() {
|
|
|
|
await this.importData();
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|
2021-12-06 21:23:34 +08:00
|
|
|
|
2022-02-07 01:14:00 +08:00
|
|
|
async load() {
|
|
|
|
await this.db.import({
|
2022-02-12 12:38:57 +08:00
|
|
|
directory: resolve(__dirname, 'collections'),
|
2021-09-23 00:16:04 +08:00
|
|
|
});
|
2023-01-09 07:35:48 +08:00
|
|
|
|
2022-04-24 10:14:46 +08:00
|
|
|
this.app.acl.allow('chinaRegions', 'list', 'loggedIn');
|
2023-01-09 07:35:48 +08:00
|
|
|
|
|
|
|
this.app.resourcer.use(async (ctx, next) => {
|
|
|
|
const { resourceName, actionName } = ctx.action.params;
|
|
|
|
|
|
|
|
if (resourceName == 'chinaRegions' && actionName !== 'list') {
|
|
|
|
ctx.throw(404, 'Not Found');
|
|
|
|
} else {
|
|
|
|
await next();
|
|
|
|
}
|
|
|
|
});
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|
2022-02-12 12:38:57 +08:00
|
|
|
|
|
|
|
async importData() {
|
2023-08-02 00:07:52 +08:00
|
|
|
const areas = getChinaDivisionData('areas');
|
|
|
|
const cities = getChinaDivisionData('cities');
|
|
|
|
const provinces = getChinaDivisionData('provinces');
|
2023-01-12 10:12:37 +08:00
|
|
|
|
2022-02-12 12:38:57 +08:00
|
|
|
const timer = Date.now();
|
|
|
|
const ChinaRegion = this.db.getModel('chinaRegions');
|
|
|
|
|
|
|
|
await ChinaRegion.bulkCreate(
|
|
|
|
provinces.map((item) => ({
|
|
|
|
code: item.code,
|
|
|
|
name: item.name,
|
|
|
|
level: 1,
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
|
|
|
|
await ChinaRegion.bulkCreate(
|
|
|
|
cities.map((item) => ({
|
|
|
|
code: item.code,
|
|
|
|
name: item.name,
|
|
|
|
level: 2,
|
|
|
|
parentCode: item.provinceCode,
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
|
|
|
|
await ChinaRegion.bulkCreate(
|
|
|
|
areas.map((item) => ({
|
|
|
|
code: item.code,
|
|
|
|
name: item.name,
|
|
|
|
level: 3,
|
|
|
|
parentCode: item.cityCode,
|
|
|
|
})),
|
|
|
|
);
|
|
|
|
|
|
|
|
// // 乡级数据 2856 条
|
|
|
|
// await ChinaRegion.bulkCreate(streets.map(item => ({
|
|
|
|
// code: item.code,
|
|
|
|
// name: item.name,
|
|
|
|
// level: 4,
|
|
|
|
// parentCode: item.areaCode
|
|
|
|
// })));
|
|
|
|
|
|
|
|
// // 村级数据 658001 条
|
|
|
|
// await ChinaRegion.bulkCreate(villages.map(item => ({
|
|
|
|
// code: item.code,
|
|
|
|
// name: item.name,
|
|
|
|
// level: 5,
|
|
|
|
// parentCode: item.streetCode
|
|
|
|
// })));
|
|
|
|
|
|
|
|
const count = await ChinaRegion.count();
|
2022-04-29 00:09:40 +08:00
|
|
|
// console.log(`${count} rows of region data imported in ${(Date.now() - timer) / 1000}s`);
|
2022-02-12 12:38:57 +08:00
|
|
|
}
|
2022-02-07 01:14:00 +08:00
|
|
|
}
|
2022-02-12 12:38:57 +08:00
|
|
|
|
2022-06-24 21:19:57 +08:00
|
|
|
export default PluginChinaRegion;
|