This commit is contained in:
yulixing 2019-06-27 10:35:29 +08:00
parent 367dc4881a
commit dd38c92ec3
4 changed files with 76 additions and 46 deletions

View File

@ -31,6 +31,7 @@
"express-session": "^1.16.1", "express-session": "^1.16.1",
"express-validator": "^5.3.1", "express-validator": "^5.3.1",
"file-stream-rotator": "^0.4.1", "file-stream-rotator": "^0.4.1",
"fontmin": "^0.9.7-beta",
"form-data": "^2.3.3", "form-data": "^2.3.3",
"fs-extra": "^8.0.0", "fs-extra": "^8.0.0",
"glob": "^7.1.4", "glob": "^7.1.4",

View File

@ -1,80 +1,82 @@
import { Router } from 'express'; import {Router} from 'express'
import FormData from 'form-data'; import request from 'request'
import request from 'request'; import fs from 'fs'
import fs from 'fs'; import path from 'path'
import path from 'path'; import painter from '../../utils/painter'
import painter from '../../utils/painter'; import config from '../../../config/config'
import config from '../../../config/config'; import Template from '../../models/mp_share/Template'
const router = new Router();
const router = new Router()
// 生成预览图 // 生成预览图
router.post('/get_pic', async (req, res, next) => { router.post('/get_pic', async (req, res, next) => {
const body = req.body; const body = req.body
const opt = body.opt; const opt = body.opt
try { try {
const imgTempName = await painter(opt); const imgTempName = await painter(opt)
const imgTempPath = path.join(__dirname, '../../temp/' + imgTempName); const imgTempPath = path.join(__dirname, '../../temp/' + imgTempName)
// 生成后上传、删除 // 生成后上传、删除
const formData = { const formData = {
'image-file': fs.createReadStream(imgTempPath), 'image-file': fs.createReadStream(imgTempPath),
sub_path: '/mp-share/', sub_path: '/mp-share/',
file_type: 'mp_share' file_type: 'mp_share',
}; }
request.post( request.post(
{ {
url: config.host + '/api/mp_share/imgs/upload', url: config.host + '/api/mp_share/imgs/upload',
formData: formData, formData: formData,
headers: { headers: {
authorization: `${req.headers.authorization}` authorization: `${req.headers.authorization}`,
} },
}, },
function(uploadErr, uploadRes, uploadBody) { function(uploadErr, uploadRes, uploadBody) {
fs.unlink(imgTempPath, function(err) { fs.unlink(imgTempPath, function(err) {
if (err) { if (err) {
console.log(err); console.log(err)
} }
}); })
if (uploadErr) { if (uploadErr) {
next(uploadErr); next(uploadErr)
return; return
} }
res.send(uploadBody); res.send(uploadBody)
} }
); )
} catch (err) { } catch (err) {
next(err); next(err)
} }
}); })
// 保存模板 // 保存模板
router.post('/save_tpl', async (req, res, next) => { router.post('/save_tpl', async (req, res, next) => {
const body = req.body
const tpl = body.tpl
try {
const searchRes = await Template.findOne({tpl_id: tpl.tpl_id})
if (searchRes) {
res.send({
errcode: 1,
errmsg: '模板ID已存在',
})
return
}
tpl.createdBy = req.user.username || ''
const result = await Template.save(tpl)
res.send({
errcode: 0,
})
} catch (err) {
next(err)
}
}) })
// 修改模板 // 修改模板
// 删除模板 // 删除模板
// 查询单个模板 // 查询单个模板
// 获取模板列表 // 获取模板列表
export default router
export default router;

View File

@ -1,20 +1,20 @@
import {Router} from 'express' import {Router} from 'express'
import path from 'path' import path from 'path'
import cors from 'cors' import cors from 'cors'
import Fontmin from 'fontmin'
const router = new Router() const router = new Router()
const whitelist = ['https://servicewechat.com'] const whitelist = ['https://servicewechat.com']
const corsOptions = { const corsOptions = {
origin: function (origin, callback) { origin: function(origin, callback) {
if (whitelist.indexOf(origin) !== -1) { if (whitelist.indexOf(origin) !== -1) {
callback(null, true) callback(null, true)
} else { } else {
callback(new Error('Not allowed by CORS')) callback(new Error('Not allowed by CORS'))
} }
} },
} }
router.get('/get_font', cors(), async (req, res, next) => { router.get('/get_font', cors(), async (req, res, next) => {
const query = req.query const query = req.query
@ -25,4 +25,31 @@ router.get('/get_font', cors(), async (req, res, next) => {
res.download(fontPath) res.download(fontPath)
}) })
router.get('/fontmin', cors(),async (req, res, next) => {
const query = req.query
const {text, font_name, ext} = query
console.log(text)
const fontPath = path.join(__dirname, `../../fonts/${font_name}.${ext}`)
const fontmin = new Fontmin().src(fontPath).use(
Fontmin.glyph({
text: text,
hinting: false,
})
)
fontmin.run(function(err, files) {
if (err) {
console.log(err)
next(err)
return
}
const data = files[0].contents
res.writeHead(200, {
'Content-Type': 'application/vnd.ms-fontobject',
'Content-disposition': `attachment;filename=${font_name}.${ext}`,
'Content-Length': data.length,
})
res.end(data)
})
})
export default router export default router

View File

@ -6,7 +6,7 @@ const ObjectId = Schema.Types.ObjectId;
/** /**
* 操作日志 * 操作日志
*/ */
const OpLog = new mongoose.Schema({ const Template = new mongoose.Schema({
// 游戏id // 游戏id
tpl_name: {type: String}, tpl_name: {type: String},
tpl_id: {type: String}, tpl_id: {type: String},