address-picker.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <view class="address-picker">
  3. <!-- 显示当前选中的地址 -->
  4. <view class="current-address" @click="openPicker">
  5. <text class="address-text">{{ currentAddress || '请选择地址' }}</text>
  6. <text class="address-arrow">›</text>
  7. </view>
  8. <!-- 地址选择面板 -->
  9. <view class="picker-mask" v-if="showPicker" @click="closePicker">
  10. <view class="picker-panel" @click.stop>
  11. <!-- 面板头部 -->
  12. <view class="picker-header">
  13. <text class="picker-btn" @click="closePicker">取消</text>
  14. <text class="picker-title">选择地址</text>
  15. <text class="picker-btn picker-confirm" @click="confirmAddress">确定</text>
  16. </view>
  17. <!-- 面包屑导航 -->
  18. <view class="picker-breadcrumb">
  19. <text
  20. :class="['crumb', currentLevel === 0 ? 'crumb-active' : '']"
  21. @click="jumpToLevel(0)"
  22. >{{ tempProvince || '省份' }}</text>
  23. <text v-if="tempProvince" class="crumb-arrow">›</text>
  24. <text
  25. :class="['crumb', currentLevel === 1 ? 'crumb-active' : '']"
  26. @click="jumpToLevel(1)"
  27. >{{ tempCity || '城市' }}</text>
  28. <text v-if="tempCity" class="crumb-arrow">›</text>
  29. <text
  30. :class="['crumb', currentLevel === 2 ? 'crumb-active' : '']"
  31. @click="jumpToLevel(2)"
  32. >{{ tempDistrict || '区县' }}</text>
  33. <text v-if="tempDistrict" class="crumb-arrow">›</text>
  34. <text
  35. :class="['crumb', currentLevel === 3 ? 'crumb-active' : '']"
  36. @click="jumpToLevel(3)"
  37. >{{ tempStreet || '街道' }}</text>
  38. </view>
  39. <!-- 选择列表 -->
  40. <scroll-view class="picker-list" scroll-y>
  41. <view
  42. v-for="(item, idx) in currentItems"
  43. :key="idx"
  44. :class="['picker-item', isSelected(item) ? 'picker-item-selected' : '']"
  45. @click="selectItem(item)"
  46. >
  47. <text class="item-text">{{ getItemName(item) }}</text>
  48. <text v-if="isSelected(item)" class="item-check">✓</text>
  49. </view>
  50. <view v-if="loading" class="picker-loading">
  51. <text>加载中...</text>
  52. </view>
  53. </scroll-view>
  54. </view>
  55. </view>
  56. </view>
  57. </template>
  58. <script>
  59. import { getProvinces, getStreetChildren } from '@/utils/api.js'
  60. export default {
  61. name: 'AddressPicker',
  62. props: {
  63. value: {
  64. type: Number,
  65. default: null
  66. }
  67. },
  68. data() {
  69. return {
  70. showPicker: false,
  71. currentLevel: 0, // 0=省份, 1=城市, 2=区县, 3=街道
  72. loading: false,
  73. // 数据列表
  74. provinces: [],
  75. cities: [],
  76. districts: [],
  77. streets: [],
  78. // 临时选中项
  79. tempProvince: null,
  80. tempCity: null,
  81. tempDistrict: null,
  82. tempStreet: null,
  83. // 完整地址文本
  84. currentAddress: ''
  85. }
  86. },
  87. computed: {
  88. currentItems() {
  89. if (this.currentLevel === 0) return this.provinces
  90. if (this.currentLevel === 1) return this.cities
  91. if (this.currentLevel === 2) return this.districts
  92. if (this.currentLevel === 3) return this.streets
  93. return []
  94. }
  95. },
  96. watch: {
  97. value: {
  98. immediate: true,
  99. handler(newVal) {
  100. if (newVal) {
  101. this.loadAddressById(newVal)
  102. }
  103. }
  104. }
  105. },
  106. mounted() {
  107. this.loadProvinces()
  108. },
  109. methods: {
  110. async loadProvinces() {
  111. try {
  112. const res = await getProvinces()
  113. this.provinces = res.data || []
  114. } catch (e) {
  115. console.error('加载省份失败', e)
  116. }
  117. },
  118. async loadCities(provinceId) {
  119. try {
  120. const res = await getStreetChildren(provinceId)
  121. this.cities = res.data || []
  122. } catch (e) {
  123. console.error('加载城市失败', e)
  124. }
  125. },
  126. async loadDistricts(cityId) {
  127. try {
  128. const res = await getStreetChildren(cityId)
  129. this.districts = res.data || []
  130. } catch (e) {
  131. console.error('加载区县失败', e)
  132. }
  133. },
  134. async loadStreets(districtId) {
  135. try {
  136. const res = await getStreetChildren(districtId)
  137. this.streets = res.data || []
  138. } catch (e) {
  139. console.error('加载街道失败', e)
  140. }
  141. },
  142. async loadAddressById(streetId) {
  143. try {
  144. const res = await getStreetChildren(streetId)
  145. const path = res.data || []
  146. if (path.length >= 4) {
  147. this.tempProvince = path[0]
  148. this.tempCity = path[1]
  149. this.tempDistrict = path[2]
  150. this.tempStreet = path[3]
  151. this.currentAddress = path[3].fullName || this.buildAddress(path)
  152. }
  153. // 加载下级选项
  154. if (this.tempProvince) await this.loadCities(this.tempProvince.id)
  155. if (this.tempCity) await this.loadDistricts(this.tempCity.id)
  156. if (this.tempDistrict) await this.loadStreets(this.tempDistrict.id)
  157. } catch (e) {
  158. console.error('加载地址路径失败', e)
  159. }
  160. },
  161. buildAddress(path) {
  162. if (!path || path.length === 0) return ''
  163. var parts = []
  164. if (path[0]) parts.push(path[0].province || path[0].name)
  165. if (path[1]) parts.push(path[1].city || path[1].name)
  166. if (path[2]) parts.push(path[2].district || path[2].name)
  167. if (path[3]) parts.push(path[3].street || path[3].name)
  168. return parts.join('')
  169. },
  170. openPicker() {
  171. this.showPicker = true
  172. if (!this.tempProvince && this.provinces.length === 0) {
  173. this.loadProvinces()
  174. }
  175. },
  176. closePicker() {
  177. this.showPicker = false
  178. },
  179. jumpToLevel(level) {
  180. if (level < this.currentLevel) {
  181. this.currentLevel = level
  182. }
  183. },
  184. selectItem(item) {
  185. if (this.currentLevel === 0) {
  186. this.tempProvince = item
  187. this.tempCity = null
  188. this.tempDistrict = null
  189. this.tempStreet = null
  190. this.cities = []
  191. this.districts = []
  192. this.streets = []
  193. this.currentLevel = 1
  194. this.loadCities(item.id)
  195. } else if (this.currentLevel === 1) {
  196. this.tempCity = item
  197. this.tempDistrict = null
  198. this.tempStreet = null
  199. this.districts = []
  200. this.streets = []
  201. this.currentLevel = 2
  202. this.loadDistricts(item.id)
  203. } else if (this.currentLevel === 2) {
  204. this.tempDistrict = item
  205. this.tempStreet = null
  206. this.streets = []
  207. this.currentLevel = 3
  208. this.loadStreets(item.id)
  209. } else if (this.currentLevel === 3) {
  210. this.tempStreet = item
  211. this.confirmAddress()
  212. }
  213. },
  214. confirmAddress() {
  215. this.updateAddressText()
  216. this.emitChange()
  217. this.closePicker()
  218. },
  219. updateAddressText() {
  220. var parts = []
  221. if (this.tempProvince) parts.push(this.tempProvince.province || this.tempProvince.name)
  222. if (this.tempCity) parts.push(this.tempCity.city || this.tempCity.name)
  223. if (this.tempDistrict) parts.push(this.tempDistrict.district || this.tempDistrict.name)
  224. if (this.tempStreet) parts.push(this.tempStreet.street || this.tempStreet.name)
  225. this.currentAddress = parts.join('')
  226. },
  227. emitChange() {
  228. const streetId = this.tempStreet ? this.tempStreet.id : null
  229. const addressData = {
  230. streetId: streetId,
  231. province: this.tempProvince ? this.tempProvince.province : null,
  232. city: this.tempCity ? this.tempCity.city : null,
  233. district: this.tempDistrict ? this.tempDistrict.district : null,
  234. street: this.tempStreet ? this.tempStreet.street : null,
  235. fullName: this.currentAddress
  236. }
  237. this.$emit('input', streetId)
  238. this.$emit('change', addressData)
  239. },
  240. getItemName(item) {
  241. if (!item) return ''
  242. return item.province || item.city || item.district || item.street || item.name || ''
  243. },
  244. isSelected(item) {
  245. var name = this.getItemName(item)
  246. if (this.currentLevel === 0) return this.tempProvince && this.getItemName(this.tempProvince) === name
  247. if (this.currentLevel === 1) return this.tempCity && this.getItemName(this.tempCity) === name
  248. if (this.currentLevel === 2) return this.tempDistrict && this.getItemName(this.tempDistrict) === name
  249. if (this.currentLevel === 3) return this.tempStreet && this.getItemName(this.tempStreet) === name
  250. return false
  251. },
  252. reset() {
  253. this.tempProvince = null
  254. this.tempCity = null
  255. this.tempDistrict = null
  256. this.tempStreet = null
  257. this.currentAddress = ''
  258. this.provinces = []
  259. this.cities = []
  260. this.districts = []
  261. this.streets = []
  262. this.currentLevel = 0
  263. this.emitChange()
  264. }
  265. }
  266. }
  267. </script>
  268. <style scoped>
  269. .address-picker {
  270. width: 100%;
  271. }
  272. .current-address {
  273. display: flex;
  274. align-items: center;
  275. justify-content: space-between;
  276. padding: 24rpx 20rpx;
  277. background: #f8f8f8;
  278. border-radius: 12rpx;
  279. border: 2rpx solid #eee;
  280. }
  281. .address-text {
  282. font-size: 28rpx;
  283. color: #333;
  284. flex: 1;
  285. }
  286. .address-arrow {
  287. font-size: 32rpx;
  288. color: #999;
  289. margin-left: 10rpx;
  290. }
  291. /* 面板遮罩 */
  292. .picker-mask {
  293. position: fixed;
  294. top: 0;
  295. left: 0;
  296. right: 0;
  297. bottom: 0;
  298. background: rgba(0, 0, 0, 0.5);
  299. z-index: 999;
  300. display: flex;
  301. align-items: flex-end;
  302. }
  303. .picker-panel {
  304. width: 100%;
  305. background: #fff;
  306. border-radius: 24rpx 24rpx 0 0;
  307. max-height: 70vh;
  308. display: flex;
  309. flex-direction: column;
  310. }
  311. /* 面板头部 */
  312. .picker-header {
  313. display: flex;
  314. align-items: center;
  315. justify-content: space-between;
  316. padding: 30rpx 30rpx 20rpx;
  317. border-bottom: 1px solid #eee;
  318. }
  319. .picker-btn {
  320. font-size: 28rpx;
  321. color: #666;
  322. padding: 10rpx 20rpx;
  323. }
  324. .picker-confirm {
  325. color: #f97316;
  326. font-weight: bold;
  327. }
  328. .picker-title {
  329. font-size: 32rpx;
  330. font-weight: bold;
  331. color: #333;
  332. }
  333. /* 面包屑导航 */
  334. .picker-breadcrumb {
  335. display: flex;
  336. align-items: center;
  337. padding: 20rpx 30rpx;
  338. background: #fafafa;
  339. border-bottom: 1px solid #eee;
  340. flex-wrap: wrap;
  341. }
  342. .crumb {
  343. font-size: 26rpx;
  344. color: #666;
  345. padding: 8rpx 16rpx;
  346. background: #eee;
  347. border-radius: 20rpx;
  348. }
  349. .crumb-active {
  350. background: #f97316;
  351. color: #fff;
  352. }
  353. .crumb-arrow {
  354. font-size: 28rpx;
  355. color: #999;
  356. margin: 0 8rpx;
  357. }
  358. /* 选择列表 */
  359. .picker-list {
  360. flex: 1;
  361. min-height: 300rpx;
  362. max-height: 500rpx;
  363. padding: 20rpx 0;
  364. }
  365. .picker-item {
  366. display: flex;
  367. align-items: center;
  368. justify-content: space-between;
  369. padding: 28rpx 40rpx;
  370. border-bottom: 1px solid #f5f5f5;
  371. }
  372. .item-text {
  373. font-size: 30rpx;
  374. color: #333;
  375. }
  376. .item-check {
  377. font-size: 32rpx;
  378. color: #f97316;
  379. font-weight: bold;
  380. }
  381. .picker-item-selected .item-text {
  382. color: #f97316;
  383. font-weight: bold;
  384. }
  385. .picker-loading {
  386. padding: 40rpx;
  387. text-align: center;
  388. color: #999;
  389. font-size: 28rpx;
  390. }
  391. </style>