mock模拟后端数据

在Mock.js官网中https://github.com/nuysoft/Mock/wiki/Getting-Started,下载客户端mock.js文件:

安装

npm install -g bower
bower install --save mockjs

1.正则匹配路径

Mock.mock(RegExp('/Api/EnterpriseQq/Manage/UsageRecord' + '.*'), 'get', function (option) {
	console.log(option);
	//获得?之后的参数
	var querys = option.url.split('?')[1];
	// var keys=[];
	// var values=[];
	var params = querys.split('&');
	var obj = {};
	for (var i = 0; i < params.length; i++) {
		// keys.push(params[i].split('=')[0]);
		// values.push(params[i].split('=')[1]);
		var key = params[i].split('=')[0];
		var value = params[i].split('=')[1];
		obj[key] = value;
	}
	// console.log(keys,values);

	return Mock.mock({
		StatusCode: '0',
		StatusMsg: '操作成功',
		StatusData: {
			ResultCode: 0,
			ResultMsg: '查询成功',
			// 

			//  { field: 'Id', title: '使用记录Id', hide: true }
            // , { field: 'QqNum', title: 'QQ号码', width: '15%', }
            // , { field: 'MerchantName', title: '使用商家', width: '20%', }
            // , { field: 'MemberName', title: '用户名', width: '15%', }
            // , { field: 'StartTime', title: '分配时间', width: '22%', }
            //  , { field: 'EndTime', title: '结束时间', width: '22%', }
            // , { field: 'Remark', title: '备注', width: '14%' }
			ResultData:{
				TotalRecord: 30,
				PageSize:10,
				PageIndex:1,
				'PageResult|0-10': [{
				'Id|+1': 2, //每次id的值加1
				'QqNum': '@integer(200,300)',
				'MerchantName': '@cname',
				'MemberName': '@cname',
				'StartTime':'@date',
				'EndTime':'@date',
				'Remark' :'@string'
				// 'Password': '@integer(20,30)', //20~30的整数
				// 'State': '@integer(0,1)',
				
				// 'MerchantType': '@constellation',
				// 
				// 'Enabled': '@boolean',
				// 'CreatTime': '@date',
				// 'first': '@cfirst',
				// 'data': '@date',
				// '城市': '@city',
				// 'city': '@city(true)',
				// 'married': '@boolean',
				// 'image': '@image(125x125,#f00,sample11)'
				}]
			}
		
		}
	})

})

2.创建自定义mock函数

Mock.mock('http://baidu.com/userlist', {
	StatusCode: '0',
	StatusMsg: '操作成功',

	ResultData: {
		ResultCode: 0,
		ResultMsg: '查询成功',
		'ResultData|1-10': [{
			'id|+1': 2, //每次id的值加1
			"name": '@cname',
			'age': '@integer(20,30)', //20~30的整数
			'first': '@cfirst',
			'data': '@date',
			'城市': '@city',
			'city': '@city(true)', //包含省
			'married': '@boolean',
			'image': '@image(125x125,#f00,sample11)',
			'字': '@cword(2,4)'
		}]
	}
	//	'list|1-10': [{
	//		//					"id": "@id", //身份证号
	//		'id|+1': 2, //每次id的值加1
	//		"name": '@cname',
	//		'age': '@integer(20,30)', //20~30的整数
	//		'first': '@cfirst',
	//		'data': '@date',
	//		'城市': '@city',
	//		'city': '@city(true)', //包含省
	//		'married': '@boolean',
	//		'image': '@image(125x125,#f00,sample11)',
	//		'字': '@cword(2,4)'
	//	}]

})

// Mock.mock('/Api/EnterpriseQq/Manage/Insert', {
// 	StatusCode: '0',
// 	StatusMsg: '操作成功',

// 	ResultData: {
// 		ResultCode: 0,
// 		ResultMsg: '查询成功',
// 		'ResultData|1-10': [{
// 			//					"id": "@id", //身份证号
// 			'id|+1': 2, //每次id的值加1
// 			"name": '@cname',
// 			'age': '@integer(20,30)', //20~30的整数
// 			'first': '@cfirst',
// 			'data': '@date',
// 			'城市': '@city',
// 			'city': '@city(true)',
// 			'married': '@boolean',
// 			'image': '@image(125x125,#f00,sample11)'
// 		}]
// 	}

// })

//
//Mock.mock('/Api/Point/UserCenter/GetPoint', {
//	'list|1-10': [{
//		//					"id": "@id", //身份证号
//		'id|+1': 2, //每次id的值加1
//		"name": '@cname',
//		'age': '@integer(20,30)', //20~30的整数
//		'first': '@cfirst',
//		'data': '@date',
//		'城市': '@city',
//		'city': '@city(true)',
//		'married': '@boolean',
//		'image': '@image(125x125,#f00,sample11)'
//	}]
//
//})
// Mock.mock(RegExp('/api/getGoodsListById'+'.*'), 'get', function(option) {
// 	console.log(option)
// /Api/EnterpriseQq/Manage/List
Mock.mock(RegExp('/Api/EnterpriseQq/Manage/List' + '.*'), 'get', function (option) {
	console.log(option);
	//获得?之后的参数
	var querys = option.url.split('?')[1];
	// var keys=[];
	// var values=[];
	var params = querys.split('&');
	var obj = {};
	for (var i = 0; i < params.length; i++) {
		// keys.push(params[i].split('=')[0]);
		// values.push(params[i].split('=')[1]);
		var key = params[i].split('=')[0];
		var value = params[i].split('=')[1];
		obj[key] = value;
	}
	// console.log(keys,values);


	return Mock.mock({
		StatusCode: '0',
		StatusMsg: '操作成功',
		StatusData: {
			ResultCode: 0,
			ResultMsg: '查询成功',
			// 
			ResultData:{
				TotalRecord: 30,
				PageSize:10,
				PageIndex:1,
				'PageResult|0-10': [{
				'Id|+1': 2, //每次id的值加1
				'QqNum': '@integer(200,300)',
				'Password': '@integer(20,30)', //20~30的整数
				'State': '@integer(0,1)',
				'MerchantName': '@cname',
				'MerchantType': '@constellation',
				'MemberName': '@cname',
				'Enabled': '@boolean',
				'CreatTime': '@date',
				'first': '@cfirst',
				'data': '@date',
				'城市': '@city',
				'city': '@city(true)',
				'married': '@boolean',
				'image': '@image(125x125,#f00,sample11)'
				}]
			}
			
		}

	})


})

//创建自定义mock函数
var random = Mock.Random;
random.extend({
	//自定义函数名字:处理函数,不要写成箭头函数,因为内部有this
	constellation: function (date) {
		//定义数组
	var  constellations = ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座']
		//随机返回数组中的数组
		return this.pick(constellations)
	}
})

// 
// Mock.mock('/Api/MerchantAuth/Manage/MerchantTypeList', {
// 	StatusCode: '0',
// 	StatusMsg: '操作成功',
// 	StatusData: {

// 		ResultCode: 0,
// 		ResultMsg: '查询成功',
// 		'ResultData|3-5':
// 			[{
// 				'Id|+1': 2, //每次id的值加1
// 				'Name': '@cname'
// 			}]

		
// 	}

// })



//模拟post请求,外层的Mock只负责拦截接口和方法的定义
Mock.mock('/api/addGoods', 'post', function (option) {
	//请求相关的参数
	//	console.log(option)
	//模拟假数据需要重新写Mock
	return Mock.mock({
		StatusCode: '0',
		StatusMsg: '操作成功',
		ResultData: {
			ResultCode: 0,
			ResultMsg: '查询成功',
			'ResultData|1-10': [{
				//					"id": "@id", //身份证号
				status: 200,
				message: '@cword(4,9)'
			}]
		}
	})
})

//修改操作
Mock.mock(RegExp('/Api/EnterpriseQq/Manage/Update' + '.*'), 'post', function (option) {
	//请求相关的参数
	//	console.log(option)
	//模拟假数据需要重新写Mock
	// debugger
	console.log(option, 'update');
	return Mock.mock({
		StatusCode: '0',
		StatusMsg: '操作成功',
		StatusData: {
			ResultCode: 0,
			ResultMsg: '更新成功',
			'ResultData': ''
		}

	})

})
// /Api/EnterpriseQq/Manage/Insert
// 添加成功
Mock.mock(RegExp('/Api/EnterpriseQq/Manage/Insert' + '.*'), 'post', function (option) {
	//请求相关的参数
	console.log(option, 'insert');
	//模拟假数据需要重新写Mock
	// debugger
	return Mock.mock({
		StatusCode: '0',
		StatusMsg: '操作成功',
		StatusData: {
			ResultCode: 0,
			ResultMsg: '添加成功',
			'ResultData': ''
		}

	})

})

///删除操作 Api/EnterpriseQq/Manage/Delete
Mock.mock(RegExp('Api/EnterpriseQq/Manage/Delete' + '.*'), 'get', function (option) {
	//请求相关的参数
	console.log(option, 'delete');
	//模拟假数据需要重新写Mock
	// debugger
	return Mock.mock({
		StatusCode: '0',
		StatusMsg: '操作成功',
		StatusData: {
			ResultCode: 0,
			ResultMsg: '删除成功',
			'ResultData': ''
		}

	})

})
// /Api/EnterpriseQq/Manage/SwitchEnabled
Mock.mock(RegExp('/Api/EnterpriseQq/Manage/SwitchEnabled' + '.*'), 'get', function (option) {
	//请求相关的参数
	console.log(option, 'switch');
	//模拟假数据需要重新写Mock
	// debugger
	return Mock.mock({
		StatusCode: '0',
		StatusMsg: '操作成功',
		StatusData: {
			ResultCode: 0,
			ResultMsg: '切换成功',
			'ResultData': ''
		}

	})

})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值