提交 66acce26 authored 作者: vipcxj's avatar vipcxj

1.服务端接口重构,故客户端对应api大改

2.新增路由相关api,位于services/route
上级 1986f61e
<component name="ProjectDictionaryState"> <component name="ProjectDictionaryState">
<dictionary name="yaohx_169"> <dictionary name="yaohx_169">
<words> <words>
<w>activex</w>
<w>anticon</w> <w>anticon</w>
<w>arcfour</w> <w>arcfour</w>
<w>dropdown</w> <w>dropdown</w>
......
const proxy = { const proxy = {
"target": "http://192.168.1.116:1180", "target": "http://192.168.1.121:8180",
"changeOrigin": true, "changeOrigin": true,
"pathRewrite": { "pathRewrite": {
"^/api": "/houseServer/restful-services" "^/api": "/big-machine/restful-services"
} }
}; };
...@@ -24,7 +24,8 @@ module.exports = { ...@@ -24,7 +24,8 @@ module.exports = {
["import", { "libraryName": "antd", "style": true }] ["import", { "libraryName": "antd", "style": true }]
], ],
"proxy": { "proxy": {
"/api/user/login2": proxy, "/api/auth": proxy,
"/api/user": proxy,
"/api/domain": proxy, "/api/domain": proxy,
"/api/datasource": proxy, "/api/datasource": proxy,
"/api/modules": proxy, "/api/modules": proxy,
......
...@@ -100,23 +100,26 @@ const dealWithData = (req) => { ...@@ -100,23 +100,26 @@ const dealWithData = (req) => {
let currentDomainId = null; let currentDomainId = null;
const wrapResponse = (response) => {
return {
errorCode: 0,
data: response,
};
};
module.exports = { module.exports = {
'/api/user/login2': (req, res) => {
currentUser = users[0];
res.send(currentUser);
},
'/api/user/logout': (req, res) => { '/api/user/logout': (req, res) => {
res.status(204).end(); res.status(204).end();
}, },
'/api/domain/all': domains, '/api/domain/all': wrapResponse(domains),
'/api/domain/switch': (req, res) => { '/api/domain/switch': (req, res) => {
const domainId = req.query.domainId; const domainId = req.query.domainId;
const intDomainId = parseInt(domainId, 10); const intDomainId = parseInt(domainId, 10);
const domainIds = domains.map(domain => domain.id); const domainIds = domains.map(domain => domain.id);
if (domainIds.indexOf(intDomainId) !== -1) { if (domainIds.indexOf(intDomainId) !== -1) {
if (currentDomainId) { if (currentDomainId) {
res.send(getDomain(currentDomainId)); res.send(wrapResponse(getDomain(currentDomainId)));
} else { } else {
res.status(204).end(); res.status(204).end();
} }
...@@ -129,7 +132,7 @@ module.exports = { ...@@ -129,7 +132,7 @@ module.exports = {
} }
}, },
'/api/domain/current': (req, res) => { '/api/domain/current': (req, res) => {
res.send(getDomain(currentDomainId)); res.send(wrapResponse(getDomain(currentDomainId)));
}, },
'/api/module/all/info': (req, res) => { '/api/module/all/info': (req, res) => {
console.log('/api/module/all/info'); console.log('/api/module/all/info');
...@@ -151,17 +154,17 @@ module.exports = { ...@@ -151,17 +154,17 @@ module.exports = {
parents.add(p); parents.add(p);
}); });
}); });
res.send([ res.send(wrapResponse([
...publics, ...publics,
...parents, ...parents,
]); ]));
}, },
'/api/bpm/task/all/count': (req, res) => { '/api/bpm/task/all/count': (req, res) => {
res.send(`${dealWithData(req).length}`); res.send(wrapResponse(dealWithData(req).length));
}, },
'/api/bpm/task/all/info': (req, res) => { '/api/bpm/task/all/info': (req, res) => {
const pst = Number.parseInt(req.query.pst, 10); const pst = Number.parseInt(req.query.pst, 10);
const psz = Number.parseInt(req.query.psz, 10); const psz = Number.parseInt(req.query.psz, 10);
res.send(dealWithData(req).slice(pst, pst + psz)); res.send(wrapResponse(dealWithData(req).slice(pst, pst + psz)));
}, },
}; };
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
"dva": "^1.2.1", "dva": "^1.2.1",
"dva-loading": "^0.2.1", "dva-loading": "^0.2.1",
"fastjson_ref_resolver": "latest", "fastjson_ref_resolver": "latest",
"fingerprintjs": "^0.5.3",
"jsencrypt": "^2.3.1", "jsencrypt": "^2.3.1",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"lodash-deep": "^2.0.0", "lodash-deep": "^2.0.0",
......
import dva from 'dva'; import dva from 'dva';
import createLoading from 'dva-loading'; import createLoading from 'dva-loading';
import { browserHistory } from 'dva/router';
import moment from 'moment'; import moment from 'moment';
import { history } from './services/route';
import { initApp } from './data/app'; import { initApp } from './data/app';
import appModel from './models/app'; import appModel from './models/app';
import routerConfig from './router'; import routerConfig from './router';
import { showError } from './utils/error'; import { processError } from './utils/error';
import './index.css'; import './index.css';
moment.locale('zh-cn'); moment.locale('zh-cn');
// 1. Initialize // 1. Initialize
const app = dva({ const app = dva({
history: browserHistory, history,
onError(error) { onError(error) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log(error); console.log(error);
showError(error); processError(error);
}, },
}); });
......
import { routerRedux } from 'dva/router'; import { routerRedux } from 'dva/router';
import { fetchDomains, switchDomain, currentDomain } from '../services/domain'; import { fetchDomains, switchDomain, currentDomain } from '../services/domain';
import { setCookie, setLocalStorge, fullPath } from '../utils/helper'; import { setCookie, fullPath } from '../utils/helper';
import { cookie, storage } from '../utils/config'; import { cookie } from '../utils/config';
export default { export default {
...@@ -18,14 +18,14 @@ export default { ...@@ -18,14 +18,14 @@ export default {
effects: { effects: {
*fetch(action, { put, call }) { *fetch(action, { put, call }) {
const list = yield call(fetchDomains); const list = yield call(fetchDomains, '/', true);
yield put({ type: 'querySuccess', payload: list }); yield put({ type: 'querySuccess', payload: list });
}, },
*switch({ payload: domainId }, { put, call }) { *switch({ payload: domainPath }, { put, call }) {
yield call(switchDomain, domainId); yield call(switchDomain, domainPath);
const { id, name } = yield call(currentDomain); const { path, name } = yield call(currentDomain);
setCookie(cookie.domain, id); setCookie(cookie.domainPath, path);
setLocalStorge(storage.domainName, name); setCookie(cookie.domainName, name);
yield put(routerRedux.push(fullPath('/main'))); yield put(routerRedux.push(fullPath('/main')));
}, },
}, },
......
import { routerRedux } from 'dva/router'; import { routerRedux } from 'dva/router';
import { login } from '../services/login'; import { login, userInfo } from '../services/login';
import { setCookie, setLocalStorge, fullPath } from '../utils/helper'; import { setCookie, fullPath } from '../utils/helper';
import { cookie, storage } from '../utils/config'; import { cookie } from '../utils/config';
export default { export default {
namespace: 'login', namespace: 'login',
...@@ -10,10 +10,11 @@ export default { ...@@ -10,10 +10,11 @@ export default {
effects: { effects: {
*login({ payload }, { call, put }) { *login({ payload }, { call, put }) {
const result = yield call(login, payload); const result = yield call(login, payload);
const { tokenId, userId, userName } = result; const { tokenId } = result;
setCookie(cookie.token, tokenId); setCookie(cookie.token, tokenId);
setCookie(cookie.user, userId); const uInfo = yield call(userInfo);
setLocalStorge(storage.userName, userName); setCookie(cookie.userId, uInfo.id);
setCookie(cookie.userName, uInfo.name);
yield put(routerRedux.push(fullPath('/domain'))); yield put(routerRedux.push(fullPath('/domain')));
}, },
}, },
......
import { routerRedux } from 'dva/router'; import { routerRedux } from 'dva/router';
import { logout, fetchModuleInfos } from '../../services/main'; import { logout, fetchModuleInfos } from '../../services/main';
import { fetchDomains, switchDomain, currentDomain } from '../../services/domain'; import { fetchDomains, switchDomain, currentDomain } from '../../services/domain';
import { cookie, storage } from '../../utils/config'; import { cookie } from '../../utils/config';
import { setModules, getModules, getModule, getPath, foreachModule, isInited } from '../../data/modules'; import { setModules, getModules, getModule, getPath, foreachModule, isInited } from '../../data/modules';
import { setCookie, delCookie, getLocalStorge, setLocalStorge, delLocalStorge, fullPath } from '../../utils/helper'; import { setCookie, delCookie, fullPath, getCookie } from '../../utils/helper';
const createMenus = () => { const createMenus = () => {
const menus = []; const menus = [];
...@@ -72,29 +72,29 @@ export default { ...@@ -72,29 +72,29 @@ export default {
effects: { effects: {
*fetchDomain(action, { put, call }) { *fetchDomain(action, { put, call }) {
let domain = getLocalStorge(storage.domainName); let domain = getCookie(cookie.domainName);
if (!domain) { if (!domain) {
const { id, name } = yield call(currentDomain); const { path, name } = yield call(currentDomain);
setCookie(cookie.domain, id); setCookie(cookie.domainPath, path);
setLocalStorge(storage.domainName, name); setCookie(cookie.domainName, name);
domain = name; domain = name;
} }
yield put({ type: 'switchDomainSuccess', payload: domain }); yield put({ type: 'switchDomainSuccess', payload: domain });
}, },
*fetchDomains(action, { put, call }) { *fetchDomains(action, { put, call }) {
const list = yield call(fetchDomains); const list = yield call(fetchDomains, '/', true);
yield put({ type: 'queryDomainsSuccess', payload: list }); yield put({ type: 'queryDomainsSuccess', payload: list });
}, },
*switchDomain({ payload: domainId }, { put, call }) { *switchDomain({ payload: domainId }, { put, call }) {
yield call(switchDomain, domainId); yield call(switchDomain, domainId);
const { id, name } = yield call(currentDomain); const { path, name } = yield call(currentDomain);
setCookie(cookie.domain, id); setCookie(cookie.domainPath, path);
setLocalStorge(storage.domainName, name); setCookie(cookie.domainName, name);
yield put({ type: 'switchDomainSuccess', payload: name }); yield put({ type: 'switchDomainSuccess', payload: name });
yield put(routerRedux.push(fullPath('/main'))); yield put(routerRedux.push(fullPath('/main')));
}, },
*fetchUser(action, { put }) { *fetchUser(action, { put }) {
const user = getLocalStorge(storage.userName); const user = getCookie(cookie.userName);
if (!user) { if (!user) {
yield put(routerRedux.push(fullPath('/login'))); yield put(routerRedux.push(fullPath('/login')));
} }
...@@ -109,10 +109,10 @@ export default { ...@@ -109,10 +109,10 @@ export default {
}, },
*logout(action, { put, call }) { *logout(action, { put, call }) {
yield call(logout); yield call(logout);
delLocalStorge(storage.domainName); delCookie(cookie.domainName);
delLocalStorge(storage.userName); delCookie(cookie.userName);
delCookie(cookie.domain); delCookie(cookie.domainPath);
delCookie(cookie.user); delCookie(cookie.userId);
delCookie(cookie.token); delCookie(cookie.token);
yield put(routerRedux.push(fullPath('/login'))); yield put(routerRedux.push(fullPath('/login')));
}, },
......
...@@ -34,9 +34,13 @@ class Domain extends React.Component { ...@@ -34,9 +34,13 @@ class Domain extends React.Component {
render() { render() {
const { form, domain, loading } = this.props; const { form, domain, loading } = this.props;
const currentDomain = getCookie(cookie.domain); const currentDomain = getCookie(cookie.domainPath);
const selectOptions = {}; const selectOptions = {};
const decoratorOptions = {}; const decoratorOptions = {
rules: [
{ required: true },
],
};
if (!currentDomain) { if (!currentDomain) {
selectOptions.placeholder = '请选择项目...'; selectOptions.placeholder = '请选择项目...';
} else { } else {
...@@ -50,8 +54,8 @@ class Domain extends React.Component { ...@@ -50,8 +54,8 @@ class Domain extends React.Component {
{ {
form.getFieldDecorator('domain', decoratorOptions)( form.getFieldDecorator('domain', decoratorOptions)(
<Select {...selectOptions}> <Select {...selectOptions}>
{domain.list.map(({ id, name }) => { {domain.list.map(({ path, name }) => {
return <Option value={id} key={id}>{name}</Option>; return <Option value={path} key={path}>{name}</Option>;
})} })}
</Select>, </Select>,
) )
......
...@@ -24,7 +24,11 @@ class LoginForm extends React.Component { ...@@ -24,7 +24,11 @@ class LoginForm extends React.Component {
if (!err) { if (!err) {
this.props.dispatch({ this.props.dispatch({
type: 'login/login', type: 'login/login',
payload: values, payload: {
type: 'userName',
authType: 'password',
...values,
},
}); });
} }
}); });
......
...@@ -5,7 +5,7 @@ import { Button } from 'antd'; ...@@ -5,7 +5,7 @@ import { Button } from 'antd';
import { connect } from 'dva'; import { connect } from 'dva';
import TableEx from '../../../../components/table/index'; import TableEx from '../../../../components/table/index';
import config from '../../../../utils/config'; import config from '../../../../utils/config';
import { push } from '../../../../utils/navigate'; import { thisPush } from '../../../../services/route';
import styles from './list.less'; import styles from './list.less';
const columns = [{ const columns = [{
...@@ -137,7 +137,7 @@ class List extends React.Component { ...@@ -137,7 +137,7 @@ class List extends React.Component {
return ( return (
<div className={styles.wrapper}> <div className={styles.wrapper}>
<div className={styles.container}> <div className={styles.container}>
<Button onClick={() => { push(this, { pathname: '../detail', state: { a: 1, b: 2, c: 3 } }); }}>detail</Button> <Button onClick={() => { thisPush(this, { pathname: '../detail', state: { a: 1, b: 2, c: 3 } }); }}>detail</Button>
<TableEx {...tableProps} /> <TableEx {...tableProps} />
</div> </div>
</div> </div>
......
/* eslint-disable no-param-reassign */
import { endsWith } from 'lodash';
import request from '../utils/request'; import request from '../utils/request';
import post from '../utils/post';
import { cookie } from '../utils/config';
import { getCookie } from '../utils/helper';
export async function fetchDomains() { const processDomainInfo = (basePath, info) => {
return request('/api/domain/all'); if (!endsWith(basePath, '/')) {
basePath = `${basePath}/`;
}
if (info.id) {
info.path = `${basePath}${info.id}`;
} else {
info.path = basePath;
}
return info;
};
export async function fetchDomains(basePath, withRoot = false) {
if (!basePath) {
basePath = getCookie(cookie.domainPath);
}
const rootDomainInfo = withRoot ? await request('/api/domain/user/info', { dmPath: basePath }) : null;
const childrenDomainInfoList = await request('/api/domain/user/children-info', { dmPath: basePath });
let infoList;
if (rootDomainInfo && childrenDomainInfoList) {
infoList = [
rootDomainInfo,
...childrenDomainInfoList,
];
} else if (rootDomainInfo) {
infoList = [rootDomainInfo];
} else if (childrenDomainInfoList) {
infoList = childrenDomainInfoList;
} else {
infoList = [];
}
return infoList.map(info => processDomainInfo(basePath, info));
} }
export async function switchDomain(domainId) { export async function switchDomain(path) {
return request('/api/domain/switch', { domainId }); return post('/api/domain/user/path', { path });
} }
export async function currentDomain() { export async function currentDomain() {
return request('/api/domain/current'); const domainInfo = await request('/api/domain/user/info');
if (domainInfo) {
domainInfo.path = await request('/api/domain/user/path');
}
return domainInfo;
} }
import Fingerprint from 'fingerprintjs';
import post from '../utils/post';
import request from '../utils/request'; import request from '../utils/request';
import { encrypt } from '../utils/helper';
export async function login(payload) { export async function login(payload) {
const sign = encrypt(JSON.stringify({ const data = {};
un: payload.userName, data.type = payload.type;
ps: payload.password, if (data.type === 'userName') {
type: 1, data.data = payload.userName;
productId: 1, }
})); if (payload.authType === 'password') {
return request(`/api/user/login2?sign=${sign}&method=password`, {}, {}, false); data.authRequest = {
type: payload.authType,
parameters: {
cipher: payload.password,
},
};
}
data.tokenInfo = {
productId: 'big-machine-web-front',
deviceId: `${new Fingerprint({ ie_activex: true }).get()}`,
};
return post('/api/auth/login', data, {}, {}, false);
}
export async function userInfo() {
return request('/api/user/info');
} }
import request from '../utils/request'; import request from '../utils/request';
import post from '../utils/post';
export async function logout() { export async function logout() {
return request('/api/user/logout'); return post('/api/auth/logout');
} }
export async function fetchModuleInfos() { export async function fetchModuleInfos() {
......
import _ from 'lodash'; import { browserHistory } from 'dva/router';
import resolvePathname from 'resolve-pathname'; import resolvePathname from 'resolve-pathname';
import { isString } from 'lodash';
import config from '../utils/config';
export function makePath(base, path) { const { contextPath } = config;
const makePath = (base, path) => {
if (path.startsWith('/')) { if (path.startsWith('/')) {
return path; return `${contextPath}${path}`;
} }
const basePath = base.endsWith('/') ? base : `${base}/`; const basePath = base.endsWith('/') ? base : `${base}/`;
return resolvePathname(path, basePath); return resolvePathname(path, basePath);
} };
const processPath = (base, path) => {
if (isString(path)) {
return makePath(base, path);
}
return {
...path,
pathname: makePath(base, path.pathname),
};
};
const getHistoryBase = (history) => {
if (history.location) {
return history.location.pathname;
} else {
return location.pathname;
}
};
export const history = browserHistory;
export const location = {};
history.listen((loc, action) => {
console.log(action, loc);
location.pathname = loc.pathname;
location.state = loc.state;
location.search = loc.search;
location.hash = loc.hash;
});
export const push = (path, state) => {
return history.push(processPath(getHistoryBase(history), path), state);
};
export const replace = (path, state) => {
return history.replace(processPath(getHistoryBase(history), path), state);
};
export const go = (n) => {
return history.go(n);
};
export const goBack = () => {
return history.goBack();
};
export const goForward = () => {
return history.goForward();
};
const checkThis = (theThis) => { const checkThis = (theThis) => {
if (!theThis || !theThis.props) { if (!theThis || !theThis.props) {
...@@ -18,7 +72,7 @@ const checkThis = (theThis) => { ...@@ -18,7 +72,7 @@ const checkThis = (theThis) => {
} }
}; };
const getBase = (theThis) => { const getThisBase = (theThis) => {
if (theThis.props.location) { if (theThis.props.location) {
return theThis.props.location.pathname; return theThis.props.location.pathname;
} else if (typeof window !== 'undefined') { } else if (typeof window !== 'undefined') {
...@@ -28,43 +82,29 @@ const getBase = (theThis) => { ...@@ -28,43 +82,29 @@ const getBase = (theThis) => {
} }
}; };
const dealWithRoute = (theThis, route) => { export const thisPush = (theThis, pathOrLoc) => {
const base = getBase(theThis);
if (_.isString(route)) {
return makePath(base, route);
} else if (route && route.pathname) {
return {
...route,
pathname: makePath(base, route.pathname),
};
} else {
throw new Error('invalid route');
}
};
export function push(theThis, pathOrLoc) {
checkThis(theThis); checkThis(theThis);
const route = dealWithRoute(theThis, pathOrLoc); const route = processPath(getThisBase(theThis), pathOrLoc);
return theThis.props.router.push(route); return theThis.props.router.push(route);
} };
export function replace(theThis, pathOrLoc) { export const thisReplace = (theThis, pathOrLoc) => {
checkThis(theThis); checkThis(theThis);
const route = dealWithRoute(theThis, pathOrLoc); const route = processPath(getThisBase(theThis), pathOrLoc);
return theThis.props.router.replace(route); return theThis.props.router.replace(route);
} };
export function go(theThis, n) { export const thisGo = (theThis, n) => {
checkThis(theThis); checkThis(theThis);
return theThis.props.router.go(n); return theThis.props.router.go(n);
} };
export function goBack(theThis) { export const thisGoBack = (theThis) => {
checkThis(theThis); checkThis(theThis);
return theThis.props.router.goBack(); return theThis.props.router.goBack();
} };
export function goForward(theThis) { export const thisGoForward = (theThis) => {
checkThis(theThis); checkThis(theThis);
return theThis.props.router.goForward(); return theThis.props.router.goForward();
} };
...@@ -9,7 +9,7 @@ export function isAuthed() { ...@@ -9,7 +9,7 @@ export function isAuthed() {
} }
export function hasDomain() { export function hasDomain() {
return !!getCookie(cookie.domain); return !!getCookie(cookie.domainPath);
} }
export function redirectLogin() { export function redirectLogin() {
......
...@@ -4,15 +4,17 @@ ...@@ -4,15 +4,17 @@
export const cookie = { export const cookie = {
token: 'token', token: 'token',
user: 'user', userId: 'userId',
domain: 'domain',
};
export const storage = {
userName: 'userName', userName: 'userName',
domainPath: 'domainPath',
domainName: 'domainName', domainName: 'domainName',
}; };
export const errors = {
exception: 0x00010000,
invalid_token: 0x00010003,
};
export const api = { export const api = {
userLogin: '/user/login', userLogin: '/user/login',
userLogout: '/user/logout', userLogout: '/user/logout',
......
import React from 'react'; import React from 'react';
import { message, Modal } from 'antd'; import { message, Modal } from 'antd';
import { push } from '../services/route';
import { errors as errorCodes } from './config';
const errStyle = { const errStyle = {
overflow: 'auto', overflow: 'auto',
...@@ -10,7 +12,20 @@ const errStyle = { ...@@ -10,7 +12,20 @@ const errStyle = {
marginTop: '24px', marginTop: '24px',
}; };
export function showError(err) { export function processError(err) {
if (err && err.data) {
const data = err.data;
switch (data.errorCode) {
case errorCodes.invalid_token:
push('/login');
break;
default:
showError(err);
}
}
}
function showError(err) {
if (err) { if (err) {
let msg; let msg;
if (err.data) { if (err.data) {
......
import _ from 'lodash';
import { Resolver } from 'fastjson_ref_resolver';
export function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
}
return response.json().then((data) => {
const error = new Error(data.message ? data.message : response.statusText);
error.data = data;
throw error;
});
}
export function normParams(unnormed) {
if (_.isPlainObject(unnormed)) {
return _(unnormed).toPairs().flatMap(([k, v]) => (_.isArray(v) ? v.map(vv => [k, vv]) : [[k, v]])).value();
} else {
return unnormed;
}
}
// eslint-disable-next-line max-len
export function parseObject(response, { num2str = false, bool2str = false, nul2str = false, ud2str = false, nil2str = false } = {}) {
if (response.status === 204) { // no-content
return null;
} else {
const contentType = response.headers.get('content-type');
if (contentType) {
const needMap = num2str || bool2str || nul2str || ud2str || nil2str;
const mapStr = (value) => {
if (num2str && _.isNumber(value)) {
return value.toString();
}
if (bool2str && _.isBoolean(value)) {
return value.toString();
}
if (nul2str && _.isNull(value)) {
return '';
}
if (ud2str && _.isUndefined(value)) {
return '';
}
if (nil2str && _.isNil(value)) {
return '';
}
return value;
};
const mapObj = (obj, mapArrFunc) => {
if (_.isArray(obj)) {
return mapArrFunc(obj, mapObj);
}
if (_.isPlainObject(obj)) {
return _.mapValues(obj, (val) => {
const ret = mapStr(val);
return mapObj(ret, mapArrFunc);
});
}
return obj;
};
const mapArr = (arr, mapObjFunc) => {
if (_.isPlainObject(arr)) {
return mapObjFunc(arr, mapArr);
}
if (_.isArray(arr)) {
return _.map(arr, (val) => {
const ret = mapStr(val);
return mapArr(ret, mapObjFunc);
});
}
return arr;
};
const mapValue = _.curry(mapObj)(_, mapArr);
if (contentType.indexOf('json') !== -1) {
return response.json()
.then((json) => {
if (json.errorCode === 0) {
let data = json.data;
if (_.isObjectLike(data)) {
data = new Resolver(json.data).resolve();
}
return needMap ? mapValue(data) : data;
} else {
throw new Error(json.message);
}
});
} else if (contentType.indexOf('xml') !== -1) {
return response.text().then((text) => {
return require.ensure([], (require) => {
const { parseString } = require('xml2js');
const options = {};
const json = JSON.parse(parseString(text, options));
return needMap ? mapValue(json) : json;
});
});
} else if (contentType.indexOf('text') !== -1) {
return response.text();
} else {
throw new Error(`Unsupported response content type: ${contentType}`);
}
} else {
throw new Error('No response content type.');
}
}
}
/* eslint-disable no-param-reassign */
import fetch from 'dva/fetch';
import _ from 'lodash';
import { getCookie, encrypt } from './helper';
import { checkStatus, normParams, parseObject } from './http-helper';
import { cookie } from './config';
import { errors } from './error';
const defaultOptions = {
headers: {
Accept: 'application/json',
},
};
export default function post(url, data, params = {}, options = {}, auth = true) {
if (!data) {
data = {};
}
if (auth) {
const token = getCookie(cookie.token);
if (!token) {
return Promise.reject(errors.unLogin);
}
data.token = encrypt(token);
}
let queryParams = normParams(params);
queryParams = queryParams.map(([k, v]) => (_.isNil(v) ? k : `${k}=${encodeURIComponent(v)}`));
queryParams = queryParams.join('&');
let realUrl = url;
if (queryParams) {
realUrl = `${url}?${queryParams}`;
}
const realOptions = _.defaults(options, defaultOptions);
if (!realOptions.headers) {
realOptions.headers = {
Accept: 'application/json',
};
}
realOptions.headers['Content-Type'] = 'application/json';
realOptions.method = 'POST';
realOptions.body = JSON.stringify(data);
return fetch(realUrl, realOptions)
.then(checkStatus)
.then(parseObject);
}
import fetch from 'dva/fetch'; import fetch from 'dva/fetch';
import _ from 'lodash'; import _ from 'lodash';
import { Resolver } from 'fastjson_ref_resolver'; import { getCookie, encrypt } from './helper';
import { getCookie, encrypt } from '../utils/helper'; import { cookie } from './config';
import { cookie } from '../utils/config'; import { errors } from './error';
import { errors } from '../utils/error'; import { checkStatus, normParams, parseObject } from './http-helper';
// eslint-disable-next-line max-len
function parseObject(response, { num2str = false, bool2str = false, nul2str = false, ud2str = false, nil2str = false } = {}) {
if (response.status === 204) { // no-content
return null;
} else {
const contentType = response.headers.get('content-type');
if (contentType) {
const needMap = num2str || bool2str || nul2str || ud2str || nil2str;
const mapStr = (value) => {
if (num2str && _.isNumber(value)) {
return value.toString();
}
if (bool2str && _.isBoolean(value)) {
return value.toString();
}
if (nul2str && _.isNull(value)) {
return '';
}
if (ud2str && _.isUndefined(value)) {
return '';
}
if (nil2str && _.isNil(value)) {
return '';
}
return value;
};
const mapObj = (obj, mapArrFunc) => {
if (_.isArray(obj)) {
return mapArrFunc(obj, mapObj);
}
if (_.isPlainObject(obj)) {
return _.mapValues(obj, (val) => {
const ret = mapStr(val);
return mapObj(ret, mapArrFunc);
});
}
return obj;
};
const mapArr = (arr, mapObjFunc) => {
if (_.isPlainObject(arr)) {
return mapObjFunc(arr, mapArr);
}
if (_.isArray(arr)) {
return _.map(arr, (val) => {
const ret = mapStr(val);
return mapArr(ret, mapObjFunc);
});
}
return arr;
};
const mapValue = _.curry(mapObj)(_, mapArr);
if (contentType.indexOf('json') !== -1) {
const json = needMap ? response.json().then(mapValue) : response.json();
return new Resolver(json).resolve();
} else if (contentType.indexOf('xml') !== -1) {
return response.text().then((text) => {
return require.ensure([], (require) => {
const { parseString } = require('xml2js');
const options = {};
const json = JSON.parse(parseString(text, options));
return needMap ? mapValue(json) : json;
});
});
} else if (contentType.indexOf('text') !== -1) {
return response.text();
} else {
throw new Error(`Unsupported response content type: ${contentType}`);
}
} else {
throw new Error('No response content type.');
}
}
}
function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
return response;
}
return response.json().then((data) => {
const error = new Error(data.message ? data.message : response.statusText);
error.data = data;
throw error;
});
}
const defaultOptions = { const defaultOptions = {
headers: { Accept: 'application/json' }, headers: { Accept: 'application/json' },
...@@ -124,10 +39,3 @@ export default function request(url, params = {}, options = {}, auth = true) { ...@@ -124,10 +39,3 @@ export default function request(url, params = {}, options = {}, auth = true) {
.then(parseObject); .then(parseObject);
} }
export function normParams(unnormed) {
if (_.isPlainObject(unnormed)) {
return _(unnormed).toPairs().flatMap(([k, v]) => (_.isArray(v) ? v.map(vv => [k, vv]) : [[k, v]])).value();
} else {
return unnormed;
}
}
...@@ -2,8 +2,8 @@ import React from 'react'; ...@@ -2,8 +2,8 @@ import React from 'react';
import { partial } from 'lodash'; import { partial } from 'lodash';
import { storiesOf } from '@storybook/react'; import { storiesOf } from '@storybook/react';
import ReactJson from 'react-json-view' import ReactJson from 'react-json-view';
import { DatePicker, Form, Input, Modal } from 'antd'; import { DatePicker, Form, Input, Modal, Button } from 'antd';
import dva from 'dva'; import dva from 'dva';
import createLoading from 'dva-loading'; import createLoading from 'dva-loading';
...@@ -14,16 +14,15 @@ import { makeCancelable } from '../src/utils/promise'; ...@@ -14,16 +14,15 @@ import { makeCancelable } from '../src/utils/promise';
import { login } from '../src/services/login'; import { login } from '../src/services/login';
import { switchDomain } from '../src/services/domain'; import { switchDomain } from '../src/services/domain';
import { getModuleConfigure } from '../src/services/modules'; import { getModuleConfigure } from '../src/services/modules';
import { setCookie, setLocalStorge } from '../src/utils/helper'; import { setCookie } from '../src/utils/helper';
import { cookie, storage } from '../src/utils/config'; import { cookie } from '../src/utils/config';
import Button from "antd/es/button/button";
const loginIt = async (userName, password, domainId) => { const loginIt = async (userName, password, domainId) => {
const result = await login({ userName, password }); const result = await login({ userName, password });
const { tokenId, userId } = result; const { tokenId, userId } = result;
setCookie(cookie.token, tokenId); setCookie(cookie.token, tokenId);
setCookie(cookie.user, userId); setCookie(cookie.userId, userId);
setLocalStorge(storage.userName, userName); setCookie(cookie.userName, userName);
await switchDomain(domainId); await switchDomain(domainId);
const config = await getModuleConfigure('test'); const config = await getModuleConfigure('test');
console.log(config); console.log(config);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论