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

小更新

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