PermissionLabel.js 6.8 KB
import React,{PropTypes} from 'react';   
import cx from 'classnames';  
import { connect } from 'react-redux'; 

const permKeys={
  "/container/home":"home",
  "/container/customer":"crm",
  "/main/customer/add":"crm",
  "/main/customer/import":"crm",
  "/container/customer_unormal_import":"crm",
  "/container/contract_index":"crm_sercontractlist",
  "/container/add-contract/":"crm_sercontractlist",
  "/container/contract-details/":"crm_sercontractlist",
  "/container/update-contract/":"crm_sercontractlist",
  "/main/provider/my":"provider_providerlist",
  "/main/provider/detail/":"provider_providerlist",
  "/main/provider/import":"provider_providerlist",
  "/main/provider/add":"provider_providerlist",
  "/container/customService/1":"provider_sercontractlist",
  "main/customServiceDetail/provider":"provider_sercontractlist",
  "/main/provider-settlement":"provider_settlelist",
  "/container/labor-contract":"contract",
  "/main/labor-contract/wait-for-sign":"contract",
  "/main/labor-contract/manage":"contract",
  "/main/labor-contract/import":"contract",
  "/main/labor-contract/laborContractImportRecords":"contract",
  "/main/labor-contract/add":"contract",
  "/main/labor-contract/tmp":"contract",
  "/main/labor-contract/add-tmp":"contract",
  "/main/labor-contract/add-tmp/":"contract",
  "/main/labor-contract/lgl-ent":"contract",
  "/main/labor-contract/sign":"contract",
  "/main/labor-contract/setting":"contract",
  "/container/newRecruit":"recruit",
  "/container/position/manage":"recruit",
  "/container/position/add":"recruit",
  "/main/position/hiring":"recruit",
  "/main/position/detail":"recruit",
  "/main/schedule":"recruit",
  "/main/rewards":"recruit",
  "/main/reward/detail":"recruit",
  "/main/results":"recruit",
  "/container/candidate_mnge":"recruit",
  "/container/myResumesContainer":"recruit",
  "/main/new/recruit/set":"recruit",
  "/container/new_server_in_index":"entsi_ticket",
  "/container/new_social_list/increase":"entsi_ticket",
  "/container/importOrder":"entsi_ticket",
  "/container/company_server_delete_records":"entsi_ticket",
  "/container/cpy_server_in":"entsi_ticket",
  "/container/cpy_server_out_index":"entsi_process",
  "/container/socialinsurances/list":"entsi_policy",
  "/main/socialinsurances/add":"entsi_policy",
  "/main/socialinsurances/edit":"entsi_policy",
  "/container/set_social_list":"entsi_policy",
  "/container/personal-index":"personsi",
  "/container/salary_manage":"payroll_mgm",
  "/container/salary_bill_list":"payroll_bill",
  "/container/onLineTrain":"trainonline",
  "/container/TrainingResult":"trainonline_results",
  "/container/attendanceRecord":"attendance",
  "/container/attendanceStatistics":"attendance_record",
  "/container/attendanceSetting":"attendance_program",
  "/container/post_management":"socialwork_position",
  "/container/labor_pool":"socialwork_laborpool",
  "/container/accountSet":"settle_acsetmgm",
  "/container/accound-index":"settle_settlemgm",
  "/main/settlement-setting":"settle_setting",
  "/container/financial_index":"finance_finance",
  "/container/finace-manage":"finance_acsetcheck",
  "/container/invoice-manages":"finance_invoicemgm",
  "/container/finance-check/wait_create_bill":"finance_paymentmgm",
  "/container/overviewData":"dashboard_overview",
  "/container/recruitReport":"dashboard_candidatestats",
  "/container/jobSeekerReport":"dashboard_candidatesearch",
  "/container/share_date_static":"dashboard_wechatpastats",
  "/container/dailyCon":"dailywage_recruit",
  "/container/dailyWdControl":"dailywage_riskmgm",
  "/container/dailyPay":"dailywage_salarydeliver",
  "/container/dailyPayDet":"dailywage_stats",
  "/container/daily_share":"dailywage_sharestats",
  "/container/attendance":"dailywage_attendance",
  "/container/custom_serve_set":"dailywage_sosetting",
  "/container/outsourcing":"outsourcing",
  "/container/setting-entmgm":"setting_entmgm",
  "/main/empInfo":"setting_entmgm",
  "/main/rolesInfo":"setting_entmgm",
  "/container/setting-entinfo":"setting_entinfo",
  "/container/setting-entwxapp":"setting_entwxapp"
};

const hasPerm = (path,perms) =>{
  const hasPerms = [];
  let pathKey = 'default',flag = true,isHit=false;
  perms.map((perm,i)=>{
    hasPerms.push(perm.module);
  });
  if(perms.length<1){
    flag = false;
    return flag;
  }
  Object.keys(permKeys).some((key,i)=>{
    if(path.indexOf(key)!=-1){
      pathKey=permKeys[key];
      flag = true;
      isHit = true;
      return true;
    }else{
      flag = false;
    }
  });
  if(permKeys[path]){
    pathKey=permKeys[path];
    flag = hasPerms.join(",").indexOf(pathKey)!=-1;
  }else if(!isHit){
    flag = true;
  }
  console.log("hasPermhasPermhasPermhasPerm:",pathKey,path);
  return flag;
}

class PermissionC extends React.Component {
	constructor (props, context) {
	    super(props, context);   
	    this.checkPermission=this.checkPermission.bind(this);
	} 
	static propTypes = {   
		perms: PropTypes.array,
    	showRoles:PropTypes.array,
    	hideMsg:PropTypes.string,
    	style:PropTypes.object,
    	resources:PropTypes.string
	} 
	checkPermission(){
		const {perms=[],sysperms=[],resources}=this.props;
		let flag=false;
		perms&&perms.map((per,i)=>{
			if(per.name==resources)
				flag=true;
		});
		return flag;
	} 
	render(){ 
		const {hideMsg,style,router,perms}=this.props;  
		const { pathname='' } = router;
		if(!hasPerm(pathname,perms)){
		    return (
		    	<div style={{height:'100%',display:'flex',alignItems:'center',justifyContent:'center'}}> 
		        <div style={{marginLeft:'-30px'}}></div>
		      </div>
		    )
		}else{
			return <div style={style}>{this.props.children}</div>; 
		}
	}
}

const temMapState = (state) => {
    const {
	    authed: { userInfo, userInfo: { perms = [] }},
	    router: { locationBeforeTransitions = {}}
 	} = state;
	return {
	    'router':locationBeforeTransitions,
	    'perms':perms
	}
};
export const PermissionContent = connect(temMapState)(PermissionC); 



class PermissionLabel extends React.Component {
	constructor (props, context) {
	    super(props, context);   
	    this.checkPermission=this.checkPermission.bind(this);
	} 
	static propTypes = {   
		perms: PropTypes.array,
    	showRoles:PropTypes.array,
    	hideMsg:PropTypes.string,
    	style:PropTypes.object,
    	resources:PropTypes.string
	} 
	checkPermission(){
		const {perms=[],sysperms=[],resources}=this.props;
		let flag=false;
		perms&&perms.map((per,i)=>{
			if(per.name==resources)
				flag=true;
		});
		return flag;
	} 
	render(){
		const {hideMsg,style}=this.props; 
		if(this.checkPermission()){
			return <div style={style}>{this.props.children}</div>; 
		}else if(hideMsg){
			return <div style={style}>{hideMsg}</div>;
		}else{
			return <div></div>;
		} 
	}
}
 

const mapState = (state) => {
    const {setting: {
            sysperms
        }} = state
    const {authed:{userInfo:{perms}}}=state;
    return {sysperms,perms}
};
export default connect(mapState)(PermissionLabel);