update_rc

This commit is contained in:
yulixing 2019-07-01 14:56:08 +08:00
parent 1f2cc26b9f
commit af8a3f4e1e
2 changed files with 220 additions and 173 deletions

View File

@ -1,88 +1,88 @@
// import GameInfo from '../../models/snoopy/GameInfo'; // import GameInfo from '../../models/snoopy/GameInfo';
import GameInfo from '../../models/admin/GameInfo'; import GameInfo from '../../models/admin/GameInfo'
import { Router } from 'express'; import {Router} from 'express'
import { userInfo } from 'os'; import {userInfo} from 'os'
import axios from 'axios'; import axios from 'axios'
import config from '../../../config/config'; import config from '../../../config/config'
import logger from '../../utils/logger'; import logger from '../../utils/logger'
const router = new Router(); const router = new Router()
// 获取游戏列表 // 获取游戏列表
router.get('/list', async (req, res, next) => { router.get('/list', async (req, res, next) => {
const query = req.query || {}; const query = req.query || {}
const userPerms = req.user.permissions; const userPerms = req.user.permissions
try { try {
let search = []; let search = []
let result = []; let result = []
if (query.type === 'all') { if (query.type === 'all') {
// 返回所有游戏信息仅包含游戏_id与游戏名 // 返回所有游戏信息仅包含游戏_id与游戏名
// logger.db(req, '游戏管理', '游戏列表', '获取所有游戏非权限信息'); // logger.db(req, '游戏管理', '游戏列表', '获取所有游戏非权限信息');
result = await GameInfo.find({ deleted: false }); result = await GameInfo.find({deleted: false})
} else { } else {
// 有权限查阅的游戏 // 有权限查阅的游戏
// logger.db(req, '游戏管理', '游戏列表', '获取权限游戏所有信息'); // logger.db(req, '游戏管理', '游戏列表', '获取权限游戏所有信息');
search = await GameInfo.find({ deleted: false }); search = await GameInfo.find({deleted: false})
result = search.filter(game => { result = search.filter(game => {
const uid = game._id; const uid = game._id
return ( return (
userPerms.includes(`${uid}-readable`) || userPerms.includes(`${uid}-readable`) ||
userPerms.includes(`${uid}-edit`) || userPerms.includes(`${uid}-edit`) ||
userPerms.includes(`${uid}-publish`) || userPerms.includes(`${uid}-publish`) ||
userPerms.includes(`games-writeable`) userPerms.includes(`games-writeable`)
); )
}); })
} }
res.send({ res.send({
errcode: 0, errcode: 0,
gameList: result, gameList: result,
user: req.user user: req.user,
}); })
} catch (err) { } catch (err) {
next(err); next(err)
} }
}); })
// 保存游戏信息 // 保存游戏信息
router.post('/save', async (req, res, next) => { router.post('/save', async (req, res, next) => {
logger.db(req, '游戏管理', '游戏列表', '添加游戏'); logger.db(req, '游戏管理', '游戏列表', '添加游戏')
// 权限判断 // 权限判断
const hasPerm = req.user.permissions.includes('games-writeable'); const hasPerm = req.user.permissions.includes('games-writeable')
if (!hasPerm) { if (!hasPerm) {
res.status(403).send({ res.status(403).send({
errcode: 1, errcode: 1,
errmsg: '用户无游戏编辑权限!' errmsg: '用户无游戏编辑权限!',
}); })
return; return
} }
const body = req.body; const body = req.body
try { try {
const search = await GameInfo.findOne({ const search = await GameInfo.findOne({
$or: [ $or: [
{ game_id: body.game_id }, {game_id: body.game_id},
{ game_name: body.game_name }, {game_name: body.game_name},
{ game_name_en: body.game_name_en } {game_name_en: body.game_name_en},
] ],
}); })
if (search && !search.deleted) { if (search && !search.deleted) {
res.send({ res.send({
errcode: 1, errcode: 1,
errmsg: '游戏已存在!' errmsg: '游戏已存在!',
}); })
} else { } else {
const newGameInfo = new GameInfo(body); const newGameInfo = new GameInfo(body)
const result = await newGameInfo.save(); const result = await newGameInfo.save()
res.send({ res.send({
errcode: 0, errcode: 0,
gameInfo: result gameInfo: result,
}); })
} }
} catch (err) { } catch (err) {
next(err); next(err)
} }
}); })
// 查找单个游戏信息 // 查找单个游戏信息
router.get('/info', async (req, res, next) => { router.get('/info', async (req, res, next) => {
@ -93,161 +93,207 @@ router.get('/info', async (req, res, next) => {
req.user.permissions.includes(`${req.query.uid}-readable`) || req.user.permissions.includes(`${req.query.uid}-readable`) ||
req.user.permissions.includes(`${req.query.uid}-edit`) || req.user.permissions.includes(`${req.query.uid}-edit`) ||
req.user.permissions.includes(`${req.query.uid}-publish`) || req.user.permissions.includes(`${req.query.uid}-publish`) ||
req.user.permissions.includes(`games-writeable`); req.user.permissions.includes(`games-writeable`)
if (!hasPerm) { if (!hasPerm) {
res.status(403).send({ res.status(403).send({
errcode: 1, errcode: 1,
errmsg: '用户无此游戏查看权限!' errmsg: '用户无此游戏查看权限!',
}); })
return; return
} }
const query = req.query; const query = req.query
try { try {
const search = await GameInfo.findOne({ const search = await GameInfo.findOne({
_id: query.uid, _id: query.uid,
deleted: false deleted: false,
}); })
if (search) { if (search) {
res.send({ res.send({
errcode: 0, errcode: 0,
gameInfo: search gameInfo: search,
}); })
} else { } else {
res.send({ res.send({
errcode: 1, errcode: 1,
errmsg: '游戏不存在或已删除!' errmsg: '游戏不存在或已删除!',
}); })
} }
} catch (err) { } catch (err) {
next(err); next(err)
} }
}); })
// 更新游戏信息 // 更新游戏信息
router.post('/update', async (req, res, next) => { router.post('/update', async (req, res, next) => {
logger.db(req, '游戏管理', '游戏列表', '更新单个游戏信息'); logger.db(req, '游戏管理', '游戏列表', '更新单个游戏信息')
// 权限判断 // 权限判断
const hasPerm = const hasPerm =
req.user.permissions.includes(`${req.body._id}-edit`) || req.user.permissions.includes(`${req.body._id}-edit`) ||
req.user.permissions.includes(`${req.body._id}-publish`) || req.user.permissions.includes(`${req.body._id}-publish`) ||
req.user.permissions.includes(`games-writeable`); req.user.permissions.includes(`games-writeable`)
if (!hasPerm) { if (!hasPerm) {
res.status(403).send({ res.status(403).send({
errcode: 1, errcode: 1,
errmsg: '用户无游戏编辑权限!' errmsg: '用户无游戏编辑权限!',
}); })
return; return
} }
const body = req.body; const body = req.body
try { try {
const search = await GameInfo.findOne({ const search = await GameInfo.findOne({
_id: body._id, _id: body._id,
deleted: false deleted: false,
}); })
if (search) { if (search) {
const result = await GameInfo.updateOne( const result = await GameInfo.updateOne(
{ {
_id: body._id, _id: body._id,
deleted: false deleted: false,
}, },
body body
); )
res.send({ res.send({
errcode: 0 errcode: 0,
}); })
} else { } else {
res.send({ res.send({
errcode: 1, errcode: 1,
errmsg: '游戏不存在或已删除!' errmsg: '游戏不存在或已删除!',
}); })
} }
} catch (err) { } catch (err) {
next(err); next(err)
} }
}); })
// 删除一个游戏 // 更新推荐信息
router.post('/del', async (req, res, next) => { router.post('/update_rc', async (req, res, next) => {
logger.db(req, '游戏管理', '游戏列表', '删除单个游戏'); logger.db(req, '游戏管理', '游戏列表', '更新游戏推荐列表')
const body = req.body;
// 权限判断 // 权限判断
const hasPerm = const hasPerm =
req.user.permissions.includes(`${body.uid}-edit`) || req.user.permissions.includes(`${req.body._id}-edit`) ||
req.user.permissions.includes(`${body.uid}-publish`)|| req.user.permissions.includes(`${req.body._id}-publish`) ||
req.user.permissions.includes(`games-writeable`); req.user.permissions.includes(`games-writeable`)
if (!hasPerm) { if (!hasPerm) {
res.status(403).send({ res.status(403).send({
errcode: 1, errcode: 1,
errmsg: '用户无游戏编辑权限!' errmsg: '用户无游戏编辑权限!',
}); })
return; return
}
const body = req.body
try {
const search = await GameInfo.findOne({
_id: body._id,
deleted: false,
})
if (search) {
const recommendation = search.recommendation ? search.recommendation : {}
recommendation[body.platform_id] = body.RCList
const result = await GameInfo.updateOne(
{
_id: body._id,
deleted: false,
},
{recommendation: recommendation}
)
res.send({
errcode: 0,
})
} else {
res.send({
errcode: 1,
errmsg: '游戏不存在或已删除!',
})
}
} catch (err) {
next(err)
}
})
// 删除一个游戏
router.post('/del', async (req, res, next) => {
logger.db(req, '游戏管理', '游戏列表', '删除单个游戏')
const body = req.body
// 权限判断
const hasPerm =
req.user.permissions.includes(`${body.uid}-edit`) ||
req.user.permissions.includes(`${body.uid}-publish`) ||
req.user.permissions.includes(`games-writeable`)
if (!hasPerm) {
res.status(403).send({
errcode: 1,
errmsg: '用户无游戏编辑权限!',
})
return
} }
try { try {
const search = await GameInfo.findOne({ const search = await GameInfo.findOne({
_id: body.uid, _id: body.uid,
deleted: false deleted: false,
}); })
if (search) { if (search) {
const result = await GameInfo.updateOne( const result = await GameInfo.updateOne(
{ {
_id: body.uid, _id: body.uid,
deleted: false deleted: false,
}, },
{ deleted: true } {deleted: true}
); )
res.send({ res.send({
errcode: 0 errcode: 0,
}); })
} else { } else {
res.send({ res.send({
errcode: 1, errcode: 1,
errmsg: '游戏不存在或已删除!' errmsg: '游戏不存在或已删除!',
}); })
} }
} catch (err) { } catch (err) {
next(err); next(err)
} }
}); })
// 创建新平台获取ftp账号 // 创建新平台获取ftp账号
router.post('/create-ftp', async (req, res, next) => { router.post('/create-ftp', async (req, res, next) => {
logger.db(req, '游戏管理', '游戏列表', '为游戏添加一个平台账号'); logger.db(req, '游戏管理', '游戏列表', '为游戏添加一个平台账号')
// 权限判断 // 权限判断
const hasPerm = const hasPerm =
req.user.permissions.includes(`${req.body.uid}-edit`) || req.user.permissions.includes(`${req.body.uid}-edit`) ||
req.user.permissions.includes(`${req.body.uid}-publish`) || req.user.permissions.includes(`${req.body.uid}-publish`) ||
req.user.permissions.includes(`games-writeable`); req.user.permissions.includes(`games-writeable`)
if (!hasPerm) { if (!hasPerm) {
res.status(403).send({ res.status(403).send({
errcode: 1, errcode: 1,
errmsg: '用户无游戏编辑权限!' errmsg: '用户无游戏编辑权限!',
}); })
return; return
} }
const body = req.body; const body = req.body
try { try {
let token = ''; let token = ''
const loginRes = await axios({ const loginRes = await axios({
url: config.minigame.api + 'gettoken/', url: config.minigame.api + 'gettoken/',
method: 'post', method: 'post',
data: { data: {
username: config.minigame.username, username: config.minigame.username,
password: config.minigame.password password: config.minigame.password,
} },
}); })
const loginData = loginRes.data; const loginData = loginRes.data
const loginStatus = loginRes.status; const loginStatus = loginRes.status
if (loginStatus === 200) { if (loginStatus === 200) {
token = loginData.token; token = loginData.token
} else { } else {
res.send({ res.send({
errcode: 1, errcode: 1,
errmsg: '创建ftp账号时发生错误' errmsg: '创建ftp账号时发生错误',
}); })
return; return
} }
const ftpRes = await axios({ const ftpRes = await axios({
url: config.minigame.api + 'minigame/', url: config.minigame.api + 'minigame/',
@ -259,31 +305,31 @@ router.post('/create-ftp', async (req, res, next) => {
game_name: body.game_name_en, game_name: body.game_name_en,
game_id: body.game_id, game_id: body.game_id,
platform: body.platformInfo.platform.name_en, platform: body.platformInfo.platform.name_en,
platform_id: body.platformInfo.platform.platform_id platform_id: body.platformInfo.platform.platform_id,
} },
}, },
headers: { headers: {
authorization: 'Bearer ' + token authorization: 'Bearer ' + token,
} },
}); })
const ftpData = ftpRes.data; const ftpData = ftpRes.data
const ftpStatus = ftpRes.status; const ftpStatus = ftpRes.status
if (ftpStatus === 200) { if (ftpStatus === 200) {
// 通知用户刷新页面等待ftp账号 // 通知用户刷新页面等待ftp账号
const search = await GameInfo.findOne({ const search = await GameInfo.findOne({
game_id: body.game_id, game_id: body.game_id,
deleted: false deleted: false,
}); })
if (!search) { if (!search) {
res.send({ res.send({
errcode: 1, errcode: 1,
errmsg: '游戏已删除或不存在!' errmsg: '游戏已删除或不存在!',
}); })
return; return
} }
const platforms = search.platforms; const platforms = search.platforms
for (let i = 0; i < platforms.length; i++) { for (let i = 0; i < platforms.length; i++) {
if ( if (
platforms[i].platform.platform_id === platforms[i].platform.platform_id ===
@ -291,103 +337,103 @@ router.post('/create-ftp', async (req, res, next) => {
) { ) {
res.send({ res.send({
errcode: 0, errcode: 0,
msg: '正在创建ftp账号请等待一段时间后刷新页面' msg: '正在创建ftp账号请等待一段时间后刷新页面',
}); })
return; return
} }
} }
const save = await GameInfo.updateOne( const save = await GameInfo.updateOne(
{ {
game_id: body.game_id, game_id: body.game_id,
deleted: false deleted: false,
}, },
{ {
$push: { platforms: body.platformInfo } $push: {platforms: body.platformInfo},
} }
); )
res.send({ res.send({
errcode: 0, errcode: 0,
msg: '正在创建ftp账号请等待一段时间后刷新页面' msg: '正在创建ftp账号请等待一段时间后刷新页面',
}); })
} else { } else {
res.send({ res.send({
errcode: 1, errcode: 1,
errmsg: '创建ftp账号时发生错误' errmsg: '创建ftp账号时发生错误',
}); })
return; return
} }
} catch (err) { } catch (err) {
next(err); next(err)
} }
}); })
// 更新 ftp 信息 // 更新 ftp 信息
router.post('/update-ftp', async (req, res, next) => { router.post('/update-ftp', async (req, res, next) => {
logger.db(req, '游戏管理', '游戏列表', '修改游戏平台信息'); logger.db(req, '游戏管理', '游戏列表', '修改游戏平台信息')
// 权限判断 // 权限判断
const hasPerm = const hasPerm =
req.user.permissions.includes(`${req.body.uid}-edit`) || req.user.permissions.includes(`${req.body.uid}-edit`) ||
req.user.permissions.includes(`${req.body.uid}-publish`) || req.user.permissions.includes(`${req.body.uid}-publish`) ||
req.user.permissions.includes(`games-writeable`); req.user.permissions.includes(`games-writeable`)
if (!hasPerm) { if (!hasPerm) {
res.status(403).send({ res.status(403).send({
errcode: 1, errcode: 1,
errmsg: '用户无游戏编辑权限!' errmsg: '用户无游戏编辑权限!',
}); })
return; return
} }
const body = req.body; const body = req.body
try { try {
const info = body.platformInfo; const info = body.platformInfo
const search = await GameInfo.findOne({ const search = await GameInfo.findOne({
game_id: body.game_id, game_id: body.game_id,
deleted: false deleted: false,
}); })
if (search) { if (search) {
const platforms = search.platforms; const platforms = search.platforms
for (let i = 0; i < platforms.length; i++) { for (let i = 0; i < platforms.length; i++) {
if (platforms[i].platform.name_en === body.platform) { if (platforms[i].platform.name_en === body.platform) {
platforms[i] = info; platforms[i] = info
break; break
} }
} }
await GameInfo.updateOne( await GameInfo.updateOne(
{ game_id: body.game_id, deleted: false }, {game_id: body.game_id, deleted: false},
{ {
platforms: platforms platforms: platforms,
} }
); )
} else { } else {
res.send({ res.send({
errcode: 1, errcode: 1,
errmsg: '游戏不存在或已删除!' errmsg: '游戏不存在或已删除!',
}); })
return; return
} }
let token = ''; let token = ''
const loginRes = await axios({ const loginRes = await axios({
url: config.minigame.api + 'gettoken/', url: config.minigame.api + 'gettoken/',
method: 'post', method: 'post',
data: { data: {
username: config.minigame.username, username: config.minigame.username,
password: config.minigame.password password: config.minigame.password,
} },
}); })
const loginData = loginRes.data; const loginData = loginRes.data
const loginStatus = loginRes.status; const loginStatus = loginRes.status
if (loginStatus === 200) { if (loginStatus === 200) {
token = loginData.token; token = loginData.token
} else { } else {
res.send({ res.send({
errcode: 1, errcode: 1,
errmsg: '发布配置时发生错误!' errmsg: '发布配置时发生错误!',
}); })
} }
const ftpRes = await axios({ const ftpRes = await axios({
@ -398,31 +444,31 @@ router.post('/update-ftp', async (req, res, next) => {
data: { data: {
config: { config: {
app_id: body.app_id, app_id: body.app_id,
app_secret: body.app_secret app_secret: body.app_secret,
}, },
game_id: body.game_id, game_id: body.game_id,
platform: body.platform platform: body.platform,
} },
}, },
headers: { headers: {
authorization: 'Bearer ' + token authorization: 'Bearer ' + token,
} },
}); })
const ftpData = ftpRes.data; const ftpData = ftpRes.data
const ftpStatus = ftpRes.status; const ftpStatus = ftpRes.status
if (ftpStatus === 200) { if (ftpStatus === 200) {
res.send({ res.send({
errcode: 0 errcode: 0,
}); })
} else { } else {
res.send({ res.send({
errcode: 1, errcode: 1,
errmsg: 'ftp账号更新发生错误' errmsg: 'ftp账号更新发生错误',
}); })
} }
} catch (err) { } catch (err) {
next(err); next(err)
} }
}); })
export default router; export default router

View File

@ -12,6 +12,7 @@ const GameInfo = new mongoose.Schema(
game_type: { type: String }, game_type: { type: String },
game_icon: { type: String }, game_icon: { type: String },
platforms: [{type: Object}], platforms: [{type: Object}],
recommendation: {type: Object},
deleted: {type: Boolean, default: false} deleted: {type: Boolean, default: false}
}, },
{ {