HySelectPopup.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view class="select-container">
  3. <!-- 下拉选择触发区域 -->
  4. <view class="custom-select" @tap="toggleDropdown">
  5. <text class="select-text">{{ selectedOptionText || placeholder }}</text>
  6. <image class="select-icon" :class="{ rotate: dropdownVisible }" :src="minioUrl + '/icon_bottom_arrow.png'" mode="aspectFill" />
  7. </view>
  8. <!-- 弹窗形式的下拉菜单 -->
  9. <u-popup v-model="dropdownVisible" mode="center" border-radius="12" :mask-close-able="true" @close="closeDropdown">
  10. <view class="popup-menu">
  11. <view class="popup-header">
  12. <text class="popup-title">选择服务区</text>
  13. <u-icon name="close" color="#999" size="32" @tap="closeDropdown"></u-icon>
  14. </view>
  15. <view class="popup-content">
  16. <view
  17. class="dropdown-item"
  18. v-for="item in parkList"
  19. :key="item.id"
  20. :class="{ selected: selectedValue === item.id }"
  21. @tap="selectItem(item)"
  22. >
  23. <text>{{ item.name }}</text>
  24. <u-icon v-if="selectedValue === item.id" name="checkmark" color="#2979ff" size="32"></u-icon>
  25. </view>
  26. </view>
  27. </view>
  28. </u-popup>
  29. </view>
  30. </template>
  31. <script setup lang="ts">
  32. import { ref, computed, watch } from 'vue'
  33. import { getParkWebAPI } from '@/api/system'
  34. import { useGlobal } from '@/composables/index'
  35. const { minioUrl } = useGlobal()
  36. // 定义组件接收的props
  37. const props = defineProps({
  38. // 当前选中的值
  39. modelValue: {
  40. type: String,
  41. default: ''
  42. },
  43. // 占位符文本
  44. placeholder: {
  45. type: String,
  46. default: '请选择'
  47. }
  48. })
  49. // 定义组件触发的事件
  50. const emit = defineEmits(['update:modelValue', 'change'])
  51. // 内部状态
  52. const dropdownVisible = ref(false)
  53. const selectedValue = ref(props.modelValue)
  54. const parkList = ref([]) as any
  55. // 计算当前选中的选项文本
  56. const selectedOptionText = computed(() => {
  57. const selected = parkList.value.find((item: any) => item.id === selectedValue.value)
  58. return selected ? selected.name : ''
  59. })
  60. // 获取全部园区楼宇
  61. const getParkWeb = async () => {
  62. const res = await getParkWebAPI()
  63. if (res.code === 200) {
  64. parkList.value = res.rows
  65. // 当数据加载完成且没有选中值时,默认选择第一项
  66. if (parkList.value.length > 0 && !selectedValue.value) {
  67. const firstItem = parkList.value[0]
  68. selectedValue.value = firstItem.id
  69. // 存储到本地缓存
  70. uni.setStorageSync('parkId', firstItem.id)
  71. // 触发事件通知父组件
  72. emit('update:modelValue', firstItem.id)
  73. emit('change', firstItem)
  74. }
  75. }
  76. }
  77. getParkWeb()
  78. // 监听modelValue变化,同步内部状态
  79. watch(
  80. () => props.modelValue,
  81. (newValue) => {
  82. selectedValue.value = newValue
  83. }
  84. )
  85. // 切换下拉菜单显示/隐藏
  86. const toggleDropdown = () => {
  87. dropdownVisible.value = !dropdownVisible.value
  88. }
  89. // 关闭下拉菜单
  90. const closeDropdown = () => {
  91. dropdownVisible.value = false
  92. }
  93. // 选择选项
  94. const selectItem = (item: { id: string; name: string }) => {
  95. // 存储到本地缓存
  96. uni.setStorageSync('parkId', item.id)
  97. selectedValue.value = item.id
  98. dropdownVisible.value = false
  99. // 触发事件通知父组件
  100. emit('update:modelValue', item.id)
  101. emit('change', item)
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. .select-container {
  106. position: relative;
  107. z-index: 100;
  108. .custom-select {
  109. width: 100%;
  110. height: 60rpx;
  111. display: flex;
  112. align-items: center;
  113. justify-content: space-between;
  114. // padding: 0 20rpx;
  115. cursor: pointer;
  116. transition: background-color 0.2s;
  117. .select-text {
  118. font-size: 30rpx;
  119. color: #3b3c3b;
  120. font-weight: 600;
  121. overflow: hidden;
  122. text-overflow: ellipsis;
  123. white-space: nowrap;
  124. flex: 1;
  125. }
  126. .select-icon {
  127. width: 13rpx;
  128. height: 10rpx;
  129. margin-left: 10rpx;
  130. transition: transform 0.2s;
  131. }
  132. .select-icon.rotate {
  133. transform: rotate(180deg);
  134. }
  135. }
  136. }
  137. /* 弹窗样式 */
  138. .popup-menu {
  139. width: 600rpx;
  140. background-color: #fff;
  141. border-radius: 12rpx;
  142. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
  143. overflow: hidden;
  144. .popup-header {
  145. height: 80rpx;
  146. padding: 0 24rpx;
  147. display: flex;
  148. align-items: center;
  149. justify-content: space-between;
  150. border-bottom: 1rpx solid #f0f0f0;
  151. .popup-title {
  152. font-size: 32rpx;
  153. color: #333;
  154. font-weight: 600;
  155. }
  156. }
  157. .popup-content {
  158. max-height: 500rpx;
  159. overflow-y: auto;
  160. padding: 15rpx;
  161. width: 100%;
  162. .dropdown-item {
  163. height: 80rpx;
  164. padding: 0 24rpx;
  165. display: flex;
  166. align-items: center;
  167. justify-content: space-between;
  168. cursor: pointer;
  169. transition: background-color 0.2s;
  170. &:last-child {
  171. border-bottom: none;
  172. }
  173. &.selected {
  174. background-color: #f0f7ff;
  175. color: #007aff;
  176. border-radius: 8rpx;
  177. text {
  178. color: #007aff;
  179. font-weight: 500;
  180. }
  181. }
  182. text {
  183. font-size: 26rpx;
  184. color: #1d2129;
  185. }
  186. .check-icon {
  187. width: 32rpx;
  188. height: 32rpx;
  189. }
  190. }
  191. }
  192. }
  193. </style>