增加基础数据接口

master
pan 4 years ago
parent 2c55cb3a18
commit e3e81dd456
  1. 3
      app.js
  2. 40
      public/javascripts/mock.js
  3. 18
      routes/account.js
  4. 69
      routes/main/data.js
  5. 8
      routes/main/main.js
  6. 57
      routes/main/project.js

@ -8,6 +8,7 @@ var indexRouter = require('./routes/index');
var accountRouter = require('./routes/account')
var mainRouter=require('./routes/main/main')
var projectRouter=require('./routes/main/project')
var dataRouter=require('./routes/main/data')
const server = require("inspector");
var app = express();
@ -28,7 +29,7 @@ app.use(cors({
}))
app.use('/', indexRouter);
app.use('/api/account',accountRouter)
app.use('/api/main',mainRouter,projectRouter)
app.use('/api/main',mainRouter,projectRouter,dataRouter)
// catch 404 and forward to error handler
app.use(function(req, res, next) {

@ -0,0 +1,40 @@
var Mock = require("mockjs")
module.exports={
//模拟基础数据
ok:function (message){
return {
code:200,
message:message,
result:'OK'
}
},
error:function (message){
return {
code:200,
message:message,
result:'FAIL'
}
},
//模拟分页数据
page:function (req,message,obj){
return Mock.mock({...this.ok(message),...{
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)
},
data:function (){
let arr=[]
for(let i=0;i<this.pageSize;i++){
arr.push(Mock.mock(obj))
}
return arr
}
}
}})
}
}

@ -6,14 +6,20 @@ router.post('/login',function(req, res, next) {
console.log(req.body);
//调用mock方法模拟数据
var data = Mock.mock({
code:/200|500/
code:/200|500/,
result:function (){
if(this.code==='200'){
this.message='登陆成功'
this.body={username:Mock.Random.cname(),sessionId:Mock.Random.guid()}
return 'OK'
}else{
this.message='登陆失败'
return 'FAIL'
}
},
message:''
}
)
if(data.code==='200'){
data={...data,...{result:'OK',message:'登陆成功',body:{username:Mock.Random.cname(),sessionId:Mock.Random.guid()}}}
}else{
data={...data,...{result:'FAIL',message:'登陆失败'}}
}
return res.json(data);
})

@ -0,0 +1,69 @@
var express = require('express');
var router = express.Router();
var Mock = require("mockjs")
var moment = require("moment")
var mock=require('../../public/javascripts/mock')
var util=require('../../public/javascripts/util')
const multipart = require("connect-multiparty");
var multipartMiddleware = multipart();
function base(req,message){
return mock.page(req,message,{
number:/\d{2}[A-Z]{2}\d{4}/,
type:/阀门(Valve)/,
name:/[A-Z]{4}-Series/,
model:/[A-Z]{2}\d{3}[A-Z]{2}\d{2}/,
manufacturer:/[A-Z]/,
'sd|100-999':0,
'su|100-999':0,
'dd|100-999':0,
'du|100-999':0,
sff:function () {
return `${Mock.Random.natural(0,100)}%`
},
dataSources:'Exida Certification',
framework:/[A-Z]/,
'tsd|0-10':0,
'mttr|1-10':0,
'ti':'',
'ctr|0-10':0,
'lt|0-10':0,
htf:'',
silLevel:function (){
return `${Mock.Random.natural(1,10)}%`
},
route:function () {
return `Route H${Mock.Random.natural(1,10)}`
},
createUser:function () {
return Mock.Random.first()
},
note:'',
createTime:function (){
return moment(Date.now()-Mock.Random.natural(3600,36000)*1000).format(util.dateFormat)
}
})
}
router.get('/data',function(req, res, next) {
var data=base(req,'基础数据获取成功')
res.json(data)
})
router.post('/data',multipartMiddleware,function(req, res, next){
if(req.body.type==='import') {
var data = Mock.mock(mock.ok(`基础数据导入成功`))
return res.json(data)
}else{
var data=Mock.mock(mock.ok('基础数据添加成功'))
return res.json(data)
}
})
router.delete('/data',multipartMiddleware,function(req, res, next){
})
module.exports=router

@ -3,13 +3,11 @@ var router = express.Router();
var Mock = require("mockjs")
var moment = require('moment')
var util=require('../../public/javascripts/util')
var mock=require('../../public/javascripts/mock')
//获取主页信息
router.get('/main',function(req, res, next) {
var data=Mock.mock({
code:200,
message:'获取主页信息成功',
result:'OK',
var data=Mock.mock({...mock.ok('获取主页信息成功'),...{
body:{
ip:Mock.Random.ip(),
'time|3600-36000':0,
@ -17,7 +15,7 @@ router.get('/main',function(req, res, next) {
return moment(Date.now()-this.time*1000).format(util.dateFormat)
}
}
})
}})
return res.json(data)
})

@ -5,12 +5,10 @@ var util=require('../../public/javascripts/util')
var moment = require('moment')
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
var mock=require('../../public/javascripts/mock')
function groupJson(message,req, res, next){
var data=Mock.mock({
code:200,
message:message,
result:'OK',
var data=Mock.mock({...mock.ok(message),...{
body: {
total:0,
"groupCount|1-20": 0,
@ -34,7 +32,7 @@ function groupJson(message,req, res, next){
return arr
}
}
})
}})
return res.json(data)
}
@ -58,38 +56,17 @@ 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++){
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)
}
}))
}
return arr
}
function projectJson(message,req, res){
var data=mock.page(req,message,{
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)
}
})
@ -103,11 +80,7 @@ router.get('/project',function(req, res, next) {
//添加项目
router.post('/project',multipartMiddleware,function(req, res, next) {
var data=Mock.mock({
code: 200,
message: `项目${JSON.stringify(req.body)}添加成功`,
result: 'OK',
})
var data=Mock.mock(mock.ok(`项目${req.body.projectName}添加成功`))
return res.json(data)
})

Loading…
Cancel
Save