LocationPicker-v2.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <!-- 编辑模式 -->
  3. <view v-if="mode === 'edit'" class="location-picker-wrapper">
  4. <!-- 显示输入框 -->
  5. <view class="input-display" @click="showPicker = true">
  6. <text class="input-text" :class="{ placeholder: !displayLabel }">
  7. {{ displayLabel || placeholder }}
  8. </text>
  9. <view class="suffix-icons">
  10. <view
  11. v-if="innerValue && displayLabel"
  12. @click.stop="clearAddress"
  13. class="clear-btn"
  14. >
  15. <text class="clear-icon">×</text>
  16. </view>
  17. <text class="arrow-icon">></text>
  18. </view>
  19. </view>
  20. <!-- 选择器弹窗 -->
  21. <uni-data-picker
  22. v-model="innerValue"
  23. :localdata="localData"
  24. :popup-title="popupTitle"
  25. :show="showPicker"
  26. @change="onChange"
  27. @popupopened="onPopupOpened"
  28. @popupclosed="onPopupClosed"
  29. />
  30. </view>
  31. <!-- 展示模式:纯文本 -->
  32. <text v-else class="location-text">
  33. {{ displayLabel || "无" }}
  34. </text>
  35. </template>
  36. <script setup>
  37. import { ref, watch, computed } from 'vue'
  38. import cityRows from '@/utils/data.json'
  39. // Props definition
  40. const props = defineProps({
  41. mode: {
  42. type: String,
  43. default: "edit"
  44. },
  45. modelValue: {
  46. type: [String, Number],
  47. default: ""
  48. },
  49. placeholder: {
  50. type: String,
  51. default: "请选择省市区"
  52. },
  53. popupTitle: {
  54. type: String,
  55. default: "请选择省市区"
  56. }
  57. })
  58. // Emits definition
  59. const emit = defineEmits(['update:modelValue', 'clear'])
  60. // Reactive data
  61. const innerValue = ref(props.modelValue)
  62. const localData = ref([])
  63. const displayLabel = ref('')
  64. const showPicker = ref(false)
  65. // Watchers
  66. watch(() => props.modelValue, (newVal) => {
  67. console.log('[LocationPicker] props.modelValue 变化:', newVal)
  68. innerValue.value = newVal
  69. updateDisplayLabel(newVal)
  70. })
  71. watch(innerValue, (newVal) => {
  72. console.log('[LocationPicker] innerValue 变化:', newVal)
  73. emit("update:modelValue", newVal)
  74. })
  75. // Methods
  76. const onPopupOpened = () => {
  77. console.log('[LocationPicker] 弹窗打开')
  78. }
  79. const onPopupClosed = () => {
  80. console.log('[LocationPicker] 弹窗关闭')
  81. showPicker.value = false
  82. }
  83. const onChange = (e) => {
  84. console.log('[LocationPicker] onChange 事件:', e)
  85. if (e && e.detail && e.detail.value && e.detail.value.length > 0) {
  86. const selectedNodes = e.detail.value
  87. const lastNode = selectedNodes[selectedNodes.length - 1]
  88. console.log('[LocationPicker] 选择的节点:', selectedNodes)
  89. console.log('[LocationPicker] 最后节点:', lastNode)
  90. // 更新值
  91. innerValue.value = lastNode.value
  92. // 构建显示文本
  93. const pathLabels = selectedNodes.map(node => node.text)
  94. displayLabel.value = pathLabels.join(' - ')
  95. console.log('[LocationPicker] 更新后 - code:', lastNode.value, 'label:', displayLabel.value)
  96. // 关闭弹窗
  97. showPicker.value = false
  98. }
  99. }
  100. const clearAddress = () => {
  101. console.log('[LocationPicker] 清空地址')
  102. innerValue.value = ""
  103. displayLabel.value = ""
  104. emit("update:modelValue", "")
  105. emit("clear")
  106. }
  107. const updateDisplayLabel = (value) => {
  108. if (!value) {
  109. displayLabel.value = ""
  110. return
  111. }
  112. const label = getLocationLabel(value)
  113. displayLabel.value = label
  114. console.log('[LocationPicker] 更新显示标签:', label)
  115. }
  116. const getLocationLabel = (value) => {
  117. if (!value) return ""
  118. let label = ""
  119. const traverse = (nodes, path = []) => {
  120. for (const node of nodes) {
  121. const currentPath = [...path, node.text]
  122. // 使用 == 而不是 === 以支持字符串和数字比较
  123. if (node.value == value) {
  124. label = currentPath.join(' - ')
  125. return true
  126. }
  127. if (node.children && node.children.length > 0) {
  128. if (traverse(node.children, currentPath)) {
  129. return true
  130. }
  131. }
  132. }
  133. return false
  134. }
  135. traverse(localData.value)
  136. return label
  137. }
  138. const get_city_tree = () => {
  139. if (!cityRows || cityRows.length === 0) {
  140. console.warn('[LocationPicker] cityRows 数据为空')
  141. return []
  142. }
  143. return handleTree(cityRows)
  144. }
  145. const handleTree = (data, parent_code = null) => {
  146. let res = []
  147. let keys = {
  148. id: "code",
  149. pid: "parent_code",
  150. children: "children",
  151. text: "name",
  152. value: "code"
  153. }
  154. for (let item of data) {
  155. if (parent_code === null) {
  156. if (!item.hasOwnProperty(keys.pid) || item[keys.pid] == parent_code) {
  157. let node = {
  158. text: item[keys.text],
  159. value: item[keys.value],
  160. children: handleTree(data, item[keys.id])
  161. }
  162. res.push(node)
  163. }
  164. } else {
  165. if (item.hasOwnProperty(keys.pid) && item[keys.pid] == parent_code) {
  166. let node = {
  167. text: item[keys.text],
  168. value: item[keys.value],
  169. children: handleTree(data, item[keys.id])
  170. }
  171. res.push(node)
  172. }
  173. }
  174. }
  175. return res
  176. }
  177. // Initialize
  178. localData.value = get_city_tree()
  179. console.log('[LocationPicker] 初始化 - localData 长度:', localData.value.length)
  180. console.log('[LocationPicker] 初始化 - modelValue:', props.modelValue)
  181. updateDisplayLabel(props.modelValue)
  182. </script>
  183. <style scoped>
  184. .location-picker-wrapper {
  185. width: 100%;
  186. }
  187. .input-display {
  188. display: flex;
  189. align-items: center;
  190. justify-content: space-between;
  191. width: 100%;
  192. min-height: 44rpx;
  193. padding: 12rpx 0;
  194. border-bottom: 1rpx solid #f0f0f0;
  195. }
  196. .input-text {
  197. flex: 1;
  198. font-size: 28rpx;
  199. color: #333;
  200. line-height: 44rpx;
  201. }
  202. .input-text.placeholder {
  203. color: #999;
  204. }
  205. .suffix-icons {
  206. display: flex;
  207. align-items: center;
  208. gap: 8rpx;
  209. flex-shrink: 0;
  210. }
  211. .clear-btn {
  212. display: flex;
  213. align-items: center;
  214. justify-content: center;
  215. width: 32rpx;
  216. height: 32rpx;
  217. }
  218. .clear-icon {
  219. font-size: 32rpx;
  220. color: #999;
  221. line-height: 1;
  222. }
  223. .arrow-icon {
  224. font-size: 24rpx;
  225. color: #999;
  226. }
  227. .location-text {
  228. font-size: 28rpx;
  229. color: #333;
  230. }
  231. </style>