How to use the Random function from mockjs

Find comprehensive JavaScript mockjs.Random code examples handpicked from public code repositorys.

0
1
2
3
4
5
6
7
8
9
/** 
 * 模拟数据动态化,引入mockjs.js来处理
 * author: liaoyanli
 */
let Mock = require('mockjs');
var Random = Mock.Random;
export function setString() {
    Random.word(3, 8);
    return Mock.mock('@word(3, 8)');
}
fork icon20
star icon102
watch icon15

29
30
31
32
33
34
35
36
37
38
  [`data|${limit}`]: [
    {
      "id|+1": 1,
      "label|1": ["【系统通知】", '', '【自动触发】', '【科技】', '【新闻】'],
      "title|1": ["该系统将于今晚凌晨2点到5点进行升级维护", '好友给你发来一条元宵祝福', '你的文章被评为年度优秀作品', 'xx炮轰安卓高抽成,称其对产业生态非常不利', '旧代码获新生!AI 重写的应用程序可用于最新计算平台', '今天你打到车了吗?滴滴突然崩了,司机接不到人、乘客付不了钱', '技术写作技巧分享:我是如何从写作小白成长为多平台优秀作者的?'],
      "time": () => mockjs.Random.date('yyyy-MM-dd HH:mm:ss')
    }
  ]
})
utils.errorHandle(ctx, 200, '获取列表成功', data.data, { total: 120 })
fork icon15
star icon51
watch icon1

+ 5 other calls in file

16
17
18
19
20
21
22
23
24
25
@Get("/users")
async getAll() {
    // init
    const user = new User();
    const msg = this.userSrv.createMsg();
    user.firstName = MockJs.Random.first();
    user.lastName = MockJs.Random.last();
    user.cName = MockJs.Random.cname() + MockJs.Random.cfirst();
    user.age = getRandom(60);
    user.sex = getRandom(100) > 50 ? 0 : 1
fork icon6
star icon65
watch icon7

+ 5 other calls in file

3
4
5
6
7
8
9
10
export function getProduct(index) {
  const res =
    index !== undefined
      ? productList[index]
      : productList[Mock.Random.integer(0, productList.length - 1)];
  //res.title += Mock.Random.ctitle(0, 12);
  return res;
}
fork icon21
star icon43
watch icon0

30
31
32
33
34
35
36
37
38
39
```js
import KoaRouter from 'koa-router'
import proxy from '../middleware/Proxy'
import { delay } from '../lib/util'
let Mock = require('mockjs')
let Random = Mock.Random

const router = new KoaRouter()
router.get('/your-mock-api', (ctx) => {
        ctx.body = '你的第一个mock接口'
fork icon6
star icon19
watch icon2

+ 3 other calls in file

-3
fork icon0
star icon0
watch icon1

+ 59 other calls in file

613
614
615
616
617
618
619
620
621
622
  mockJson: context.mockJson,
  params: Object.assign({}, context.ctx.query, context.ctx.request.body),
  resHeader: context.resHeader,
  httpCode: context.httpCode,
  delay: context.httpCode,
  Random: Mock.Random
};
sandbox.cookie = {};

context.ctx.header.cookie &&
fork icon0
star icon0
watch icon1

+ 59 other calls in file

174
175
176
177
178
179
180
181
182
183
let property = JSON.parse(content);
let res = {};
for (const item in property) {
  switch (property[item].type) {
    case '文字输入框':
      res[property[item].label] = mock.Random.cword();
      break;
    case '数字输入框':
      res[property[item].label] = mock.Random.natural();
      break;
fork icon0
star icon0
watch icon1

+ 1007 other calls in file