From 6307e31366a9ca6dd3c1f3be368f77ab4de3bd99 Mon Sep 17 00:00:00 2001 From: zuiidea Date: Wed, 19 Oct 2016 17:25:46 +0800 Subject: [PATCH] local mock data --- mock/users.js | 112 ++++++++++++++++-------------------------- proxy.config.js | 2 + src/models/users.js | 7 ++- src/services/users.js | 3 +- src/utils/mock.js | 28 +++++------ src/utils/request.js | 9 ---- 6 files changed, 62 insertions(+), 99 deletions(-) diff --git a/mock/users.js b/mock/users.js index ad3a3da..9673bc1 100644 --- a/mock/users.js +++ b/mock/users.js @@ -7,18 +7,20 @@ const mockjs = require('mockjs') let tableListData = {} if (!global.tableListData) { const data = mockjs.mock({ - 'data|100': [{ - 'id|+1': 1, - name: '@cname', - nickName:'@name', - phone:/^1[34578]\d{9}$/, - 'age|11-99': 1, - address: '@county(true)', - isMale:'@boolean', - email:'@email', - createTime:'@datetime', - avatar:mockjs.Random.image('100x100',mockjs.Random.color()), - }], + 'data|100': [ + { + 'id|+1': 1, + name: '@cname', + nickName: '@name', + phone: /^1[34578]\d{9}$/, + 'age|11-99': 1, + address: '@county(true)', + isMale: '@boolean', + email: '@email', + createTime: '@datetime', + avatar: mockjs.Random.image('100x100', mockjs.Random.color()) + } + ], page: { total: 100, current: 1 @@ -44,7 +46,7 @@ module.exports = { if (page.field) { const d = newData.filter(function (item) { - return item[page.field].indexOf(page.keyword) > -1 + return item[page.field].indexOf(decodeURI(page.keyword)) > -1 }) data = d.slice((currentPage - 1) * pageSize, currentPage * pageSize) @@ -61,80 +63,50 @@ module.exports = { total: tableListData.page.total } } - res.json({ - success: true, - data, - page: newPage - }) - - // setTimeout(function () { - // res.json({ - // success: true, - // data, - // page: newPage - // }) - // }, 500) + res.json({success: true, data, page: newPage}) }, 'POST /api/users' (req, res) { - setTimeout(function () { - const newData = qs.parse(req.body) + const newData = qs.parse(req.body) - newData.id = tableListData.data.length + 1 - tableListData.data.unshift(newData) + newData.id = tableListData.data.length + 1 + tableListData.data.unshift(newData) - tableListData.page.total = tableListData.data.length - tableListData.page.current = 1 + tableListData.page.total = tableListData.data.length + tableListData.page.current = 1 - global.tableListData = tableListData - res.json({ - success: true, - data: tableListData.data, - page: tableListData.page - }) - }, 500) + global.tableListData = tableListData + res.json({success: true, data: tableListData.data, page: tableListData.page}) }, 'DELETE /api/users' (req, res) { - setTimeout(function () { - const deleteItem = qs.parse(req.body) + const deleteItem = qs.parse(req.body) - tableListData.data = tableListData.data.filter(function (item) { - if (item.id == deleteItem.id) { - return false - } - return true - }) + tableListData.data = tableListData.data.filter(function (item) { + if (item.id == deleteItem.id) { + return false + } + return true + }) - tableListData.page.total = tableListData.data.length + tableListData.page.total = tableListData.data.length - global.tableListData = tableListData - res.json({ - success: true, - data: tableListData.data, - page: tableListData.page - }) - }, 500) + global.tableListData = tableListData + res.json({success: true, data: tableListData.data, page: tableListData.page}) }, 'PUT /api/users' (req, res) { - setTimeout(function () { - const editItem = qs.parse(req.body) + const editItem = qs.parse(req.body) - tableListData.data = tableListData.data.map(function (item) { - if (item.id == editItem.id) { - return editItem - } - return item - }) + tableListData.data = tableListData.data.map(function (item) { + if (item.id == editItem.id) { + return editItem + } + return item + }) - global.tableListData = tableListData - res.json({ - success: true, - data: tableListData.data, - page: tableListData.page - }) - }, 500) + global.tableListData = tableListData + res.json({success: true, data: tableListData.data, page: tableListData.page}) } } diff --git a/proxy.config.js b/proxy.config.js index d3c83e0..ccdf9e5 100644 --- a/proxy.config.js +++ b/proxy.config.js @@ -4,6 +4,8 @@ const mock = {} require('fs').readdirSync(require('path').join(__dirname + '/mock')) .forEach(function (file) { + console.log(file); + console.log(require('./mock/' + file)); Object.assign(mock, require('./mock/' + file)) }) diff --git a/src/models/users.js b/src/models/users.js index 9446c23..6fb02ad 100644 --- a/src/models/users.js +++ b/src/models/users.js @@ -37,7 +37,6 @@ export default { *query({ payload }, { call, put }) { yield put({ type: 'showLoading' }) const data = yield call(query, parse(payload)) - console.log("modae",data); if (data) { yield put({ type: 'querySuccess', @@ -53,7 +52,7 @@ export default { }, *'delete'({ payload }, { call, put }) { yield put({ type: 'showLoading' }) - const { data } = yield call(remove, { id: payload }) + const data = yield call(remove, { id: payload }) if (data && data.success) { yield put({ type: 'deleteSuccess', @@ -64,7 +63,7 @@ export default { *create({ payload }, { call, put }) { yield put({ type: 'hideModal' }) yield put({ type: 'showLoading' }) - const { data } = yield call(create, payload) + const data = yield call(create, payload) if (data && data.success) { yield put({ type: 'createSuccess', @@ -77,7 +76,7 @@ export default { yield put({ type: 'showLoading' }) const id = yield select(({ users }) => users.currentItem.id) const newUser = { ...payload, id } - const { data } = yield call(update, newUser) + const data = yield call(update, newUser) if (data && data.success) { yield put({ type: 'updateSuccess', diff --git a/src/services/users.js b/src/services/users.js index 02d964d..aebebc4 100644 --- a/src/services/users.js +++ b/src/services/users.js @@ -7,7 +7,6 @@ export async function query(params) { body: qs.stringify(params), data:params, }) - // return request(`/api/users?${qs.stringify(params)}`) } export async function create(params) { @@ -30,6 +29,6 @@ export async function update(params) { return request('/api/users', { method: 'put', body: qs.stringify(params), - data:params, + data: params, }) } diff --git a/src/utils/mock.js b/src/utils/mock.js index dca8ea5..2c4e1a4 100644 --- a/src/utils/mock.js +++ b/src/utils/mock.js @@ -1,6 +1,6 @@ -const qs = require('qs') const Mock = require('mockjs') const mockUser = require("../../mock/users") +const mockData = [require("../../mock/users")] function serialize(str) { let paramArray = str.split("&") @@ -11,19 +11,19 @@ function serialize(str) { return query } -for (let key in mockUser) { - Mock.mock(eval("/" + key.split(" ")[1]), key.split(" ")[0].toLowerCase(), function (options) { - options.query = !!options.url.split("?")[1] - ? serialize(options.url.split("?")[1]) - : {} - let res = {} - let result = {} - res.json = function (data) { - result = data - } - mockUser[key](options, res) - return result - }) +for (let i in mockData) { + for (let key in mockData[i]) { + Mock.mock(eval("/" + key.split(" ")[1]), key.split(" ")[0].toLowerCase(), function (options) { + options.query = !!options.url.split("?")[1] ? serialize(options.url.split("?")[1]) : (!!options.body ? serialize(options.body) : {}) + let res = {} + let result = {} + res.json = function (data) { + result = data + } + mockUser[key](options, res) + return result + }) + } } Mock.setup({timeout: 400}) diff --git a/src/utils/request.js b/src/utils/request.js index 6f8a0d1..63d08df 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -1,4 +1,3 @@ -import fetch from 'dva/fetch' const Ajax = require("robe-ajax") function parseJSON(response) { @@ -23,14 +22,6 @@ function checkStatus(response) { * @return {object} An object containing either "data" or "err" */ export default function request(url, options) { - // return fetch(url, options) - // .then(checkStatus) - // .then(parseJSON) - // .then((data) => { - // console.log(data); - // return data - // }) - // .catch((err) => ({ err })) return Ajax.ajax({ url: url,