AppLinkSelectDialog.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <Dialog v-model="dialogVisible" title="选择链接" width="65%">
  3. <div class="h-500px flex gap-8px">
  4. <!-- 左侧分组列表 -->
  5. <el-scrollbar wrap-class="h-full" ref="groupScrollbar" view-class="flex flex-col">
  6. <el-button
  7. v-for="(group, groupIndex) in APP_LINK_GROUP_LIST"
  8. :key="groupIndex"
  9. :class="[
  10. 'm-r-16px m-l-0px! justify-start! w-90px',
  11. { active: activeGroup === group.name }
  12. ]"
  13. ref="groupBtnRefs"
  14. :text="activeGroup !== group.name"
  15. :type="activeGroup === group.name ? 'primary' : 'default'"
  16. @click="handleGroupSelected(group.name)"
  17. >
  18. {{ group.name }}
  19. </el-button>
  20. </el-scrollbar>
  21. <!-- 右侧链接列表 -->
  22. <el-scrollbar class="h-full flex-1" @scroll="handleScroll" ref="linkScrollbar">
  23. <div v-for="(group, groupIndex) in APP_LINK_GROUP_LIST" :key="groupIndex">
  24. <!-- 分组标题 -->
  25. <div class="font-bold" ref="groupTitleRefs">{{ group.name }}</div>
  26. <!-- 链接列表 -->
  27. <el-tooltip
  28. v-for="(appLink, appLinkIndex) in group.links"
  29. :key="appLinkIndex"
  30. :content="appLink.path"
  31. placement="bottom"
  32. >
  33. <el-button
  34. class="m-b-8px m-r-8px m-l-0px!"
  35. :type="isSameLink(appLink.path, activeAppLink.path) ? 'primary' : 'default'"
  36. @click="handleAppLinkSelected(appLink)"
  37. >
  38. {{ appLink.name }}
  39. </el-button>
  40. </el-tooltip>
  41. </div>
  42. </el-scrollbar>
  43. </div>
  44. <!-- 底部对话框操作按钮 -->
  45. <template #footer>
  46. <el-button type="primary" @click="handleSubmit">确 定</el-button>
  47. <el-button @click="dialogVisible = false">取 消</el-button>
  48. </template>
  49. </Dialog>
  50. <Dialog v-model="detailSelectDialog.visible" title="" width="50%">
  51. <el-form class="min-h-200px">
  52. <el-form-item
  53. label="选择分类"
  54. v-if="detailSelectDialog.type === APP_LINK_TYPE_ENUM.PRODUCT_CATEGORY_LIST"
  55. >
  56. <ProductCategorySelect
  57. v-model="detailSelectDialog.id"
  58. :parent-id="0"
  59. @update:model-value="handleProductCategorySelected"
  60. />
  61. </el-form-item>
  62. </el-form>
  63. </Dialog>
  64. </template>
  65. <script lang="ts" setup>
  66. import { APP_LINK_GROUP_LIST, APP_LINK_TYPE_ENUM, AppLink } from './data'
  67. import { ButtonInstance, ScrollbarInstance } from 'element-plus'
  68. import { split } from 'lodash-es'
  69. import ProductCategorySelect from '@/views/mall/product/category/components/ProductCategorySelect.vue'
  70. import { getUrlNumberValue } from '@/utils'
  71. // APP 链接选择弹框
  72. defineOptions({ name: 'AppLinkSelectDialog' })
  73. // 选中的分组,默认选中第一个
  74. const activeGroup = ref(APP_LINK_GROUP_LIST[0].name)
  75. // 选中的 APP 链接
  76. const activeAppLink = ref({} as AppLink)
  77. /** 打开弹窗 */
  78. const dialogVisible = ref(false)
  79. const open = (link: string) => {
  80. activeAppLink.value.path = link
  81. dialogVisible.value = true
  82. // 滚动到当前的链接
  83. const group = APP_LINK_GROUP_LIST.find((group) =>
  84. group.links.some((linkItem) => {
  85. const sameLink = isSameLink(linkItem.path, link)
  86. if (sameLink) {
  87. activeAppLink.value = { ...linkItem, path: link }
  88. }
  89. return sameLink
  90. })
  91. )
  92. if (group) {
  93. // 使用 nextTick 的原因:可能 Dom 还没生成,导致滚动失败
  94. nextTick(() => handleGroupSelected(group.name))
  95. }
  96. }
  97. defineExpose({ open })
  98. // 处理 APP 链接选中
  99. const handleAppLinkSelected = (appLink: AppLink) => {
  100. if (!isSameLink(appLink.path, activeAppLink.value.path)) {
  101. activeAppLink.value = appLink
  102. }
  103. switch (appLink.type) {
  104. case APP_LINK_TYPE_ENUM.PRODUCT_CATEGORY_LIST:
  105. detailSelectDialog.value.visible = true
  106. detailSelectDialog.value.type = appLink.type
  107. // 返显
  108. detailSelectDialog.value.id =
  109. getUrlNumberValue('id', 'http://127.0.0.1' + activeAppLink.value.path) || undefined
  110. break
  111. default:
  112. break
  113. }
  114. }
  115. // 处理绑定值更新
  116. const emit = defineEmits<{
  117. change: [link: string]
  118. appLinkChange: [appLink: AppLink]
  119. }>()
  120. const handleSubmit = () => {
  121. dialogVisible.value = false
  122. emit('change', activeAppLink.value.path)
  123. emit('appLinkChange', activeAppLink.value)
  124. }
  125. // 分组标题引用列表
  126. const groupTitleRefs = ref<HTMLInputElement[]>([])
  127. /**
  128. * 处理右侧链接列表滚动
  129. * @param scrollTop 滚动条的位置
  130. */
  131. const handleScroll = ({ scrollTop }: { scrollTop: number }) => {
  132. const titleEl = groupTitleRefs.value.find((titleEl: HTMLInputElement) => {
  133. // 获取标题的位置信息
  134. const { offsetHeight, offsetTop } = titleEl
  135. // 判断标题是否在可视范围内
  136. return scrollTop >= offsetTop && scrollTop < offsetTop + offsetHeight
  137. })
  138. // 只需处理一次
  139. if (titleEl && activeGroup.value !== titleEl.textContent) {
  140. activeGroup.value = titleEl.textContent || ''
  141. // 同步左侧的滚动条位置
  142. scrollToGroupBtn(activeGroup.value)
  143. }
  144. }
  145. // 右侧滚动条
  146. const linkScrollbar = ref<ScrollbarInstance>()
  147. // 处理分组选中
  148. const handleGroupSelected = (group: string) => {
  149. activeGroup.value = group
  150. const titleRef = groupTitleRefs.value.find((item: HTMLInputElement) => item.textContent === group)
  151. if (titleRef) {
  152. // 滚动分组标题
  153. linkScrollbar.value?.setScrollTop(titleRef.offsetTop)
  154. }
  155. }
  156. // 分组滚动条
  157. const groupScrollbar = ref<ScrollbarInstance>()
  158. // 分组引用列表
  159. const groupBtnRefs = ref<ButtonInstance[]>([])
  160. // 自动滚动分组按钮,确保分组按钮保持在可视区域内
  161. const scrollToGroupBtn = (group: string) => {
  162. const groupBtn = groupBtnRefs.value
  163. .map((btn: ButtonInstance) => btn['ref'])
  164. .find((ref: Node) => ref.textContent === group)
  165. if (groupBtn) {
  166. groupScrollbar.value?.setScrollTop(groupBtn.offsetTop)
  167. }
  168. }
  169. // 是否为相同的链接(不比较参数,只比较链接)
  170. const isSameLink = (link1: string, link2: string) => {
  171. return split(link1, '?', 1)[0] === split(link2, '?', 1)[0]
  172. }
  173. // 详情选择对话框
  174. const detailSelectDialog = ref<{
  175. visible: boolean
  176. id?: number
  177. type?: APP_LINK_TYPE_ENUM
  178. }>({
  179. visible: false,
  180. id: undefined,
  181. type: undefined
  182. })
  183. // 处理详情选择
  184. const handleProductCategorySelected = (id: number) => {
  185. const url = new URL(activeAppLink.value.path, 'http://127.0.0.1')
  186. // 修改 id 参数
  187. url.searchParams.set('id', `${id}`)
  188. // 排除域名
  189. activeAppLink.value.path = `${url.pathname}${url.search}`
  190. // 关闭对话框
  191. detailSelectDialog.value.visible = false
  192. // 重置 id
  193. detailSelectDialog.value.id = undefined
  194. }
  195. </script>
  196. <style lang="scss" scoped></style>