|
22 | 22 | :strink="false"
|
23 | 23 | v-model.trim="searchValue"
|
24 | 24 | :placeholder="$t('关键字/字段值')"
|
| 25 | + :remote-method="fetchRemoteOptions" |
| 26 | + @input-change="handleInputChange" |
25 | 27 | @change="handleChange"
|
26 | 28 | @menu-select="handleMenuSelect"
|
27 | 29 | @key-enter="handleKeyEnter"
|
|
39 | 41 | import has from 'has'
|
40 | 42 | import { CONFIG_MODE } from '@/service/service-template/index.js'
|
41 | 43 | import { PROPERTY_TYPES } from '@/dictionary/property-constants'
|
| 44 | + import useSearchUser from '@/hooks/use-search-user' |
42 | 45 |
|
43 | 46 | export default {
|
44 | 47 | props: {
|
45 | 48 | mode: String
|
46 | 49 | },
|
47 | 50 | data() {
|
48 | 51 | return {
|
| 52 | + isTyeing: false, |
49 | 53 | showClear: false,
|
50 | 54 | searchOptions: [],
|
51 | 55 | fullOptions: [],
|
|
87 | 91 | },
|
88 | 92 | created() {
|
89 | 93 | this.initOptions()
|
| 94 | + this.userSearch = useSearchUser().search |
90 | 95 | },
|
91 | 96 | mounted() {
|
92 | 97 | Bus.$on('host-apply-clear-search', (value) => {
|
|
95 | 100 | })
|
96 | 101 | },
|
97 | 102 | methods: {
|
| 103 | + async fetchRemoteOptions(val, menu) { |
| 104 | + // 根据具体类型走具体的远程方法拉取options |
| 105 | + const fetchs = { |
| 106 | + [PROPERTY_TYPES.OBJUSER]: this.fetchMember |
| 107 | + } |
| 108 | + return fetchs[menu.type](val, menu) |
| 109 | + }, |
| 110 | + async fetchMember(val, menu) { |
| 111 | + if (!this.isTyeing || val?.length < 2 || val === `${menu.name}:`) { |
| 112 | + return [] |
| 113 | + } |
| 114 | + const result = await this.userSearch(val) |
| 115 | + return result |
| 116 | + }, |
98 | 117 | async initOptions() {
|
99 | 118 | const availableProperties = this.configPropertyList.filter(property => property.host_apply_enabled)
|
100 | 119 | this.searchOptions = availableProperties.map((property) => {
|
101 | 120 | const type = property.bk_property_type
|
102 | 121 | const data = { id: property.id, name: property.bk_property_name, type, disabled: false }
|
103 |
| - if (type === 'enum') { |
| 122 | + if (type === PROPERTY_TYPES.ENUM) { |
104 | 123 | // eslint-disable-next-line max-len
|
105 | 124 | data.children = (property.option || []).map(option => ({ id: option.id, name: option.name, disabled: false }))
|
106 | 125 | data.multiable = true
|
107 |
| - } else if (type === 'list') { |
| 126 | + } else if (type === PROPERTY_TYPES.LIST) { |
108 | 127 | data.children = (property.option || []).map(option => ({ id: option, name: option, disabled: false }))
|
109 | 128 | data.multiable = true
|
110 |
| - } else if (type === 'timezone') { |
| 129 | + } else if (type === PROPERTY_TYPES.TIMEZONE) { |
111 | 130 | data.children = TIMEZONE.map(timezone => ({ id: timezone, name: timezone, disabled: false }))
|
112 | 131 | data.multiable = true
|
113 |
| - } else if (type === 'bool') { |
| 132 | + } else if (type === PROPERTY_TYPES.BOOL) { |
114 | 133 | data.children = [{ id: true, name: 'true' }, { id: false, name: 'false' }]
|
| 134 | + } else if (type === PROPERTY_TYPES.OBJUSER) { |
| 135 | + data.remote = true |
115 | 136 | } else {
|
116 | 137 | data.children = []
|
117 | 138 | }
|
118 | 139 | return data
|
119 | 140 | })
|
120 | 141 | this.fullOptions = this.searchOptions.slice(0)
|
121 | 142 | },
|
| 143 | + handleInputChange() { |
| 144 | + this.isTyeing = true |
| 145 | + }, |
122 | 146 | handleChange(values) {
|
123 | 147 | const keywords = values.filter(value => !has(value, 'type') && has(value, 'id'))
|
124 | 148 | if (keywords.length > 1) {
|
|
130 | 154 | this.currentMenu = null
|
131 | 155 | },
|
132 | 156 | handleFocus() {
|
| 157 | + this.isTyeing = true |
133 | 158 | this.showClear = true
|
134 | 159 | },
|
135 | 160 | handleBlur() {
|
136 | 161 | this.showClear = false
|
| 162 | + this.isTyeing = false |
137 | 163 | },
|
138 | 164 | handleClear() {
|
139 | 165 | this.searchValue = []
|
|
146 | 172 | handleSearch() {
|
147 | 173 | Bus.$emit(this.searchEventName, this.getSearchValue())
|
148 | 174 | },
|
| 175 | + getSingleTypeVal(type, value) { |
| 176 | + let val = value.id |
| 177 | + switch (type) { |
| 178 | + case PROPERTY_TYPES.ENUM: |
| 179 | + val = value.name |
| 180 | + break |
| 181 | + case PROPERTY_TYPES.OBJUSER: |
| 182 | + val = value.username |
| 183 | + break |
| 184 | + default: |
| 185 | + break |
| 186 | + } |
| 187 | + return val |
| 188 | + }, |
149 | 189 | getSearchValue() {
|
150 | 190 | const params = {
|
151 | 191 | query_filter: {
|
|
164 | 204 | if (isAny) {
|
165 | 205 | rule.operator = 'exist'
|
166 | 206 | } else {
|
167 |
| - // 对枚举类型特殊处理 |
168 |
| - const val = item.type === PROPERTY_TYPES.ENUM ? value.name : value.id |
| 207 | + // 对枚举类型/用户类型特殊处理 |
| 208 | + const val = this.getSingleTypeVal(item.type, value) |
169 | 209 | rule.operator = 'contains'
|
170 | 210 | rule.value = String(val).trim()
|
171 | 211 | }
|
|
0 commit comments