广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-02 80ff784bf60637cd348ae665fc907f7b1e527dd8
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<script setup lang="ts">
import { computed, reactive, ref, watch } from 'vue';
import { useLoading } from '@sa/hooks';
import { dataScopeOptions } from '@/constants/business';
import { fetchGetRoleDeptTreeSelect, fetchUpdateRoleDataScope } from '@/service/api/system/role';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { $t } from '@/locales';
import DeptTree from '@/components/custom/dept-tree.vue';
 
defineOptions({
  name: 'RoleDataScopeDrawer'
});
 
interface Props {
  /** the edit row data */
  rowData?: Api.System.Role | null;
}
 
const props = defineProps<Props>();
 
interface Emits {
  (e: 'submitted'): void;
}
 
const emit = defineEmits<Emits>();
 
const deptTreeRef = ref<InstanceType<typeof DeptTree> | null>(null);
 
const visible = defineModel<boolean>('visible', {
  default: false
});
 
const deptOptions = ref<Api.System.Dept[]>([]);
 
const { loading: deptLoading, startLoading: startDeptLoading, endLoading: endDeptLoading } = useLoading();
 
const { formRef, validate, restoreValidation } = useNaiveForm();
const { createRequiredRule } = useFormRules();
 
const title = computed(() => '分配数据权限');
 
type Model = Api.System.RoleOperateParams;
 
const model: Model = reactive(createDefaultModel());
 
function createDefaultModel(): Model {
  return {
    roleId: props.rowData?.roleId,
    roleName: props.rowData?.roleName,
    roleKey: props.rowData?.roleKey,
    roleSort: props.rowData?.roleSort,
    deptIds: [],
    menuIds: [],
    deptCheckStrictly: true,
    dataScope: '1'
  };
}
 
type RuleKey = Extract<keyof Model, 'dataScope'>;
 
const rules: Record<RuleKey, App.Global.FormRule> = {
  dataScope: createRequiredRule('数据权限范围不能为空')
};
 
async function handleUpdateModelWhenEdit() {
  startDeptLoading();
  deptOptions.value = [];
  model.deptIds = [];
 
  if (props.rowData) {
    Object.assign(model, props.rowData);
    const { error, data } = await fetchGetRoleDeptTreeSelect(props.rowData.roleId!);
    if (error) return;
    deptOptions.value = data.depts;
    model.deptIds = data.checkedKeys;
  }
  endDeptLoading();
}
 
function closeDrawer() {
  visible.value = false;
}
 
async function handleSubmit() {
  await validate();
 
  const { roleId, roleName, roleKey, roleSort, dataScope, deptIds, menuIds } = model;
 
  const { error } = await fetchUpdateRoleDataScope({
    roleId,
    roleName,
    roleKey,
    roleSort,
    dataScope,
    deptIds: dataScope === '2' ? deptIds : [],
    menuIds
  });
  if (error) return;
 
  window.$message?.success($t('common.updateSuccess'));
  closeDrawer();
  emit('submitted');
}
 
watch(visible, () => {
  if (visible.value) {
    handleUpdateModelWhenEdit();
    restoreValidation();
  }
});
</script>
 
<template>
  <NDrawer v-model:show="visible" :title="title" display-directive="show" :width="800" class="max-w-90%">
    <NDrawerContent :title="title" :native-scrollbar="false" closable>
      <NForm ref="formRef" :model="model" :rules="rules">
        <NFormItem label="角色名称" path="roleName">
          <NInput v-model:value="model.roleName" disabled placeholder="请输入角色名称" />
        </NFormItem>
        <NFormItem path="roleKey">
          <template #label>
            <div class="flex-center">
              <FormTip content="控制器中定义的权限字符,如:@SaCheckRole('admin')" />
              <span class="pl-3px">权限字符</span>
            </div>
          </template>
          <NInput v-model:value="model.roleKey" disabled placeholder="请输入权限字符" />
        </NFormItem>
        <NFormItem label="权限范围" path="dataScope">
          <NSelect v-model:value="model.dataScope" :options="dataScopeOptions" />
        </NFormItem>
        <NFormItem v-if="model.dataScope === '2'" label="数据权限" path="deptIds" class="pr-24px">
          <DeptTree
            v-if="visible"
            ref="deptTreeRef"
            v-model:value="model.deptIds"
            v-model:options="deptOptions"
            v-model:loading="deptLoading"
            v-model:cascade="model.deptCheckStrictly"
            :immediate="false"
          />
        </NFormItem>
      </NForm>
      <template #footer>
        <NSpace :size="16">
          <NButton @click="closeDrawer">{{ $t('common.cancel') }}</NButton>
          <NButton type="primary" @click="handleSubmit">{{ $t('common.confirm') }}</NButton>
        </NSpace>
      </template>
    </NDrawerContent>
  </NDrawer>
</template>
 
<style scoped></style>