nyx_server
Version:
Node内容发布
236 lines (213 loc) • 4.99 kB
JavaScript
import reqwest from 'reqwest';
import { fromJS } from 'immutable';
import history from '../history';
import * as types from '../constants/ActionTypes';
export function setProjectsRequest () {
return {
type: types.SET_PROJECTS_REQUEST
};
}
export function setProjectsSuccess (data) {
return {
type: types.SET_PROJECTS_SUCCESS,
data: data
};
}
export function setProjectsFailure (error) {
return {
type: types.SET_PROJECTS_FAILURE,
error: error
};
}
export function getProjectsAsync () {
return dispatch => {
dispatch(setProjectsRequest());
reqwest({
url: REQUESTADDRESS + '/manageapi/serverProjects',
type: 'json',
method: 'get',
crossOrigin: true,
error: function (err) {
dispatch(setProjectsFailure('服务器错误,请稍后重试'));
},
success: function (resp) {
dispatch(setProjectsSuccess(resp));
}
});
};
}
export function setCurrentProject (name) {
return {
type: types.SET_CURRENT_PROJECT,
name: name
};
}
export function setCacheRequest (name) {
return {
type: types.SET_CACHE_REQUEST,
name: name
};
}
export function setCacheSuccess (name, data) {
return {
type: types.SET_CACHE_SUCCESS,
name: name,
data: data
};
}
export function setCacheFailure (name, error) {
return {
type: types.SET_CACHE_FAILURE,
name: name,
error: error
};
}
export function getProjectAsync (name) {
return dispatch => {
dispatch(setCacheRequest(name));
reqwest({
url: REQUESTADDRESS + '/manageapi/project',
method: 'get',
data: {name: name},
type: 'json',
error: function (err) {
dispatch(setCacheFailure(name, err));
},
success: function (resp) {
dispatch(setCacheSuccess(name, resp));
}
});
};
}
export function getProject (name) {
return (dispatch, getState) => {
dispatch(setCurrentProject(name));
var project = getState().project.getIn(["cache", name]);
if (!project) {
dispatch(setCacheRequest(name));
reqwest({
url: REQUESTADDRESS + '/manageapi/project',
method: 'get',
data: {name: name},
type: 'json',
error: function (err) {
dispatch(setCacheFailure(name, err));
},
success: function (resp) {
console.log(resp);
dispatch(setCacheSuccess(name, resp));
}
});
}
};
}
export function getTemplatesDataIds (data) {
return {
type: types.GET_TEMPLATES_DATAIDS,
data: data
};
}
export function getChipsDataIds (data) {
return {
type: types.GET_CHIPS_DATAIDS,
data: data
};
}
export function setTemplateDataId (basePath, id) {
return {
type: types.SET_TEMPLATE_DATAID,
id: id,
basePath: basePath
};
}
export function setChipDataId (basePath, id) {
return {
type: types.SET_CHIP_DATAID,
id: id,
basePath: basePath
};
}
export function setIps (ips) {
return {
type: types.SET_IPS,
ips: ips
}
}
export function getIpsAsync () {
return dispatch => {
reqwest({
url: REQUESTADDRESS + '/manageapi/ips',
type: 'json',
method: 'get',
success: function (resp) {
dispatch(setIps(resp));
}
});
};
}
function saveToLocal(userInfo, storageName) {
if (window.localStorage) {
window.localStorage.setItem(storageName, JSON.stringify(userInfo));
}
}
export function setUserinfo (userinfo) {
saveToLocal(userinfo, 'nyxUserInfo');
return {
type: types.SET_USERINFO,
userinfo: userinfo
}
}
export function logout () {
return dispatch => {
reqwest({
url: REQUESTADDRESS + '/auth/logout',
type: 'json',
method: 'get',
crossOrigin: true,
error: function (err) {
console.log(err);
},
success: function (resp) {
if (resp.success) {
dispatch(setUserinfo({}));
history.push('/manage/login');
} else {
alert(resp.message);
}
}
});
};
}
var getParams = function () {
return window.location.search.slice(1).split('&').reduce(function(initValue, item) {
var a = item.split('=');
if (a[0] === 'from') {
return decodeURIComponent(a[1]);
}
return initValue;
}, '');
};
export function login (username, password) {
return dispatch => {
reqwest({
url: REQUESTADDRESS + '/auth/login?username='+username,
type: 'json',
method: 'post',
crossOrigin: true,
data: {username: username, password: password},
error: function (err) {
console.log(err);
},
success: function (resp) {
if (resp.success) {
dispatch(setUserinfo(resp.userInfo, 'userInfo'));
// history.push('/manage/columns');
window.location.href = getParams();
//window.open("/manage/columns", '', '');
} else {
alert(resp.message);
}
}
});
};
}