|
@@ -8,8 +8,11 @@ import {
|
|
|
ROUTE_TYPE_POP_OR_FULL, SIDE_TYPE_NOMAL
|
|
ROUTE_TYPE_POP_OR_FULL, SIDE_TYPE_NOMAL
|
|
|
} from "@constants/routes";
|
|
} from "@constants/routes";
|
|
|
import {REQ_STATUS} from "@api/config";
|
|
import {REQ_STATUS} from "@api/config";
|
|
|
-import {buildReqReducer, reqGetRoutes} from "@features/api";
|
|
|
|
|
-import {dark} from "@mui/material/styles/createPalette";
|
|
|
|
|
|
|
+import {buildReqReducer, reqGetRoutes, reqLogin, reqLogout, reqRegister} from "@features/api";
|
|
|
|
|
+import {toast} from 'react-toastify';
|
|
|
|
|
+import {localStorageGet, localStorageSet} from "@utils/helpers";
|
|
|
|
|
+import {LOCAL_IS_LOGIN, LOCAL_USER_INFO} from "@constants/localkey";
|
|
|
|
|
+const i18n=global.i18n;
|
|
|
const generateNavs=(routes)=>{
|
|
const generateNavs=(routes)=>{
|
|
|
const navs=[];
|
|
const navs=[];
|
|
|
const allNavs=[];
|
|
const allNavs=[];
|
|
@@ -42,6 +45,9 @@ const generateRoutes=(routes,asyncRoutes=[])=>{
|
|
|
route.plv=plevel;
|
|
route.plv=plevel;
|
|
|
route.ppath=ppath;
|
|
route.ppath=ppath;
|
|
|
route.lv=plevel+key+"_";
|
|
route.lv=plevel+key+"_";
|
|
|
|
|
+ // if (route.path==="#/signin"||route.path==="#/signup"){
|
|
|
|
|
+ // route.component="Login";
|
|
|
|
|
+ // }
|
|
|
allMap[route.path]=route;
|
|
allMap[route.path]=route;
|
|
|
let type=route.type;
|
|
let type=route.type;
|
|
|
if (type===ROUTE_TYPE_IN||type===ROUTE_TYPE_FULL)inRoutes.push(route);
|
|
if (type===ROUTE_TYPE_IN||type===ROUTE_TYPE_FULL)inRoutes.push(route);
|
|
@@ -118,6 +124,85 @@ const reducerRoutes=(state,action)=>{
|
|
|
const routesStatus=(state,status,arg)=>{
|
|
const routesStatus=(state,status,arg)=>{
|
|
|
state.status=status;
|
|
state.status=status;
|
|
|
}
|
|
}
|
|
|
|
|
+//login
|
|
|
|
|
+const getLocalUserInfo=()=>{
|
|
|
|
|
+ let user={};
|
|
|
|
|
+ let value=localStorageGet(LOCAL_USER_INFO,null);
|
|
|
|
|
+ if (value&&value!=""){
|
|
|
|
|
+ user=JSON.parse(value);
|
|
|
|
|
+ }
|
|
|
|
|
+ return user;
|
|
|
|
|
+}
|
|
|
|
|
+const getLocalIsLogin=()=>{
|
|
|
|
|
+ let islogin=localStorageGet(LOCAL_IS_LOGIN,0);
|
|
|
|
|
+ islogin=Number(islogin);
|
|
|
|
|
+ return islogin;
|
|
|
|
|
+}
|
|
|
|
|
+const getMsg=(payload)=>{
|
|
|
|
|
+ let result=payload.result;
|
|
|
|
|
+ if (result==null)result=payload.msg;
|
|
|
|
|
+ let msg="";
|
|
|
|
|
+ if (result!=null){
|
|
|
|
|
+ let key=result[0];
|
|
|
|
|
+ let def=result[1];
|
|
|
|
|
+ if (i18n.exists(key))msg=i18n.t(key);
|
|
|
|
|
+ else msg=def;
|
|
|
|
|
+ }
|
|
|
|
|
+ return msg;
|
|
|
|
|
+}
|
|
|
|
|
+const parseLogin=(state,action,arg)=>{
|
|
|
|
|
+ let payload=action.payload;
|
|
|
|
|
+ let code=payload.code;
|
|
|
|
|
+ let msg=getMsg(payload);
|
|
|
|
|
+ //success
|
|
|
|
|
+ if (code==200){
|
|
|
|
|
+ let data=payload.data;
|
|
|
|
|
+ state.userInfo=data;
|
|
|
|
|
+ state.isLogin=1;
|
|
|
|
|
+ localStorageSet(LOCAL_USER_INFO,JSON.stringify(data));
|
|
|
|
|
+ localStorageSet(LOCAL_IS_LOGIN,1);
|
|
|
|
|
+ toast.success(msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ toast.error(msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ let callback=arg.callback;
|
|
|
|
|
+ if (callback!=null)callback(code);
|
|
|
|
|
+}
|
|
|
|
|
+const reducerLogin=(state,action,arg)=>{
|
|
|
|
|
+ // toast.success("success");
|
|
|
|
|
+ parseLogin(state,action,arg);
|
|
|
|
|
+}
|
|
|
|
|
+const loginStatus=(state,status,arg)=>{
|
|
|
|
|
+ state.loginStatus.login=status;
|
|
|
|
|
+}
|
|
|
|
|
+const reducerReg=(state,action,arg)=>{
|
|
|
|
|
+ parseLogin(state,action,arg);
|
|
|
|
|
+}
|
|
|
|
|
+const regStatus=(state,status,arg)=>{
|
|
|
|
|
+ state.loginStatus.register=status;
|
|
|
|
|
+}
|
|
|
|
|
+const reducerLogout=(state,action,arg)=>{
|
|
|
|
|
+ let payload=action.payload;
|
|
|
|
|
+ let code=payload.code;
|
|
|
|
|
+ let msg=getMsg(payload);
|
|
|
|
|
+ if (code==200){
|
|
|
|
|
+ state.userInfo={};
|
|
|
|
|
+ state.isLogin=0;
|
|
|
|
|
+ localStorageSet(LOCAL_USER_INFO,"");
|
|
|
|
|
+ localStorageSet(LOCAL_IS_LOGIN,0);
|
|
|
|
|
+ toast.success(msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ toast.error(msg);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+const logoutStatus=(state,status,arg)=>{
|
|
|
|
|
+ state.loginStatus.logout=status;
|
|
|
|
|
+}
|
|
|
|
|
+//login
|
|
|
export const Main=createSlice({
|
|
export const Main=createSlice({
|
|
|
name: 'main',
|
|
name: 'main',
|
|
|
initialState: {
|
|
initialState: {
|
|
@@ -126,17 +211,33 @@ export const Main=createSlice({
|
|
|
asyncRoutes:[],
|
|
asyncRoutes:[],
|
|
|
styles:{},
|
|
styles:{},
|
|
|
blocks:{},
|
|
blocks:{},
|
|
|
- ...generateRoutes(routes)
|
|
|
|
|
|
|
+ ...generateRoutes(routes),
|
|
|
|
|
+ loginStatus:{
|
|
|
|
|
+ login:REQ_STATUS.NONE,
|
|
|
|
|
+ register:REQ_STATUS.NONE,
|
|
|
|
|
+ logout:REQ_STATUS.NONE,
|
|
|
|
|
+ },
|
|
|
|
|
+ userInfo:getLocalUserInfo(),
|
|
|
|
|
+ isLogin:getLocalIsLogin()
|
|
|
},
|
|
},
|
|
|
reducers: {
|
|
reducers: {
|
|
|
|
|
+ logout:(state, action)=>{
|
|
|
|
|
+ state.userInfo={};
|
|
|
|
|
+ state.isLogin=0;
|
|
|
|
|
+ localStorageSet(LOCAL_USER_INFO,"");
|
|
|
|
|
+ localStorageSet(LOCAL_IS_LOGIN,0);
|
|
|
|
|
+ }
|
|
|
},
|
|
},
|
|
|
extraReducers: (builder) => {
|
|
extraReducers: (builder) => {
|
|
|
// Add reducers for additional action types here, and handle loading state as needed
|
|
// Add reducers for additional action types here, and handle loading state as needed
|
|
|
// reducerRoutes(builder);
|
|
// reducerRoutes(builder);
|
|
|
buildReqReducer(builder,reqGetRoutes,reducerRoutes,routesStatus);
|
|
buildReqReducer(builder,reqGetRoutes,reducerRoutes,routesStatus);
|
|
|
|
|
+ buildReqReducer(builder,reqLogin,reducerLogin,loginStatus);
|
|
|
|
|
+ buildReqReducer(builder,reqRegister,reducerReg,regStatus);
|
|
|
|
|
+ buildReqReducer(builder,reqLogout,reducerLogout,logoutStatus);
|
|
|
},
|
|
},
|
|
|
})
|
|
})
|
|
|
-export const {} = Main.actions;
|
|
|
|
|
|
|
+export const {logout} = Main.actions;
|
|
|
//数据获取
|
|
//数据获取
|
|
|
export const getFetchStatus=(state)=>{
|
|
export const getFetchStatus=(state)=>{
|
|
|
return state.main.status;
|
|
return state.main.status;
|
|
@@ -207,6 +308,18 @@ export const isOutRoute=(path)=>(state)=>{
|
|
|
if (route)return route.type===ROUTE_TYPE_OUTLINK;
|
|
if (route)return route.type===ROUTE_TYPE_OUTLINK;
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
+//login
|
|
|
|
|
+export const getLoginStatus=(state)=>{
|
|
|
|
|
+ return state.main.loginStatus.login;
|
|
|
|
|
+}
|
|
|
|
|
+export const getRegStatus=(state)=>{
|
|
|
|
|
+ return state.main.loginStatus.register;
|
|
|
|
|
+}
|
|
|
|
|
+export const isLogined=(state)=>{
|
|
|
|
|
+ // console.log(state.main.isLogin)
|
|
|
|
|
+ return state.main.isLogin;
|
|
|
|
|
+ // return 1;
|
|
|
|
|
+}
|
|
|
//数据获取
|
|
//数据获取
|
|
|
export default Main.reducer;
|
|
export default Main.reducer;
|
|
|
|
|
|