index.vue 5.0 KB

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