login.js
1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { fakeAccountLogin } from '../services/api';
import { setAuthority } from '../utils/authority';
export default {
namespace: 'login',
state: {
status: undefined,
},
effects: {
*login({ payload }, { call, put }) {
const response = yield call(fakeAccountLogin, payload);
yield put({
type: 'changeLoginStatus',
payload: response,
});
// Login successfully
if (response.ext.status === 'ok') {
// 非常粗暴的跳转,登陆成功之后权限会变成user或admin,会自动重定向到主页
// Login success after permission changes to admin or user
// The refresh will automatically redirect to the home page
// yield put(routerRedux.push('/'));
window.location.reload();
}
},
*logout(_, { put, select }) {
try {
// get location pathname
console.log("_logout");
const urlParams = new URL(window.location.href);
const pathname = yield select(state => state.routing.location.pathname);
// add the parameters in the url
urlParams.searchParams.set('redirect', pathname);
window.history.pushState(null, 'login', urlParams.href);
} finally {
// yield put(routerRedux.push('/user/login'));
// Login out after permission changes to admin or user
// The refresh will automatically redirect to the login page
yield put({
type: 'changeLoginStatus',
payload: {
ext:{
status: false,
currentAuthority: 'guest',
},
token:{
value:"unknow"
},
},
});
window.location.reload();
}
},
},
reducers: {
changeLoginStatus(state, { payload }) {
setAuthority(payload.ext.currentAuthority,payload.token.value);
return {
...state,
status: payload.ext.status,
type: payload.ext.type,
};
},
},
};