import * as React from 'react'; import UserSelectorComp from '@beisen/user-selector' import { getApiUrl, taker, getQueryString, getUserInfo } from '../helper'; export interface UserSelectorProps { type?:number multi?: boolean onSure?:Function onClose?:Function singleSelect:boolean } export interface State { departments:any[] results:any[] staffs:any[] usedusers: any[] multi: boolean searchs: any[] } export default class UserSelector extends React.Component { constructor(p:UserSelectorProps) { super(p); this.state = { departments:[], results:[], staffs:[], usedusers: [], multi: p.multi || false, searchs: [] } //@ts-ignore this.tenantId = BSGlobal.tenantInfo.Id; //@ts-ignore this.userId = BSGlobal.loginUserInfo.Id this.config = { singleSelect: false, // 单人选人,没有高级模式 TenantId: this.tenantId, // 租户ID UserId: this.userId, // 用户ID apiType: 'tita', // 接口产品 isUseInitial: true, // 是否使用内置接口 onSure: p.onSure, // 精简模式下 返回点击的人 数据格式 高级模式下 确定回调 返回已选择的数组 onClose: p.onClose, // 高级模式下 取消回调 } this.getUsedInfo = this.getUsedInfo.bind(this); this.getSearchInfo = this.getSearchInfo.bind(this); this.getDepartments = this.getDepartments.bind(this); this.getStaff = this.getStaff.bind(this); this.getDepInfo = this.getDepInfo.bind(this); } config: any; tenantId:any; userId:any async getDepartments(){ const departmentsUrl = `${getApiUrl('department/All')}` const res = await taker(departmentsUrl); this.setState({departments:res.Data}) } async getDepInfo(id:any){ const {departments} = this.state; const depInfoUrl = `${getApiUrl('department/DepartmentOfUser')}?department_id=${id}` const res = await taker(depInfoUrl); const users = res.Data; const index = departments.findIndex((de:any)=>de.Id == id); departments[index].childUser = users; this.setState({departments:departments}) } addResults = (id:any, dataNow:any, pid:any, onSure:any) => { if(isNaN(pid) && dataNow == 'departments') return; let {results} = this.state; if (pid || pid == 0) { let index_dep_ADDRESULTS = this.state[dataNow].findIndex((v:any) => v.Id == pid); let dep_users = this.state[dataNow][index_dep_ADDRESULTS].childUser; let select_dep_users = dep_users.filter((v1:any) => v1.Id == id); results.push(...select_dep_users); } else { let index_ADDRESULTS = this.state[dataNow].findIndex((v:any) => v.Id == id); let value_ADDRESULTS = this.state[dataNow][index_ADDRESULTS]; if (dataNow == 'departments') { results.push(value_ADDRESULTS) } else { results.push(value_ADDRESULTS) } } this.sortData(); } sortData=()=>{ let state = this.state; let _resultsTemp; _resultsTemp = state.results; let resIdArray = []; let resArray = []; for (var i = 0; i < _resultsTemp.length; i++) { if(!_resultsTemp[i]) continue; else if(resIdArray.indexOf(_resultsTemp[i].Id) == -1) { resIdArray.push(_resultsTemp[i].Id); resArray.push(_resultsTemp[i]); } }; this.setState({results:resArray}) } clearResults = (isAll:any, id:any) => { const {results} = this.state; if (isAll) { this.setState({results:[]}) } else { const newResults = results.filter((re:any)=>re.Id != id); this.setState({results:newResults}) } } async getStaff(){ const diffUrl = `${getApiUrl('org/GetMySubordinates')}?lv=2&type=0` const res = await taker(diffUrl); let temp_GETSTAFF = []; for (let i = 1; i < res.Data.length; i++) { temp_GETSTAFF.push( res.Data[i]['User']); } this.setState({staffs:temp_GETSTAFF}); } getUsedInfo = async() =>{ if(this.props.type && this.props.type==2){ this.setState({usedusers:[]}) }else{ const contactUrl = `${getApiUrl('Contact')}`; const res = await taker(contactUrl); this.setState({usedusers:Object.values(res.Data)}) } } closeComponent = () => { this.setState({multi:false,results:[],searchs:[]}) } setMulti = () =>{ this.setState({multi:true}) } addAllUsers = (dataNow:any) => { const {results} = this.state; results.push(...this.state[dataNow]); this.sortData() } async getSearchInfo(value:any) { // const searchUrl = `${getApiUrl('user')}?filters=${value}`; // const res = await taker(searchUrl); // const val = res && res.Data && res.Data.SimpleUserModel?Object.values(res.Data.SimpleUserModel):[]; // this.setState({searchs:val}) // type是因为加人的时候,加人弹层做权限设置,搜索接口使用可以进行权限判断的组件 let val:any[] = []; if(this.props.type && this.props.type == 2){ const pageCode = getQueryString()['pageCode'] ? getQueryString()['pageCode'] : ''; const searchUrl = `${getApiUrl('proxy/GetUPaaSCoreGateway')}?metaname=ProfileAuthority&funname=SearchStaffWithAuthority&bizUserId=${getUserInfo()['Id']}&keyword=${value}&originPageCode=${pageCode}`; const res = await taker(searchUrl); val = res && res.OperationObject ?Object.values(res.OperationObject):[]; }else{ const searchUrl = `${getApiUrl('user')}?filters=${value}`; const res = await taker(searchUrl); val = res && res.Data && res.Data.SimpleUserModel?Object.values(res.Data.SimpleUserModel):[]; } this.setState({searchs:val}) } public render() { return (
); } }