index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.name}}
  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.name}}
  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. var md5 = require("../../common/md5.js");
  40. export default {
  41. props: {
  42. safeAreaInsetBottom: {
  43. type: Boolean,
  44. default: false
  45. },
  46. // 是否允许通过点击遮罩关闭Picker
  47. maskCloseAble: {
  48. type: Boolean,
  49. default: true
  50. },
  51. // 通过双向绑定控制组件的弹出与收起
  52. value: {
  53. type: Boolean,
  54. default: false
  55. },
  56. },
  57. data() {
  58. return {
  59. pickVal:[0, 0],
  60. districtsObj: {
  61. provinces: [],
  62. cities: [],
  63. // areas: [],
  64. },
  65. province: 0,
  66. city: 0,
  67. // area: 0
  68. }
  69. },
  70. watch: {
  71. // 如果地区发生变化,为了让picker联动起来,必须重置this.citys和this.areas
  72. province(val) {
  73. this.loadCities(this.districtsObj.provinces[this.province].id);
  74. },
  75. city(val) {
  76. this.loadAreas(this.districtsObj.cities[this.city].id);
  77. }
  78. },
  79. mounted() {
  80. this.loadDistrict()
  81. },
  82. methods: {
  83. close() {
  84. this.$emit('input', false);
  85. },
  86. async loadDistrict() {
  87. this.loadProvinces()
  88. },
  89. loadProvinces() { // 加载省份
  90. // uni.request({
  91. // url: 'http://test-api.tiananhub.com/api/province/GetListProvince',
  92. // method: 'get',
  93. // success: async (res) => {
  94. // let {data} = res.data
  95. // this.districtsObj.provinces = data
  96. // this.loadCities(data[0].AreaId)
  97. // },
  98. // fail:async(res) => {
  99. // }
  100. // })
  101. console.log('province')
  102. let that = this;
  103. uni.request({
  104. url:getApp().globalData.shareUrl, //需要设置为全局
  105. method: 'POST',
  106. header: {
  107. 'content-type': 'application/x-www-form-urlencoded'
  108. },
  109. data: {
  110. method: 'province',
  111. timestamp: getApp().globalData.globalTimestamp, //Date.now()
  112. sign: md5('province' + getApp().globalData.globalTimestamp),
  113. },
  114. success: async res => {
  115. //that.provinceArray = res.data.msg;
  116. that.districtsObj.provinces = res.data.msg
  117. that.loadCities(that.districtsObj.provinces[0].id)
  118. }
  119. });
  120. },
  121. loadCities(AreaId) {
  122. // uni.request({
  123. // url: 'http://test-api.tiananhub.com/api/province/GetListCity',
  124. // data: {
  125. // AreaId
  126. // },
  127. // method: 'get',
  128. // success: async (res) => {
  129. // let {data} = res.data
  130. // this.districtsObj.cities = data
  131. // this.loadAreas(data[0].AreaId)
  132. // },
  133. // fail:async(res) => {
  134. // }
  135. // })
  136. let that = this;
  137. uni.request({
  138. url:getApp().globalData.shareUrl, //需要设置为全局
  139. method: 'POST',
  140. header: {
  141. 'content-type': 'application/x-www-form-urlencoded'
  142. },
  143. data: {
  144. method: 'city',
  145. timestamp: getApp().globalData.globalTimestamp, //Date.now()
  146. sign: md5('city' + getApp().globalData.globalTimestamp),
  147. pid:AreaId
  148. },
  149. success: async res => {
  150. that.districtsObj.cities = res.data.msg
  151. }
  152. });
  153. },
  154. loadAreas(AreaId) {
  155. uni.request({
  156. url: 'http://test-api.tiananhub.com/api/province/GetListCity',
  157. data: {
  158. AreaId
  159. },
  160. method: 'get',
  161. success: async (res) => {
  162. let {data} = res.data
  163. this.districtsObj.areas = data
  164. },
  165. fail:async(res) => {
  166. }
  167. })
  168. },
  169. bindChange(event) {
  170. this.pickVal = event.detail.value;
  171. let i = 0;
  172. this.province = this.pickVal[i++];
  173. this.city = this.pickVal[i++];
  174. //this.area = this.pickVal[i++];
  175. },
  176. getResult(event = null) {
  177. let result = {
  178. province: this.districtsObj.provinces[this.province],
  179. city: this.districtsObj.cities[this.city],
  180. //area: this.districtsObj.areas[this.area],
  181. }
  182. if (event) this.$emit(event, result);
  183. this.close();
  184. }
  185. },
  186. components:{
  187. uPopup
  188. }
  189. }
  190. </script>
  191. <style lang="scss" scoped>
  192. .u-datetime-picker {
  193. position: relative;
  194. z-index: 999;
  195. }
  196. .u-picker-view {
  197. height: 100%;
  198. box-sizing: border-box;
  199. }
  200. .u-picker-header {
  201. width: 100%;
  202. height: 90rpx;
  203. padding: 0 40rpx;
  204. display: flex;
  205. justify-content: space-between;
  206. align-items: center;
  207. box-sizing: border-box;
  208. font-size: 32rpx;
  209. background: #ddd;
  210. position: relative;
  211. }
  212. .u-picker-header::after {
  213. content: '';
  214. position: absolute;
  215. border-bottom: 1rpx solid #eaeef1;
  216. -webkit-transform: scaleY(0.5);
  217. transform: scaleY(0.5);
  218. bottom: 0;
  219. right: 0;
  220. left: 0;
  221. }
  222. .u-picker-body {
  223. width: 100%;
  224. height: 500rpx;
  225. overflow: hidden;
  226. background-color: #fff;
  227. }
  228. .u-column-item {
  229. display: flex;
  230. align-items: center;
  231. justify-content: center;
  232. font-size: 32rpx;
  233. padding: 0 8rpx;
  234. }
  235. .u-text {
  236. font-size: 24rpx;
  237. padding-left: 8rpx;
  238. }
  239. .u-btn-picker {
  240. padding: 16rpx;
  241. box-sizing: border-box;
  242. text-align: center;
  243. text-decoration: none;
  244. }
  245. .u-opacity {
  246. opacity: 0.5;
  247. }
  248. .u-btn-picker--primary {
  249. }
  250. .u-btn-picker--tips {
  251. }
  252. </style>