authed.js 9.9 KB
import * as types from '../constants/ActionTypes';
import {loadMyUserInfo,getSmsCode,registerHro,userLogin,getResetSmsCode,modifyPassword,modify_Password,getQuickLoginSmsCode ,getPermission} from '../../utils/authedUtil';
import {getBasicInfoAction,loadSysperms} from '../actions/settingAction'
import {getTenants,changeTenant} from  '../../utils/hrUtil';
import {getSysperms} from '../../utils/settingUtil'
import {SubmissionError,change} from 'redux-form';
import {loadTodo,loadDone} from './home';
import {loadOrganizations} from './hr';
import {message} from 'antd';
import moment from 'moment';
import { loadDepartmentNameAction } from './custome'

export function sendLoginSmsCode(parmas,current){//获取验证码
    return dispatch => {
        return getSmsCode(parmas)
            .then(data => {
                if(data.code&&data.code>=300){
                    if(current==0)
                        dispatch(change('register_form','current','0'));
                    else if(current)
                        dispatch(change('register_form','current',1));
                    message.error(data.message);
                }else{
                    dispatch(change('register_form','current',1));
                }
                if(data.error){
                    throw new SubmissionError({_error:data.error})
                }
            })
    };
}


export function sendResetPwdSmsCode(parmas){//获取重置验证码
    return dispatch => {
        return getResetSmsCode(parmas)
            .then(data => {
                 if(data&&data.code>=300&&data.message){
                   message.error(data.message,2)
                 }else if (data&&data.code == 200){
                   message.success("验证码发送成功,请注意查收",2)
                 }
            })
            .catch(err => { throw err; });
    };
}

export function sendQuickLoginSmsCode(parmas){//获取重置验证码
    return dispatch => {
        return getQuickLoginSmsCode(parmas)
            .then(data => {
                 if(data&&data.code>=300&&data.message){
                   message.error(data.message,2)
                 }else if(data&&data.code==200){
                   message.success("验证码发送成功,请注意查收",2)
                 }
            })
            .catch(err => { throw err; });
    };
}


export function doRegister(values){
    return dispatch => {
        return registerHro(values)
            .then(data => {
                if(data&&data.code>=300){
                    message.error(data.message,2)
                    throw new SubmissionError({_error:data.message});
                }else {
                    dispatch({
                        type: types.REGISTER_SECCESS,
                        registerSeccess:true
                    });
                }
            });
    }
}

export function doLogin(parmas){
    return dispatch => {
        return userLogin(parmas)
            .then(data => {
               if(data.message){
                    throw new SubmissionError({_error:data.message})
                }
                if(data&&data.user_info){
                    // getTenants().then((tenants)=>{
                    //     const {items=[]}=tenants;
                    //     dispatch({
                    //         type: types.LOAD_TENANTS,
                    //         tenants: items
                    //     });
                    //     if(items.length>1){
                    //       document.location.href="#/choose-tenant";
                    //     }else if(items.length==1){
                    //         changeTenant({'id':items[0].id}).then(back=>{
                    //              document.location.href='#container/home';
                    //         });
                    //     }else{
                    //         message.error('您没有权限登录',2);
                    //         window.location.href="#";
                    //     }
                    // });
                    if(window._czc&&window._czc.push)
                      window._czc.push(["_trackPageview", "登录"]);
                    dispatch(getTenantsAction())
                }
            });
    };
}

export function getTenantsAction(){
  return dispatch => {
    return getTenants().then((tenants)=>{
        const {items=[]}=tenants;
        dispatch({
            type   : types.LOAD_TENANTS,
            tenants: items
        });
        if(items.length>1){
          document.location.href="#/login/choose-tenant";
        }else if(items.length==1){
            changeTenant({'id':items[0].id}).then(back=>{
                 document.location.href='#container/home';
            });
        }else{
            message.error('您没有权限登录',2);
            window.location.href="#";
        }
    });
  }
}

//登录前重置密码
export function changePassword(values){
    return dispatch => {
        return modifyPassword(values).then(data => {
                if(data&&data.id){
                  message.success('修改成功',2,()=>{
                    window.location = '#'
                  });
                }else{
                  message.error(data.message,2);
                }
			        })
            .catch(err => { throw err; });
    };
}

//登录后重置密码
export function change_Password(values){
    return dispatch => {
        return modifyPassword(values).then(data => {
                if(data&&data.id){
                  message.success('修改成功',2);
                }else{
                  message.error(data.message,2);
                }
			        })
            .catch(err => { throw err; });
    };
}

function addMenus(user_info,name_ref){
  const {perms=[]}=user_info,menu1={},menu2={},menus=[];
  if(perms){
    perms.map((perm,i)=>{
      if(!perm.parent_module){
        menu1[perm.module]={
          ...perm,
          display_name:perm.module_name
        }
      }else{
        if(menu2[perm.parent_module]){
          menu2[perm.parent_module].push({
             ...perm,
            display_name:perm.module_name
          })
        }else{
          menu2[perm.parent_module]=[{
             ...perm,
            display_name:perm.module_name
          }]
        }
      }
    });
    const keys=Object.keys(menu1);
    keys.map((key,i)=>{ 
      if(menu2[key]){ 
        menus.push({
          ...menu1[key],
          children:menu2[key]
        })
      }else{
        menus.push({
          ...menu1[key]
        })
      }
    });
    user_info.menus=menus;
  } 
  return user_info;
}
export function loadMenuForUserInfo(info){
   return dispatch => {
        return getSysperms()
            .then(data => {
              const {items=[],name_ref={}}=data;
              let {user_info}=info;
              dispatch(changeUserInfo(addMenus(user_info,name_ref)));
      }).catch(err => { throw err; });
    };
}

export function loadUserInfo(){
    return dispatch => {
        return loadMyUserInfo()
            .then(data => {
                let {user_info}=data;
                dispatch(loadMenuForUserInfo(data));
                // dispatch(changeUserInfo(user_info));
                return user_info;
			}).catch(err => { throw err; });
    };
}


export function changeUserInfo(user) {
    return dispatch => {
        dispatch({
            type: types.CHANGE_USER_INFO,
            userInfo:user
        });
    };
}

export function changeAvatar(user) {
    return dispatch => {
        let time = new Date().getTime();
        dispatch({
            type: types.CHANGE_AVATAR,
            time:time
        });
    };
}


//切换机构

//切换租户
export function modifyTenant(id,name) {
    return dispatch => {

        return changeTenant({'id': id}).then(data => {
            if (data && data.code == 200) {
                message.success('切换成功', 2,()=>{
                  dispatch(getBasicInfoAction(id));
                  dispatch({type:types.TENANT_NAME,tenantName:name});
                  dispatch(changeAvatar());
                  dispatch(loadUserInfo());
                  dispatch(loadTodo(moment(new Date())));
                  dispatch(loadDone(0,15))
                  dispatch(loadOrganizations());
                  dispatch(loadSysperms());
                  dispatch(loadDepartmentNameAction())
                  document.location.href=`#container/home`;
                });

            } else if (data && data.code >= 300) {
              message.error(data.message,2)
            }

        }).catch(err => {
            throw err;
        });
    };
}

//选择企业
export function chooseTenant(id) {
    return dispatch => {
        return changeTenant({'id': id}).then(data => {
            if (data && data.code == 200) {
              // getSysperms().then(back=>{
              //   const {items=[]} = back;
              //   if(items&&items.length>0){
              //       dispatch(getBasicInfoAction(id));
              //       document.location.href='#container/home';
              //   }else{
              //     message.error('您没有权限登录',2);
              //     window.location.href="#";
              //   }
              // })
            dispatch(getBasicInfoAction(id));
            document.location.href='#container/home';
            } else if (data && data.code >= 300) {
              message.error(data.message,2)
            }

        }).catch(err => {
            throw err;
        });
    };
}

//选择企业
export function changeRegisterStatus(status) {
    return dispatch => {
      dispatch({
          type: types.REGISTER_SECCESS,
          registerSeccess:status
      });
    };
}
//申报操作权限查询
export function getPermissionAction() {
    return dispatch => {
      // return getPermission()
      //   .then(data => {
      //     dispatch({
      //       type             : types.GETPERMISSION,
      //       getPermission: data,
      //       onlySee:data.has_perm == 'y' ? true : false
      //     });
      //     return data;
      //   })
      //   .catch(err => { throw err; });
    };
  }