diff --git a/src/bin/express.js b/src/bin/express.js index 2565da5..77331e9 100644 --- a/src/bin/express.js +++ b/src/bin/express.js @@ -11,7 +11,7 @@ import helmet from 'helmet'; import FileStreamRotator from 'file-stream-rotator'; import morgan from 'morgan'; import fs from 'fs'; -import logger from './../utils/logger' +import logger from './../utils/logger'; import expressUtils from './../utils/express-utils'; import config from './../../config/config'; import connectMongo from 'connect-mongo'; @@ -96,6 +96,15 @@ app.get('/robots.txt', function(req, res) { res.send('User-agent: *\nDisallow: /agent/'); }); +// TODO: 待删 字体测试用 +app.use('/get_font', async (req, res, next) => { + const query = req.query; + const font_name = query.font_name; + const ext = query.ext; + + res.download(`fonts/${font_name}.${ext}`); +}); + // -- We don't want to serve sessions for static resources // -- Save database write on every resources app.use(compress()); @@ -169,15 +178,15 @@ app.use(function(err, req, res, next) { }); res.json({ errcode: 10, errmsg: err.message }); // if (req.path.startsWith('/api')) { - + // } else { - // TODO: - // res.render('error', { - // message: err.status === 404 ? err.message : '服务器君开小差啦 @(・●・)@', - // error: err, - // title: err.status - // }); + // TODO: + // res.render('error', { + // message: err.status === 404 ? err.message : '服务器君开小差啦 @(・●・)@', + // error: err, + // title: err.status + // }); // } }); diff --git a/src/controllers/games/mp_share.js b/src/controllers/games/mp_share.js index 7e338eb..443dd66 100644 --- a/src/controllers/games/mp_share.js +++ b/src/controllers/games/mp_share.js @@ -8,89 +8,10 @@ import config from '../../../config/config'; const router = new Router(); -router.get('/test', async (req, res, next) => { - var dataBuffer = new Buffer(base64, 'base64'); - // var imgBuffer = streamifier.createReadStream(dataBuffer); - var imgpath = path.join(__dirname, '../../../temp/flower.jpg'); - console.log(path.join(__dirname, '../../../temp/logo.png')); - var formData = { - 'image-file': fs.createReadStream(imgpath), - // 'image-file': dataBuffer, - sub_path: '/mp-share/', - file_type: 'mp_share' - }; - request.post( - { - url: 'http://localhost:2333/api/common/upload', - formData: formData, - headers: { - authorization: - 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Inl1bGl4aW5nIiwiaWF0IjoxNTYwOTIyOTEyLCJleHAiOjE1NjEwMDkzMTJ9.EAVRtbGxBIp4nDaWUAoO0IqGb6OSqDG5a_GXwbfjkBI' - } - }, - function(err, res1, body) { - if (err) { - console.log(err); - res.send(err); - return; - } - - fs.unlink(imgpath, function(error) { - if (error) { - console.log(error); - return false; - } - console.log('删除文件成功'); - }); - - res.send(body); - } - ); -}); - -router.get('/test1', async (req, res, next) => { - const opt = { - baseInfo: { - width: '250px', - height: '736px', - fonts: [ - { - name: 'shoushuti', - path: 'fonts/shoushuti.ttf' - } - ] - }, - views: [ - { - type: 'image', - url: - 'https://client-1256832210.cos.ap-beijing.myqcloud.com/mp-share/5d09e26a62bb32868763dba3.jpeg', - style: { - top: '50px', - left: '20px', - width: '210px', //可选 - height: '210px', // 可选 - 'border-radius': '105px', // 可选 - border: '1px solid #fff' // 可选 - } - }, - { - type: 'text', - text: - '人间四月芳菲尽,山寺桃花始盛开。\n长恨春归无觅处,不知转入此中来。\n——白居易·大林寺桃花', - style: { - left: '165px', - top: '300px', - color: '#fae', - 'max-width': '693px', - 'font-size': '24px', - 'font-family': 'shoushuti', - 'line-height': '50px', - 'writing-mode': 'vertical-rl' - } - } - ] - }; +router.post('/get_pic', async (req, res, next) => { + const body = req.body; + const opt = body.opt; + console.log(opt); try { const imgTempName = await painter(opt); @@ -103,7 +24,7 @@ router.get('/test1', async (req, res, next) => { }; request.post( { - url: config.host + '/api/common/upload', + url: config.host + '/api/mp_share/imgs/upload', formData: formData, headers: { authorization: `${req.headers.authorization}` diff --git a/src/controllers/mp_share/index.js b/src/controllers/mp_share/index.js new file mode 100644 index 0000000..1bd4cb0 --- /dev/null +++ b/src/controllers/mp_share/index.js @@ -0,0 +1,14 @@ +/* 小程序专用 */ + +import {Router} from 'express' + +import uploadRouter from '../common/upload' +import shareRouter from '../games/mp_share'; + + +const router = new Router() + +router.use('/imgs', uploadRouter) +router.use('/imgs', shareRouter) + +export default router diff --git a/src/router/index.js b/src/router/index.js index dec6245..1865f76 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,20 +1,24 @@ -import { Router } from 'express'; +import {Router} from 'express' -import permission from './../middleware/permission'; -import commonRouter from './../controllers/common'; -import sysRouter from './../controllers/sys'; -import userRouter from './../controllers/user'; -import gamesRouter from './../controllers/games'; +import permission from './../middleware/permission' +import commonRouter from './../controllers/common' +import sysRouter from './../controllers/sys' +import userRouter from './../controllers/user' +import gamesRouter from './../controllers/games' +import mpShareRouter from './../controllers/mp_share' -const router = new Router(); +const router = new Router() // 公共接口 -router.use('/common', permission,commonRouter); +router.use('/common', permission, commonRouter) // 系统管理 -router.use('/sys', permission,sysRouter); +router.use('/sys', permission, sysRouter) // 用户相关 -router.use('/', userRouter); +router.use('/', userRouter) // 游戏列表 -router.use('/games', permission, gamesRouter); +router.use('/games', permission, gamesRouter) -export default router; +// 小程序专用 +router.use('/mp_share', mpShareRouter) + +export default router diff --git a/src/utils/logger.js b/src/utils/logger.js index 8611de1..b5239b7 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -99,7 +99,7 @@ export default { } }, db(req, type, subType, content, info) { - const user = req.user; + const user = req.user || {}; const ip = req.headers['x-forwarded-for']; const path = req.baseUrl + req.path; const params = req.method === 'GET' ? req.query : req.body; diff --git a/src/utils/painter/index.js b/src/utils/painter/index.js index 635acfe..029e725 100644 --- a/src/utils/painter/index.js +++ b/src/utils/painter/index.js @@ -21,7 +21,7 @@ module.exports = async function paint(opt) { if (fonts.length > 0) { for (let i = 0; i < fonts.length; i++) { const fontName = fonts[i].name; - registerFont(fonts[i].path, { family: `${fontName}` }); + registerFont(fonts[i].loc_path, { family: `${fontName}` }); } }