UNPKG

ttk-app-core

Version:
246 lines (230 loc) 7.7 kB
/** * mock.js 提供应用截获ajax请求,为脱离后台测试使用 * 模拟查询更改内存中mockData,并返回数据 */ import { fetch } from 'edf-utils' import cloneDeep from 'lodash.clonedeep' import moment from 'moment' import utils from 'edf-utils' import img1 from './img/img1.png' import img2 from './img/img2.jpg' let _provinces = [ { 'n': "北京市", 'c': "110000" }, { 'n': "天津市", 'c': "120000" }, { 'n': "河北省", 'c': "130000" }, { 'n': "山西省", 'c': "140000" }, { 'n': "内蒙古自治区", 'c': "150000" }, { 'n': "辽宁省", 'c': "210000" }, { 'n': "吉林省", 'c': "220000" }, { 'n': "黑龙江省", 'c': "230000" }, { 'n': "上海市", 'c': "310000" }, { 'n': "江苏省", 'c': "320000" }, { 'n': "浙江省", 'c': "330000" }, { 'n': "安徽省", 'c': "340000" }, { 'n': "福建省", 'c': "350000" }, { 'n': "江西省", 'c': "360000" }, { 'n': "山东省", 'c': "370000" }, { 'n': "河南省", 'c': "410000" }, { 'n': "湖北省", 'c': "420000" }, { 'n': "湖南省", 'c': "430000" }, { 'n': "广东省", 'c': "440000" }, { 'n': "广西壮族自治区", 'c': "450000" }, { 'n': "海南省", 'c': "460000" }, { 'n': "重庆市", 'c': "500000" }, { 'n': "四川省", 'c': "510000" }, { 'n': "贵州省", 'c': "520000" }, { 'n': "云南省", 'c': "530000" }, { 'n': "西藏自治区", 'c': "540000" }, { 'n': "陕西省", 'c': "610000" }, { 'n': "甘肃省", 'c': "620000" }, { 'n': "青海省", 'c': "630000" }, { 'n': "宁夏回族自治区", 'c': "640000" }, { 'n': "新疆维吾尔自治区", 'c': "650000" } ] const origin = ['淘宝', '京东', '苏宁', '当当', '唯品会', '天猫'] const status = ['已收款', '未收款'] function createListFun (len) { let num = 0 let time = moment().startOf('year').toDate().getTime() function createList(listLen, level){ let arr = [] level++ for( let i = 0; i<listLen; i++ ){ num++ time = time + 1000*60*60*24*2 let obj = { key: num, date: moment(time).format('YYYY-MM-DD'), number: Math.ceil(Math.random()*1000), origin: origin[Math.floor(Math.random()*6)], id: Math.ceil(Math.random()*100000), status: status[Math.floor(Math.random()*2)], province: _provinces[ Math.floor(Math.random()*31)].n, children: Math.ceil(Math.random()*1000)%2 == 0 && level<3 ? createList(Math.ceil(Math.random()*4)) : null } if( !obj.children ){ delete obj.children } arr.push(obj) } return arr } return createList(len, 1) } const dbData = createListFun(100) fetch.mock('/v1/complexlist/init', option => { option.pagination.total = 1000 let filter = option.filter let data = dateFilter(dbData, filter.common) //filter status data = dbFilter(data, 'status', filter.status) //filter area data = dbFilter(data, 'province', filter.customer) //filter number data = dbFilter(data, 'number', filter.code) return { result: true, value: { pagination: option.pagination, filter: option.filter, list: data, total: 1000 } } }) function dbFilter (data, key, value){ if( !value || value == 'all'){ return data } let arr = [] data.map(item => { if( item[key] == value ) { let childArr = cloneDeep(item) if( item.children && item.children.length > 0 ){ childArr.children = dbFilter(childArr.children, key, value) } arr.push(childArr) } }) return arr } function dateFilter(data, key){ if( key == 'all' ){ return data } let arr = [] if( key == 'today' ){ data.forEach(item => { if( moment().format('YYYYMMDD') == moment(item.date).format('YYYYMMDD') ){ let childArr = cloneDeep(item) if( item.children && item.children.length > 0 ){ childArr.children = dateFilter(childArr.children, key) } arr.push(childArr) } }) } if(key == 'thisWeek'){ data.forEach(item => { if( moment().format('YYYYWW') == moment(item.date).format('YYYYWW') ){ let childArr = cloneDeep(item) if( item.children && item.children.length > 0 ){ childArr.children = dateFilter(childArr.children, key) } arr.push(childArr) } }) } if(key == 'thisMonth'){ data.forEach(item => { if( moment().format('YYYYMM') == moment(item.date).format('YYYYMM') ){ let childArr = cloneDeep(item) if( item.children && item.children.length > 0 ){ childArr.children = dateFilter(childArr.children, key) } arr.push(childArr) } }) } if( key == 'thisYear' ){ data.forEach(item => { if( moment().format('YYYY') == moment(item.date).format('YYYY') ){ let childArr = cloneDeep(item) if( item.children && item.children.length > 0 ){ childArr.children = dateFilter(childArr.children, key) } arr.push(childArr) } }) } return arr } function setIndex (data) { let num = 0 let arr = [] function childSet (origin) { return origin.map(item => { num++ let obj = { ...item, index: num, children: item.children&&item.children.length > 0 ? childSet(item.children) : null } if( !obj.children ){ delete obj.children } return obj }) } return childSet(data) } fetch.mock('/v1/complexlist/query', ({filter, pagination}) => { // 过滤时间 let data = dateFilter(dbData, filter.common) //filter status data = dbFilter(data, 'status', filter.status) //filter area data = dbFilter(data, 'province', filter.customer) //filter number data = dbFilter(data, 'number', filter.code) //set index const a = filter.status=="all" ? data : setIndex(data) return { result: true, value: { pagination: pagination, filter: filter, list: a, total: 1000 } } }) fetch.mock('/v1/complexlist/del', ({id}) => { let parentIndex = null let childIndex = null dbData.find((item, index) => { if( item.id == id ){ parentIndex = index return true } if( item.children && item.children.length > 0 ){ item.children.find((child, key) => { if( child.id == id ){ parentIndex = index childIndex = key return true } }) } }) if( childIndex == null ){ dbData.splice(parentIndex, 1) } if( parentIndex != null && childIndex != null ){ dbData[parentIndex].children = dbData[parentIndex].children.splice(childIndex, 1) } return { result: true, value: {} } })