import { addRoleOrPolicy, bindRolesOrPolicy } from '../server'; import { useGetAppId } from '../utils/hooks'; import { difference, intersectionWith } from 'lodash'; import { useCallback, useEffect, useState } from 'react'; import { useIntl, useRequest, useSelector } from 'umi'; interface IBindRoleOrPolicy { name?: string; comType?: 'role' | 'policy'; type?: 'feature' | 'option' | 'policy' | 'user' | 'role'; // 选择类型 id?: number; flagId?: number; form: any; isHaveDate?: boolean; appIdCustomer?: number; } export const useBindRoleOrPolicy = ({ name = 'data', comType, type, id, flagId, form, isHaveDate, appIdCustomer, }: IBindRoleOrPolicy) => { const { formatMessage } = useIntl(); const appId = useGetAppId(); const { roleList, policyList } = useSelector<{ public: API.StoreState }, API.StoreState>( (state: any) => state.public, ); const [dataMap, setDataMap] = useState({}); const allData = comType === 'role' ? roleList : policyList; const { data: tempData, run, loading, } = useRequest(bindRolesOrPolicy(comType, { appId: appIdCustomer ? appIdCustomer : appId }), { manual: true, }); const { list } = tempData || {}; // const list = tempList?.map((item) => ({ // ...item, // expiredAt: isHaveDate ? item.expiredAt * 1000 : undefined, // })); const updateFn = useCallback( async (data, oldList, formatFn) => { const map = {}; const params: number[] = data?.map((val) => { map[val?.id] = val; return parseInt(val?.id, 10); }); const oldParams: number[] = oldList?.map((item) => item.id); // 这是增加的 const addParams = difference(params, oldParams)?.map((item) => formatFn(item, map)); // 这是减少的 const deleteParams = difference(oldParams, params)?.map((item) => formatFn(item, dataMap)); // 这是需要更新的 const updateParams = intersectionWith(data, oldList, (oldItem: any, newItem: any) => { if (!isHaveDate) { return false; } if (`${oldItem?.id}` === `${newItem?.id}`) { if (oldItem?.expiredAt !== newItem?.expiredAt) { return true; } } return false; }); const urlMap = { role: { feature: 'role_feature', option: 'role_option', policy: 'role_policy', user: 'user_role', }, policy: { // 'feature': 'policy_feature', // 'option': 'policy_option', role: 'role_policy', user: 'user_policy', }, }; await addRoleOrPolicy( urlMap[comType || ''][type], [ ...addParams, ...updateParams?.map((p: API.RoleListItem) => formatFn(parseInt(`${p.id}`, 10), map)), ], deleteParams, ); }, [comType, type, dataMap], ); const initFetch = useCallback(() => { // todo // if (allData?.length < 1) { // } if (!id) return; run({ [`${type === 'option' ? 'flagOption' : type}Id`]: id, flagId }); }, [type, id, run, flagId]); useEffect(() => { const obj = {}; list?.forEach((item) => { obj[item.id] = { ...item, expiredAt: isHaveDate ? item.expiredAt * 1000 : undefined, }; }); setDataMap(obj); }, [list, setDataMap, isHaveDate]); useEffect(() => { // if (open) { // if (list && list.length > 0) { const ref = form?.setFieldsValue ? form : form?.current; ref?.setFieldsValue({ [name]: { data: list?.map((d) => ({ id: d.id, expiredAt: isHaveDate ? d.expiredAt * 1000 : undefined, })), }, }); // } // } }, [list, form, name, isHaveDate]); return { allData, loading, initFetch, updateFn, appId: appIdCustomer ? appIdCustomer : appId, list: list?.map((item) => ({ ...item, expiredAt: isHaveDate ? item.expiredAt * 1000 : undefined, })), title: formatMessage({ id: `component.bind.${comType}.title`, defaultMessage: '绑定角色' }), }; };