524 lines
13 KiB
JavaScript
524 lines
13 KiB
JavaScript
import getGameInfoModel 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 GameInfo = getGameInfoModel()
|
||
const GameInfoTest = getGameInfoModel('test')
|
||
|
||
// 获取游戏列表
|
||
router.get('/list', async (req, res, next) => {
|
||
const query = req.query || {}
|
||
const userPerms = req.user.permissions
|
||
try {
|
||
let search = []
|
||
let result = []
|
||
if (query.type === 'all') {
|
||
// 返回所有游戏信息(仅包含游戏_id与游戏名)
|
||
// logger.db(req, '游戏管理', '游戏列表', '获取所有游戏非权限信息');
|
||
result = await GameInfo.find({deleted: false})
|
||
} else {
|
||
// 有权限查阅的游戏
|
||
// logger.db(req, '游戏管理', '游戏列表', '获取权限游戏所有信息');
|
||
search = await GameInfo.find({deleted: false})
|
||
result = search.filter(game => {
|
||
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,
|
||
})
|
||
} catch (err) {
|
||
next(err)
|
||
}
|
||
})
|
||
|
||
// 保存游戏信息
|
||
router.post('/save', async (req, res, next) => {
|
||
logger.db(req, '游戏管理', '游戏列表', '添加游戏')
|
||
// 权限判断
|
||
const hasPerm = 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({
|
||
$or: [
|
||
{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: '游戏已存在!',
|
||
})
|
||
} else {
|
||
const newGameInfo = new GameInfo(body)
|
||
const result = await newGameInfo.save()
|
||
res.send({
|
||
errcode: 0,
|
||
gameInfo: result,
|
||
})
|
||
}
|
||
} catch (err) {
|
||
next(err)
|
||
}
|
||
})
|
||
|
||
// 查找单个游戏信息
|
||
router.get('/info', async (req, res, next) => {
|
||
// logger.db(req, '游戏管理', '游戏列表', '查询单个游戏信息');
|
||
|
||
// 权限判断
|
||
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
|
||
}
|
||
|
||
const query = req.query
|
||
|
||
const data_type = query.data_type
|
||
const GameInfoModel = data_type === 'dev' ? GameInfoTest : GameInfo
|
||
delete req.query.data_type
|
||
try {
|
||
const search = await GameInfoModel.findOne({
|
||
_id: query.uid,
|
||
deleted: false,
|
||
})
|
||
if (search) {
|
||
res.send({
|
||
errcode: 0,
|
||
gameInfo: search,
|
||
})
|
||
} else {
|
||
res.send({
|
||
errcode: 1,
|
||
errmsg: '游戏不存在或已删除!',
|
||
})
|
||
}
|
||
} catch (err) {
|
||
next(err)
|
||
}
|
||
})
|
||
|
||
// 更新游戏信息
|
||
router.post('/update', 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) {
|
||
if (body.info) {
|
||
const info = body.info
|
||
const platformInfo = search.platforms[info.platformIndex]
|
||
const curStatus = platformInfo ? platformInfo.status : ''
|
||
delete body.platforms[info.platformIndex].newPlatform
|
||
delete body.info
|
||
|
||
// 状态变更会新增平台 发送通知
|
||
if (curStatus !== info.status || !platformInfo) {
|
||
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
|
||
if (loginStatus === 200) {
|
||
token = loginData.token
|
||
} else {
|
||
res.send({
|
||
errcode: 1,
|
||
errmsg: '游戏状态修改发生异常!',
|
||
})
|
||
return
|
||
}
|
||
|
||
const statusRes = await axios({
|
||
url: config.minigame.api + 'minigame/',
|
||
method: 'post',
|
||
data: {
|
||
action: 'game_status_change',
|
||
data: {
|
||
game: search.game_name,
|
||
game_name: search.game_name_en,
|
||
game_id: search.game_id,
|
||
platform: body.platforms[info.platformIndex].platform.name_en,
|
||
platform_name: body.platforms[info.platformIndex].platform.name,
|
||
platform_id:
|
||
body.platforms[info.platformIndex].platform.platform_id,
|
||
status_id: info.status,
|
||
},
|
||
},
|
||
headers: {
|
||
authorization: 'Bearer ' + token,
|
||
},
|
||
})
|
||
const statusStatus = statusRes.status
|
||
if (statusStatus !== 200) {
|
||
res.send({
|
||
errcode: 1,
|
||
errmsg: '游戏状态修改发生异常!',
|
||
})
|
||
return
|
||
}
|
||
}
|
||
}
|
||
|
||
const result = await GameInfo.updateOne(
|
||
{
|
||
_id: body._id,
|
||
deleted: false,
|
||
},
|
||
body
|
||
)
|
||
res.send({
|
||
errcode: 0,
|
||
})
|
||
} else {
|
||
res.send({
|
||
errcode: 1,
|
||
errmsg: '游戏不存在或已删除!',
|
||
})
|
||
}
|
||
} catch (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
|
||
const isDev = body.isDev
|
||
const GameInfoModel = isDev ? GameInfoTest : GameInfo
|
||
|
||
delete body.isDev
|
||
|
||
try {
|
||
const search = await GameInfoModel.findOne({
|
||
_id: body._id,
|
||
deleted: false,
|
||
})
|
||
|
||
// TODO:
|
||
|
||
if (search) {
|
||
// 更新
|
||
const recommendation = search.recommendation ? search.recommendation : {}
|
||
if (body.platform_id) {
|
||
recommendation[body.platform_id] = body.RCList
|
||
}
|
||
const result = await GameInfoModel.updateOne(
|
||
{
|
||
_id: body._id,
|
||
deleted: false,
|
||
},
|
||
{recommendation: recommendation, rc_published: body.published}
|
||
)
|
||
res.send({
|
||
errcode: 0,
|
||
})
|
||
} else {
|
||
// 新建
|
||
const gameInfo = body.gameInfo
|
||
gameInfo.recommendation = {}
|
||
if (body.platform_id) {
|
||
gameInfo.recommendation[body.platform_id] = body.RCList
|
||
}
|
||
gameInfo.rc_published = body.published
|
||
|
||
const newGameInfo = new GameInfoModel(gameInfo)
|
||
const result = await newGameInfo.save()
|
||
res.send({
|
||
errcode: 0,
|
||
})
|
||
}
|
||
} 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 {
|
||
const search = await GameInfo.findOne({
|
||
_id: body.uid,
|
||
deleted: false,
|
||
})
|
||
if (search) {
|
||
const result = await GameInfo.updateOne(
|
||
{
|
||
_id: body.uid,
|
||
deleted: false,
|
||
},
|
||
{deleted: true}
|
||
)
|
||
res.send({
|
||
errcode: 0,
|
||
})
|
||
} else {
|
||
res.send({
|
||
errcode: 1,
|
||
errmsg: '游戏不存在或已删除!',
|
||
})
|
||
}
|
||
} catch (err) {
|
||
next(err)
|
||
}
|
||
})
|
||
|
||
// 创建新平台,获取ftp账号
|
||
router.post('/create-ftp', async (req, res, next) => {
|
||
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`)
|
||
if (!hasPerm) {
|
||
res.status(403).send({
|
||
errcode: 1,
|
||
errmsg: '用户无游戏编辑权限!',
|
||
})
|
||
return
|
||
}
|
||
const body = req.body
|
||
|
||
try {
|
||
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
|
||
if (loginStatus === 200) {
|
||
token = loginData.token
|
||
} else {
|
||
res.send({
|
||
errcode: 1,
|
||
errmsg: '创建ftp账号时发生错误',
|
||
})
|
||
return
|
||
}
|
||
const ftpRes = await axios({
|
||
url: config.minigame.api + 'minigame/',
|
||
method: 'post',
|
||
data: {
|
||
action: 'game_deploy',
|
||
data: {
|
||
game: body.game_name,
|
||
game_name: body.game_name_en,
|
||
game_id: body.game_id,
|
||
platform: body.platformInfo.platform.name_en,
|
||
platform_id: body.platformInfo.platform.platform_id,
|
||
},
|
||
},
|
||
headers: {
|
||
authorization: 'Bearer ' + token,
|
||
},
|
||
})
|
||
const ftpStatus = ftpRes.status
|
||
if (ftpStatus === 200) {
|
||
// 通知用户刷新页面等待ftp账号
|
||
res.send({
|
||
errcode: 0,
|
||
msg: '正在创建ftp账号,请等待一段时间后刷新页面!',
|
||
})
|
||
} else {
|
||
res.send({
|
||
errcode: 1,
|
||
errmsg: '创建ftp账号时发生错误',
|
||
})
|
||
return
|
||
}
|
||
} catch (err) {
|
||
next(err)
|
||
}
|
||
})
|
||
|
||
// 发布游戏配置
|
||
router.post('/deploy-config', async (req, res, next) => {
|
||
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`)
|
||
if (!hasPerm) {
|
||
res.status(403).send({
|
||
errcode: 1,
|
||
errmsg: '用户无游戏编辑权限!',
|
||
})
|
||
return
|
||
}
|
||
|
||
const body = req.body
|
||
try {
|
||
const info = body.platformInfo
|
||
const search = await GameInfo.findOne({
|
||
game_id: body.game_id,
|
||
deleted: false,
|
||
})
|
||
if (search) {
|
||
const platforms = search.platforms
|
||
for (let i = 0; i < platforms.length; i++) {
|
||
if (platforms[i].platform.name_en === body.platform) {
|
||
platforms[i] = info
|
||
break
|
||
}
|
||
}
|
||
await GameInfo.updateOne(
|
||
{game_id: body.game_id, deleted: false},
|
||
{
|
||
platforms: platforms,
|
||
}
|
||
)
|
||
} else {
|
||
res.send({
|
||
errcode: 1,
|
||
errmsg: '游戏不存在或已删除!',
|
||
})
|
||
return
|
||
}
|
||
|
||
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
|
||
if (loginStatus === 200) {
|
||
token = loginData.token
|
||
} else {
|
||
res.send({
|
||
errcode: 1,
|
||
errmsg: '发布配置时发生错误!',
|
||
})
|
||
}
|
||
|
||
const ftpRes = 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,
|
||
},
|
||
game_id: body.game_id,
|
||
platform: body.platform,
|
||
},
|
||
},
|
||
headers: {
|
||
authorization: 'Bearer ' + token,
|
||
},
|
||
})
|
||
const ftpStatus = ftpRes.status
|
||
if (ftpStatus === 200) {
|
||
res.send({
|
||
errcode: 0,
|
||
})
|
||
} else {
|
||
res.send({
|
||
errcode: 1,
|
||
errmsg: 'ftp账号更新发生错误!',
|
||
})
|
||
}
|
||
} catch (err) {
|
||
next(err)
|
||
}
|
||
})
|
||
|
||
export default router
|