提交 0495c15c authored 作者: vipcxj's avatar vipcxj

fix: delete方法不能保护http body,传参必须用query参数。

fix: 生成菜单数据出错。
上级 76451936
import { routerRedux } from 'dva/router'; import { routerRedux } from 'dva/router';
import { isString } from 'lodash';
import { logout } from '../../services/main'; import { logout } from '../../services/main';
import { fetchDomains, switchDomain, currentDomain } from '../../services/domain'; import { fetchDomains, switchDomain, currentDomain } from '../../services/domain';
import { cookie } from '../../utils/config'; import { cookie } from '../../utils/config';
import { getMenus } from '../../data/modules'; import { getMenus, getModuleInfo } from '../../data/modules';
import { setCookie, delCookie, fullPath, getCookie } from '../../utils/helper'; import { setCookie, delCookie, fullPath, getCookie } from '../../utils/helper';
const createMenu = (configure) => { const createMenu = async (configure) => {
return { const menu = {
name: configure.name, name: configure.name,
icon: configure.icon, icon: configure.icon,
text: configure.showName, text: configure.showName,
children: [
...(configure.children || []).map(child => createMenu(child)),
...(configure.modules || []).map(module => ({
name: module.name,
icon: module.icon,
text: module.showName,
})),
],
}; };
if (!menu.children) {
menu.children = [];
}
if (configure.children) {
for (const child of configure.children) {
menu.children.push(await createMenu(child));
}
}
if (configure.modules) {
for (const module of configure.modules) {
let info = module;
if (isString(module)) {
info = await getModuleInfo(module);
}
menu.children.push({
name: info.name,
icon: info.icon,
text: info.showName,
});
}
}
return menu;
}; };
const createMenus = (menusConfigure) => { const createMenus = async (menusConfigure) => {
return (menusConfigure || []).map(configure => createMenu(configure)); const menus = [];
if (menusConfigure) {
for (const configure of menusConfigure) {
menus.push(await createMenu(configure));
}
}
return menus;
}; };
// const createMenus = () => { // const createMenus = () => {
...@@ -122,7 +143,8 @@ export default { ...@@ -122,7 +143,8 @@ export default {
}, },
*fetchModules(action, { put, call }) { *fetchModules(action, { put, call }) {
const configures = yield call(getMenus); const configures = yield call(getMenus);
yield put({ type: 'queryMenusSuccess', payload: createMenus(configures) }); const menus = yield call(createMenus, configures);
yield put({ type: 'queryMenusSuccess', payload: menus });
}, },
*logout(action, { put, call }) { *logout(action, { put, call }) {
yield call(logout); yield call(logout);
......
/* eslint-disable no-param-reassign */
import fetch from 'dva/fetch'; import fetch from 'dva/fetch';
import _ from 'lodash'; import _ from 'lodash';
import { getCookie, encrypt } from './helper'; import { getCookie, encrypt } from './helper';
import { checkStatus, normParams, parseObject } from './http-helper';
import { cookie } from './config'; import { cookie } from './config';
import { errors } from './error'; import { errors } from './error';
import { checkStatus, normParams, parseObject } from './http-helper';
const defaultOptions = { const defaultOptions = {
headers: { headers: { Accept: 'application/json' },
Accept: 'application/json',
},
}; };
/**
export default function doDelete(url, data, params = {}, options = {}, auth = true) { * Requests a URL, returning a promise.
if (!data) { *
data = {}; * @param {string} url The URL we want to request
} * @param auth
* @param params
* @param {object} [options] The options we want to pass to "fetch"
* @return {object} An object containing either "data" or "err"
*/
export default function doDelete(url, params = {}, options = {}, auth = true) {
let token;
if (auth) { if (auth) {
const token = getCookie(cookie.token); token = getCookie(cookie.token);
if (!token) { if (!token) {
return Promise.reject(errors.tokenMissing()); return Promise.reject(errors.tokenMissing());
} }
data.token = encrypt(token); token = encrypt(token);
} }
let queryParams = normParams(params); let queryParams = token ? [...normParams(params), ['token', token]] : normParams(params);
queryParams = queryParams.map(([k, v]) => (_.isNil(v) ? k : `${k}=${encodeURIComponent(v)}`)); queryParams = queryParams.map(([k, v]) => (_.isNil(v) ? k : `${k}=${encodeURIComponent(v)}`));
queryParams = queryParams.join('&'); queryParams = queryParams.join('&');
let realUrl = url; let realUrl = url;
...@@ -32,15 +35,9 @@ export default function doDelete(url, data, params = {}, options = {}, auth = tr ...@@ -32,15 +35,9 @@ export default function doDelete(url, data, params = {}, options = {}, auth = tr
realUrl = `${url}?${queryParams}`; realUrl = `${url}?${queryParams}`;
} }
const realOptions = _.defaults(options, defaultOptions); const realOptions = _.defaults(options, defaultOptions);
if (!realOptions.headers) { realOptions.method = 'POST';
realOptions.headers = {
Accept: 'application/json',
};
}
realOptions.headers['Content-Type'] = 'application/json';
realOptions.method = 'DELETE';
realOptions.body = JSON.stringify(data);
return fetch(realUrl, realOptions) return fetch(realUrl, realOptions)
.then(checkStatus) .then(checkStatus)
.then(parseObject); .then(parseObject);
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论