# 内部成本字段权限控制
## 需求说明
在业务订单列表中添加内部成本字段的显示,但只有指定角色的用户才能看到该字段。
## 权限配置
文件:`server/config/project.php`
```php
// 处方业务订单 internal_cost 等财务字段:以下角色 ID + root 可见
'prescription_order_finance_roles' => [0, 3, 6],
```
- 角色 0:超级管理员
- 角色 3:管理员
- 角色 6:药师
## 后端权限控制
文件:`server/app/adminapi/logic/tcm/PrescriptionOrderLogic.php`
后端已经实现了权限控制逻辑:
```php
public static function canViewInternalCost(array $adminInfo): bool
{
if (!empty($adminInfo['root']) && (int) $adminInfo['root'] === 1) {
return true;
}
$allow = Config::get('project.prescription_order_finance_roles', [0, 3]);
$allow = array_map('intval', is_array($allow) ? $allow : []);
$myRoles = array_map('intval', $adminInfo['role_id'] ?? []);
return count(array_intersect($myRoles, $allow)) > 0;
}
public static function maskInternalCostIfNeeded(array &$row, array $adminInfo): void
{
if (!self::canViewInternalCost($adminInfo)) {
unset($row['internal_cost']);
}
}
```
后端在返回数据前会自动移除 `internal_cost` 字段,如果用户没有权限。
## 前端权限控制
文件:`admin/src/views/consumer/prescription/order_list.vue`
### 1. 添加权限检查函数
```typescript
import useUserStore from '@/stores/modules/user'
const userStore = useUserStore()
/** 与 server/config/project.php prescription_order_finance_roles 保持一致 */
const FINANCE_ROLE_IDS = [0, 3, 6]
/** 判断当前用户是否有查看财务字段(内部成本)的权限 */
const canViewFinanceFields = () => {
const u = userStore.userInfo
if (!u) return false
if (Number(u.root) === 1) return true
const ids = Array.isArray(u.role_ids) ? u.role_ids.map((n: unknown) => Number(n)) : []
return FINANCE_ROLE_IDS.some((rid) => ids.includes(rid))
}
```
### 2. 在列表中添加权限控制
**表格列(只有有权限的用户才能看到整列):**
```vue