mock.js 学习
安装
npm install mockjs
使用
// 引入
import Mock from 'mockjs'
Mock.setup({
timeout: '200 - 400'
})
const Random = Mock.Random;
// 导航信息
const produceNewsData = function () {
let articles = [];
for (let i = 0; i < 20; i++) {
let newArticleObject = {
id: i,
title: Random.csentence(2, 5),
thumbnail_pic_s: Random.dataImage('80*80', 'mock的图片'),
// author_name: Random.cname(),
date: Random.date() + ' ' + Random.time()
}
articles.push(newArticleObject)
}
return {
articles: articles
}
}
// 推荐商家
const recommendBusinessman = function () {
let articles = [];
for (let i = 0; i < 40; i++) {
let newArticleObject = {
id: i,
title: Random.csentence(2, 12),
thumbnail_pic_s: Random.dataImage('65*65', 'mock的图片'),
brand: Random.boolean(),
basePrice: Random.natural(0, 100),
expressPrcie: Random.natural(0, 10),
popularity: Random.boolean(),
}
articles.push(newArticleObject)
}
return {
articles: articles
}
}
// 订单列表
const orderList = function () {
let articles = [];
for (let i = 0; i < 40; i++) {
let newArticleObject = {
id: i,
title: Random.csentence(2, 12),
orderState: Random.boolean(),
productInfo: Random.csentence(2, 12),
productPrice: Random.natural(0, 10),
date: Random.date() + ' ' + Random.time()
}
articles.push(newArticleObject)
}
return {
articles: articles
}
}
// 我的信息
const myInfo = function () {
let articles = [];
for (let i = 0; i < 1; i++) {
let newArticleObject = {
userName: Random.csentence(2, 5),
userTel: '17748682298',
userRedPacketNum: Random.csentence(0, 12),
userGoldNum: Random.natural(0, 10),
date: Random.date() + ' ' + Random.time()
}
articles.push(newArticleObject)
}
return {
articles: articles
}
}
const userInfoWord = {
userName: '17748682298',
userPassWord: '187521ZZ'
}
//验证登陆信息
function loginFun(prarms) {
const prarmsObj = prarms.body
let splitObj = prarmsObj.split("=")
prarms = {
name: splitObj[1].split('&')[0],
userWord: splitObj[2]
}
if (userInfoWord.userName === prarms.user || prarms.userWord === userInfoWord.userPassWord) {
return {
code: 1,
text: '登录成功'
}
} else {
return {
code: 2,
text: '用户名或密码错误'
}
}
}
// 订单列表
Mock.mock('/orderlist', /post|get/i, orderList)
// 导航信息
Mock.mock('/nav', /post|get/i, produceNewsData)
// 推荐商家
Mock.mock('/recommendBusinessman', /post|get/i, recommendBusinessman)
// 我的信息
Mock.mock('/myinfo', /post|get/i, myInfo)
// 推荐登陆
Mock.mock('/loginfun', /post|get/i, loginFun)