提交 f4aa4d6c authored 作者: vipcxj's avatar vipcxj

小更新

上级 b787e0d1
......@@ -4,6 +4,7 @@
<w>activex</w>
<w>anticon</w>
<w>arcfour</w>
<w>authed</w>
<w>dropdown</w>
<w>infos</w>
<w>inited</w>
......
......@@ -15,7 +15,6 @@ const app = dva({
history,
onError(error) {
// eslint-disable-next-line no-console
console.log(error);
processError(error);
},
});
......
......@@ -2,6 +2,7 @@ import { routerRedux } from 'dva/router';
import { login, userInfo } from '../services/login';
import { setCookie, fullPath } from '../utils/helper';
import { cookie } from '../utils/config';
import { errors } from '../utils/error';
export default {
namespace: 'login',
......@@ -10,7 +11,14 @@ export default {
effects: {
*login({ payload }, { call, put }) {
const result = yield call(login, payload);
const { tokenId } = result;
const { tokenId, authResponse, remainedAuthRequirements } = result;
if (authResponse.status !== 'authed' && authResponse.status !== 'skipped') {
throw errors.wrongPassword();
}
const { requirements } = remainedAuthRequirements;
if (requirements.length > 0) {
throw errors.unsupportedAuthType(requirements);
}
setCookie(cookie.token, tokenId);
const uInfo = yield call(userInfo);
setCookie(cookie.userId, uInfo.id);
......
/* eslint-disable no-param-reassign */
import { endsWith } from 'lodash';
import request from '../utils/request';
import post from '../utils/post';
import { cookie } from '../utils/config';
import { getCookie } from '../utils/helper';
const processDomainInfo = (basePath, info) => {
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);
......@@ -36,7 +23,7 @@ export async function fetchDomains(basePath, withRoot = false) {
} else {
infoList = [];
}
return infoList.map(info => processDomainInfo(basePath, info));
return infoList;
}
export async function switchDomain(path) {
......@@ -44,9 +31,5 @@ export async function switchDomain(path) {
}
export async function currentDomain() {
const domainInfo = await request('/api/domain/user/info');
if (domainInfo) {
domainInfo.path = await request('/api/domain/user/path');
}
return domainInfo;
return request('/api/domain/user/info');
}
......@@ -34,8 +34,7 @@ const getHistoryBase = (history) => {
export const history = browserHistory;
export const location = {};
history.listen((loc, action) => {
console.log(action, loc);
history.listen((loc) => {
location.pathname = loc.pathname;
location.state = loc.state;
location.search = loc.search;
......
......@@ -12,7 +12,12 @@ export const cookie = {
export const errors = {
exception: 0x00010000,
no_such_user: 0x00010001,
invalid_token: 0x00010003,
// client error:
token_missing: 0x00000001,
wrong_password: 0x00000002,
unsupported_auth_type: 0x00000003,
};
export const api = {
......
/* 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 doDelete(url, data, params = {}, options = {}, auth = true) {
if (!data) {
data = {};
}
if (auth) {
const token = getCookie(cookie.token);
if (!token) {
return Promise.reject(errors.tokenMissing());
}
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 = 'DELETE';
realOptions.body = JSON.stringify(data);
return fetch(realUrl, realOptions)
.then(checkStatus)
.then(parseObject);
}
......@@ -16,6 +16,9 @@ export function processError(err) {
if (err && err.data) {
const data = err.data;
switch (data.errorCode) {
case errorCodes.no_such_user:
message.error('用户不存在!');
break;
case errorCodes.invalid_token:
push('/login');
break;
......@@ -56,8 +59,16 @@ export function createError({ code, msg }) {
}
export const errors = {
unLogin: createError({
code: 1,
msg: '未登录',
tokenMissing: () => createError({
code: errorCodes.token_missing,
msg: '需要登录!',
}),
wrongPassword: () => createError({
code: errorCodes.wrong_password,
msg: '密码错误!',
}),
unsupportedAuthType: (...types) => createError({
code: errorCodes.unsupported_auth_type,
msg: `不支持的客户端验证方式:${types.join('、')}.`,
}),
};
......@@ -20,7 +20,7 @@ export default function post(url, data, params = {}, options = {}, auth = true)
if (auth) {
const token = getCookie(cookie.token);
if (!token) {
return Promise.reject(errors.unLogin);
return Promise.reject(errors.tokenMissing());
}
data.token = encrypt(token);
}
......
......@@ -23,7 +23,7 @@ export default function request(url, params = {}, options = {}, auth = true) {
if (auth) {
token = getCookie(cookie.token);
if (!token) {
return Promise.reject(errors.unLogin);
return Promise.reject(errors.tokenMissing());
}
token = encrypt(token);
}
......
......@@ -17,13 +17,13 @@ import { getModuleConfigure } from '../src/services/modules';
import { setCookie } from '../src/utils/helper';
import { cookie } from '../src/utils/config';
const loginIt = async (userName, password, domainId) => {
const loginIt = async (userName, password, domainPath) => {
const result = await login({ userName, password });
const { tokenId, userId } = result;
setCookie(cookie.token, tokenId);
setCookie(cookie.userId, userId);
setCookie(cookie.userName, userName);
await switchDomain(domainId);
await switchDomain(domainPath);
const config = await getModuleConfigure('test');
console.log(config);
};
......@@ -77,7 +77,7 @@ const lazy = action => (Comp) => {
storiesOf('a', module)
.add('1', () => {
const Comp = lazy(partial(loginIt, 'cxj', '111111', 5))(wrap()(DsTable));
const Comp = lazy(partial(loginIt, 'admin', 'admin', '/'))(wrap()(DsTable));
const coordinate = {
containerType: 'module',
containerName: 'test',
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论