cityPicker.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <view class="main" @touchmove.stop.prevent>
  3. <view class="zhezao" v-if="isShow" @tap.stop="isClose_city"></view>
  4. <view class="picker" v-if="isShow">
  5. <view class="title">
  6. <view class="cancel" @tap.stop="isClose_city">取消</view>
  7. <view class="text">选择省市区街道</view>
  8. <view class="comfim" @tap="confim_city">确定</view>
  9. </view>
  10. <view class="tab">
  11. <text v-if="province && level >= 1" :class="selectTabIndex == 1 ? 'active' : ''" @tap="selectTap(1)">{{
  12. province
  13. }}</text>
  14. <text v-if="province_id && level >= 2" :class="selectTabIndex == 2 ? 'active' : ''" @tap="selectTap(2)">{{
  15. city
  16. }}</text>
  17. <text v-if="city_id && level >= 3" :class="selectTabIndex == 3 ? 'active' : ''" @tap="selectTap(3)">{{
  18. area
  19. }}</text>
  20. <text
  21. v-if="area_id && level >= 4"
  22. style="width: 200rpx"
  23. :class="selectTabIndex == 4 ? 'active' : ''"
  24. @tap="selectTap(4)"
  25. >{{ street }}</text
  26. >
  27. </view>
  28. <view class="title1">选择省市区街道</view>
  29. <scroll-view scroll-y="true" class="scroll-Y" style="max-height: 800rpx">
  30. <view class="list" v-for="(item, index) in dataList" :key="index">
  31. <view class="item" @tap="selectItem(item)">
  32. <text>{{ item.name }}</text>
  33. </view>
  34. </view>
  35. </scroll-view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. props: {
  42. dataList: {
  43. type: Array,
  44. default: () => []
  45. },
  46. level: {
  47. type: Number,
  48. default: 4
  49. },
  50. isShow: false
  51. },
  52. data() {
  53. return {
  54. selectTabIndex: 1, // 1==省 2=市 3=区 4=街道
  55. province: '选择省', //省
  56. province_id: '', // 省id
  57. city: '选择市', // 城市
  58. city_id: '', //城市id
  59. area: '选择区', // 区
  60. area_id: '', // 区id
  61. street: '选择街道', // 街道
  62. street_id: '' // 街道id
  63. }
  64. },
  65. onLoad() {},
  66. methods: {
  67. selectTap(index) {
  68. this.selectTabIndex = index
  69. if (index == 1) {
  70. this.$emit('getCityData', '')
  71. } else if (index == 2) {
  72. this.$emit('getCityData', this.province_id)
  73. } else if (index == 3) {
  74. this.$emit('getCityData', this.city_id)
  75. } else if (index == 4) {
  76. this.$emit('getCityData', this.area_id)
  77. }
  78. },
  79. selectItem(item) {
  80. console.log(item)
  81. if (this.selectTabIndex == 1) {
  82. if (this.province != item.name) {
  83. this.city_id = ''
  84. this.city = '选择市'
  85. this.area_id = ''
  86. this.area = '选择区'
  87. this.street_id = ''
  88. this.street = '选择街道'
  89. }
  90. this.province_id = item.id
  91. this.province = item.name
  92. if (this.selectTabIndex >= this.level) return false
  93. this.selectTabIndex = 2
  94. this.$emit('getCityData', item.id)
  95. } else if (this.selectTabIndex == 2) {
  96. if (this.city != item.name) {
  97. this.area_id = ''
  98. this.area = '选择区'
  99. this.street_id = ''
  100. this.street = '选择街道'
  101. }
  102. this.city_id = item.id
  103. this.city = item.name
  104. if (this.selectTabIndex >= this.level) return false
  105. this.selectTabIndex = 3
  106. this.$emit('getCityData', item.id)
  107. } else if (this.selectTabIndex == 3) {
  108. if (this.area != item.name) {
  109. this.street_id = ''
  110. this.street = '选择街道'
  111. }
  112. this.area_id = item.id
  113. this.area = item.name
  114. if (this.selectTabIndex >= this.level) return false
  115. this.selectTabIndex = 4
  116. this.$emit('getCityData', item.id)
  117. } else if (this.selectTabIndex == 4) {
  118. this.street_id = item.id
  119. this.street = item.name
  120. }
  121. },
  122. // searchList(){
  123. // this.$emit("getProductType",this.keyword)
  124. // },
  125. isClose_city() {
  126. this.$emit('isClose_city', false)
  127. },
  128. confim_city() {
  129. if (!this.province_id && this.level >= 1) {
  130. uni.showToast({
  131. title: '请选择省',
  132. icon: 'none'
  133. })
  134. return
  135. } else if (!this.city_id && this.level >= 2) {
  136. uni.showToast({
  137. title: '请选择市',
  138. icon: 'none'
  139. })
  140. return
  141. } else if (!this.area_id && this.level >= 3) {
  142. uni.showToast({
  143. title: '请选择区',
  144. icon: 'none'
  145. })
  146. return
  147. } else if (!this.street_id && this.level >= 4) {
  148. uni.showToast({
  149. title: '请选择街道',
  150. icon: 'none'
  151. })
  152. return
  153. } else {
  154. const obj = {
  155. province: this.province, //省
  156. province_id: this.province_id, // 省id
  157. city: this.city, // 城市
  158. city_id: this.city_id, //城市id
  159. area: this.area, // 区
  160. area_id: this.area_id, // 区id
  161. street: this.street, // 街道
  162. street_id: this.street_id // 街道id
  163. }
  164. this.$emit('confim_city', obj)
  165. this.isClose_city()
  166. }
  167. }
  168. }
  169. }
  170. </script>
  171. <style scoped lang="scss">
  172. .main {
  173. width: 100%;
  174. height: 100vh;
  175. position: fixed;
  176. left: 0;
  177. top: 0;
  178. z-index: 1000;
  179. .zhezao {
  180. width: 100%;
  181. height: 100%;
  182. background: rgba($color: #000000, $alpha: 0.5);
  183. }
  184. .title {
  185. display: flex;
  186. justify-content: space-between;
  187. align-items: center;
  188. font-weight: 700;
  189. margin-bottom: 36rpx;
  190. }
  191. .title1 {
  192. margin-top: 16rpx;
  193. font-size: 28rpx;
  194. color: #999999;
  195. margin-bottom: 20rpx;
  196. }
  197. .tab {
  198. width: 100%;
  199. height: 100rpx;
  200. display: flex;
  201. align-items: center;
  202. border-top: 1rpx solid #eeeeee;
  203. border-bottom: 1rpx solid #eeeeee;
  204. text {
  205. width: 150rpx;
  206. text-align: center;
  207. }
  208. .active {
  209. color: #007fff;
  210. }
  211. }
  212. .picker {
  213. width: 100%;
  214. // min-height: 800rpx;
  215. height: 1100rpx;
  216. position: absolute;
  217. left: 0;
  218. bottom: 0;
  219. background: #ffffff;
  220. z-index: 11;
  221. border-radius: 40rpx 40rpx 0 0;
  222. padding: 40rpx 28rpx 10rpx;
  223. font-size: 32rpx;
  224. color: #333333;
  225. padding-bottom: 100rpx;
  226. box-sizing: border-box;
  227. .list {
  228. font-size: 28rpx;
  229. padding-left: 22rpx;
  230. .item {
  231. width: 100%;
  232. height: 68rpx;
  233. display: flex;
  234. justify-content: space-between;
  235. align-items: center;
  236. image {
  237. width: 24rpx;
  238. height: 24rpx;
  239. }
  240. }
  241. .active {
  242. color: #007fff;
  243. }
  244. }
  245. }
  246. }
  247. </style>