Ben Lin
2025-03-05 abbc201b41a1a25a19ccf7cff8df633dadcf5d88
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="500px"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm">
      <template #menu="{ model, field }">
        <BasicTree
          v-model:value="model[field]"
          :treeData="treeData"
          :fieldNames="{ title: 'menuName', key: 'id' }"
          @gethalfCheckedKeys="gethalfCheckedKeys"
          :expanded-keys="expandedKeys"
          checkable
          toolbar
          title="菜单分配"
        />
      </template>
      <template #org="{ model, field }">
        <BasicTree
          v-model:value="model[field]"
          :treeData="treeDataOrg"
          :renderIcon="createIconOrg"
          :fieldNames="{ title: 'deptName', key: 'id' }"
          @gethalfCheckedKeys="gethalfCheckedKeysOrg"
          :expanded-keys="expandedKeysOrg"
          checkable
          toolbar
          title="组织分配"
        />
      </template>
      <template #fty="{ model, field }">
        <BasicTree
          v-model:value="model[field]"
          :treeData="treeDataProd"
          :renderIcon="createIconOrg"
          :fieldNames="{ title: 'NAME', key: 'ID' }"
          @gethalfCheckedKeys="gethalfCheckedKeysFty"
          :expanded-keys="expandedKeysFty"
          checkable
          toolbar
          title="工厂分配"
        />
      </template>
      <template #wh="{ model, field }">
        <BasicTree
          v-model:value="model[field]"
          :treeData="treeDataWh"
          :renderIcon="createIcon"
          :fieldNames="{ title: 'houseName', key: 'id' }"
          @gethalfCheckedKeys="gethalfCheckedKeysWh"
          :expanded-keys="expandedKeysWh"
          checkable
          toolbar
          title="仓库分配"
        />
      </template>
    </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from './role.data';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { BasicTree, TreeItem } from '/@/components/Tree';
 
  import {
    getMenuList,
    SaveRole,
    getRMList,
    getROList,
    getWhList,
    getProdList,
    getEntity,
    convertToTree,
  } from '/@/api/tigerapi/system';
  import { getProdTreeList, getTreeList } from '/@/api/tigerapi/dept';
  import { getTreeList as getTreeListWh } from '/@/api/tigerapi/wms/house';
 
  type CheckKeys = KeyType[] | { checked: string[] | number[]; halfChecked: string[] | number[] };
  type _expandAll = KeyType[] | undefined;
  const emits = defineEmits(['success', 'register']);
  const isUpdate = ref(true);
  const treeData = ref<TreeItem[]>([]);
  const treeDataOrg = ref<TreeItem[]>([]);
  const treeDataProd = ref<TreeItem[]>([]);
  const treeDataWh = ref<TreeItem[]>([]);
  const expandedKeys = ref<_expandAll>([]);
  const expandedKeysOrg = ref<_expandAll>([]);
  const expandedKeysFty = ref<_expandAll>([]);
  const expandedKeysWh = ref<_expandAll>([]);
  const halfCheckedKeys = ref<CheckKeys>([]);
  const halfCheckedKeysOrg = ref<CheckKeys>([]);
  const halfCheckedKeysFty = ref<CheckKeys>([]);
  const halfCheckedKeysWh = ref<CheckKeys>([]);
  const gethalfCheckedKeys = (obj) => {
    halfCheckedKeys.value = obj;
    console.log(obj);
  };
  const gethalfCheckedKeysOrg = (obj) => {
    halfCheckedKeysOrg.value = obj;
    console.log(obj);
  };
  const gethalfCheckedKeysFty = (obj) => {
    halfCheckedKeysFty.value = obj;
    console.log(obj);
  };
  const gethalfCheckedKeysWh = (obj) => {
    halfCheckedKeysWh.value = obj;
    console.log(obj);
  };
 
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 90,
    schemas: formSchema,
    showActionButtonGroup: false,
  });
 
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    resetFields();
    setDrawerProps({ confirmLoading: false });
    // 需要在setFieldsValue之前先填充treeData,否则Tree组件可能会报key not exist警告
    if (unref(treeData).length === 0) {
      treeData.value = (await getMenuList({menuName: 'SYSTEM'})) as any as TreeItem[];
    }
    if (unref(treeDataOrg).length === 0) {
      treeDataOrg.value = (await getTreeList()) as any as TreeItem[];
    }
    if (unref(treeDataProd).length === 0) {
      // treeDataProd.value = (await getProdTreeList()) as any as TreeItem[];
      let prodTreeData = await getEntity({sqlcmd: '', entityName: 'V_PROD_TREE', order: ''});
      treeDataProd.value = convertToTree(prodTreeData.Data.Items,'PARENT_CODE', 'CODE', '');
    }
    if (unref(treeDataWh).length === 0) {
      treeDataWh.value = (await getTreeListWh()) as any as TreeItem[];
    }
    isUpdate.value = !!data?.isUpdate;
 
    if (unref(isUpdate)) {
      const menuList = await getRMList(data.record.ID); //获取角色对应的菜单
      var list = [];
      var list2 = [];
      for (var key in menuList) {
        if (menuList[key].HALF_CHECK == 'N') {
          var item2 = menuList[key].FUNC_CODE as never;
          list2.push(item2);
        } else {
          var item = menuList[key].FUNC_CODE as never;
          list.push(item);
        }
      }
      //console.log(list,11);
      data.record.menu = list2;
      halfCheckedKeys.value = list;
      expandedKeys.value = list;
      await getRuleOrg(data);
      await getRuleWh(data);
      await getRuleProd(data);
      setFieldsValue({
        ...data.record,
      });
    }
  });
 
  /**
   * @description: 获取角色对应的组织
   * @param {*} data
   * @return {*}
   */  
  async function getRuleOrg(data) {
    const orgList = await getROList(data.record.ID);
    var hfList = [];
    var list = [];
    for (var key in orgList) {
      if (orgList[key].HALF_CHECK == 'N') {
        var item2 = orgList[key].ORG_CODE as never;
        hfList.push(item2);
      } else {
        var item = orgList[key].ORG_CODE as never;
        list.push(item);
      }
    }
    data.record.org = hfList;
    halfCheckedKeysOrg.value = list;
    expandedKeysOrg.value = list;
  }
 
  /**
   * @description: 获取角色对应的仓库
   * @param {*} data
   * @return {*}
   */  
  async function getRuleWh(data) {
    const whList = await getWhList(data.record.ID);
    var hfList = [];
    var list = [];
    for (var key in whList) {
      if (whList[key].HALF_CHECK == 'N') {
        var item2 = whList[key].WH_CODE as never;
        hfList.push(item2);
      } else {
        var item = whList[key].WH_CODE as never;
        list.push(item);
      }
    }
    data.record.wh = hfList;
    halfCheckedKeysWh.value = list;
    expandedKeysWh.value = list;
  }
 
  /**
   * @description: 获取角色对应的生产
   * @param {*} data
   * @return {*}
   */  
  async function getRuleProd(data) {
    const prodList = await getProdList(data.record.ID);
    var hfList = [];
    var list = [];
    for (var key in prodList) {
      if (prodList[key].HALF_CHECK == 'N') {
        var item2 = prodList[key].PROD_CODE as never;
        hfList.push(item2);
      } else {
        var item = prodList[key].PROD_CODE as never;
        list.push(item);
      }
    }
    data.record.fty = hfList;
    halfCheckedKeysFty.value = list;
    expandedKeysFty.value = list;
  }
 
  const getTitle = computed(() => (!unref(isUpdate) ? '新增角色' : '编辑角色'));
 
  async function handleSubmit() {
    try {
      const values = await validate();
      setDrawerProps({ confirmLoading: true });
      let currentValue = halfCheckedKeys.value as [];
      values.menu = { checked: values.menu, halfChecked: currentValue };
      let curOrgValue = halfCheckedKeysOrg.value as [];
      values.org = { checked: values.org, halfChecked: curOrgValue };
      let curFtyValue = halfCheckedKeysFty.value as [];
      values.fty = { checked: values.fty, halfChecked: curFtyValue };
      let curWhValue = halfCheckedKeysWh.value as [];
      values.wh = { checked: values.wh, halfChecked: curWhValue };
      // TODO custom api
      //保存角色
      SaveRole(values, unref(isUpdate)).then((action) => {
        if (action.Result.IsSuccessed) {
          closeDrawer();
          emits('success');
        }
      });
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
  function createIcon({ houseType }) {
    if (houseType === 'Warehouse') {
      return 'warehouse|svg';
    }
    if (houseType === 'Region') {
      return 'Region|svg';
    }
    if (houseType === 'Shelf') {
      return 'Shelf|svg';
    }
    return '';
  }
  function createIconOrg() {
    return 'Org|svg';
  }
</script>