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