生成器相关

This commit is contained in:
yulixing 2019-11-21 16:41:07 +08:00
parent ec33c45c61
commit 61c1e77adb
14 changed files with 494 additions and 8 deletions

View File

@ -80,4 +80,41 @@ const extension = function(file) {
});
});
router.post('/upload-file', uploadMsg.single('game-file'), function(req, res, next) {
logger.db(req, '公共', '文件上传', '文件上传');
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: urlCdn,
// url: `https://${data.Location}`,
name: file.originalname,
});
})
.catch((err) => {
next(err);
});
});
export default router

View File

View File

@ -22,9 +22,13 @@ router.post('/', async (req, res, next) => {
const title = body.title
const tip = body.tip
const text = body.text
const bg_img = body.bg_img
const bg_music = body.bg_music
const newText = new ZpText({
title,
tip,
bg_img,
bg_music,
text,
})
@ -43,6 +47,8 @@ router.put('/', async (req, res, next) => {
const _id = body._id
const title = body.title
const tip = body.tip
const bg_img = body.bg_img
const bg_music = body.bg_music
const text = body.text
const result = await ZpText.updateOne(
@ -52,6 +58,8 @@ router.put('/', async (req, res, next) => {
{
title,
tip,
bg_img,
bg_music,
text,
}
)

View File

@ -0,0 +1,15 @@
/* 小程序专用 */
import {Router} from 'express'
import testRouter from './test'
import makerRouter from './maker'
const router = new Router()
router.use('/test', testRouter)
router.use('/maker', makerRouter)
export default router

View File

@ -0,0 +1,106 @@
import { Router } from 'express'
import Maker from '../../models/maker/Maker'
const router = new Router()
router.get('/', async (req, res, next) => {
try {
const query = req.query
if (query._id) {
// 单条详情查询
const result = await Maker.findOne({ _id: query._id })
res.send({
errcode: 0,
result,
})
} else {
// 列表查询
const currentPage = query.currentPage || 0
const pageSize = query.pageSize || 0
const start = (currentPage - 1) * pageSize
const limit = Number(pageSize)
const result = await Maker.find({}).skip(start).limit(limit).sort({ createdAt: 'desc' }).select('_id title desc')
const total = await Maker.find({}).countDocuments()
res.send({
errcode: 0,
result,
total
})
}
} catch (err) {
next(err)
}
})
router.post('/', async (req, res, next) => {
try {
const body = req.body
const title = body.title
const desc = body.desc
const template = body.template
const list = body.list
const newMaker = new Maker({
title,
desc,
template,
list,
})
const result = await newMaker.save()
res.send({
errcode: 0,
result
})
} catch (err) {
next(err)
}
})
router.put('/', async (req, res, next) => {
try {
const body = req.body
const _id = body._id
const title = body.title
const desc = body.desc
const template = body.template
const list = body.list
const result = await Maker.updateOne(
{
_id,
},
{
title,
desc,
template,
list,
}
)
res.send({
errcode: 0,
})
} catch (err) {
next(err)
}
})
router.delete('/', async (req, res, next) => {
try {
const body = req.body
const _id = body._id
const result = await Maker.deleteOne({ _id })
res.send({
errcode: 0,
})
} catch (err) {
next(err)
}
})
export default router

View File

@ -0,0 +1,93 @@
import {Router} from 'express'
import ZpText from '../../models/mp_test/TestText'
const router = new Router()
router.get('/text', async (req, res, next) => {
try {
const result = await ZpText.find({})
res.send({
errcode: 0,
result,
})
} catch (err) {
next(err)
}
})
router.post('/text', async (req, res, next) => {
try {
const body = req.body
const title = body.title
const tip = body.tip
const text = body.text
const bg_img = body.bg_img
const bg_music = body.bg_music
const cfg = body.cfg
const newText = new ZpText({
title,
tip,
bg_img,
bg_music,
text,
cfg
})
const result = await newText.save()
res.send({
errcode: 0,
})
} catch (err) {
next(err)
}
})
router.put('/text', async (req, res, next) => {
try {
const body = req.body
const _id = body._id
const title = body.title
const tip = body.tip
const bg_img = body.bg_img
const bg_music = body.bg_music
const text = body.text
const cfg = body.cfg
const result = await ZpText.updateOne(
{
_id,
},
{
title,
tip,
bg_img,
bg_music,
text,
cfg
}
)
res.send({
errcode: 0,
})
} catch (err) {
next(err)
}
})
router.delete('/text', async (req, res, next) => {
try {
const body = req.body
const _id = body._id
const result = await ZpText.deleteOne({_id})
res.send({
errcode: 0,
})
} catch (err) {
next(err)
}
})
export default router

View File

@ -9,6 +9,7 @@ import jumpRouter from './jump'
import gameListRouter from './game-list'
import cfgRouter from './cfg'
import zpRouter from './zp'
import testRouter from './test'
const router = new Router()
@ -20,6 +21,7 @@ router.use('/jump', jumpRouter)
router.use('/game-list', gameListRouter)
router.use('/cfg',cfgRouter)
router.use('/zp',zpRouter)
router.use('/test',testRouter)
export default router

View File

@ -0,0 +1,177 @@
// 小程序-心里测试专用
import { Router } from 'express'
import ZpText from '../../models/mp_zp/ZpText'
const router = new Router()
const text = `
# 测试题
## 1.题目一
### A.题目一的选项A
#### 2
### B.题目一的选项B
#### 3
## 2.题目二
### A.题目二的选项A
#### 4
### B.题目二的选项B
#### A
## 3.题目三
### A.题目三的选项A
#### 4
### B.题目三的选项B
#### D
## 4.题目四
### A.题目四的选项A
#### B
### B.题目四的选项B
#### C
## A.结果A
### desc:结果A的大段文字
## B.结果B
### desc:结果B的大段文字
## C.结果C
### desc:结果C的大段文字
## D.结果D
### desc:结果D的大段文字
`
// 获取转盘数据
router.get('/text', async (req, res, next) => {
try {
const query = req.query
const _id = query._id
const opt = {}
if (_id) {
opt._id = _id
}
let title = ''
let tip = ''
let bg_img = ''
let bg_music = ''
if (_id) {
// const search = await ZpText.findOne({_id: _id})
// title = search.title
// tip = search.tip
// bg_img = search.bg_img
// bg_music = search.bg_music
// result = parseText(search.text)
const textParse = parseText(text)
res.send({
errcode: 0,
questions: textParse.questions,
analyses: textParse.analyses
})
} else {
const result = await ZpText.find({}).select('_id title')
res.send({
errcode: 0,
result
})
}
} catch (err) {
next(err)
}
})
function parseText(text) {
const result = []
let textArr = text.split('\n')
const qReg = /^## (\d+).(.*)/ //检测问题
const oReg = /^### ([A-Z]+).(.*)/ //检测解析标题
const gReg = /^#### (.*)/ //goto检测
const aReg = /^## ([A-Z]+).(.*)/ //检测解析标题
const dReg = /^### desc:(.*)/ //检测解析描述
let curAIdx = ''
let curOIdx = 0
const questions = []
const analyses = {}
// 过滤空行
textArr = textArr.filter(item => {
return item !== ''
})
// 解析文本
textArr.map(item => {
if (qReg.exec(item)) {
const result = qReg.exec(item)
const idx = result[1]
const title = result[2]
const question = {
title,
opts: []
}
questions[idx - 1] = question
} else if (oReg.exec(item)) {
const result = oReg.exec(item)
const text = result[2]
const opt = {
text,
goto: ''
}
questions[questions.length - 1].opts.push(opt)
curOIdx = questions[questions.length - 1].opts.length - 1
} else if (gReg.exec(item)) {
const result = gReg.exec(item)
const go = result[1]
questions[questions.length - 1].opts[curOIdx].goto = go
} else if (aReg.exec(item)) {
const result = aReg.exec(item)
const idx = result[1]
const title = result[2]
const analyse = {
title,
desc: ''
}
curAIdx = idx
analyses[idx] = analyse
} else if (dReg.exec(item)) {
const result = dReg.exec(item)
const desc = result[1]
analyses[curAIdx].desc = desc
}
})
return {
questions,
analyses
}
}
export default router

View File

@ -52,11 +52,15 @@ router.get('/text', async (req, res, next) => {
let result = []
let title = ''
let tip = ''
let bg_img = ''
let bg_music = ''
if (_id) {
const search = await ZpText.findOne({_id: _id})
title = search.title
tip = search.tip
bg_img = search.bg_img
bg_music = search.bg_music
result = parseText(search.text)
// result = parseText(text)
} else {
@ -67,6 +71,8 @@ router.get('/text', async (req, res, next) => {
errcode: 0,
title: title,
tip: tip,
bg_img: bg_img,
bg_music: bg_music,
result: result,
})
} catch (err) {

18
src/models/maker/Maker.js Normal file
View File

@ -0,0 +1,18 @@
'use strict';
import mongoose from 'mongoose';
/**
* 生成器
*/
const Maker = new mongoose.Schema({
title: {type: String},
desc: {type: String},
template: {type: String},
list: {type: Object},
}, {
collection: 'maker',
timestamps: true,
});
export default mongoose.model('Maker', Maker);

View File

@ -0,0 +1,22 @@
'use strict'
import mongoose from 'mongoose'
/**
* 小程序-心里测试-配置
*/
const TestText = new mongoose.Schema(
{
title: { type: String },
tip: { type: String },
bg_img: { type: String },
bg_music: { type: String },
text: { type: String },
cfg: { type: Object }
},
{
collection: 'test_text',
timestamps: true,
}
)
export default mongoose.model('TestText', TestText)

View File

@ -2,13 +2,16 @@
import mongoose from 'mongoose'
/**
* 小程序-转盘-文本
* 小程序-转盘-文本 (弃用暂保留)
*/
const ZpText = new mongoose.Schema(
{
title: {type: String},
tip: {type: String},
text: {type: String},
title: { type: String },
tip: { type: String },
bg_img: { type: String },
bg_music: { type: String },
text: { type: String },
cfg: { type: String }
},
{
collection: 'zp_text',

View File

@ -5,8 +5,7 @@ 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'
import mpZpRouter from './../controllers/mp_zp'
import mpsRouter from './../controllers/mps'
import openRouter from './../controllers/open'
const router = new Router()
@ -20,8 +19,8 @@ router.use('/', userRouter)
// 游戏列表
router.use('/games', permission, gamesRouter)
// 小程序专用
router.use('/mp_share', mpShareRouter)
router.use('/mp_Zp', mpZpRouter)
router.use('/mps', mpsRouter)
// 开放接口 不做验证
router.use('/open', openRouter)