123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- import { ref, computed, toRefs, reactive } from 'vue'
- import { intersection, without } from 'lodash'
- import SearchBidListApi from '../plugins/search-bid'
- import SearchEntListApi from '../plugins/search-ent'
- import SearchSupplyListApi from '../plugins/search-supply'
- import SearchPurchaseListApi from '../plugins/search-purchase'
- import SearchNzjListApi from '../plugins/search-nzj'
- const APIS = {
- 'search-bid': SearchBidListApi,
- 'search-ent': SearchEntListApi,
- 'search-supply': SearchSupplyListApi,
- 'search-purchase': SearchPurchaseListApi,
- 'search-nzj': SearchNzjListApi
- }
- function useQuickSearchModel(config) {
- const { type } = config
- const useSearchListApi = APIS[type]
- const useApiModel = new useSearchListApi(config)
- 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 useQuickSearchModel
|