address-picker.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <template>
  2. <view class="address-picker">
  3. <!-- 显示当前选中的地址(show-trigger=false 时由页面自己绘制入口行) -->
  4. <view v-if="showTrigger" class="current-address" @click="open">
  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. <block v-if="effectiveLevels >= 4">
  34. <text v-if="tempDistrict" class="crumb-arrow">›</text>
  35. <text
  36. :class="['crumb', currentLevel === 3 ? 'crumb-active' : '']"
  37. @click="jumpToLevel(3)"
  38. >{{ tempStreet || '街道' }}</text>
  39. </block>
  40. </view>
  41. <!-- 选择列表 -->
  42. <scroll-view class="picker-list" scroll-y>
  43. <view
  44. v-for="(item, idx) in currentItems"
  45. :key="idx"
  46. :class="['picker-item', isSelected(item) ? 'picker-item-selected' : '']"
  47. @click="selectItem(item)"
  48. >
  49. <text class="item-text">{{ getItemName(item) }}</text>
  50. <text v-if="isSelected(item)" class="item-check">✓</text>
  51. </view>
  52. <view v-if="loading" class="picker-loading">
  53. <text>加载中...</text>
  54. </view>
  55. </scroll-view>
  56. </view>
  57. </view>
  58. </view>
  59. </template>
  60. <script>
  61. import { getProvinces, getStreetChildren, getStreetPath } from '@/utils/api.js'
  62. export default {
  63. name: 'AddressPicker',
  64. props: {
  65. value: {
  66. type: Number,
  67. default: null
  68. },
  69. // 联动层级:3=省/市/区县(收货地址等,详细地址另行输入);4=省/市/区县/街道(默认)
  70. levels: {
  71. type: Number,
  72. default: 4
  73. },
  74. // 是否渲染组件自带的入口行;页面已有自己的表单行时传 false,通过 ref.open() 打开
  75. showTrigger: {
  76. type: Boolean,
  77. default: true
  78. },
  79. // 编辑回显用:{ province, city, district } 名称,打开面板时按名称回选
  80. initial: {
  81. type: Object,
  82. default: null
  83. }
  84. },
  85. data() {
  86. return {
  87. showPicker: false,
  88. currentLevel: 0, // 0=省份, 1=城市, 2=区县, 3=街道
  89. loading: false,
  90. // 数据列表
  91. provinces: [],
  92. cities: [],
  93. districts: [],
  94. streets: [],
  95. // 临时选中项
  96. tempProvince: null,
  97. tempCity: null,
  98. tempDistrict: null,
  99. tempStreet: null,
  100. // 完整地址文本
  101. currentAddress: ''
  102. }
  103. },
  104. computed: {
  105. // 支持 3 级(省市区)与 4 级(省市区街道),外部传入越界值时兜底
  106. effectiveLevels() {
  107. var n = Number(this.levels) || 4
  108. return Math.min(4, Math.max(3, n))
  109. },
  110. currentItems() {
  111. if (this.currentLevel === 0) return this.provinces
  112. if (this.currentLevel === 1) return this.cities
  113. if (this.currentLevel === 2) return this.districts
  114. if (this.currentLevel === 3) return this.streets
  115. return []
  116. }
  117. },
  118. watch: {
  119. value: {
  120. immediate: true,
  121. handler(newVal) {
  122. if (newVal) {
  123. this.loadAddressById(newVal)
  124. }
  125. }
  126. }
  127. },
  128. methods: {
  129. async loadProvinces() {
  130. this.loading = true
  131. try {
  132. const res = await getProvinces()
  133. this.provinces = res.data || []
  134. } catch (e) {
  135. console.error('加载省份失败', e)
  136. } finally {
  137. this.loading = false
  138. }
  139. },
  140. async loadCities(provinceId) {
  141. this.loading = true
  142. try {
  143. const res = await getStreetChildren(provinceId)
  144. this.cities = res.data || []
  145. } catch (e) {
  146. console.error('加载城市失败', e)
  147. } finally {
  148. this.loading = false
  149. }
  150. },
  151. async loadDistricts(cityId) {
  152. this.loading = true
  153. try {
  154. const res = await getStreetChildren(cityId)
  155. this.districts = res.data || []
  156. } catch (e) {
  157. console.error('加载区县失败', e)
  158. } finally {
  159. this.loading = false
  160. }
  161. },
  162. async loadStreets(districtId) {
  163. this.loading = true
  164. try {
  165. const res = await getStreetChildren(districtId)
  166. this.streets = res.data || []
  167. } catch (e) {
  168. console.error('加载街道失败', e)
  169. } finally {
  170. this.loading = false
  171. }
  172. },
  173. async loadAddressById(streetId) {
  174. try {
  175. // 必须用「完整路径」接口:/api/streets/{id}/path 返回 [省, 市, 区县, 街道]
  176. const res = await getStreetPath(streetId)
  177. const path = res.data || []
  178. if (path.length >= 3) {
  179. this.tempProvince = path[0]
  180. this.tempCity = path[1]
  181. this.tempDistrict = path[2]
  182. this.tempStreet = path[3] || null
  183. this.updateAddressText()
  184. }
  185. // 加载下级选项
  186. if (this.tempProvince) await this.loadCities(this.tempProvince.id)
  187. if (this.tempCity) await this.loadDistricts(this.tempCity.id)
  188. if (this.tempDistrict && this.effectiveLevels >= 4) await this.loadStreets(this.tempDistrict.id)
  189. } catch (e) {
  190. console.error('加载地址路径失败', e)
  191. }
  192. },
  193. // 对外入口:页面可用 ref.open() 打开面板(配合 show-trigger=false)
  194. async open() {
  195. this.showPicker = true
  196. if (this.provinces.length === 0) {
  197. await this.loadProvinces()
  198. }
  199. // 编辑回显:按传入的名称逐级回选
  200. if (this.initial && !this.tempProvince) {
  201. await this.applyInitial()
  202. }
  203. },
  204. // 兼容旧调用名
  205. openPicker() {
  206. return this.open()
  207. },
  208. async applyInitial() {
  209. var init = this.initial || {}
  210. if (!init.province) return
  211. var p = this.provinces.find((x) => this.getItemName(x) === init.province)
  212. if (!p) return
  213. this.tempProvince = p
  214. this.currentLevel = 1
  215. await this.loadCities(p.id)
  216. if (!init.city) return
  217. var c = this.cities.find((x) => this.getItemName(x) === init.city)
  218. if (!c) return
  219. this.tempCity = c
  220. this.currentLevel = 2
  221. await this.loadDistricts(c.id)
  222. if (!init.district) return
  223. var d = this.districts.find((x) => this.getItemName(x) === init.district)
  224. if (!d) return
  225. this.tempDistrict = d
  226. // 三级模式:停在区县列表,当前区县保持高亮,不再加载街道
  227. if (this.effectiveLevels <= 3) {
  228. this.currentLevel = 2
  229. return
  230. }
  231. this.currentLevel = 3
  232. await this.loadStreets(d.id)
  233. },
  234. closePicker() {
  235. this.showPicker = false
  236. },
  237. jumpToLevel(level) {
  238. if (level < this.currentLevel) {
  239. this.currentLevel = level
  240. }
  241. },
  242. selectItem(item) {
  243. if (this.currentLevel === 0) {
  244. this.tempProvince = item
  245. this.tempCity = null
  246. this.tempDistrict = null
  247. this.tempStreet = null
  248. this.cities = []
  249. this.districts = []
  250. this.streets = []
  251. this.currentLevel = 1
  252. this.loadCities(item.id)
  253. } else if (this.currentLevel === 1) {
  254. this.tempCity = item
  255. this.tempDistrict = null
  256. this.tempStreet = null
  257. this.districts = []
  258. this.streets = []
  259. this.currentLevel = 2
  260. this.loadDistricts(item.id)
  261. } else if (this.currentLevel === 2) {
  262. this.tempDistrict = item
  263. this.tempStreet = null
  264. this.streets = []
  265. if (this.effectiveLevels <= 3) {
  266. // 三级模式:选完区县即完成
  267. this.confirmAddress()
  268. } else {
  269. this.currentLevel = 3
  270. this.loadStreets(item.id)
  271. }
  272. } else if (this.currentLevel === 3) {
  273. this.tempStreet = item
  274. this.confirmAddress()
  275. }
  276. },
  277. confirmAddress() {
  278. // 三级模式必须选完整省/市/区县,避免只选省就确定把已存地区清空
  279. if (this.effectiveLevels <= 3) {
  280. if (!this.tempProvince || !this.tempCity || !this.tempDistrict) {
  281. uni.showToast({ title: '请选择省/市/区县', icon: 'none' })
  282. return
  283. }
  284. }
  285. this.updateAddressText()
  286. this.emitChange()
  287. this.closePicker()
  288. },
  289. updateAddressText() {
  290. var parts = []
  291. if (this.tempProvince) parts.push(this.tempProvince.province || this.tempProvince.name)
  292. if (this.tempCity) parts.push(this.tempCity.city || this.tempCity.name)
  293. if (this.tempDistrict) parts.push(this.tempDistrict.district || this.tempDistrict.name)
  294. if (this.effectiveLevels >= 4 && this.tempStreet) {
  295. parts.push(this.tempStreet.street || this.tempStreet.name)
  296. }
  297. this.currentAddress = parts.join('')
  298. },
  299. emitChange() {
  300. var hasStreet = this.effectiveLevels >= 4 && !!this.tempStreet
  301. const streetId = hasStreet ? this.tempStreet.id : null
  302. const addressData = {
  303. streetId: streetId,
  304. province: this.tempProvince ? this.tempProvince.province : null,
  305. city: this.tempCity ? this.tempCity.city : null,
  306. district: this.tempDistrict ? this.tempDistrict.district : null,
  307. street: hasStreet ? this.tempStreet.street : null,
  308. fullName: this.currentAddress
  309. }
  310. this.$emit('input', streetId)
  311. this.$emit('change', addressData)
  312. },
  313. getItemName(item) {
  314. if (!item) return ''
  315. return item.province || item.city || item.district || item.street || item.name || ''
  316. },
  317. isSelected(item) {
  318. var name = this.getItemName(item)
  319. if (this.currentLevel === 0) return this.tempProvince && this.getItemName(this.tempProvince) === name
  320. if (this.currentLevel === 1) return this.tempCity && this.getItemName(this.tempCity) === name
  321. if (this.currentLevel === 2) return this.tempDistrict && this.getItemName(this.tempDistrict) === name
  322. if (this.currentLevel === 3) return this.tempStreet && this.getItemName(this.tempStreet) === name
  323. return false
  324. },
  325. reset() {
  326. this.tempProvince = null
  327. this.tempCity = null
  328. this.tempDistrict = null
  329. this.tempStreet = null
  330. this.currentAddress = ''
  331. this.provinces = []
  332. this.cities = []
  333. this.districts = []
  334. this.streets = []
  335. this.currentLevel = 0
  336. this.emitChange()
  337. }
  338. }
  339. }
  340. </script>
  341. <stylescoped lang="scss">
  342. .address-picker {
  343. width: 100%;
  344. }
  345. .current-address {
  346. display: flex;
  347. align-items: center;
  348. justify-content: space-between;
  349. padding: 24rpx 20rpx;
  350. background: $facade-fill-page;
  351. border-radius: 12rpx;
  352. border: 2rpx solid $facade-hairline-2;
  353. }
  354. .address-text {
  355. font-size: 28rpx;
  356. color: $facade-text-strong;
  357. flex: 1;
  358. }
  359. .address-arrow {
  360. font-size: 32rpx;
  361. color: $facade-text-muted;
  362. margin-left: 10rpx;
  363. }
  364. /* 面板遮罩 */
  365. .picker-mask {
  366. position: fixed;
  367. top: 0;
  368. left: 0;
  369. right: 0;
  370. bottom: 0;
  371. background: rgba(0, 0, 0, 0.5);
  372. z-index: 999;
  373. display: flex;
  374. align-items: flex-end;
  375. }
  376. .picker-panel {
  377. width: 100%;
  378. background: $app-surface;
  379. border-radius: 24rpx 24rpx 0 0;
  380. max-height: 70vh;
  381. display: flex;
  382. flex-direction: column;
  383. }
  384. /* 面板头部 */
  385. .picker-header {
  386. display: flex;
  387. align-items: center;
  388. justify-content: space-between;
  389. padding: 30rpx 30rpx 20rpx;
  390. border-bottom: 1px solid $facade-hairline-2;
  391. }
  392. .picker-btn {
  393. font-size: 28rpx;
  394. color: $facade-text-body;
  395. padding: 10rpx 20rpx;
  396. }
  397. .picker-confirm {
  398. color: $brand-primary;
  399. font-weight: bold;
  400. }
  401. .picker-title {
  402. font-size: 32rpx;
  403. font-weight: bold;
  404. color: $facade-text-strong;
  405. }
  406. /* 面包屑导航 */
  407. .picker-breadcrumb {
  408. display: flex;
  409. align-items: center;
  410. padding: 20rpx 30rpx;
  411. background: $facade-fill-ghost;
  412. border-bottom: 1px solid $facade-hairline-2;
  413. flex-wrap: wrap;
  414. }
  415. .crumb {
  416. font-size: 26rpx;
  417. color: $facade-text-body;
  418. padding: 8rpx 16rpx;
  419. background: $facade-hairline-2;
  420. border-radius: 20rpx;
  421. }
  422. .crumb-active {
  423. background: $brand-primary;
  424. color: $app-surface;
  425. }
  426. .crumb-arrow {
  427. font-size: 28rpx;
  428. color: $facade-text-muted;
  429. margin: 0 8rpx;
  430. }
  431. /* 选择列表 */
  432. .picker-list {
  433. flex: 1;
  434. min-height: 300rpx;
  435. max-height: 500rpx;
  436. padding: 20rpx 0;
  437. }
  438. .picker-item {
  439. display: flex;
  440. align-items: center;
  441. justify-content: space-between;
  442. padding: 28rpx 40rpx;
  443. border-bottom: 1px solid $facade-fill-2;
  444. }
  445. .item-text {
  446. font-size: 30rpx;
  447. color: $facade-text-strong;
  448. }
  449. .item-check {
  450. font-size: 32rpx;
  451. color: $brand-primary;
  452. font-weight: bold;
  453. }
  454. .picker-item-selected .item-text {
  455. color: $brand-primary;
  456. font-weight: bold;
  457. }
  458. .picker-loading {
  459. padding: 40rpx;
  460. text-align: center;
  461. color: $facade-text-muted;
  462. font-size: 28rpx;
  463. }
  464. </style>