Mockjs批量生成随机JSON数据

示例一

/**
 * 将字符串url转换为正则匹配
 * 转正则的原因:如果url是字符串类型,默认会完全匹配mock地址与应用请求地址,参数不同也会造成mock拦截失效,解决方式就是用正则匹配
 * @param {String} url 接口地址
 * @return {RegExp} 返回正则匹配规则
 */
function regUrl(url) {
    let urlStr = baseUrl() + url + '.*';
    return RegExp(urlStr);
}

let Random = Mock.Random; //随机对象
Random.datetime('yyyy-MM-dd hh:mm:ss'); //格式化日期
Random.province(); //省
Random.city(); //市
Random.county(); //区县


Mock.mock(regUrl('http://www.lihefei.com/api/test?name=lihefei'), /get|post/i, {
    code: 0,
    msg: 'Mock接口模拟数据',
    'data|30': [
        {
            'direction|0-359': 0, //角度0-359
            gpsTime: '@datetime', //中线格式的日期
            'posType|1': ['GPS', 'LBS', 'WIFI'], 
            bd: {
                'latitude|-90-90.2-6': 119.789456, //全球经度
                'longitude|-180-180.2-6': 24.12345 //全球纬度
            },
            address: '@province@city@county' //省市县地址
        }
    ]
});

示例二

var Random = Mock.Random;
Random.date();
var dataMock = Mock.mock({
    'list|50': [{
        'simCar|13000000000-19900000000': 1440191734422,
        'operator|1': [
            '移动',
            '联通',
            '电信'
        ],
        'packageName|1': [
            '5元30M',
            '10元500M',
            '50元100G'
        ],
        validityPeriod: '@date',
        'unitPrice|5-10': 5,
        'quantity|10-30': 12,
        'amount|1-100': 60,
        'imei|800000000000000-900000000000000': 868120203709164,
        user: 'test'
    }]
});

示例二生成结果

{
    "list": [
    {
        "simCar": 14514939551,
        "operator": "电信",
        "packageName": "10元500M",
        "validityPeriod": "1975-06-02",
        "unitPrice": 8,
        "quantity": 29,
        "amount": 22,
        "imei": 890030819391592,
        "user": "test"
    },
    {
        "simCar": 14798794081,
        "operator": "联通",
        "packageName": "50元100G",
        "validityPeriod": "2007-09-21",
        "unitPrice": 7,
        "quantity": 25,
        "amount": 93,
        "imei": 898466852698561,
        "user": "test"
    },
    {
        "simCar": 17362393380,
        "operator": "移动",
        "packageName": "50元100G",
        "validityPeriod": "1996-12-17",
        "unitPrice": 10,
        "quantity": 28,
        "amount": 26,
        "imei": 850777689961984,
        "user": "test"
    },
    {
        "simCar": 19114404056,
        "operator": "移动",
        "packageName": "10元500M",
        "validityPeriod": "1994-05-08",
        "unitPrice": 10,
        "quantity": 28,
        "amount": 7,
        "imei": 880231778334631,
        "user": "test"
    },
    {
        "simCar": 14341788357,
        "operator": "电信",
        "packageName": "5元30M",
        "validityPeriod": "1995-05-30",
        "unitPrice": 9,
        "quantity": 21,
        "amount": 67,
        "imei": 895030529408831,
        "user": "test"
    },
    {
        "simCar": 19058352658,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "1975-03-01",
        "unitPrice": 6,
        "quantity": 27,
        "amount": 53,
        "imei": 840609290767727,
        "user": "test"
    },
    {
        "simCar": 18717423357,
        "operator": "电信",
        "packageName": "5元30M",
        "validityPeriod": "1988-09-21",
        "unitPrice": 6,
        "quantity": 14,
        "amount": 39,
        "imei": 873577112757604,
        "user": "test"
    },
    {
        "simCar": 19725221212,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "1976-09-05",
        "unitPrice": 8,
        "quantity": 29,
        "amount": 66,
        "imei": 831579513686365,
        "user": "test"
    },
    {
        "simCar": 16387773012,
        "operator": "电信",
        "packageName": "5元30M",
        "validityPeriod": "1998-03-30",
        "unitPrice": 10,
        "quantity": 24,
        "amount": 25,
        "imei": 880733635343513,
        "user": "test"
    },
    {
        "simCar": 18483950265,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "2010-08-20",
        "unitPrice": 7,
        "quantity": 28,
        "amount": 66,
        "imei": 803481587922867,
        "user": "test"
    },
    {
        "simCar": 19414859014,
        "operator": "移动",
        "packageName": "50元100G",
        "validityPeriod": "2002-08-19",
        "unitPrice": 5,
        "quantity": 21,
        "amount": 61,
        "imei": 861228533451413,
        "user": "test"
    },
    {
        "simCar": 13440450307,
        "operator": "移动",
        "packageName": "10元500M",
        "validityPeriod": "1987-01-17",
        "unitPrice": 7,
        "quantity": 23,
        "amount": 100,
        "imei": 852493999846531,
        "user": "test"
    },
    {
        "simCar": 15902022052,
        "operator": "电信",
        "packageName": "50元100G",
        "validityPeriod": "2013-04-13",
        "unitPrice": 10,
        "quantity": 20,
        "amount": 8,
        "imei": 888607365510290,
        "user": "test"
    },
    {
        "simCar": 16501149527,
        "operator": "联通",
        "packageName": "50元100G",
        "validityPeriod": "1994-08-28",
        "unitPrice": 7,
        "quantity": 19,
        "amount": 36,
        "imei": 867827865296261,
        "user": "test"
    },
    {
        "simCar": 18296419375,
        "operator": "移动",
        "packageName": "50元100G",
        "validityPeriod": "1999-08-03",
        "unitPrice": 10,
        "quantity": 13,
        "amount": 67,
        "imei": 866229428429445,
        "user": "test"
    },
    {
        "simCar": 14155215618,
        "operator": "电信",
        "packageName": "10元500M",
        "validityPeriod": "1985-11-23",
        "unitPrice": 8,
        "quantity": 28,
        "amount": 32,
        "imei": 893924086803311,
        "user": "test"
    },
    {
        "simCar": 17593572196,
        "operator": "电信",
        "packageName": "50元100G",
        "validityPeriod": "1982-07-07",
        "unitPrice": 5,
        "quantity": 17,
        "amount": 13,
        "imei": 862740470986838,
        "user": "test"
    },
    {
        "simCar": 18227798069,
        "operator": "联通",
        "packageName": "50元100G",
        "validityPeriod": "1998-03-28",
        "unitPrice": 8,
        "quantity": 21,
        "amount": 56,
        "imei": 884237504914401,
        "user": "test"
    },
    {
        "simCar": 19748681145,
        "operator": "电信",
        "packageName": "10元500M",
        "validityPeriod": "1995-02-25",
        "unitPrice": 7,
        "quantity": 28,
        "amount": 86,
        "imei": 826490478796925,
        "user": "test"
    },
    {
        "simCar": 14576670496,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "1979-08-12",
        "unitPrice": 9,
        "quantity": 24,
        "amount": 95,
        "imei": 897536555045253,
        "user": "test"
    },
    {
        "simCar": 16109710752,
        "operator": "移动",
        "packageName": "10元500M",
        "validityPeriod": "1984-08-05",
        "unitPrice": 10,
        "quantity": 20,
        "amount": 13,
        "imei": 874745209730625,
        "user": "test"
    },
    {
        "simCar": 16239886599,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "2009-03-11",
        "unitPrice": 5,
        "quantity": 27,
        "amount": 12,
        "imei": 836832619872943,
        "user": "test"
    },
    {
        "simCar": 17939356307,
        "operator": "电信",
        "packageName": "10元500M",
        "validityPeriod": "1991-09-07",
        "unitPrice": 6,
        "quantity": 18,
        "amount": 88,
        "imei": 851356917290854,
        "user": "test"
    },
    {
        "simCar": 18527263012,
        "operator": "电信",
        "packageName": "10元500M",
        "validityPeriod": "1985-06-24",
        "unitPrice": 6,
        "quantity": 17,
        "amount": 11,
        "imei": 886757637504435,
        "user": "test"
    },
    {
        "simCar": 13028088454,
        "operator": "移动",
        "packageName": "5元30M",
        "validityPeriod": "1977-05-19",
        "unitPrice": 7,
        "quantity": 29,
        "amount": 80,
        "imei": 834721094464242,
        "user": "test"
    },
    {
        "simCar": 18510025307,
        "operator": "联通",
        "packageName": "5元30M",
        "validityPeriod": "1996-07-08",
        "unitPrice": 7,
        "quantity": 30,
        "amount": 80,
        "imei": 882198262193475,
        "user": "test"
    },
    {
        "simCar": 13808030351,
        "operator": "移动",
        "packageName": "10元500M",
        "validityPeriod": "2005-03-10",
        "unitPrice": 10,
        "quantity": 26,
        "amount": 90,
        "imei": 827948448994677,
        "user": "test"
    },
    {
        "simCar": 18510172371,
        "operator": "联通",
        "packageName": "5元30M",
        "validityPeriod": "1983-07-07",
        "unitPrice": 8,
        "quantity": 23,
        "amount": 50,
        "imei": 869892205577047,
        "user": "test"
    },
    {
        "simCar": 19110928665,
        "operator": "电信",
        "packageName": "50元100G",
        "validityPeriod": "2003-08-10",
        "unitPrice": 9,
        "quantity": 23,
        "amount": 38,
        "imei": 801251309400261,
        "user": "test"
    },
    {
        "simCar": 13888834166,
        "operator": "联通",
        "packageName": "5元30M",
        "validityPeriod": "2008-05-10",
        "unitPrice": 5,
        "quantity": 22,
        "amount": 39,
        "imei": 884055425949977,
        "user": "test"
    },
    {
        "simCar": 18931203845,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "2018-01-09",
        "unitPrice": 5,
        "quantity": 23,
        "amount": 80,
        "imei": 852828152985024,
        "user": "test"
    },
    {
        "simCar": 15904304760,
        "operator": "移动",
        "packageName": "5元30M",
        "validityPeriod": "1999-09-16",
        "unitPrice": 9,
        "quantity": 16,
        "amount": 28,
        "imei": 865702567049672,
        "user": "test"
    },
    {
        "simCar": 15675281891,
        "operator": "移动",
        "packageName": "5元30M",
        "validityPeriod": "1994-09-20",
        "unitPrice": 6,
        "quantity": 11,
        "amount": 53,
        "imei": 853793107424855,
        "user": "test"
    },
    {
        "simCar": 16858724167,
        "operator": "联通",
        "packageName": "50元100G",
        "validityPeriod": "1972-12-13",
        "unitPrice": 6,
        "quantity": 10,
        "amount": 20,
        "imei": 885580073956485,
        "user": "test"
    },
    {
        "simCar": 13122536790,
        "operator": "联通",
        "packageName": "50元100G",
        "validityPeriod": "1975-08-14",
        "unitPrice": 6,
        "quantity": 11,
        "amount": 39,
        "imei": 855558646675381,
        "user": "test"
    },
    {
        "simCar": 15919696562,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "1983-11-10",
        "unitPrice": 9,
        "quantity": 18,
        "amount": 88,
        "imei": 827319552951166,
        "user": "test"
    },
    {
        "simCar": 17494835189,
        "operator": "移动",
        "packageName": "10元500M",
        "validityPeriod": "2016-02-17",
        "unitPrice": 5,
        "quantity": 25,
        "amount": 30,
        "imei": 801064067705639,
        "user": "test"
    },
    {
        "simCar": 17448674712,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "2008-08-29",
        "unitPrice": 7,
        "quantity": 25,
        "amount": 24,
        "imei": 825519785186945,
        "user": "test"
    },
    {
        "simCar": 14491395241,
        "operator": "电信",
        "packageName": "5元30M",
        "validityPeriod": "2003-10-12",
        "unitPrice": 8,
        "quantity": 21,
        "amount": 9,
        "imei": 855451560160596,
        "user": "test"
    },
    {
        "simCar": 13183974573,
        "operator": "联通",
        "packageName": "50元100G",
        "validityPeriod": "1987-12-14",
        "unitPrice": 6,
        "quantity": 29,
        "amount": 22,
        "imei": 853969910690622,
        "user": "test"
    },
    {
        "simCar": 18289632548,
        "operator": "电信",
        "packageName": "10元500M",
        "validityPeriod": "1998-04-01",
        "unitPrice": 7,
        "quantity": 10,
        "amount": 51,
        "imei": 846344615227595,
        "user": "test"
    },
    {
        "simCar": 16307571986,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "1981-04-25",
        "unitPrice": 8,
        "quantity": 23,
        "amount": 79,
        "imei": 872662352550854,
        "user": "test"
    },
    {
        "simCar": 13720011760,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "2002-10-25",
        "unitPrice": 9,
        "quantity": 30,
        "amount": 57,
        "imei": 886732013286832,
        "user": "test"
    },
    {
        "simCar": 14894482772,
        "operator": "移动",
        "packageName": "10元500M",
        "validityPeriod": "2012-06-02",
        "unitPrice": 5,
        "quantity": 29,
        "amount": 43,
        "imei": 865478233173510,
        "user": "test"
    },
    {
        "simCar": 17101923732,
        "operator": "移动",
        "packageName": "50元100G",
        "validityPeriod": "1991-03-12",
        "unitPrice": 5,
        "quantity": 21,
        "amount": 91,
        "imei": 815066465978981,
        "user": "test"
    },
    {
        "simCar": 18257632347,
        "operator": "电信",
        "packageName": "5元30M",
        "validityPeriod": "2011-01-13",
        "unitPrice": 7,
        "quantity": 13,
        "amount": 55,
        "imei": 841578662709560,
        "user": "test"
    },
    {
        "simCar": 14844088265,
        "operator": "联通",
        "packageName": "10元500M",
        "validityPeriod": "1998-03-09",
        "unitPrice": 5,
        "quantity": 15,
        "amount": 89,
        "imei": 824780531386859,
        "user": "test"
    },
    {
        "simCar": 19074350385,
        "operator": "联通",
        "packageName": "50元100G",
        "validityPeriod": "2015-05-26",
        "unitPrice": 9,
        "quantity": 13,
        "amount": 81,
        "imei": 882936029645768,
        "user": "test"
    },
    {
        "simCar": 18333624949,
        "operator": "联通",
        "packageName": "50元100G",
        "validityPeriod": "2018-03-28",
        "unitPrice": 8,
        "quantity": 14,
        "amount": 25,
        "imei": 830672063452912,
        "user": "test"
    },
    {
        "simCar": 18074045598,
        "operator": "移动",
        "packageName": "10元500M",
        "validityPeriod": "1996-12-06",
        "unitPrice": 9,
        "quantity": 19,
        "amount": 35,
        "imei": 887540376467915,
        "user": "test"
    }]
}

你可能感兴趣的:(mockjs)