area-picker.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <view>
  3. <u-popup :show="areaShow" @close="cancel">
  4. <view class="flex warp">
  5. <view class="warp-left" @click="cancel"> 取消 </view>
  6. <view class="warp-right" @click="setArea"> 确定 </view>
  7. </view>
  8. <picker-view style="height: 500rpx; text-align: center; line-height: 68rpx;" :value="value" @change="bindChange">
  9. <picker-view-column>
  10. <view v-for="(item,index) in provinceRes" :key="index">{{item.areaName}}</view>
  11. </picker-view-column>
  12. <picker-view-column>
  13. <view v-for="(item,index) in cityRes" :key="index">{{item.areaName}}</view>
  14. </picker-view-column>
  15. <picker-view-column>
  16. <view v-for="(item,index) in areaRes" :key="index">{{item.areaName}}</view>
  17. </picker-view-column>
  18. </picker-view>
  19. </u-popup>
  20. </view>
  21. </template>
  22. <script>
  23. import $http from '@/utils/request.js'
  24. export default {
  25. name: "area-picker",
  26. props: {
  27. areaShow: {
  28. type: [Boolean],
  29. default: false
  30. },
  31. },
  32. data() {
  33. return {
  34. value: [0,0,0],//所选下标
  35. provinceRes: [],//省
  36. cityRes: [],//市
  37. areaRes: [],//区
  38. };
  39. },
  40. mounted() {
  41. this.getArea()
  42. },
  43. methods: {
  44. //切换
  45. async bindChange(e){
  46. if(e.detail.value[0] != this.value[0]){
  47. let cityRes = await $http.post('/api/v1/mp/area/listByPid', {
  48. pid: this.provinceRes[e.detail.value[0]].areaId
  49. })
  50. let areaRes = await $http.post('/api/v1/mp/area/listByPid', {
  51. pid: cityRes && cityRes.data && cityRes.data[0].areaId
  52. })
  53. this.cityRes = cityRes.data
  54. this.areaRes = areaRes.data
  55. this.value = [e.detail.value[0],0,0]
  56. return
  57. }
  58. if(e.detail.value[1] != this.value[1]){
  59. let areaRes = await $http.post('/api/v1/mp/area/listByPid', {
  60. pid: this.cityRes[e.detail.value[1]].areaId
  61. })
  62. this.areaRes = areaRes.data
  63. this.value = [e.detail.value[0],e.detail.value[1],0]
  64. return
  65. }else{
  66. this.value = e.detail.value
  67. return
  68. }
  69. },
  70. //确定修改
  71. setArea() {
  72. let confirmObj = {
  73. province: this.provinceRes[this.value[0]].areaName,
  74. provinceId: this.provinceRes[this.value[0]].areaId,
  75. city: this.cityRes[this.value[1]].areaName,
  76. cityId:this.cityRes[this.value[1]].areaId,
  77. area: this.areaRes[this.value[2]].areaName,
  78. areaId: this.areaRes[this.value[2]].areaId,
  79. cityShow: `${ this.provinceRes[this.value[0]].areaName }-${ this.cityRes[this.value[1]].areaName }-${ this.areaRes[this.value[2]].areaName }`
  80. }
  81. this.$emit('confirmArea', confirmObj)
  82. },
  83. //初始加载
  84. async getArea() {
  85. let provinceRes = await $http.post('/api/v1/mp/area/listByPid', {
  86. pid: 0
  87. })
  88. let cityRes = await $http.post('/api/v1/mp/area/listByPid', {
  89. pid: provinceRes && provinceRes.data && provinceRes.data[0].areaId
  90. })
  91. let areaRes = await $http.post('/api/v1/mp/area/listByPid', {
  92. pid: cityRes && cityRes.data && cityRes.data[0].areaId
  93. })
  94. this.provinceRes = provinceRes.data
  95. this.cityRes = cityRes.data
  96. this.areaRes = areaRes.data
  97. },
  98. cancel() {
  99. this.$emit('cancel')
  100. },
  101. },
  102. }
  103. </script>
  104. <style lang="scss">
  105. .warp {
  106. justify-content: space-between;
  107. padding: 15px;
  108. &-left {
  109. color: #909193;
  110. font-size: 30rpx;
  111. }
  112. &-right {
  113. color: #3c9cff;
  114. font-size: 30rpx;
  115. }
  116. }
  117. </style>