inleft
2022-02-09 9bcb19959eeb9da9bde2561e7278f6d0a55eb151
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
<template>
  <a-select
    :mode="model"
    showSearch
    :value="selectValue"
    :filter-option="false"
    :placeholder="placeholder"
    :not-found-content="fetching ? undefined : null"
    @search="fetchUser"
    @change="handleChange"
  >
    <a-spin v-if="fetching" slot="notFoundContent" size="small" />
    <a-select-option v-for="d in data" :key="d.value">
      {{ d.text }}
    </a-select-option>
  </a-select>
</template>
<script>
import debounce from 'lodash/debounce'
import { getUserPage } from '@/api/modular/system/userManage'
 
export default {
  name: 'UserSelect',
  props: {
    placeholder: {
      type: String
    },
    value: {
      type: String
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const multiple = this.multiple
    this.fetchUser = debounce(this.fetchUser, 800)
    return {
      data: [],
      fetching: false,
      selectValue: multiple ? [] : undefined,
      model: multiple ? 'multiple' : 'default'
    }
  },
  methods: {
    fetchUser(key) {
      console.log('fetching user', key)
      this.data = []
      this.fetching = true
 
      const params = {
        pageNo: 1,
        pageSize: 10,
        searchValue: key
      }
      this.userFetching = true
 
      getUserPage(params).then((res) => {
        this.data = res.data.rows.map(user => ({
          text: `${user.name} ${user.account}`,
          value: user.id
        }))
      }).finally(() => {
        this.fetching = false
      })
    },
    handleChange(value) {
      Object.assign(this, {
        selectValue: value,
        fetching: false
      })
      this.$emit('change', value)
    }
  }
}
</script>