From 66b5ef4aa9752f49e6c7c1a42e13aa896f042e55 Mon Sep 17 00:00:00 2001 From: yulixing Date: Wed, 7 Aug 2019 11:05:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=A2=E6=9C=8D=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/common/loc.js | 88 ++--------------- src/controllers/common/minigame.js | 49 ++++----- src/controllers/games/ad.js | 151 +++++----------------------- src/controllers/games/data.js | 18 +++- src/controllers/games/games.js | 8 +- src/controllers/games/lib.js | 6 +- src/controllers/games/platforms.js | 6 +- src/controllers/games/reward.js | 153 +++++++++++++++++++++++++---- src/controllers/sys/logs.js | 2 +- src/controllers/sys/permission.js | 72 +++++++------- 10 files changed, 250 insertions(+), 303 deletions(-) diff --git a/src/controllers/common/loc.js b/src/controllers/common/loc.js index c422514..bb4b368 100644 --- a/src/controllers/common/loc.js +++ b/src/controllers/common/loc.js @@ -7,85 +7,9 @@ import logger from '../../utils/logger' const router = new Router(); -const parseAreaCode = code => { - code = code + ''; - const p = code.substring(0, 2); - const c = code.substring(2, 4); - const d = code.substring(4, 6); - return { - province: p + '0000', - city: p + c + '00', - district: p + c + d - }; -}; -/* 根据区域代码, 判断代码类型*/ -const areaCodeType = code => { - code = code + ''; - const p = code.substring(0, 2); - const c = code.substring(2, 4); - const d = code.substring(4, 6); - if (c === '00' && d === '00') { - return 'province'; - } else if (d === '00' && c !== '00') { - return 'city'; - } else { - return 'district'; - } -}; -const getNameById = aid => { - const obj = parseAreaCode(aid); - const province = data['86'][obj.province]; - const city = data[obj.province][obj.city]; - const district = data[obj.city][obj.district]; - let result = province; - if (city) { - result += '-' + city; - } - if (district) { - result += '-' + district; - } - return result; -}; -router.get('/loc/all', (req, res, next) => { - const country = '86'; - const result = []; - for (const province in data['86']) { - result.push({ - id: province, - parent: '#', - text: data[country][province] - }); - for (const city in data[province]) { - result.push({ - id: city, - parent: province, - text: data[province][city] - }); - for (const district in data[city]) { - result.push({ - id: district, - parent: city, - text: data[city][district] - }); - } - } - } - res.json(result); -}); -router.get('/loc/sub/:aid', (req, res, next) => { - let areaId = req.params.aid; - if (!areaId) { - areaId = '86'; - } - const records = data[areaId]; - if (records) { - res.json({ success: true, records: records }); - } else { - res.json({ success: false }); - } -}); + /* 获取省市列表, id为地区英文名*/ -router.get('/china_region/py_list_all', async (req, res, next) => { +router.get('/china_region', async (req, res, next) => { try { const provinces = await ChinaRegion.find({ level: 1 }); const citys = await ChinaRegion.find({ level: 2, pinyin: { $ne: null } }); @@ -125,7 +49,7 @@ router.get('/china_region/py_list_all', async (req, res, next) => { }); /* 获取所有定义好的地域列表*/ -router.get('/china_area/all', async (req, res, next) => { +router.get('/china_area', async (req, res, next) => { try { const records = await ChinaArea.find({ deleted: false }); res.json({ errcode: 0, records: records }); @@ -133,8 +57,9 @@ router.get('/china_area/all', async (req, res, next) => { next(err); } }); + /* 更新*/ -router.post('/china_area/save', async (req, res, next) => { +router.post('/china_area', async (req, res, next) => { logger.db(req, '公共', '地域', '保存地域'); const body = req.body; const id = body.id; @@ -155,8 +80,9 @@ router.post('/china_area/save', async (req, res, next) => { next(err); } }); + /* 删除*/ -router.post('/china_area/delete', async (req, res, next) => { +router.delete('/china_area', async (req, res, next) => { logger.db(req, '公共', '地域', '删除地域'); const body = req.body; const id = body.id; diff --git a/src/controllers/common/minigame.js b/src/controllers/common/minigame.js index b26ed2e..18ecdfa 100644 --- a/src/controllers/common/minigame.js +++ b/src/controllers/common/minigame.js @@ -1,53 +1,56 @@ -import { Router } from 'express'; +import {Router} from 'express' import getGameInfoModel from '../../models/admin/GameInfo' -import logger from '../../utils/logger'; +import logger from '../../utils/logger' const GameInfo = getGameInfoModel() -const router = new Router(); +const router = new Router() // 接收ftp账号 router.post('/minigame', async (req, res, next) => { - logger.db(req, '游戏管理', '游戏详情', 'FTP账号创建'); - const body = req.body; + logger.db(req, '游戏管理', '游戏详情', 'FTP账号创建') + const body = req.body try { if (body.type === 'minigame_ftp') { - const search = await GameInfo.findOne({ game_id: body.game_id, deleted: false }); + const search = await GameInfo.findOne({ + game_id: body.game_id, + deleted: false, + }) if (search) { // 已有账号 存储相应平台ftp信息 - const platforms = search.platforms; + const platforms = search.platforms for (let i = 0; i < platforms.length; i++) { if (platforms[i].platform.name_en === body.platform) { platforms[i].ftp = {} - platforms[i].ftp.ftp_user = body.ftp_user; - platforms[i].ftp.ftp_pass = body.ftp_pass; - break; + platforms[i].ftp.ftp_user = body.ftp_user + platforms[i].ftp.ftp_pass = body.ftp_pass + break } } const result = await GameInfo.updateOne( - { game_id: body.game_id, deleted: false }, + {game_id: body.game_id, deleted: false}, { - platforms + platforms, } - ); + ) res.send({ - errcode: 0 - }); + errcode: 0, + }) } else { res.send({ errcode: 1, - errmsg: '游戏不存在!' - }); + errmsg: '游戏不存在!', + }) } } else { + console.log(body) res.send({ - errcode: 1, - msg: '信息有误!' - }); + errcode: 0, + }) } } catch (err) { - next(err); + next(err) } -}); +}) -export default router; +export default router diff --git a/src/controllers/games/ad.js b/src/controllers/games/ad.js index 92e6269..cb87273 100644 --- a/src/controllers/games/ad.js +++ b/src/controllers/games/ad.js @@ -10,8 +10,10 @@ const router = new Router() const adApiUrl = config.ad_api +// ---------------------------------------------- // + // 获取单个游戏广告位 -router.get('/get-pos', async (req, res, next) => { +router.get('/pos', async (req, res, next) => { // 权限判断 const hasPerm = req.user.permissions.includes(`${req.query.uid}-readable`) || @@ -55,7 +57,7 @@ router.get('/get-pos', async (req, res, next) => { }) // 为游戏添加广告位 -router.post('/add-pos', async (req, res, next) => { +router.post('/pos', async (req, res, next) => { // 权限判断 const hasPerm = req.user.permissions.includes(`${req.body.uid}-edit`) || @@ -102,7 +104,7 @@ router.post('/add-pos', async (req, res, next) => { // 修改广告位信息 -router.put('/modify-pos', async (req, res, next) => { +router.put('/pos', async (req, res, next) => { // 权限判断 const hasPerm = req.user.permissions.includes(`${req.body.uid}-edit`) || @@ -153,7 +155,7 @@ router.put('/modify-pos', async (req, res, next) => { // 删除广告位信息 -router.delete('/del-pos', async (req, res, next) => { +router.delete('/pos', async (req, res, next) => { // 权限判断 const hasPerm = req.user.permissions.includes(`${req.body.uid}-edit`) || @@ -194,8 +196,11 @@ router.delete('/del-pos', async (req, res, next) => { } }) +// ---------------------------------------------- // +// ---------------------------------------------- // + // 获取广告区域列表 -router.get('/get-area', async (req, res, next) => { +router.get('/area', async (req, res, next) => { try { const result = await AdArea.find({}) res.send({ @@ -208,7 +213,7 @@ router.get('/get-area', async (req, res, next) => { }) // 新增区域 -router.post('/save-area', async (req, res, next) => { +router.post('/area', async (req, res, next) => { logger.db(req, '游戏管理', '广告区域管理', '新增广告区域') // 权限判断 const hasPerm = req.user.permissions.includes(`ad-area-writeable`) @@ -242,7 +247,7 @@ router.post('/save-area', async (req, res, next) => { }) // 编辑区域信息 -router.post('/edit-area', async (req, res, next) => { +router.put('/area', async (req, res, next) => { logger.db(req, '游戏管理', '广告区域管理', '修改广告区域信息') // 权限判断 const hasPerm = req.user.permissions.includes(`ad-area-writeable`) @@ -281,7 +286,7 @@ router.post('/edit-area', async (req, res, next) => { }) // 编辑区域信息 -router.post('/del-area', async (req, res, next) => { +router.delete('/area', async (req, res, next) => { logger.db(req, '游戏管理', '广告区域管理', '删除广告区域') // 权限判断 const hasPerm = req.user.permissions.includes(`ad-area-writeable`) @@ -311,131 +316,17 @@ router.post('/del-area', async (req, res, next) => { } }) -// 获取广告区域列表 -router.get('/get-area', async (req, res, next) => { - try { - const result = await AdArea.find({}) - res.send({ - errcode: 0, - adAreaList: result, - }) - } catch (err) { - next(err) - } -}) - -// 新增区域 -router.post('/save-area', async (req, res, next) => { - logger.db(req, '游戏管理', '广告区域管理', '新增广告区域') - // 权限判断 - const hasPerm = req.user.permissions.includes(`ad-area-writeable`) - if (!hasPerm) { - res.status(403).send({ - errcode: 1, - errmsg: '用户无广告区域编辑权限!', - }) - return - } - const body = req.body - try { - const search = await AdArea.findOne({ - $or: [{name: body.name}, {area_id: body.area_id}, {key: body.key}], - }) - if (search) { - res.send({ - errcode: 1, - errmsg: '区域名称或区域 ID 已存在!', - }) - } else { - const newAdArea = new AdArea(body) - const result = await newAdArea.save() - res.send({ - errcode: 0, - }) - } - } catch (err) { - next(err) - } -}) - -// 编辑区域信息 -router.post('/edit-area', async (req, res, next) => { - logger.db(req, '游戏管理', '广告区域管理', '修改广告区域信息') - // 权限判断 - const hasPerm = req.user.permissions.includes(`ad-area-writeable`) - if (!hasPerm) { - res.status(403).send({ - errcode: 1, - errmsg: '用户无广告区域编辑权限!', - }) - return - } - const body = req.body - try { - const search = await AdArea.findOne({area_id: body.area_id}) - if (search) { - const result = await AdArea.updateOne( - {area_id: body.area_id}, - { - name: body.name, - key: body.key, - area_id: body.area_id, - comment: body.comment, - } - ) - res.send({ - errcode: 0, - }) - } else { - res.send({ - errcode: 1, - errmsg: '区域不存在,修改失败!', - }) - } - } catch (err) { - next(err) - } -}) - -// 编辑区域信息 -router.post('/del-area', async (req, res, next) => { - logger.db(req, '游戏管理', '广告区域管理', '删除广告区域') - // 权限判断 - const hasPerm = req.user.permissions.includes(`ad-area-writeable`) - if (!hasPerm) { - res.status(403).send({ - errcode: 1, - errmsg: '用户无广告区域编辑权限!', - }) - return - } - const body = req.body - try { - const search = await AdArea.findOne({area_id: body.area_id}) - if (search) { - const result = await AdArea.deleteOne({area_id: body.area_id}) - res.send({ - errcode: 0, - }) - } else { - res.send({ - errcode: 1, - errmsg: '区域不存在,删除失败!', - }) - } - } catch (err) { - next(err) - } -}) +// ---------------------------------------------- // +// ---------------------------------------------- // // 获取广告Uid列表 -router.get('/get-uid', async (req, res, next) => { +router.get('/uid', async (req, res, next) => { const query = req.query try { const result = await AdUid.find({ game_id: query.game_id, platform_id: query.platform_id, - deleted: false + deleted: false, }) res.send({ errcode: 0, @@ -447,7 +338,7 @@ router.get('/get-uid', async (req, res, next) => { }) // 新增Uid -router.post('/save-uid', async (req, res, next) => { +router.post('/uid', async (req, res, next) => { logger.db(req, '游戏管理', '广告Uid', '新增广告Uid') // 权限判断 const hasPerm = @@ -481,7 +372,7 @@ router.post('/save-uid', async (req, res, next) => { }) // 编辑Uid信息 -router.post('/edit-uid', async (req, res, next) => { +router.put('/uid', async (req, res, next) => { logger.db(req, '游戏管理', '广告Uid', '修改广告Uid信息') // 权限判断 const hasPerm = @@ -525,7 +416,7 @@ router.post('/edit-uid', async (req, res, next) => { }) // 编辑Uid信息 -router.post('/del-uid', async (req, res, next) => { +router.delete('/uid', async (req, res, next) => { logger.db(req, '游戏管理', '广告Uid', '删除广告Uid') // 权限判断 const hasPerm = @@ -558,4 +449,6 @@ router.post('/del-uid', async (req, res, next) => { } }) +// ---------------------------------------------- // + export default router diff --git a/src/controllers/games/data.js b/src/controllers/games/data.js index 8e0d742..e876a73 100644 --- a/src/controllers/games/data.js +++ b/src/controllers/games/data.js @@ -68,9 +68,9 @@ router.get('/report', async (req, res, next) => { ) }) - const dataArr = await Promise.all(getDataArr) const allData = Object.assign(...dataArr) + const result = {} for (const key in categoryInfo) { @@ -81,8 +81,20 @@ router.get('/report', async (req, res, next) => { for (let i = 0; i < cate.length; i++) { const field = cate[i] const fieldName = field.name - field.value = allData[fieldName] - if (field.value) { + if (fieldName.endsWith('_%')) { + let idx = 1 + let tempName = fieldName.replace(/%/, idx) + while (allData[tempName]) { + const obj = JSON.parse(JSON.stringify(field)) + obj.name = tempName + obj.explan += `_${idx}` + obj.value = allData[tempName] + result[key].push(obj) + idx++ + tempName = fieldName.replace(/%/, idx) + } + } else { + field.value = allData[fieldName] result[key].push(field) } } diff --git a/src/controllers/games/games.js b/src/controllers/games/games.js index 119defd..84449c1 100644 --- a/src/controllers/games/games.js +++ b/src/controllers/games/games.js @@ -464,7 +464,7 @@ router.post('/deploy-config', async (req, res, next) => { const token = await getOpsToken() - const ftpRes = await axios({ + const cfgRes = await axios({ url: config.minigame.api + 'minigame/', method: 'post', data: { @@ -482,15 +482,15 @@ router.post('/deploy-config', async (req, res, next) => { authorization: 'Bearer ' + token, }, }) - const ftpStatus = ftpRes.status - if (ftpStatus === 200) { + const cfgStatus = cfgRes.status + if (cfgStatus === 200) { res.send({ errcode: 0, }) } else { res.send({ errcode: 1, - errmsg: 'ftp账号更新发生错误!', + errmsg: '发布配置时发生错误!', }) } } catch (err) { diff --git a/src/controllers/games/lib.js b/src/controllers/games/lib.js index 0d6a03d..4436269 100644 --- a/src/controllers/games/lib.js +++ b/src/controllers/games/lib.js @@ -5,7 +5,7 @@ import GameLib from '../../models/admin/GameLib' const router = new Router() // 获取资源 -router.get('/list', async (req, res, next) => { +router.get('/', async (req, res, next) => { const query = req.query const type = query.type const currentPage = query.currentPage || 0 @@ -34,7 +34,7 @@ router.get('/list', async (req, res, next) => { }) // 保存资源 -router.post('/save', async (req, res, next) => { +router.post('/', async (req, res, next) => { const body = req.body body.createdBy = req.user.username try { @@ -50,7 +50,7 @@ router.post('/save', async (req, res, next) => { }) // 删除资源 -router.post('/del', async (req, res, next) => { +router.delete('/', async (req, res, next) => { const body = req.body try { const result = await GameLib.where({_id: {$in: body.ids}}).updateMany({ diff --git a/src/controllers/games/platforms.js b/src/controllers/games/platforms.js index 707791d..c5ca3f7 100644 --- a/src/controllers/games/platforms.js +++ b/src/controllers/games/platforms.js @@ -4,7 +4,7 @@ import logger from '../../utils/logger'; const router = new Router(); // 获取平台列表 -router.get('/list', async (req, res, next) => { +router.get('/', async (req, res, next) => { // logger.db(req, '游戏管理', '平台管理', '获取所有平台信息'); try { const result = await Platform.find({}); @@ -17,7 +17,7 @@ router.get('/list', async (req, res, next) => { } }); // 新增平台 -router.post('/save', async (req, res, next) => { +router.post('/', async (req, res, next) => { logger.db(req, '游戏管理', '平台管理', '新增平台'); // 权限判断 const hasPerm = req.user.permissions.includes(`platforms-writeable`); @@ -54,7 +54,7 @@ router.post('/save', async (req, res, next) => { } }); // 编辑平台信息 -router.post('/edit', async (req, res, next) => { +router.put('/', async (req, res, next) => { logger.db(req, '游戏管理', '平台管理', '编辑平台信息'); // 权限判断 const hasPerm = req.user.permissions.includes(`platforms-writeable`); diff --git a/src/controllers/games/reward.js b/src/controllers/games/reward.js index 298b74e..ba6350c 100644 --- a/src/controllers/games/reward.js +++ b/src/controllers/games/reward.js @@ -1,13 +1,19 @@ import {Router} from 'express' import logger from '../../utils/logger' import getCustomerReplayModel from '../../models/snoopy/CustomerReplay' +import RedisDao from '../../redis/redis.dao' +import getOpsToken from '../../utils/get-ops-token' +import config from '../../../config/config' +import axios from 'axios' const router = new Router() +const redisDao = new RedisDao() + const CustomerReplay = getCustomerReplayModel() const CustomerReplayTest = getCustomerReplayModel('test') // 获取奖励列表 -router.get('/list', async (req, res, next) => { +router.get('/', async (req, res, next) => { // 权限判断 const hasPerm = req.user.permissions.includes(`${req.query.uid}-readable`) || @@ -53,7 +59,7 @@ router.get('/list', async (req, res, next) => { }) // 保存奖励配置 -router.post('/save', async (req, res, next) => { +router.post('/', async (req, res, next) => { // 权限判断 const hasPerm = req.user.permissions.includes(`${req.body.uid}-edit`) || @@ -98,8 +104,45 @@ router.post('/save', async (req, res, next) => { } }) +// 启用禁用奖励配置 +router.put('/', async (req, res, next) => { + // 权限判断 + const hasPerm = + req.user.permissions.includes(`${req.body.uid}-edit`) || + req.user.permissions.includes(`${req.body.uid}-publish`) || + req.user.permissions.includes(`games-writeable`) + if (!hasPerm) { + res.status(403).send({ + errcode: 1, + errmsg: '用户无此游戏奖励编辑权限!', + }) + return + } + logger.db(req, '游戏管理', '客服奖励', '切换客服奖励状态') + const body = req.body + const type = body.type // 启用: true 禁用 false + const isDev = body.isDev + const CustomerReplayModel = isDev ? CustomerReplayTest : CustomerReplay + delete body.isDev + + try { + if (body.ids) { + const record = await CustomerReplayModel.where({ + _id: {$in: body.ids}, + }) + .updateMany({actived: type}) + .exec() + res.json({errcode: 0, errmsg: '', count: record.n}) + } else { + res.json({errcode: 101, errmsg: '无有效奖励!'}) + } + } catch (err) { + next(err) + } +}) + // 删除奖励配置 -router.post('/del', async (req, res, next) => { +router.delete('/', async (req, res, next) => { // 权限判断 const hasPerm = req.user.permissions.includes(`${req.body.uid}-edit`) || @@ -136,8 +179,37 @@ router.post('/del', async (req, res, next) => { } }) -// 启用禁用奖励配置 -router.post('/switch', async (req, res, next) => { +// 获取客服配置 +router.get('/kefu-cfg', async (req, res, next) => { + // 权限判断 + const hasPerm = + req.user.permissions.includes(`${req.query.uid}-readable`) || + req.user.permissions.includes(`${req.query.uid}-edit`) || + req.user.permissions.includes(`${req.query.uid}-publish`) || + req.user.permissions.includes(`games-writeable`) + if (!hasPerm) { + res.status(403).send({ + errcode: 1, + errmsg: '用户无此游戏客服配置查看权限!', + }) + return + } + + try { + const query = req.query + const key = `kefu:${query.game_id}:6001` + const result = await redisDao.getByKey(key) + res.send({ + errcode: 0, + result: result, + }) + } catch (err) { + next(err) + } +}) + +// 发布客服配置 +router.post('/kefu-cfg', async (req, res, next) => { // 权限判断 const hasPerm = req.user.permissions.includes(`${req.body.uid}-edit`) || @@ -146,32 +218,73 @@ router.post('/switch', async (req, res, next) => { if (!hasPerm) { res.status(403).send({ errcode: 1, - errmsg: '用户无此游戏奖励编辑权限!', + errmsg: '用户无此游戏客服配置编辑权限!', }) return } - logger.db(req, '游戏管理', '客服奖励', '切换客服奖励状态') - const body = req.body - const type = body.type // 启用: true 禁用 false - const isDev = body.isDev - const CustomerReplayModel = isDev ? CustomerReplayTest : CustomerReplay - delete body.isDev - try { - if (body.ids) { - const record = await CustomerReplayModel.where({ - _id: {$in: body.ids}, + const body = req.body + const token = await getOpsToken() + + const kefuRes = await axios({ + url: config.minigame.api + 'minigame/', + method: 'post', + data: { + action: 'config_deploy', + data: { + config: { + app_id: body.app_id, + app_secret: body.app_secret, + msg_key: body.msg_key, + img_url: body.img_url, + }, + game_id: body.game_id, + platform: 'weixin', + kefu: true, + }, + }, + headers: { + authorization: 'Bearer ' + token, + }, + }) + const kefuStatus = kefuRes.status + + if (kefuStatus === 200) { + const key = `kefu:${body.game_id}:6001` + await redisDao.updateOneKey( + key, + JSON.stringify({msg_key: body.msg_key, img_url: body.img_url}) + ) + + res.send({ + errcode: 0, }) - .updateMany({actived: type}) - .exec() - res.json({errcode: 0, errmsg: '', count: record.n}) } else { - res.json({errcode: 101, errmsg: '无有效奖励!'}) + res.send({ + errcode: 1, + errmsg: '发布客服配置时发生错误', + }) } } catch (err) { next(err) } + + logger.db(req, '游戏管理', '客服奖励', '发布客服配置') +}) + +// 获取上传 Token + +router.get('/ops-token', async (req, res, next) => { + try { + const token = await getOpsToken() + res.send({ + errcode: 0, + result: token, + }) + } catch (err) { + next(err) + } }) export default router diff --git a/src/controllers/sys/logs.js b/src/controllers/sys/logs.js index f9b82f2..891471f 100644 --- a/src/controllers/sys/logs.js +++ b/src/controllers/sys/logs.js @@ -3,7 +3,7 @@ import { Router } from 'express'; import logger from '../../utils/logger'; const router = new Router(); -router.get('/list', async function permissionListCtrl(req, res, next) { +router.get('/', async function permissionListCtrl(req, res, next) { // logger.db(req, '系统管理', '操作日志', '获取所有操作日志'); // 权限判断 const hasPerm = diff --git a/src/controllers/sys/permission.js b/src/controllers/sys/permission.js index 557a744..79be01f 100644 --- a/src/controllers/sys/permission.js +++ b/src/controllers/sys/permission.js @@ -3,7 +3,7 @@ import { Router } from 'express'; import logger from '../../utils/logger'; const router = new Router(); -router.get('/list', async function permissionListCtrl(req, res, next) { +router.get('/', async function permissionListCtrl(req, res, next) { // logger.db(req, '系统管理', '权限管理', '获取所有角色信息'); // 权限判断 // const hasPerm = @@ -27,7 +27,40 @@ router.get('/list', async function permissionListCtrl(req, res, next) { } }); -router.post('/edit', async function permissionEditCtrl(req, res, next) { +router.post('/', async function permissionAddCtrl(req, res, next) { + logger.db(req, '系统管理', '权限管理', '添加角色'); + + // 权限判断 + const hasPerm = req.user.permissions.includes(`permission-writeable`); + if (!hasPerm) { + res.status(403).send({ + errcode: 1, + errmsg: '用户无权限编辑权限!' + }); + return; + } + + const body = req.body; + const newRole = new Role(body); + try { + const searchResult = await Role.findOne({ rolename: body.rolename }); + if (searchResult !== null) { + res.send({ + errcode: 1, + errmsg: '已有该角色,不可重复' + }); + } else { + await newRole.save(); + res.send({ + errcode: 0 + }); + } + } catch (err) { + next(err); + } +}); + +router.put('/', async function permissionEditCtrl(req, res, next) { logger.db(req, '系统管理', '权限管理', '编辑角色权限'); // 权限判断 const hasPerm = req.user.permissions.includes(`permission-writeable`); @@ -61,40 +94,7 @@ router.post('/edit', async function permissionEditCtrl(req, res, next) { } }); -router.post('/add', async function permissionAddCtrl(req, res, next) { - logger.db(req, '系统管理', '权限管理', '添加角色'); - - // 权限判断 - const hasPerm = req.user.permissions.includes(`permission-writeable`); - if (!hasPerm) { - res.status(403).send({ - errcode: 1, - errmsg: '用户无权限编辑权限!' - }); - return; - } - - const body = req.body; - const newRole = new Role(body); - try { - const searchResult = await Role.findOne({ rolename: body.rolename }); - if (searchResult !== null) { - res.send({ - errcode: 1, - errmsg: '已有该角色,不可重复' - }); - } else { - await newRole.save(); - res.send({ - errcode: 0 - }); - } - } catch (err) { - next(err); - } -}); - -router.post('/del', async function permissionDelCtrl(req, res, next) { +router.delete('/', async function permissionDelCtrl(req, res, next) { logger.db(req, '系统管理', '权限管理', '删除角色'); // 权限判断 const hasPerm = req.user.permissions.includes(`permission-writeable`);