index.js 2.2 KB

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