uni-areaCascadePicker.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <view class="addressPicker">
  3. <u-popup :show="modelshow" @close="close" round="20rpx" bgColor="#fff" mode="bottom"
  4. :closeOnClickOverlay="true">
  5. <view class="">
  6. <view class="header header-padding dis a-c j-c borderBottom">
  7. <text>选择地区</text>
  8. </view>
  9. <view class="content">
  10. <view class="quickOptions">
  11. <picker-view :indicator-style="indicatorStyle" :value="value" @change="monthbindChange"
  12. class="picker-view">
  13. <picker-view-column>
  14. <view class="item dis a-c j-c " v-for="(item,index) in columns[0]" :key="index">
  15. {{item.name}}
  16. </view>
  17. </picker-view-column>
  18. <picker-view-column>
  19. <view class="item dis a-c j-c " v-for="(item,index) in columns[1]" :key="index">
  20. {{item.name}}
  21. </view>
  22. </picker-view-column>
  23. <picker-view-column>
  24. <view class="item dis a-c j-c " v-for="(item,index) in columns[2]" :key="index">
  25. {{item.name}}
  26. </view>
  27. </picker-view-column>
  28. </picker-view>
  29. <view class="custom-footer dis j-s a-c ">
  30. <view class="dis a-c j-c" @tap="statusConfirm">确定</view>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. </u-popup>
  36. <!-- <u-picker :show="shows" ref="uPicker" :defaultIndex="defaultIndex" :columns="columns" keyName="name"
  37. @change="changeHandler" @cancel="cancel" @confirm="confirm" @close="close"
  38. :closeOnClickOverlay="true"></u-picker> -->
  39. </view>
  40. </template>
  41. <script>
  42. export default {
  43. props: {
  44. show: Boolean,
  45. defaultIndex: {
  46. type: Array,
  47. default: () => {
  48. return [0, 0, 0]
  49. },
  50. },
  51. defaultCodes: { // 改为接收标准6位码数组
  52. type: Array,
  53. default: () => ['110000', '110100', '110101'] // 默认北京东城区
  54. }
  55. },
  56. data() {
  57. return {
  58. modelshow: this.show,
  59. columns: [
  60. [],
  61. [],
  62. []
  63. ], // 省/市/区三级数据
  64. fullData: [], // 包含完整结构的缓存数据
  65. value: [0, 0, 0],
  66. lastValue: [0, 0, 0], // 保存上一次的选中值
  67. indicatorStyle: `height: 41px;`
  68. }
  69. },
  70. watch: {
  71. show(val) {
  72. this.modelshow = val
  73. if (val) this.initByCodes(this.defaultCodes)
  74. },
  75. defaultCodes: {
  76. immediate: true,
  77. handler(codes) {
  78. this.initByCodes(codes)
  79. }
  80. }
  81. },
  82. async created() {
  83. await this.areaQueryTree();
  84. },
  85. methods: {
  86. // 接口获取省市区树(替代静态 address.json),返回数据直接交给 fullData
  87. async areaQueryTree() {
  88. let res = await this.$http.post('/area/queryTree');
  89. if (res.code == 200) {
  90. // res.data 可能是数组(多个省份),也可能单棵树,统一转为数组
  91. let list = Array.isArray(res.data) ? res.data : (res.data?.child || []);
  92. this.fullData = list.map(p => this.mapArea(p)).filter(Boolean);
  93. this.columns[0] = this.fullData.map(p => ({
  94. name: p.name,
  95. code: p.code
  96. }));
  97. this.initDefaultSelection();
  98. }
  99. },
  100. //月份滚动事件
  101. monthbindChange: function(e) {
  102. const val = e.detail.value; // 当前选中下标数组 [省索引, 市索引, 区索引]
  103. const lastVal = this.lastValue;
  104. let provinceIndex = val[0];
  105. let cityIndex = val[1];
  106. let areaIndex = val[2] || 0;
  107. // 判断是否是省份变化
  108. if (provinceIndex !== lastVal[0]) {
  109. if (this.fullData[provinceIndex]) {
  110. const cities = this.fullData[provinceIndex].children || [];
  111. this.columns[1] = cities.map(c => ({
  112. name: c.name,
  113. code: c.code
  114. }));
  115. // 重置城市索引到第一个
  116. cityIndex = 0;
  117. // 更新区级数据
  118. if (cities[0]) {
  119. const areas = cities[0].children || [];
  120. this.columns[2] = areas.map(a => ({
  121. name: a.name,
  122. code: a.code
  123. }));
  124. areaIndex = 0;
  125. } else {
  126. this.columns[2] = [];
  127. }
  128. }
  129. }
  130. // 判断是否是城市变化
  131. else if (cityIndex !== lastVal[1]) {
  132. if (this.fullData[provinceIndex]?.children?.[cityIndex]) {
  133. const areas = this.fullData[provinceIndex].children[cityIndex].children || [];
  134. this.columns[2] = areas.map(a => ({
  135. name: a.name,
  136. code: a.code
  137. }));
  138. areaIndex = 0;
  139. } else {
  140. this.columns[2] = [];
  141. }
  142. }
  143. // 保存当前值
  144. this.lastValue = [provinceIndex, cityIndex, areaIndex];
  145. // 使用nextTick延迟更新value,避免阻塞用户滑动
  146. this.$nextTick(() => {
  147. this.value = [provinceIndex, cityIndex, areaIndex];
  148. });
  149. },
  150. initDefaultSelection() {
  151. if (this.defaultCodes && this.defaultCodes.length >= 3) {
  152. // 如果传入了defaultCodes,优先使用
  153. this.initByCodes(this.defaultCodes);
  154. } else {
  155. // 否则自动选择第一个省+第一个市+第一个区
  156. const firstProvince = this.fullData[0];
  157. const firstCity = firstProvince.children?.[0];
  158. const firstArea = firstCity?.children?.[0];
  159. this.columns[1] = firstProvince.children?.map(c => ({
  160. name: c.name,
  161. code: c.code
  162. })) || [];
  163. this.columns[2] = firstCity?.children?.map(a => ({
  164. name: a.name,
  165. code: a.code
  166. })) || [];
  167. // 设置Picker选中状态
  168. this.$nextTick(() => {
  169. this.$refs.uPicker?.setIndexs([0, 0, 0]);
  170. });
  171. }
  172. },
  173. // 根据6位码初始化选中状态
  174. initByCodes(codes) {
  175. if (!codes || codes.length < 3) return
  176. // 1. 查找省级索引
  177. const provIndex = this.fullData.findIndex(
  178. p => p.code === codes[0]
  179. )
  180. if (provIndex === -1) return
  181. // 2. 设置市级数据
  182. const cities = this.fullData[provIndex].children
  183. this.columns[1] = cities.map(c => ({
  184. name: c.name,
  185. code: c.code
  186. }))
  187. // 3. 查找市级索引
  188. const cityIndex = cities.findIndex(
  189. c => c.code === codes[1]
  190. )
  191. if (cityIndex === -1) return
  192. // 4. 设置区级数据
  193. const areas = cities[cityIndex].children
  194. this.columns[2] = areas.map(a => ({
  195. name: a.name,
  196. code: a.code
  197. }))
  198. },
  199. // 确保6位行政区划码
  200. ensure6DigitCode(code) {
  201. if (!code) return ''
  202. if (code.length === 2) return code + '0000'
  203. if (code.length === 4) return code + '00'
  204. return code // 6位或9位码保持不变
  205. },
  206. // 接口数据(areaCode/areaCname/child) → 组件结构(code/name/children),code 统一补全6位
  207. mapArea(node) {
  208. if (!node) return null
  209. return {
  210. name: node.areaCname,
  211. code: this.ensure6DigitCode(node.areaCode),
  212. children: (node.child || []).map(c => this.mapArea(c)).filter(Boolean)
  213. }
  214. },
  215. // 选择变化事件(适配6位码)
  216. changeHandler(e) {
  217. const {
  218. columnIndex,
  219. index
  220. } = e
  221. if (columnIndex === 0) {
  222. // 省份变化
  223. const cities = this.fullData[index].children
  224. this.columns[1] = cities.map(c => ({
  225. name: c.name,
  226. code: c.code
  227. }))
  228. this.columns[2] = cities[0]?.children.map(a => ({
  229. name: a.name,
  230. code: a.code
  231. })) || []
  232. this.$refs.uPicker.setColumnValues(1, this.columns[1])
  233. this.$refs.uPicker.setColumnValues(2, this.columns[2])
  234. } else if (columnIndex === 1) {
  235. // 城市变化
  236. const areas = this.fullData[e.indexs[0]].children[index].children
  237. this.columns[2] = areas.map(a => ({
  238. name: a.name,
  239. code: a.code
  240. }))
  241. this.$refs.uPicker.setColumnValues(2, this.columns[2])
  242. }
  243. },
  244. statusConfirm() {
  245. const provinceIndex = this.value[0] || 0;
  246. const cityIndex = this.value[1] || 0;
  247. const areaIndex = this.value[2] || 0;
  248. const province = this.columns[0][provinceIndex];
  249. const city = this.columns[1][cityIndex];
  250. const area = this.columns[2][areaIndex];
  251. this.$emit('confirm', {
  252. names: [province?.name, city?.name, area?.name].filter(Boolean),
  253. codes: [province?.code, city?.code, area?.code].filter(Boolean),
  254. fullAddress: [province?.name, city?.name, area?.name].filter(Boolean).join('-')
  255. });
  256. this.close();
  257. },
  258. cancel() {
  259. this.$emit('cancel')
  260. },
  261. close() {
  262. this.$emit('close')
  263. },
  264. confirm(e) {
  265. const selected = e.value
  266. this.$emit('confirm', {
  267. names: selected.map(i => i.name),
  268. codes: selected.map(i => i.code), // 标准6位码
  269. fullAddress: selected.map(i => i.name).join('-')
  270. })
  271. }
  272. }
  273. }
  274. </script>
  275. <style lang="scss" scoped>
  276. .header {
  277. color: #333;
  278. font-size: 32rpx;
  279. font-weight: bold;
  280. }
  281. .header-padding {
  282. padding: 27rpx 30rpx;
  283. }
  284. .borderBottom {
  285. border-bottom: 1px solid #EEEEEE;
  286. }
  287. .quickOptions {
  288. padding: 32rpx;
  289. box-sizing: border-box;
  290. margin-bottom: 20rpx;
  291. }
  292. .custom-footer {
  293. padding: 40rpx 0 0;
  294. box-sizing: border-box;
  295. view {
  296. font-size: 30rpx;
  297. padding: 20rpx;
  298. flex: 1;
  299. }
  300. view:first-child {
  301. color: #333;
  302. background: transparent;
  303. border-radius: 50rpx;
  304. border: 1rpx solid #FDE935;
  305. margin-right: 28rpx;
  306. }
  307. view:last-child {
  308. background: #FDE935;
  309. border-radius: 50rpx;
  310. color: #1F1F1F;
  311. }
  312. }
  313. .picker-view {
  314. width: 100%;
  315. height: 400rpx;
  316. .item {
  317. font-size: 30rpx;
  318. color: #333;
  319. }
  320. }
  321. </style>