init
This commit is contained in:
1
ruoyi-ui/mock/1.csv
Normal file
1
ruoyi-ui/mock/1.csv
Normal file
@@ -0,0 +1 @@
|
||||
序号,客户类型,身份证号,客户姓名,所属客户经理,归属机构
|
||||
|
36
ruoyi-ui/mock/customerBase.js
Normal file
36
ruoyi-ui/mock/customerBase.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const Mock = require('mockjs')
|
||||
const {parse} = require('json2csv')
|
||||
const fs = require('fs')
|
||||
//模拟生成表头
|
||||
const csvHeaders = ['序号','身份证号','客户名','归属客户经理号','归属机构']
|
||||
|
||||
const csvData = Mock.mock({
|
||||
'data|372': [{
|
||||
'序号|+1': 1,
|
||||
'身份证号': '@id',
|
||||
'客户名':'@cname',
|
||||
'归属客户经理号:':'admin',
|
||||
'归属机构': '@ctitle'+`支行`
|
||||
}]
|
||||
}).data
|
||||
|
||||
const csv = parse(csvData)
|
||||
fs.writeFileSync('test.csv',csv,'utf-8')
|
||||
|
||||
const csvContent = [csvHeaders.join(',')].concat(csvData.map(item => {
|
||||
Object.values(item).join(',')
|
||||
})).join('\n')
|
||||
|
||||
// module.exports =[
|
||||
// {
|
||||
// url: '/system/download/csv',
|
||||
// type: 'get',
|
||||
// response: () => {
|
||||
// const blob = new Blob([csvContent],{type:'text/csv'})
|
||||
// return blob
|
||||
// }
|
||||
// }
|
||||
// ]
|
||||
|
||||
|
||||
|
||||
62
ruoyi-ui/mock/index.js
Normal file
62
ruoyi-ui/mock/index.js
Normal file
@@ -0,0 +1,62 @@
|
||||
const Mock = require('mockjs')
|
||||
const { param2Obj } = require('./utils')
|
||||
|
||||
const user = require('./user')
|
||||
const table = require('./table')
|
||||
const mycustomer = require('./mycustomer')
|
||||
// const customerBase = require('./customerBase')
|
||||
// require('./mycustomer')
|
||||
|
||||
const mocks = [
|
||||
...user,
|
||||
...table,
|
||||
...mycustomer,
|
||||
// ...customerBase
|
||||
]
|
||||
|
||||
// for front mock
|
||||
// please use it cautiously, it will redefine XMLHttpRequest,
|
||||
// which will cause many of your third-party libraries to be invalidated(like progress event).
|
||||
function mockXHR() {
|
||||
// mock patch
|
||||
// https://github.com/nuysoft/Mock/issues/300
|
||||
Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
|
||||
Mock.XHR.prototype.send = function() {
|
||||
if (this.custom.xhr) {
|
||||
this.custom.xhr.withCredentials = this.withCredentials || false
|
||||
|
||||
if (this.responseType) {
|
||||
this.custom.xhr.responseType = this.responseType
|
||||
}
|
||||
}
|
||||
this.proxy_send(...arguments)
|
||||
}
|
||||
|
||||
function XHR2ExpressReqWrap(respond) {
|
||||
return function(options) {
|
||||
let result = null
|
||||
if (respond instanceof Function) {
|
||||
const { body, type, url } = options
|
||||
// https://expressjs.com/en/4x/api.html#req
|
||||
result = respond({
|
||||
method: type,
|
||||
body: JSON.parse(body),
|
||||
query: param2Obj(url)
|
||||
})
|
||||
} else {
|
||||
result = respond
|
||||
}
|
||||
return Mock.mock(result)
|
||||
}
|
||||
}
|
||||
|
||||
for (const i of mocks) {
|
||||
Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
mocks,
|
||||
mockXHR
|
||||
}
|
||||
|
||||
81
ruoyi-ui/mock/mock-server.js
Normal file
81
ruoyi-ui/mock/mock-server.js
Normal file
@@ -0,0 +1,81 @@
|
||||
const chokidar = require('chokidar')
|
||||
const bodyParser = require('body-parser')
|
||||
const chalk = require('chalk')
|
||||
const path = require('path')
|
||||
const Mock = require('mockjs')
|
||||
|
||||
const mockDir = path.join(process.cwd(), 'mock')
|
||||
|
||||
function registerRoutes(app) {
|
||||
let mockLastIndex
|
||||
const { mocks } = require('./index.js')
|
||||
const mocksForServer = mocks.map(route => {
|
||||
return responseFake(route.url, route.type, route.response)
|
||||
})
|
||||
for (const mock of mocksForServer) {
|
||||
app[mock.type](mock.url, mock.response)
|
||||
mockLastIndex = app._router.stack.length
|
||||
}
|
||||
const mockRoutesLength = Object.keys(mocksForServer).length
|
||||
return {
|
||||
mockRoutesLength: mockRoutesLength,
|
||||
mockStartIndex: mockLastIndex - mockRoutesLength
|
||||
}
|
||||
}
|
||||
|
||||
function unregisterRoutes() {
|
||||
Object.keys(require.cache).forEach(i => {
|
||||
if (i.includes(mockDir)) {
|
||||
delete require.cache[require.resolve(i)]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// for mock server
|
||||
const responseFake = (url, type, respond) => {
|
||||
return {
|
||||
url: new RegExp(`${process.env.VUE_APP_MOCK_API}${url}`),
|
||||
type: type || 'get',
|
||||
response(req, res) {
|
||||
console.log('request invoke:' + req.path)
|
||||
res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = app => {
|
||||
// parse app.body
|
||||
// https://expressjs.com/en/4x/api.html#req.body
|
||||
// app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({
|
||||
extended: true
|
||||
}))
|
||||
|
||||
const mockRoutes = registerRoutes(app)
|
||||
var mockRoutesLength = mockRoutes.mockRoutesLength
|
||||
var mockStartIndex = mockRoutes.mockStartIndex
|
||||
|
||||
// watch files, hot reload mock server
|
||||
chokidar.watch(mockDir, {
|
||||
ignored: /mock-server/,
|
||||
ignoreInitial: true
|
||||
}).on('all', (event, path) => {
|
||||
if (event === 'change' || event === 'add') {
|
||||
try {
|
||||
// remove mock routes stack
|
||||
app._router.stack.splice(mockStartIndex, mockRoutesLength)
|
||||
|
||||
// clear routes cache
|
||||
unregisterRoutes()
|
||||
|
||||
const mockRoutes = registerRoutes(app)
|
||||
mockRoutesLength = mockRoutes.mockRoutesLength
|
||||
mockStartIndex = mockRoutes.mockStartIndex
|
||||
|
||||
console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`))
|
||||
} catch (error) {
|
||||
console.log(chalk.redBright(error))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
82
ruoyi-ui/mock/mycustomer.js
Normal file
82
ruoyi-ui/mock/mycustomer.js
Normal file
@@ -0,0 +1,82 @@
|
||||
const Mock = require('mockjs')
|
||||
|
||||
//模拟企业信息返回数据
|
||||
const custTags = ["001","010","100","011","110","101","111"]
|
||||
//生成随机的结果
|
||||
|
||||
const generateBinaryCombination= ()=>{
|
||||
const index = Mock.Random.integer(0,custTags.length-1);
|
||||
return custTags[index];
|
||||
}
|
||||
|
||||
const generateRandomPhone = () => {
|
||||
const prefixes = ['132','135','136','137','138','139','150','151','152','157','188','187','173','155'];
|
||||
return Mock.Random.pick(prefixes)
|
||||
}
|
||||
|
||||
const generateSuffix = () => {
|
||||
return Mock.Random.integer(10000000,99999999).toString()
|
||||
}
|
||||
|
||||
|
||||
let customerInfoList = Mock.mock({
|
||||
'list|187': [{
|
||||
'id|+1': 1,
|
||||
'custName': '@ctitle(2,4)有限责任公司',
|
||||
'belongBranchName': '@ctitle(2,3)支行',
|
||||
'belongOutletName': '@ctitle(2,4)网点',
|
||||
'belongUserName': '@cname',
|
||||
'lpName':'@cname',
|
||||
'custPhone': generateRandomPhone()+generateSuffix(),
|
||||
'custScale|0-2': 0,
|
||||
'custTag': generateBinaryCombination(),
|
||||
'custPattern|0-2': 0 ,
|
||||
'custType|0-1':0
|
||||
}]
|
||||
}).list;
|
||||
|
||||
|
||||
module.exports =[
|
||||
{
|
||||
url: '/system/custBaseInfo/list',
|
||||
type: 'get',
|
||||
response: config => {
|
||||
const params = config.query;
|
||||
console.log(params);
|
||||
const pagenum = params.pageNum;
|
||||
const custType = params.custType;
|
||||
const custName1 = params.custName
|
||||
const {custScale,custTags} = params;
|
||||
// console.log(pageNum,pageSize);
|
||||
let filterData = customerInfoList.filter(item => item.custPattern==0)
|
||||
//分页返回
|
||||
const total = filterData.length;
|
||||
const currentPage = parseInt(pagenum) || 1;
|
||||
const currentPageSize = 10;
|
||||
const start = (currentPage-1) * currentPageSize;
|
||||
const currentPageSize2= Math.min(currentPageSize,total-start)
|
||||
const end = start+currentPageSize2;
|
||||
// console.log(total,'=====',currentPage,'=====',currentPageSize+'=====',start);
|
||||
|
||||
const pageData = filterData.slice(start,end);
|
||||
const finalData = custName1? pageData.filter(item =>{
|
||||
return item.custName.includes(custName1)||item.lpName.includes(custName1)
|
||||
} ):pageData
|
||||
|
||||
const custTypeData = (custType||custScale)? finalData.filter(item => {return item.custType==custType||item.custScale==custScale||
|
||||
item.custTag==custTags}):finalData;
|
||||
return {
|
||||
code: 200,
|
||||
data: {
|
||||
total: (custType||custScale||custTags)?custTypeData.length:custName1?finalData.length:filterData.length,
|
||||
pageSize:parseInt(currentPageSize2),
|
||||
pageNum: parseInt(currentPage),
|
||||
rows: (custType||custScale||custTags)?custTypeData : finalData
|
||||
},
|
||||
message:'查询成功!'
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
30
ruoyi-ui/mock/table.js
Normal file
30
ruoyi-ui/mock/table.js
Normal file
@@ -0,0 +1,30 @@
|
||||
const Mock = require('mockjs')
|
||||
|
||||
const data = Mock.mock({
|
||||
'items|30': [{
|
||||
id: '@id',
|
||||
title: '@sentence(10, 20)',
|
||||
'status|1': ['published', 'draft', 'deleted'],
|
||||
author: 'name',
|
||||
display_time: '@datetime',
|
||||
pageviews: '@integer(300, 5000)'
|
||||
}]
|
||||
})
|
||||
|
||||
module.exports = [
|
||||
{
|
||||
url: '/vue-admin-template/table/list',
|
||||
type: 'get',
|
||||
response: config => {
|
||||
console.log(config.params);
|
||||
const items = data.items
|
||||
return {
|
||||
code: 20000,
|
||||
data: {
|
||||
total: items.length,
|
||||
items: items
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
84
ruoyi-ui/mock/user.js
Normal file
84
ruoyi-ui/mock/user.js
Normal file
@@ -0,0 +1,84 @@
|
||||
|
||||
const tokens = {
|
||||
admin: {
|
||||
token: 'admin-token'
|
||||
},
|
||||
editor: {
|
||||
token: 'editor-token'
|
||||
}
|
||||
}
|
||||
|
||||
const users = {
|
||||
'admin-token': {
|
||||
roles: ['admin'],
|
||||
introduction: 'I am a super administrator',
|
||||
avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
|
||||
name: 'Super Admin'
|
||||
},
|
||||
'editor-token': {
|
||||
roles: ['editor'],
|
||||
introduction: 'I am an editor',
|
||||
avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
|
||||
name: 'Normal Editor'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = [
|
||||
// user login
|
||||
{
|
||||
url: '/vue-admin-template/user/login',
|
||||
type: 'post',
|
||||
response: config => {
|
||||
const { username } = config.body
|
||||
const token = tokens[username]
|
||||
|
||||
// mock error
|
||||
if (!token) {
|
||||
return {
|
||||
code: 60204,
|
||||
message: 'Account and password are incorrect.'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
code: 20000,
|
||||
data: token
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// get user info
|
||||
{
|
||||
url: '/vue-admin-template/user/info\.*',
|
||||
type: 'get',
|
||||
response: config => {
|
||||
const { token } = config.query
|
||||
const info = users[token]
|
||||
|
||||
// mock error
|
||||
if (!info) {
|
||||
return {
|
||||
code: 50008,
|
||||
message: 'Login failed, unable to get user details.'
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
code: 20000,
|
||||
data: info
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// user logout
|
||||
{
|
||||
url: '/vue-admin-template/user/logout',
|
||||
type: 'post',
|
||||
response: _ => {
|
||||
return {
|
||||
code: 20000,
|
||||
data: 'success'
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
25
ruoyi-ui/mock/utils.js
Normal file
25
ruoyi-ui/mock/utils.js
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* @param {string} url
|
||||
* @returns {Object}
|
||||
*/
|
||||
function param2Obj(url) {
|
||||
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
|
||||
if (!search) {
|
||||
return {}
|
||||
}
|
||||
const obj = {}
|
||||
const searchArr = search.split('&')
|
||||
searchArr.forEach(v => {
|
||||
const index = v.indexOf('=')
|
||||
if (index !== -1) {
|
||||
const name = v.substring(0, index)
|
||||
const val = v.substring(index + 1, v.length)
|
||||
obj[name] = val
|
||||
}
|
||||
})
|
||||
return obj
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
param2Obj
|
||||
}
|
||||
Reference in New Issue
Block a user