init
This commit is contained in:
parent
4aba59a4fc
commit
8bb48ad2b5
@ -11,12 +11,15 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"biguint-format": "^1.0.1",
|
||||
"bluebird": "^3.5.4",
|
||||
"body-parser": "^1.19.0",
|
||||
"bson": "^4.0.2",
|
||||
"bunyan": "^1.8.12",
|
||||
"compression": "^1.7.4",
|
||||
"connect-mongo": "^2.0.3",
|
||||
"cookie-parser": "^1.4.4",
|
||||
"cos-nodejs-sdk-v5": "^2.5.9",
|
||||
"express": "^4.16.4",
|
||||
"express-flash": "0.0.2",
|
||||
"express-session": "^1.16.1",
|
||||
@ -28,8 +31,11 @@
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"ldapjs": "^1.0.2",
|
||||
"method-override": "^3.0.0",
|
||||
"mime-types": "^2.1.24",
|
||||
"moment": "^2.24.0",
|
||||
"mongoose": "^5.5.7",
|
||||
"morgan": "^1.9.1",
|
||||
"multer": "^1.4.1",
|
||||
"node-schedule": "^1.3.2",
|
||||
"nodemon": "^1.19.0",
|
||||
"request": "^2.88.0",
|
||||
|
@ -9,5 +9,4 @@ const router = new Router();
|
||||
router.get('/test',permission, testCtrl);
|
||||
|
||||
|
||||
|
||||
export default router;
|
||||
|
81
src/controllers/common/upload.js
Normal file
81
src/controllers/common/upload.js
Normal file
@ -0,0 +1,81 @@
|
||||
import {Router} from 'express';
|
||||
import moment from 'moment';
|
||||
import mime from 'mime-types';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import multer from 'multer';
|
||||
import bson from 'bson';
|
||||
import config from '../../../config/config';
|
||||
import logger from '../../utils/logger';
|
||||
import cdnUtil from '../../utils/cdn.utils';
|
||||
|
||||
const router = new Router();
|
||||
const ObjectID = bson.ObjectId;
|
||||
|
||||
const extension = function(file) {
|
||||
let ext = mime.extension(file.mimetype);
|
||||
if (ext) {
|
||||
ext = '.' + ext;
|
||||
} else {
|
||||
ext = path.extname(file.originalname);
|
||||
ext = ext ? ext.toLowerCase() : '';
|
||||
}
|
||||
return ext;
|
||||
};
|
||||
const storageMsg = multer.diskStorage({
|
||||
destination: function(req, file, cb) {
|
||||
const type = req.body.type;
|
||||
const m = moment();
|
||||
const path = `${config.upload_to}/${m.format('YYYY')}/${m.format('MM')}/${m.format('DD')}/${type}`;
|
||||
fs.exists(path, function(exists) {
|
||||
if (exists) {
|
||||
cb(null, path);
|
||||
} else {
|
||||
fs.mkdirs(path, function() {
|
||||
cb(null, path);
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
filename: function(req, file, cb) {
|
||||
cb(null, new ObjectID().toString() + extension(file));
|
||||
},
|
||||
});
|
||||
const uploadMsg = multer({storage: storageMsg});
|
||||
/**
|
||||
* 上传一个图片
|
||||
* type: 类型
|
||||
* */
|
||||
router.post('/', uploadMsg.single('image-file'), function(req, res, next) {
|
||||
const file = req.file;
|
||||
const fileName = file.path;
|
||||
const subPath = req.body.sub_path ? req.body.sub_path : '/';
|
||||
const fileKey = `${subPath}${file.filename}`;
|
||||
cdnUtil.uploadToCDN(fileKey, fileName)
|
||||
.then((data) => {
|
||||
logger.info(data);
|
||||
const urlCdn = `https://resource.kingsome.cn/${data.Key}`;
|
||||
if (req.body.refresh_cdn) {
|
||||
process.nextTick(async function() {
|
||||
try {
|
||||
const result = await cdnUtil.refreshOneUrl(urlCdn);
|
||||
logger.error('refresh cdn result:');
|
||||
logger.error(result);
|
||||
} catch (err) {
|
||||
logger.error('refresh cdn url error: ' + urlCdn);
|
||||
}
|
||||
});
|
||||
}
|
||||
res.json({
|
||||
errcode: 0, errmsg: '',
|
||||
url_cdn: urlCdn,
|
||||
url: `https://${data.Location}`,
|
||||
name: file.originalname,
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
next(err);
|
||||
});
|
||||
});
|
||||
|
||||
export default router
|
@ -1,8 +1,19 @@
|
||||
import GameInfo from '../../models/snoopy/GameInfo';
|
||||
import GameInfo from '../../models/snoopy/GameInfo';
|
||||
|
||||
async function gameListCtrl(req, res, next) {
|
||||
const query = req.query || {};
|
||||
query.deleted = false;
|
||||
|
||||
try {
|
||||
const result = await GameInfo.find({deleted: false});
|
||||
let result = await GameInfo.find(query).populate('linked_games');
|
||||
// 获取虚拟字段数据
|
||||
result = result.map(item => {
|
||||
const game = JSON.parse(JSON.stringify(item));
|
||||
game.status_show = item.status_show;
|
||||
game.show_type = item.show_type;
|
||||
game.platform_show = item.platform_show;
|
||||
return game;
|
||||
});
|
||||
res.send({
|
||||
errcode: 0,
|
||||
gameList: result
|
||||
@ -12,7 +23,120 @@ async function gameListCtrl(req, res, next) {
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
gameListCtrl
|
||||
async function gameTypeCtrl(req, res, next) {
|
||||
const gameType = GameInfo.all_type();
|
||||
const result = [];
|
||||
for (const key in gameType) {
|
||||
if ({}.hasOwnProperty.call(gameType, key)) {
|
||||
result.push({
|
||||
value: key,
|
||||
name: gameType[key]
|
||||
});
|
||||
}
|
||||
}
|
||||
res.send({
|
||||
errcode: 0,
|
||||
gameType: result
|
||||
});
|
||||
}
|
||||
|
||||
async function platformCtrl(req, res, next) {
|
||||
const platform = GameInfo.all_platform();
|
||||
const result = [];
|
||||
for (const key in platform) {
|
||||
if ({}.hasOwnProperty.call(platform, key)) {
|
||||
result.push({
|
||||
value: key,
|
||||
name: platform[key]
|
||||
});
|
||||
}
|
||||
}
|
||||
res.send({
|
||||
errcode: 0,
|
||||
platform: result
|
||||
});
|
||||
}
|
||||
|
||||
async function statusCtrl(req, res, next) {
|
||||
const platform = GameInfo.status_list();
|
||||
res.send({
|
||||
errcode: 0,
|
||||
statusList: platform
|
||||
});
|
||||
}
|
||||
|
||||
async function gameEditCtrl(req, res, next) {
|
||||
const body = req.body;
|
||||
try {
|
||||
const searchResult = await GameInfo.findOne({
|
||||
_id: body._id,
|
||||
deleted: false
|
||||
});
|
||||
if (searchResult) {
|
||||
const saveResult = await GameInfo.updateOne({ _id: body._id }, body);
|
||||
res.send({
|
||||
errcode: 0
|
||||
});
|
||||
} else {
|
||||
res.send({
|
||||
errcode: 1,
|
||||
errmsg: '该游戏已被删除,无法编辑!'
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
|
||||
async function gameAddCtrl(req, res, next) {
|
||||
const body = req.body;
|
||||
try {
|
||||
const searchResult = await GameInfo.findOne({
|
||||
game_id: body.game_id,
|
||||
deleted: false
|
||||
});
|
||||
if (searchResult) {
|
||||
res.send({
|
||||
errcode: 1,
|
||||
errmsg: '游戏 ID 已存在!'
|
||||
});
|
||||
} else {
|
||||
const newGame = new GameInfo(body);
|
||||
const saveResult = await newGame.save();
|
||||
res.send({
|
||||
errcode: 0,
|
||||
userInfo: saveResult
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async function gameDelCtrl(req, res, next) {
|
||||
const body = req.body;
|
||||
const gameList = body.gameList;
|
||||
try {
|
||||
for(let i = 0; i < gameList.length; i++) {
|
||||
const delResult = await GameInfo.updateOne({_id: gameList[i]._id}, {
|
||||
deleted: true
|
||||
})
|
||||
}
|
||||
res.send({
|
||||
errcode: 0
|
||||
})
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
gameListCtrl,
|
||||
gameTypeCtrl,
|
||||
platformCtrl,
|
||||
statusCtrl,
|
||||
gameEditCtrl,
|
||||
gameAddCtrl,
|
||||
gameDelCtrl
|
||||
};
|
||||
|
@ -1,10 +1,17 @@
|
||||
import { Router } from 'express';
|
||||
import {gameListCtrl} from './games';
|
||||
import {gameListCtrl, gameTypeCtrl, platformCtrl, statusCtrl, gameEditCtrl, gameAddCtrl, gameDelCtrl} from './games';
|
||||
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.get('/list', gameListCtrl);
|
||||
router.get('/type', gameTypeCtrl);
|
||||
router.get('/platform', platformCtrl);
|
||||
router.get('/status', statusCtrl);
|
||||
|
||||
router.post('/edit', gameEditCtrl);
|
||||
router.post('/add', gameAddCtrl);
|
||||
router.post('/del', gameDelCtrl);
|
||||
|
||||
|
||||
export default router;
|
||||
|
@ -1,6 +1,6 @@
|
||||
import jwt from 'jsonwebtoken';
|
||||
import config from '../../../config/config';
|
||||
import { LdapUser } from '../../models/admin/User';
|
||||
import { LdapUser, User } from '../../models/admin/User';
|
||||
import {combPer, combRole} from '../../utils/comb-permissions';
|
||||
|
||||
export default function(req, res, next) {
|
||||
@ -32,11 +32,8 @@ export default function(req, res, next) {
|
||||
// 获取用户信息
|
||||
try {
|
||||
const username = decode.username;
|
||||
const searchResult = await LdapUser.findOne({ uid: username }).populate({
|
||||
path: 'userInfo',
|
||||
populate: {
|
||||
path: 'permissions'
|
||||
}
|
||||
const searchResult = await User.findOne({ username }).populate({
|
||||
path: 'permissions'
|
||||
}).exec();
|
||||
if (!searchResult) {
|
||||
res.send({
|
||||
@ -45,10 +42,10 @@ export default function(req, res, next) {
|
||||
});
|
||||
} else {
|
||||
|
||||
let permissions = [...searchResult.userInfo.permissions];
|
||||
let permissions = [...searchResult.permissions];
|
||||
let roles = combRole(permissions);
|
||||
permissions = combPer(permissions);
|
||||
const userInfo = JSON.parse(JSON.stringify(searchResult.userInfo));
|
||||
const userInfo = JSON.parse(JSON.stringify(searchResult));
|
||||
userInfo.permissions = permissions;
|
||||
userInfo.roles = roles;
|
||||
res.send({
|
||||
|
@ -1,6 +1,6 @@
|
||||
import jwt from 'jsonwebtoken';
|
||||
import config from '../../config/config';
|
||||
import { LdapUser } from '../models/admin/User';
|
||||
import { LdapUser,User } from '../models/admin/User';
|
||||
import { combPer, combRole } from '../utils/comb-permissions';
|
||||
|
||||
export default function(req, res, next) {
|
||||
@ -32,12 +32,9 @@ export default function(req, res, next) {
|
||||
// 获取用户信息
|
||||
try {
|
||||
const username = decode.username;
|
||||
const searchResult = await LdapUser.findOne({ uid: username })
|
||||
const searchResult = await User.findOne({ username })
|
||||
.populate({
|
||||
path: 'userInfo',
|
||||
populate: {
|
||||
path: 'permissions'
|
||||
}
|
||||
path: 'permissions'
|
||||
})
|
||||
.exec();
|
||||
if (!searchResult) {
|
||||
@ -46,10 +43,10 @@ export default function(req, res, next) {
|
||||
errmsg: '用户信息发生异常。'
|
||||
});
|
||||
} else {
|
||||
let permissions = [...searchResult.userInfo.permissions];
|
||||
let permissions = [...searchResult.permissions];
|
||||
permissions = combPer(permissions);
|
||||
const userInfo = JSON.parse(JSON.stringify(searchResult));
|
||||
req.user = userInfo.userInfo;
|
||||
req.user = userInfo;
|
||||
next();
|
||||
}
|
||||
} catch (err) {
|
||||
|
@ -2,6 +2,7 @@
|
||||
import {Router} from 'express';
|
||||
|
||||
import commonRouter from './../controllers/common'
|
||||
import uploadRouter from './../controllers/common/upload'
|
||||
import sysRouter from './../controllers/sys'
|
||||
import userRouter from './../controllers/user'
|
||||
import gamesRouter from './../controllers/games'
|
||||
@ -9,6 +10,7 @@ import gamesRouter from './../controllers/games'
|
||||
const router = new Router();
|
||||
|
||||
router.use('/common', commonRouter);
|
||||
router.use('/common/upload', uploadRouter);
|
||||
router.use('/sys', sysRouter);
|
||||
router.use('/user', userRouter);
|
||||
router.use('/games', gamesRouter);
|
||||
|
Loading…
x
Reference in New Issue
Block a user