index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { ref, computed, toRefs, reactive } from 'vue'
  2. import { intersection, without } from 'lodash'
  3. import SearchBidListApi from '../plugins/search-bid'
  4. import SearchEntListApi from '../plugins/search-ent'
  5. import SearchSupplyListApi from '../plugins/search-supply'
  6. import SearchPurchaseListApi from '../plugins/search-purchase'
  7. import SearchNzjListApi from '../plugins/search-nzj'
  8. import SearchSunListApi from '../plugins/search-sun'
  9. const APIS = {
  10. 'search-bid': SearchBidListApi,
  11. 'search-ent': SearchEntListApi,
  12. 'search-supply': SearchSupplyListApi,
  13. 'search-purchase': SearchPurchaseListApi,
  14. 'search-sun': SearchSunListApi,
  15. 'search-nzj': SearchNzjListApi
  16. }
  17. function useQuickSearchModel(config) {
  18. const { type } = config
  19. const useSearchListApi = APIS[type]
  20. const useApiModel = new useSearchListApi(config)
  21. const { doQuery } = useApiModel
  22. const { list, loading, finished, total } = toRefs(reactive(useApiModel))
  23. const selectIds = ref([])
  24. const listIds = computed(() => {
  25. return list.value.map((item) => item.id || item._id)
  26. })
  27. const searchResultCount = computed(() => {
  28. return total.value > 100000000 ? '超过' + (total.value / 100000000).toFixed(1) + '亿' : total.value
  29. })
  30. const isSelectSomeCheckbox = computed(() => {
  31. return selectIds.value.length > 0
  32. })
  33. const selectCheckboxCount = computed(() => {
  34. return selectIds.value.length
  35. })
  36. const isSelectListAllCheckbox = computed(() => {
  37. const result = intersection(listIds.value, selectIds.value)
  38. if (listIds.value.length === 0) {
  39. return false
  40. }
  41. return result.length === list.value.length
  42. })
  43. /**
  44. * 单个item选中、取消操作
  45. * @param id
  46. * @param [type] - 选中 true \ 取消 false
  47. */
  48. function doToggleItemSelection(id, type) {
  49. if (typeof type !== 'boolean') {
  50. type = !selectIds.value.includes(id)
  51. }
  52. selectIds.value = without(selectIds.value, id)
  53. if (type) {
  54. selectIds.value.push(id)
  55. }
  56. }
  57. /**
  58. * 全选当前列表操作
  59. * @param [type] - 选中 true \ 取消 false
  60. */
  61. function doToggleListSelection(type = true) {
  62. // 移除ids
  63. selectIds.value = without(selectIds.value, ...listIds.value)
  64. if (type) {
  65. selectIds.value = selectIds.value.concat(listIds.value)
  66. }
  67. }
  68. /**
  69. * 清空所有选择项
  70. */
  71. function doClearAllSelection() {
  72. selectIds.value = []
  73. }
  74. return {
  75. list,
  76. total,
  77. loading,
  78. finished,
  79. selectIds,
  80. listIds,
  81. searchResultCount,
  82. isSelectSomeCheckbox,
  83. selectCheckboxCount,
  84. isSelectListAllCheckbox,
  85. doToggleItemSelection,
  86. doToggleListSelection,
  87. doClearAllSelection,
  88. doQuery
  89. }
  90. }
  91. export default useQuickSearchModel