基于Mock.js,使用C#生成模拟数据
获取某前端框架, 使用 Mock.js 生成模拟数据, 想要对api进行改造,并且保留原始数据,需要使用C# 重写后端api 的数据
模拟的内容:
Random.guid()
uuid: '@uuid',
id: '@id',
email: '@email',
datatime: '@datetime'
switch: '@boolean'
percent: '@integer(80,99)'
'rate|1': [1, 2, 3, 4, 5]
'status|1': ['published', 'draft', 'deleted'],
description: '@csentence',
title: '@title(1, 2)',
对应的C# 类
public static class MockHelper { private static Random _random; private static Random random { get { if (_random == null) { _random = new Random(); } return _random; } } public static string uuid() { return Guid.NewGuid().ToString(); } public static string id() { //18位 //100000000000000000 //999999999999999999
//return random.Next(100000000, 999999999).ToString();
return random.NextInt64(100000000000000000, 999999999999999999).ToString(); } /// <summary> /// 获取英文标题, n-m 个 /// </summary> /// <param name="n"></param> /// <param name="m"></param> /// <returns></returns> public static string title(int n = 1, int m = 1) { return ""; } /// <summary> /// 获取中文描述 /// </summary> /// <returns></returns> public static string csentence() { return ""; } /// <summary> /// 获取该状态的数组 /// </summary> /// <param name="t"></param> /// <param name="itemList"></param> /// <returns></returns> public static string arr(int t, List<string> itemList) { //传入 itemList 中, 随机 获取 t 个元素 var s = Enumerable.Range(1, t).Select(index => itemList[_random.Next(itemList.Count)]).ToArray(); return string.Join(",",s); } /// <summary> /// 获取该状态的数组 /// </summary> /// <param name="t"></param> /// <param name="itemList"></param> /// <returns></returns> public static int arr(int t, List<int> itemList) { //传入 itemList 中, 随机 获取 t 个元素 var s = Enumerable.Range(1, t).Select(index => itemList[_random.Next(itemList.Count)]).ToArray(); return s[0]; } /// <summary> /// 获取该状态的数组 /// </summary> /// <param name="t"></param> /// <param name="itemList"></param> /// <returns></returns> public static List<string> arr2(int t, List<string> itemList) { //传入 itemList 中, 随机 获取 t 个元素 return Enumerable.Range(1, t).Select(index => itemList[_random.Next(itemList.Count)]).ToList(); } /// <summary> /// 获取中文 名字 /// </summary> /// <returns></returns> public static string cnname() { return ""; } /// <summary> /// 随机的时间 /// </summary> /// <returns></returns> public static string datetime() { //随机的 datetime DateTime dateTime = DateTime.Now; int s = random.Next(99999999); return dateTime.AddSeconds(s).ToString("yyyy-MM-dd HH:mm:ss"); } public static int integer(int n,int m) { return random.Next(n, m); } public static bool boolean() { int t = random.Next(1, 3); return t==1; } }
对应的调用
/// <summary> /// 对应 search/getList /// </summary> /// <returns></returns> [HttpGet("table/getList")] public IActionResult table_getList() { List<tableinfo> list = new List<tableinfo>(); Random random = new Random(); var arr_status = new List<string>() { "published", "draft", "deleted" }; var arr_rate = new List<int>() { 1, 2, 3, 4, 5 }; for (int i = 0; i < 50; i++) { list.Add(new tableinfo() { uuid = MockHelper.uuid(), id = MockHelper.id(), title = gettitle(), description = getdescription(), author = getcnname(), datetime = MockHelper.datetime(), pageViews = MockHelper.integer(300, 500), status = MockHelper.arr(1, arr_status), img = $"https://cdn.jsdelivr.net/gh/chuzhixin/image/table/vab-image-{random.Next(1, 38)}.jpg", rate = MockHelper.arr(1, arr_rate), @switch = MockHelper.boolean(), percent = MockHelper.integer(88, 99), percentage = MockHelper.integer(0, 100) }); } var response = new { list = list, total = list.Count }; return SUCCESS(response, TIME_FORMAT_FULL); } static string[] cnnameArr = new string[] { "黄超", "武平", "顾洋", "阎磊", "姚敏", "韩杰", "赖娜" }; static string getcnname() { Random random = new Random(); int t = random.Next(0, cnnameArr.Length); return cnnameArr[t]; } static string[] titleArr = new string[] { "Kchcw Cethdb", "Aylm", "Qnq", "Kbpbdlx Sqevhkscop", "Dcse", "Hismm", "Uhmq Qvkfn" }; static string gettitle() { Random random = new Random(); int t = random.Next(0, titleArr.Length); return titleArr[t]; } static string[] descriptionArr = new string[] { "后研率非体才求儿且口心华热联造层相。", "由五报生什造其第铁龙历完何代直复会。", "历采率正道省社金比事正满打。", "使代消事住并眼质及两住才。", "立开决从教报得口只毛市立。", "代电代需产出声况级名连此且大志持。", "全路称合思管还话较教门并织。" }; static string getdescription() { Random random = new Random(); int t = random.Next(0, descriptionArr.Length); return descriptionArr[t]; }
本文来自博客园,作者:兴想事成,转载请注明原文链接:https://www.cnblogs.com/mjxxsc/p/17022548.html