基於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];
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章