hooks.ts
1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { useModel } from 'umi';
import { OPERATION_TYPE, ROLE } from './type.d';
function setAllValuesToTrue(obj: Record<string, boolean | object>) {
return Object.keys(obj).reduce(
(result, key) => {
if (typeof obj[key] === 'object') {
result[key] = setAllValuesToTrue(obj[key] as Record<string, object>);
} else {
result[key] = true;
}
return result;
},
{} as Record<string, boolean | object>,
);
}
// 获取用户可编辑字段
export const useFieldAuth = ({ operation }: { operation: OPERATION_TYPE }) => {
const { role } = useModel('user');
let authFields = {
bank: false,
bankAccountNumber: false,
createTime: false,
customerContactNumber: false,
customerName: false,
customerShippingAddress: false,
id: false,
institution: false,
institutionContactName: false,
invoiceIdentificationNumber: false,
notes: false,
salesCode: false,
subOrderInformationLists: {
id: false,
invoicingStatus: false,
mainOrderId: false,
orderStatus: false,
parameters: false,
paymentChannel: false,
productCode: false,
productName: false,
quantity: false,
subOrderPayment: false,
},
} as Record<string, boolean | object>;
if (operation === OPERATION_TYPE.CREATE) {
return authFields;
} else if (operation === OPERATION_TYPE.EDIT) {
if (role === ROLE.ADMIN) {
return authFields;
} else if (role === ROLE.SALESMAN) {
// 将所有的字段设置为没有权限编辑
authFields = setAllValuesToTrue(authFields);
// 筛选字段为可编辑
authFields = {
...authFields,
invoiceInformation: false,
};
}
}
return { authFields };
};