You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
riskcloudmock/routes/main/project.js

115 lines
3.6 KiB

4 years ago
var express = require('express');
var router = express.Router();
var Mock = require("mockjs")
var util=require('../../public/javascripts/util')
var moment = require('moment')
4 years ago
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
4 years ago
4 years ago
function groupJson(message,req, res, next){
4 years ago
var data=Mock.mock({
code:200,
4 years ago
message:message,
4 years ago
result:'OK',
body: {
total:0,
"groupCount|1-20": 0,
group:function (){
let arr=[]
let names=[]
for(let i=0;i<this.groupCount;i++){
let obj
do{
obj={
4 years ago
id:i,
4 years ago
name: Mock.mock('@first'),
count:Mock.Random.natural(1,100)
}
}while (names.includes(obj.name))
names.push(obj.name)
arr.push(obj)
this.total+=obj.count
}
return arr
}
}
})
return res.json(data)
4 years ago
}
//项目分组信息
router.get('/project/group',function(req, res, next) {
return groupJson('获取项目分组信息成功',req,res,next)
4 years ago
})
4 years ago
//添加分组
router.post('/project/group',multipartMiddleware,function(req, res, next) {
return groupJson(`添加项目分组${req.body.name}成功`,req,res,next)
})
//分组重命名
router.patch('/project/group/:name',multipartMiddleware,function(req, res, next) {
return groupJson(`项目分组${req.params.name}重命名为${req.body.name}`,req,res,next)
})
//删除分组
router.delete('/project/group/:name',multipartMiddleware,function(req, res, next) {
return groupJson(`项目分组${req.params.name}删除成功`,req,res,next)
})
function projectJson(message,req, res, next){
var data=Mock.mock({
code:200,
message:message,
result:'OK',
body:{
'totalCount|10-100':0,
pageSize:req.query.pageSize,
totalPage:function (){
return Math.ceil(this.totalCount/this.pageSize)
},
currentPage:function (){
return Mock.Random.natural(1,this.totalPage)
},
project:function (){
let arr=[]
for(let i=0;i<this.pageSize;i++){
4 years ago
arr.push(Mock.mock({
projectName:/[a-zA-Z0-9]{3,6}/,
templateName:/(GB|国际标准)模板/,
createUser:Mock.mock('@first'),
createTime:function (){
return moment(Date.now()-Mock.Random.natural(3600,36000)*1000).format(util.dateFormat)
},
lastModifyUser:Mock.mock('@first'),
lastModifyTime:function (){
return moment(moment(this.createTime,util.dateFormat).toDate().getTime()+Mock.Random.natural(1,3600)*1000).format(util.dateFormat)
}
}))
4 years ago
}
return arr
}
}
})
return res.json(data)
}
4 years ago
4 years ago
//项目列表数据
router.get('/project',function(req, res, next) {
return projectJson('获取项目列表信息成功',req,res,next)
})
//添加项目
router.post('/project',multipartMiddleware,function(req, res, next) {
var data=Mock.mock({
code: 200,
message: `项目${JSON.stringify(req.body)}添加成功`,
result: 'OK',
})
return res.json(data)
4 years ago
})
module.exports=router