import { ref, computed, toRefs, reactive } from 'vue' import { intersection, without } from 'lodash' import CollectBidListApi from '../plugins/collect-list' // import CollectTagsApi from '../plugins/collect-tags' function useQuickCollectBidModel({ params }) { const useApiModel = new CollectBidListApi(params) const { doQuery } = useApiModel const { list, loading, finished, total } = toRefs(reactive(useApiModel)) const selectIds = ref([]) const listIds = computed(() => { return list.value.map((item) => item.id || item._id) }) const searchResultCount = computed(() => { return total.value > 100000000 ? '超过' + (total.value / 100000000).toFixed(1) + '亿' : total.value }) const isSelectSomeCheckbox = computed(() => { return selectIds.value.length > 0 }) const selectCheckboxCount = computed(() => { return selectIds.value.length }) const isSelectListAllCheckbox = computed(() => { const result = intersection(listIds.value, selectIds.value) if (listIds.value.length === 0) { return false } return result.length === list.value.length }) /** * 单个item选中、取消操作 * @param id * @param [type] - 选中 true \ 取消 false */ function doToggleItemSelection(id, type) { if (typeof type !== 'boolean') { type = !selectIds.value.includes(id) } selectIds.value = without(selectIds.value, id) if (type) { selectIds.value.push(id) } } /** * 全选当前列表操作 * @param [type] - 选中 true \ 取消 false */ function doToggleListSelection(type = true) { // 移除ids selectIds.value = without(selectIds.value, ...listIds.value) if (type) { selectIds.value = selectIds.value.concat(listIds.value) } } /** * 清空所有选择项 */ function doClearAllSelection() { selectIds.value = [] } return { list, total, loading, finished, selectIds, listIds, searchResultCount, isSelectSomeCheckbox, selectCheckboxCount, isSelectListAllCheckbox, doToggleItemSelection, doToggleListSelection, doClearAllSelection, doQuery } } export default useQuickCollectBidModel