Skip to content

Commit

Permalink
ci(mock): fix dev mock error
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Aug 4, 2020
1 parent 6ee5548 commit 34d2dfa
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 23 deletions.
17 changes: 12 additions & 5 deletions build/middleware/mockMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ module.exports = function createMockMiddleware({

require('@babel/register');
let mockData = getConfig();

console.log('======================');
console.log(mockData);
console.log('======================');
watch();

function watch() {
Expand Down Expand Up @@ -72,16 +74,15 @@ module.exports = function createMockMiddleware({
const mod = require(join(absMockPath, mockFile));
memo = {
...memo,
...mod.default,
...mod,
};
return memo;
}, {});
} catch (error) {
console.log(`${chalk.red('mock reload error!')}`);
ret = {};
// console.log(`${chalk.red('mock reload error!')}`);
}
}

return normalizeConfig(ret);
}
function randomFrom(lowerValue, upperValue) {
Expand Down Expand Up @@ -241,12 +242,18 @@ module.exports = function createMockMiddleware({
}

return mockData.filter(({ method, re }) => {
return method === exceptMethod && re.test(exceptPath);
const appUrl = process.env.GLOB_API_URL;
let _path = exceptPath;
if (exceptPath.startsWith(appUrl)) {
_path = exceptPath.replace(appUrl, '');
}
return method === exceptMethod && re.test(_path);
})[0];
}

return (req, res, next) => {
const match = matchMock(req);

if (match) {
console.log(
`${chalk.yellow('mock matched:')} ${chalk.green.bold(
Expand Down
4 changes: 2 additions & 2 deletions config/vue/entry.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const { configPolyfill } = require('./polyfill');

const { getEnvFn } = require('../../build/utils');
const { getEnvFn, isProductionFn } = require('../../build/utils');

function configEntry(config) {
let entry = ['./src/main.ts'];
entry = configPolyfill(entry);

const { VUE_APP_USE_MOCK } = getEnvFn();
// use mock
if (VUE_APP_USE_MOCK === 'TRUE') {
if (VUE_APP_USE_MOCK === 'TRUE' && isProductionFn()) {
entry.unshift('./mock/_util/mock.config.js');
}
config.entry = entry;
Expand Down
5 changes: 3 additions & 2 deletions mock/_util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* @param {string} url
* @returns {Object}
*/
export function param2Obj(url) {

exports.param2Obj = function (url) {
const search = url.split('?')[1];
if (!search) {
return {};
Expand All @@ -16,4 +17,4 @@ export function param2Obj(url) {
.replace(/\+/g, ' ') +
'"}'
);
}
};
4 changes: 2 additions & 2 deletions mock/_util/mock.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Mock from 'mockjs';
import { param2Obj } from './index';
const Mock = require('mockjs');
const { param2Obj } = require('./index');

const mockApiMethods = require.context('../', true, /^[^\_]*\.js$/);
let modules = {};
Expand Down
4 changes: 2 additions & 2 deletions mock/_util/resultUtil.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default class ResultUtil {
module.exports = class ResultUtil {
static success(result, { message = 'ok' } = {}) {
return {
code: 0,
Expand Down Expand Up @@ -35,4 +35,4 @@ export default class ResultUtil {
? array.slice(offset, array.length)
: array.slice(offset, offset + pageSize);
}
}
};
6 changes: 3 additions & 3 deletions mock/dashboard/wokb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ResultUtil from '../_util/resultUtil';
import Mock from 'mockjs';
const ResultUtil = require('../_util/resultUtil');
const Mock = require('mockjs');

const wokbProd = Mock.mock([
{
Expand Down Expand Up @@ -79,7 +79,7 @@ const annoList = Mock.mock({
},
],
});
export default {
module.exports = {
'POST /wokb/allData ': ({ query }) => {
return ResultUtil.success({
prodList: wokbProd,
Expand Down
6 changes: 3 additions & 3 deletions mock/demo/table-demo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Mock from 'mockjs';
import ResultUtil from '../_util/resultUtil';
const ResultUtil = require('../_util/resultUtil');
const Mock = require('mockjs');

const demoList = Mock.mock({
'items|60': [
Expand All @@ -15,7 +15,7 @@ const demoList = Mock.mock({
],
});

export default {
module.exports = {
'GET /v1.0/table/getDemoList 500': ({ query }) => {
const { page = 1, pageSize = 20 } = query;
const demoListItems = demoList.items;
Expand Down
4 changes: 2 additions & 2 deletions mock/sys/menu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ResultUtil from '../_util/resultUtil';
const ResultUtil = require('../_util/resultUtil');

const id1MenuList = {
path: '/auth/role',
Expand Down Expand Up @@ -123,7 +123,7 @@ const fakeCodeList = {

2: ['10010', '20020', '30030'],
};
export default {
module.exports = {
'GET /v1.0/getMenuListByUserId 20': ({ query }) => {
const { userId } = query;
return ResultUtil.success(userId ? fakeList[~~userId] || {} : {});
Expand Down
5 changes: 3 additions & 2 deletions mock/sys/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ResultUtil from '../_util/resultUtil';
const ResultUtil = require('../_util/resultUtil');

const getFakeUserList = () => [
{
userId: '1',
Expand Down Expand Up @@ -31,7 +32,7 @@ const fakeRoles = [
},
];

export default {
module.exports = {
// 300 延时时间
'POST /login 300': ({ body }) => {
const { username, password } = body;
Expand Down

0 comments on commit 34d2dfa

Please sign in to comment.