干燥机配套车间生产管理系统/云平台服务端
baoshiwei
2023-03-10 1fb197352b6a263646e4ccd3ed1c7854ede031dd
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
<template>
  <div>
    <div v-if="isSearchFormComp" @click="click2Add" :class="disabled?'disabled-user-select':''" style="padding:0 5px;background-color: #fff;border: 1px solid #ccc;border-radius: 3px;box-sizing: border-box;display:flex;color: #9e9e9e;font-size: 14px;flex-wrap: wrap;min-height: 32px;">
      <template v-if="selectedUserList.length > 0">
        <SelectedUserItem v-for="item in showUserList" :info="item" @unSelect="unSelectUser" query />
      </template>
      <span v-else style="height: 30px;line-height: 30px;display: inline-block;margin-left: 7px;color: #bfbfbf;">请选择用户</span>
      <div v-if="ellipsisInfo.status" class="user-selected-item">
        <div class="user-select-ellipsis">
          <span style="color: red">+{{ellipsisInfo.count}}...</span>
        </div>
      </div>
    </div>    
    
    <div v-else style="display: flex; flex-wrap: wrap; flex-direction: row" >
      <template v-if="selectedUserList.length > 0">
        <SelectedUserItem v-for="item in selectedUserList" :info="item" @unSelect="unSelectUser" />
      </template>
      <a-button v-if="showAddButton" shape="circle" @click="onShowModal"><PlusOutlined /></a-button>
    </div>
 
    <user-select-modal :multi="multi" :getContainer="getContainer" @register="registerModal" @selected="onSelected" :izExcludeMy="izExcludeMy"></user-select-modal>
  </div>
</template>
 
<script lang="ts">
  import { defineComponent, watch, ref, computed, toRaw } from 'vue';
  import { Form } from 'ant-design-vue';
  import { PlusOutlined } from '@ant-design/icons-vue';
  import { useModal } from '/@/components/Modal';
  import UserSelectModal from './UserSelectModal.vue';
  import { defHttp } from '/@/utils/http/axios';
  import SelectedUserItem from './SelectedUserItem.vue';
 
  export default defineComponent({
    name: 'UserSelect',
    components: {
      PlusOutlined,
      UserSelectModal,
      SelectedUserItem,
    },
    props: {
      store: {
        type: String,
        default: 'id',
      },
      value: {
        type: String,
        default: '',
      },
      multi: {
        type: Boolean,
        default: false,
      },
      getContainer: {
        type: Function,
        default: null,
      },
      // 是否作为查询条件
      query:{
        type: Boolean,
        default: false,
      },
      //最多显示几个人员-query为true有效
      maxCount:{
        type: Number,
        default: 2
      },
      disabled:{
        type: Boolean,
        default: false,
      },
      //是否排除我自己
      izExcludeMy:{
        type: Boolean,
        default: false,
      }
    },
    emits: ['update:value', 'change'],
    setup(props, { emit }) {
      const formItemContext = Form.useInjectFormItemContext();
      const loading = ref(true);
      const selectedUserList = ref<any[]>([]);
      const showUserList = computed(()=>{
        let list = selectedUserList.value
        let max = props.maxCount;
        if(list.length<=max){
          return list;
        }
        return list.filter((_item, index)=>index<max);
      });
      const ellipsisInfo = computed(()=>{
        let max = props.maxCount;
        let len = selectedUserList.value.length
        if(len > max){
          return {status: true, count: len-max};
        }else{
          return {status: false}
        }
      });
 
      // 注册弹窗
      const [registerModal, { openModal, closeModal }] = useModal();
      function onShowModal() {
        if(props.disabled===true){
          return ;
        }
        let list = toRaw(selectedUserList.value);
        openModal(true, {
          list,
        });
      }
 
      function onSelected(arr) {
        console.log('onSelected', arr);
        selectedUserList.value = arr;
        onSelectedChange();
        closeModal();
      }
 
      function onSelectedChange() {
        loading.value = false;
        let temp: any[] = [];
        let arr = selectedUserList.value;
        if (arr && arr.length > 0) {
          temp = arr.map((k) => {
            return k[props.store];
          });
        }
        let str = temp.join(',');
        emit('update:value', str);
        emit('change', str);
        formItemContext.onFieldChange();
        console.log('选中数据', str);
      }
 
      watch(
        () => props.value,
        async (val) => {
          if (val) {
            if (loading.value === true) {
              await getUserList(val);
            }
          } else {
            selectedUserList.value = [];
          }
          loading.value = true;
        },
        { immediate: true }
      );
 
      async function getUserList(ids) {
        const url = '/sys/user/list';
        let params = {
          [props.store]: ids,
        };
        selectedUserList.value = [];
        const data = await defHttp.get({ url, params }, { isTransformResponse: false });
        if (data.success) {
          const { records } = data.result;
          selectedUserList.value = records;
        } else {
          console.error(data.message);
        }
        console.log('getUserList', data);
      }
 
      const showAddButton = computed(() => {
        if(props.disabled === true){
          return false;
        }
        if (props.multi === true) {
          return true;
        } else {
          if (selectedUserList.value.length > 0) {
            return false;
          } else {
            return true;
          }
        }
      });
 
      function unSelectUser(id) {
        console.log('unSelectUser', id);
        let arr = selectedUserList.value;
        let index = -1;
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].id == id) {
            index = i;
            break;
          }
        }
        if (index >= 0) {
          arr.splice(index, 1);
          selectedUserList.value = arr;
 
          onSelectedChange();
        }
      }
      
      function click2Add(e) {
        e.preventDefault();
        e.stopPropagation();
        onShowModal();
      }
      
      const isSearchFormComp = computed(()=>{
        if(props.query===true){
          return true;
        }else{
          return false
        }
      });
      
      return {
        registerModal,
        onShowModal,
        isSearchFormComp,
        onSelected,
        showAddButton,
        unSelectUser,
        selectedUserList,
        showUserList,
        ellipsisInfo,
        click2Add
      };
    },
  });
</script>
 
<style lang="less" scoped>
  .user-select-ellipsis{
    width: 40px;
    height: 24px;
    text-align: center;
    line-height: 22px;
    border-radius: 8px;
    background: #f5f5f5;
    border: 1px solid #f0f0f0;
  }
  .disabled-user-select{
    cursor: not-allowed;
    background-color: #f5f5f5 !important;
  }
</style>