CombinationTableSelect.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <template>
  2. <Dialog v-model="dialogVisible" :appendToBody="true" title="选择活动" width="70%">
  3. <ContentWrap>
  4. <!-- 搜索工作栏 -->
  5. <el-form
  6. ref="queryFormRef"
  7. :inline="true"
  8. :model="queryParams"
  9. class="-mb-15px"
  10. label-width="68px"
  11. >
  12. <el-form-item label="活动名称" prop="name">
  13. <el-input
  14. v-model="queryParams.name"
  15. placeholder="请输入活动名称"
  16. clearable
  17. @keyup.enter="handleQuery"
  18. class="!w-240px"
  19. />
  20. </el-form-item>
  21. <el-form-item label="活动状态" prop="status">
  22. <el-select
  23. v-model="queryParams.status"
  24. placeholder="请选择活动状态"
  25. clearable
  26. class="!w-240px"
  27. >
  28. <el-option
  29. v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
  30. :key="dict.value"
  31. :label="dict.label"
  32. :value="dict.value"
  33. />
  34. </el-select>
  35. </el-form-item>
  36. <el-form-item>
  37. <el-button @click="handleQuery">
  38. <Icon class="mr-5px" icon="ep:search" />
  39. 搜索
  40. </el-button>
  41. <el-button @click="resetQuery">
  42. <Icon class="mr-5px" icon="ep:refresh" />
  43. 重置
  44. </el-button>
  45. </el-form-item>
  46. </el-form>
  47. <el-table v-loading="loading" :data="list" show-overflow-tooltip>
  48. <!-- 1. 多选模式(不能使用type="selection",Element会忽略Header插槽) -->
  49. <el-table-column width="55" v-if="multiple">
  50. <template #header>
  51. <el-checkbox
  52. v-model="isCheckAll"
  53. :indeterminate="isIndeterminate"
  54. @change="handleCheckAll"
  55. />
  56. </template>
  57. <template #default="{ row }">
  58. <el-checkbox
  59. v-model="checkedStatus[row.id]"
  60. @change="(checked: boolean) => handleCheckOne(checked, row, true)"
  61. />
  62. </template>
  63. </el-table-column>
  64. <!-- 2. 单选模式 -->
  65. <el-table-column label="#" width="55" v-else>
  66. <template #default="{ row }">
  67. <el-radio
  68. :value="row.id"
  69. v-model="selectedActivityId"
  70. @change="handleSingleSelected(row)"
  71. >
  72. <!-- 空格不能省略,是为了让单选框不显示label,如果不指定label不会有选中的效果 -->
  73. &nbsp;
  74. </el-radio>
  75. </template>
  76. </el-table-column>
  77. <el-table-column label="活动编号" prop="id" min-width="80" />
  78. <el-table-column label="活动名称" prop="name" min-width="140" />
  79. <el-table-column label="活动时间" min-width="210">
  80. <template #default="scope">
  81. {{ formatDate(scope.row.startTime, 'YYYY-MM-DD') }}
  82. ~ {{ formatDate(scope.row.endTime, 'YYYY-MM-DD') }}
  83. </template>
  84. </el-table-column>
  85. <el-table-column label="商品图片" prop="spuName" min-width="80">
  86. <template #default="scope">
  87. <el-image
  88. :src="scope.row.picUrl"
  89. class="h-40px w-40px"
  90. :preview-src-list="[scope.row.picUrl]"
  91. preview-teleported
  92. />
  93. </template>
  94. </el-table-column>
  95. <el-table-column label="商品标题" prop="spuName" min-width="300" />
  96. <el-table-column
  97. label="原价"
  98. prop="marketPrice"
  99. min-width="100"
  100. :formatter="fenToYuanFormat"
  101. />
  102. <el-table-column label="拼团价" prop="seckillPrice" min-width="100">
  103. <template #default="scope">
  104. {{ formatCombinationPrice(scope.row.products) }}
  105. </template>
  106. </el-table-column>
  107. <el-table-column label="开团组数" prop="groupCount" min-width="100" />
  108. <el-table-column label="成团组数" prop="groupSuccessCount" min-width="100" />
  109. <el-table-column label="购买次数" prop="recordCount" min-width="100" />
  110. <el-table-column label="活动状态" align="center" prop="status" min-width="100">
  111. <template #default="scope">
  112. <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
  113. </template>
  114. </el-table-column>
  115. <el-table-column
  116. label="创建时间"
  117. align="center"
  118. prop="createTime"
  119. :formatter="dateFormatter"
  120. width="180px"
  121. />
  122. </el-table>
  123. <!-- 分页 -->
  124. <Pagination
  125. v-model:limit="queryParams.pageSize"
  126. v-model:page="queryParams.pageNo"
  127. :total="total"
  128. @pagination="getList"
  129. />
  130. </ContentWrap>
  131. <template #footer v-if="multiple">
  132. <el-button type="primary" @click="handleEmitChange">确 定</el-button>
  133. <el-button @click="dialogVisible = false">取 消</el-button>
  134. </template>
  135. </Dialog>
  136. </template>
  137. <script lang="ts" setup>
  138. import { handleTree } from '@/utils/tree'
  139. import * as ProductCategoryApi from '@/api/mall/product/category'
  140. import { propTypes } from '@/utils/propTypes'
  141. import { CHANGE_EVENT } from 'element-plus'
  142. import * as CombinationActivityApi from '@/api/mall/promotion/combination/combinationActivity'
  143. import { fenToYuanFormat } from '@/utils/formatter'
  144. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  145. import { dateFormatter, formatDate } from '@/utils/formatTime'
  146. import { fenToYuan } from '@/utils'
  147. type CombinationActivityVO = Required<CombinationActivityApi.CombinationActivityVO>
  148. /**
  149. * 活动表格选择对话框
  150. * 1. 单选模式:
  151. * 1.1 点击表格左侧的单选框时,结束选择,并关闭对话框
  152. * 1.2 再次打开时,保持选中状态
  153. * 2. 多选模式:
  154. * 2.1 点击表格左侧的多选框时,记录选中的活动
  155. * 2.2 切换分页时,保持活动的选中状态
  156. * 2.3 点击右下角的确定按钮时,结束选择,关闭对话框
  157. * 2.4 再次打开时,保持选中状态
  158. */
  159. defineOptions({ name: 'CombinationTableSelect' })
  160. defineProps({
  161. // 多选模式
  162. multiple: propTypes.bool.def(false)
  163. })
  164. // 列表的总页数
  165. const total = ref(0)
  166. // 列表的数据
  167. const list = ref<CombinationActivityVO[]>([])
  168. // 列表的加载中
  169. const loading = ref(false)
  170. // 弹窗的是否展示
  171. const dialogVisible = ref(false)
  172. // 查询参数
  173. const queryParams = ref({
  174. pageNo: 1,
  175. pageSize: 10,
  176. name: null,
  177. status: undefined
  178. })
  179. /** 打开弹窗 */
  180. const open = (CombinationList?: CombinationActivityVO[]) => {
  181. // 重置
  182. checkedActivitys.value = []
  183. checkedStatus.value = {}
  184. isCheckAll.value = false
  185. isIndeterminate.value = false
  186. // 处理已选中
  187. if (CombinationList && CombinationList.length > 0) {
  188. checkedActivitys.value = [...CombinationList]
  189. checkedStatus.value = Object.fromEntries(
  190. CombinationList.map((activityVO) => [activityVO.id, true])
  191. )
  192. }
  193. dialogVisible.value = true
  194. resetQuery()
  195. }
  196. // 提供 open 方法,用于打开弹窗
  197. defineExpose({ open })
  198. /** 查询列表 */
  199. const getList = async () => {
  200. loading.value = true
  201. try {
  202. const data = await CombinationActivityApi.getCombinationActivityPage(queryParams.value)
  203. list.value = data.list
  204. total.value = data.total
  205. // checkbox绑定undefined会有问题,需要给一个bool值
  206. list.value.forEach(
  207. (activityVO) =>
  208. (checkedStatus.value[activityVO.id] = checkedStatus.value[activityVO.id] || false)
  209. )
  210. // 计算全选框状态
  211. calculateIsCheckAll()
  212. } finally {
  213. loading.value = false
  214. }
  215. }
  216. /** 搜索按钮操作 */
  217. const handleQuery = () => {
  218. queryParams.value.pageNo = 1
  219. getList()
  220. }
  221. /** 重置按钮操作 */
  222. const resetQuery = () => {
  223. queryParams.value = {
  224. pageNo: 1,
  225. pageSize: 10,
  226. name: '',
  227. createTime: []
  228. }
  229. getList()
  230. }
  231. /**
  232. * 格式化拼团价格
  233. * @param products
  234. */
  235. const formatCombinationPrice = (products) => {
  236. const combinationPrice = Math.min(...products.map((item) => item.combinationPrice))
  237. return `¥${fenToYuan(combinationPrice)}`
  238. }
  239. // 是否全选
  240. const isCheckAll = ref(false)
  241. // 全选框是否处于中间状态:不是全部选中 && 任意一个选中
  242. const isIndeterminate = ref(false)
  243. // 选中的活动
  244. const checkedActivitys = ref<CombinationActivityVO[]>([])
  245. // 选中状态:key为活动ID,value为是否选中
  246. const checkedStatus = ref<Record<string, boolean>>({})
  247. // 选中的活动 activityId
  248. const selectedActivityId = ref()
  249. /** 单选中时触发 */
  250. const handleSingleSelected = (combinationActivityVO: CombinationActivityVO) => {
  251. emits(CHANGE_EVENT, combinationActivityVO)
  252. // 关闭弹窗
  253. dialogVisible.value = false
  254. // 记住上次选择的ID
  255. selectedActivityId.value = combinationActivityVO.id
  256. }
  257. /** 多选完成 */
  258. const handleEmitChange = () => {
  259. // 关闭弹窗
  260. dialogVisible.value = false
  261. emits(CHANGE_EVENT, [...checkedActivitys.value])
  262. }
  263. /** 确认选择时的触发事件 */
  264. const emits = defineEmits<{
  265. change: [CombinationActivityApi: CombinationActivityVO | CombinationActivityVO[] | any]
  266. }>()
  267. /** 全选/全不选 */
  268. const handleCheckAll = (checked: boolean) => {
  269. isCheckAll.value = checked
  270. isIndeterminate.value = false
  271. list.value.forEach((combinationActivity) => handleCheckOne(checked, combinationActivity, false))
  272. }
  273. /**
  274. * 选中一行
  275. * @param checked 是否选中
  276. * @param combinationActivity 活动
  277. * @param isCalcCheckAll 是否计算全选
  278. */
  279. const handleCheckOne = (
  280. checked: boolean,
  281. combinationActivity: CombinationActivityVO,
  282. isCalcCheckAll: boolean
  283. ) => {
  284. if (checked) {
  285. checkedActivitys.value.push(combinationActivity)
  286. checkedStatus.value[combinationActivity.id] = true
  287. } else {
  288. const index = findCheckedIndex(combinationActivity)
  289. if (index > -1) {
  290. checkedActivitys.value.splice(index, 1)
  291. checkedStatus.value[combinationActivity.id] = false
  292. isCheckAll.value = false
  293. }
  294. }
  295. // 计算全选框状态
  296. if (isCalcCheckAll) {
  297. calculateIsCheckAll()
  298. }
  299. }
  300. // 查找活动在已选中活动列表中的索引
  301. const findCheckedIndex = (activityVO: CombinationActivityVO) =>
  302. checkedActivitys.value.findIndex((item) => item.id === activityVO.id)
  303. // 计算全选框状态
  304. const calculateIsCheckAll = () => {
  305. isCheckAll.value = list.value.every((activityVO) => checkedStatus.value[activityVO.id])
  306. // 计算中间状态:不是全部选中 && 任意一个选中
  307. isIndeterminate.value =
  308. !isCheckAll.value && list.value.some((activityVO) => checkedStatus.value[activityVO.id])
  309. }
  310. // 分类列表
  311. const categoryList = ref()
  312. // 分类树
  313. const categoryTreeList = ref()
  314. /** 初始化 **/
  315. onMounted(async () => {
  316. await getList()
  317. // 获得分类树
  318. categoryList.value = await ProductCategoryApi.getCategoryList({})
  319. categoryTreeList.value = handleTree(categoryList.value, 'id', 'parentId')
  320. })
  321. </script>