fix loc delbug
This commit is contained in:
parent
5f2ebd3983
commit
858c34232c
@ -1,116 +1,117 @@
|
|||||||
import { Router } from 'express';
|
import {Router} from 'express'
|
||||||
import data from '../../../config/china_area';
|
import data from '../../../config/china_area'
|
||||||
import ChinaRegion from '../../models/snoopy/ChinaRegion';
|
import ChinaRegion from '../../models/snoopy/ChinaRegion'
|
||||||
import ChinaArea from '../../models/snoopy/ChinaArea';
|
import ChinaArea from '../../models/snoopy/ChinaArea'
|
||||||
import GameShareImage from '../../models/snoopy/GameShareImage';
|
import getGameShareImage from '../../models/snoopy/GameShareImage'
|
||||||
import logger from '../../utils/logger'
|
import logger from '../../utils/logger'
|
||||||
|
|
||||||
const router = new Router();
|
const GameShareImage = getGameShareImage()
|
||||||
|
|
||||||
|
const router = new Router()
|
||||||
|
|
||||||
/* 获取省市列表, id为地区英文名*/
|
/* 获取省市列表, id为地区英文名*/
|
||||||
router.get('/china_region', async (req, res, next) => {
|
router.get('/china_region', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const provinces = await ChinaRegion.find({ level: 1 });
|
const provinces = await ChinaRegion.find({level: 1})
|
||||||
const citys = await ChinaRegion.find({ level: 2, pinyin: { $ne: null } });
|
const citys = await ChinaRegion.find({level: 2, pinyin: {$ne: null}})
|
||||||
const result = [];
|
const result = []
|
||||||
const map = new Map();
|
const map = new Map()
|
||||||
const pIdx = {};
|
const pIdx = {}
|
||||||
for (let i = 0; i < provinces.length; i++) {
|
for (let i = 0; i < provinces.length; i++) {
|
||||||
result.push({
|
result.push({
|
||||||
id: provinces[i].pinyin,
|
id: provinces[i].pinyin,
|
||||||
parent: '#',
|
parent: '#',
|
||||||
text: provinces[i].name,
|
text: provinces[i].name,
|
||||||
children: []
|
children: [],
|
||||||
});
|
})
|
||||||
map.set(provinces[i]._id, provinces[i]);
|
map.set(provinces[i]._id, provinces[i])
|
||||||
|
|
||||||
if (!pIdx[provinces[i]._id]) {
|
if (!pIdx[provinces[i]._id]) {
|
||||||
pIdx[provinces[i]._id] = i;
|
pIdx[provinces[i]._id] = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const c of citys) {
|
for (const c of citys) {
|
||||||
if (map.has(c.parent_id)) {
|
if (map.has(c.parent_id)) {
|
||||||
const pIndex = pIdx[c.parent_id];
|
const pIndex = pIdx[c.parent_id]
|
||||||
result[pIndex].children.push({
|
result[pIndex].children.push({
|
||||||
id: `${map.get(c.parent_id).pinyin}_${c.pinyin}`,
|
id: `${map.get(c.parent_id).pinyin}_${c.pinyin}`,
|
||||||
parent: map.get(c.parent_id).pinyin,
|
parent: map.get(c.parent_id).pinyin,
|
||||||
text: c.name
|
text: c.name,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res.json({
|
res.json({
|
||||||
errcode: 0,
|
errcode: 0,
|
||||||
records: result
|
records: result,
|
||||||
});
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err);
|
next(err)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
/* 获取所有定义好的地域列表*/
|
/* 获取所有定义好的地域列表*/
|
||||||
router.get('/china_area', async (req, res, next) => {
|
router.get('/china_area', async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const records = await ChinaArea.find({ deleted: false });
|
const records = await ChinaArea.find({deleted: false})
|
||||||
res.json({ errcode: 0, records: records });
|
res.json({errcode: 0, records: records})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err);
|
next(err)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
/* 更新*/
|
/* 更新*/
|
||||||
router.post('/china_area', async (req, res, next) => {
|
router.post('/china_area', async (req, res, next) => {
|
||||||
logger.db(req, '公共', '地域', '保存地域');
|
logger.db(req, '公共', '地域', '保存地域')
|
||||||
const body = req.body;
|
const body = req.body
|
||||||
const id = body.id;
|
const id = body.id
|
||||||
const name = body.name;
|
const name = body.name
|
||||||
const locations = body.locations;
|
const locations = body.locations
|
||||||
let record;
|
let record
|
||||||
try {
|
try {
|
||||||
if (id) {
|
if (id) {
|
||||||
record = await ChinaArea.findById(id);
|
record = await ChinaArea.findById(id)
|
||||||
} else {
|
} else {
|
||||||
record = new ChinaArea({});
|
record = new ChinaArea({})
|
||||||
}
|
}
|
||||||
record.name = name;
|
record.name = name
|
||||||
record.locations = locations;
|
record.locations = locations
|
||||||
await record.save();
|
await record.save()
|
||||||
res.json({ errcode: 0, record: record });
|
res.json({errcode: 0, record: record})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err);
|
next(err)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
/* 删除*/
|
/* 删除*/
|
||||||
router.delete('/china_area', async (req, res, next) => {
|
router.delete('/china_area', async (req, res, next) => {
|
||||||
logger.db(req, '公共', '地域', '删除地域');
|
logger.db(req, '公共', '地域', '删除地域')
|
||||||
const body = req.body;
|
const body = req.body
|
||||||
const id = body.id;
|
const id = body.id
|
||||||
try {
|
try {
|
||||||
if (!id) {
|
if (!id) {
|
||||||
return res.json({ errcode: 101, errmsg: '未定义要删除的地域id' });
|
return res.json({errcode: 101, errmsg: '未定义要删除的地域id'})
|
||||||
}
|
}
|
||||||
const record = await ChinaArea.findById(id);
|
const record = await ChinaArea.findById(id)
|
||||||
if (!record) {
|
if (!record) {
|
||||||
return res.json({ errcode: 102, errmsg: '未找到要删除的地域' });
|
return res.json({errcode: 102, errmsg: '未找到要删除的地域'})
|
||||||
}
|
}
|
||||||
const usedRecord = await GameShareImage.findOne({
|
const usedRecord = await GameShareImage.findOne({
|
||||||
area: id,
|
area: id,
|
||||||
deleted: false
|
deleted: false,
|
||||||
});
|
})
|
||||||
if (usedRecord) {
|
if (usedRecord) {
|
||||||
return res.json({
|
return res.json({
|
||||||
errcode: 103,
|
errcode: 103,
|
||||||
errmsg: '要删除的记录已被使用,不能删除!'
|
errmsg: '要删除的记录已被使用,不能删除!',
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
record.deleted = true;
|
record.deleted = true
|
||||||
record.delete_time = new Date();
|
record.delete_time = new Date()
|
||||||
await record.save();
|
await record.save()
|
||||||
res.json({ errcode: 0, record: record });
|
res.json({errcode: 0, record: record})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err);
|
next(err)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
export default router;
|
export default router
|
||||||
|
Loading…
x
Reference in New Issue
Block a user