index.js 2.6 KB

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