index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <template>
  2. <u-popup :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="value" length="auto"
  3. :safeAreaInsetBottom="safeAreaInsetBottom" @close="close" z-index="999">
  4. <view class="u-picker-header" @touchmove.stop.prevent="stop" catchtouchmove="stop">
  5. <view class="u-btn-picker u-btn-picker--tips" hover-class="u-opacity"
  6. :hover-stay-time="150" @tap="getResult('cancel')">取消</view>
  7. <view class="u-btn-picker u-btn-picker--primary" hover-class="u-opacity"
  8. :hover-stay-time="150" @touchmove.stop="" @tap.stop="getResult('confirm')">确定</view>
  9. </view>
  10. <view class="u-picker-body">
  11. <picker-view :value="pickVal" @change="bindChange" class="u-picker-view">
  12. <picker-view-column>
  13. <view class="u-column-item" v-for="(item,index) in districtsObj.provinces" :key="index">
  14. <view class="u-line-1">
  15. {{item.AreaName}}
  16. </view>
  17. </view>
  18. </picker-view-column>
  19. <picker-view-column>
  20. <view class="u-column-item" v-for="(item,index) in districtsObj.cities" :key="index">
  21. <view class="u-line-1">
  22. {{item.AreaName}}
  23. </view>
  24. </view>
  25. </picker-view-column>
  26. <picker-view-column>
  27. <view class="u-column-item" v-for="(item,index) in districtsObj.areas" :key="index">
  28. <view class="u-line-1">
  29. {{item.AreaName}}
  30. </view>
  31. </view>
  32. </picker-view-column>
  33. </picker-view>
  34. </view>
  35. </u-popup>
  36. </template>
  37. <script>
  38. import uPopup from './u-popup'
  39. export default {
  40. props: {
  41. safeAreaInsetBottom: {
  42. type: Boolean,
  43. default: false
  44. },
  45. // 是否允许通过点击遮罩关闭Picker
  46. maskCloseAble: {
  47. type: Boolean,
  48. default: true
  49. },
  50. // 通过双向绑定控制组件的弹出与收起
  51. value: {
  52. type: Boolean,
  53. default: false
  54. },
  55. },
  56. data() {
  57. return {
  58. pickVal:[0, 0, 0],
  59. districtsObj: {
  60. provinces: [],
  61. cities: [],
  62. areas: [],
  63. },
  64. province: 0,
  65. city: 0,
  66. area: 0
  67. }
  68. },
  69. watch: {
  70. // 如果地区发生变化,为了让picker联动起来,必须重置this.citys和this.areas
  71. province(val) {
  72. this.loadCities(this.districtsObj.provinces[this.province].AreaId);
  73. },
  74. city(val) {
  75. this.loadAreas(this.districtsObj.cities[this.city].AreaId);
  76. }
  77. },
  78. mounted() {
  79. this.loadDistrict()
  80. },
  81. methods: {
  82. close() {
  83. this.$emit('input', false);
  84. },
  85. async loadDistrict() {
  86. this.loadProvinces()
  87. },
  88. loadProvinces() { // 加载省份
  89. uni.request({
  90. url: 'http://test-api.tiananhub.com/api/province/GetListProvince',
  91. method: 'get',
  92. success: async (res) => {
  93. let {data} = res.data
  94. this.districtsObj.provinces = data
  95. this.loadCities(data[0].AreaId)
  96. },
  97. fail:async(res) => {
  98. }
  99. })
  100. },
  101. loadCities(AreaId) {
  102. uni.request({
  103. url: 'http://test-api.tiananhub.com/api/province/GetListCity',
  104. data: {
  105. AreaId
  106. },
  107. method: 'get',
  108. success: async (res) => {
  109. let {data} = res.data
  110. this.districtsObj.cities = data
  111. if(data[0]){this.loadAreas(data[0].AreaId)}
  112. },
  113. fail:async(res) => {
  114. }
  115. })
  116. },
  117. loadAreas(AreaId) {
  118. uni.request({
  119. url: 'http://test-api.tiananhub.com/api/province/GetListCity',
  120. data: {
  121. AreaId
  122. },
  123. method: 'get',
  124. success: async (res) => {
  125. let {data} = res.data
  126. this.districtsObj.areas = data
  127. },
  128. fail:async(res) => {
  129. }
  130. })
  131. },
  132. bindChange(event) {
  133. this.pickVal = event.detail.value;
  134. let i = 0;
  135. this.province = this.pickVal[i++];
  136. this.city = this.pickVal[i++];
  137. this.area = this.pickVal[i++];
  138. },
  139. getResult(event = null) {
  140. let result = {
  141. province: this.districtsObj.provinces[this.province],
  142. city: this.districtsObj.cities[this.city],
  143. area: this.districtsObj.areas[this.area]|| new Object(),
  144. }
  145. if (event) this.$emit(event, result);
  146. this.close();
  147. }
  148. },
  149. components:{
  150. uPopup
  151. }
  152. }
  153. </script>
  154. <style lang="scss" scoped>
  155. .u-datetime-picker {
  156. position: relative;
  157. z-index: 999;
  158. }
  159. .u-picker-view {
  160. height: 100%;
  161. box-sizing: border-box;
  162. }
  163. .u-picker-header {
  164. width: 100%;
  165. height: 90rpx;
  166. padding: 0 40rpx;
  167. display: flex;
  168. justify-content: space-between;
  169. align-items: center;
  170. box-sizing: border-box;
  171. font-size: 32rpx;
  172. background: #ddd;
  173. position: relative;
  174. }
  175. .u-picker-header::after {
  176. content: '';
  177. position: absolute;
  178. border-bottom: 1rpx solid #eaeef1;
  179. -webkit-transform: scaleY(0.5);
  180. transform: scaleY(0.5);
  181. bottom: 0;
  182. right: 0;
  183. left: 0;
  184. }
  185. .u-picker-body {
  186. width: 100%;
  187. height: 500rpx;
  188. overflow: hidden;
  189. background-color: #fff;
  190. }
  191. .u-column-item {
  192. display: flex;
  193. align-items: center;
  194. justify-content: center;
  195. font-size: 32rpx;
  196. padding: 0 8rpx;
  197. }
  198. .u-text {
  199. font-size: 24rpx;
  200. padding-left: 8rpx;
  201. }
  202. .u-btn-picker {
  203. padding: 16rpx;
  204. box-sizing: border-box;
  205. text-align: center;
  206. text-decoration: none;
  207. }
  208. .u-opacity {
  209. opacity: 0.5;
  210. }
  211. .u-btn-picker--primary {
  212. }
  213. .u-btn-picker--tips {
  214. }
  215. </style>