selectCity.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <view class="">
  3. <u-popup
  4. :show="show"
  5. mode="bottom"
  6. @close="$emit('close')"
  7. :round="10"
  8. :closeable="true"
  9. @open="open"
  10. >
  11. <view
  12. style="
  13. height: 100rpx;
  14. line-height: 100rpx;
  15. text-align: center;
  16. font-size: 36rpx;
  17. color: #222;
  18. "
  19. >
  20. 选择省市区
  21. </view>
  22. <view>
  23. <input type="text" class="inp" disabled />
  24. <view class="title"> 热门城市 </view>
  25. <view class="city-list">
  26. <view class="city" v-for="(item, index) in hotCityList" :key="index">
  27. {{ item }}
  28. </view>
  29. </view>
  30. <picker-view
  31. class="picker-view"
  32. :indicator-style="indicatorStyle"
  33. :value="value"
  34. @change="bindChange"
  35. :mask-top-style="maskTopStyle"
  36. :mask-bottom-style="maskBottomStyle"
  37. >
  38. <picker-view-column class="picker-view-column">
  39. <view class="item" v-for="(item, index) in cityList" :key="index"
  40. ><text class="text">{{ item.name }}</text></view
  41. >
  42. </picker-view-column>
  43. <picker-view-column class="picker-view-column">
  44. <view class="item" v-for="(item, index) in marketList" :key="index"
  45. ><text class="text">{{ item.name }}</text>
  46. </view>
  47. </picker-view-column>
  48. <picker-view-column class="picker-view-column">
  49. <view
  50. class="item"
  51. v-for="(item, index) in distinguishList"
  52. :key="index"
  53. ><text class="text">{{ item.name }}</text>
  54. </view>
  55. </picker-view-column>
  56. </picker-view>
  57. <button @click="submit" class="confirm">确定</button>
  58. </view>
  59. </u-popup>
  60. </view>
  61. </template>
  62. <script>
  63. export default {
  64. props: {
  65. show: {
  66. typeof: Boolean,
  67. default: false,
  68. },
  69. cityId: {
  70. typeof: Array,
  71. default: () => {
  72. return [];
  73. },
  74. },
  75. countryId: {
  76. typeof: String,
  77. default: "100000",
  78. },
  79. },
  80. watch: {
  81. cityId(newVal) {
  82. if (newVal.length == 3) {
  83. this.cityList.forEach((item, index) => {
  84. if (item.pid == newVal[0]) {
  85. this.value.push(index);
  86. item.children.forEach((items, indexs) => {
  87. if (items.pid == newVal[1]) {
  88. this.value.push(indexs);
  89. items.children.forEach((elem, i) => {
  90. if (elem.pid == newVal[2]) {
  91. this.value.push(i);
  92. }
  93. });
  94. }
  95. });
  96. }
  97. });
  98. } else {
  99. this.value = [0, 0, 0];
  100. }
  101. },
  102. countryId(newVal) {
  103. if (newVal != "100000") {
  104. this.getCityList();
  105. }
  106. },
  107. },
  108. data() {
  109. return {
  110. hotCityList: [
  111. "北京",
  112. "上海",
  113. "深圳",
  114. "广州",
  115. "天津",
  116. "青岛",
  117. "长沙",
  118. "成都",
  119. ],
  120. cityList: [],
  121. title: "picker-view",
  122. marketList: [],
  123. distinguishList: [],
  124. value: [],
  125. result: [],
  126. indicatorStyle: "height: 50px;",
  127. maskTopStyle: "",
  128. maskBottomStyle: "",
  129. economize: {},
  130. market: {},
  131. distinguish: {},
  132. };
  133. },
  134. mounted() {
  135. // this.getCityList();
  136. },
  137. methods: {
  138. open() {
  139. console.log(111);
  140. this.getCityList();
  141. },
  142. //当选中的城市发生变化时调用
  143. bindChange(e) {
  144. const val = e.detail.value;
  145. this.value = val;
  146. this.marketList = this.cityList[val[0]].children;
  147. this.distinguishList = this.marketList[val[1] || 0].children;
  148. this.economize = this.cityList[val[0]];
  149. this.market = this.marketList[val[1] || 0];
  150. this.distinguish = this.distinguishList[val[2] || 0];
  151. },
  152. //获取城市
  153. getCityList() {
  154. uni.$u.http.get(`/api/area/tree?pid=${this.countryId}`).then((res) => {
  155. this.cityList = res;
  156. if (this.countryId == "100000") {
  157. this.marketList = res[0].children;
  158. this.distinguishList = this.marketList[0].children;
  159. }
  160. });
  161. },
  162. //提交选中的城市
  163. submit() {
  164. const obj = {
  165. ...this.economize,
  166. children: {
  167. ...this.market,
  168. children: this.distinguish,
  169. },
  170. };
  171. this.$emit("close", obj);
  172. },
  173. },
  174. };
  175. </script>
  176. <style lang="scss" scoped>
  177. .title {
  178. margin: 32rpx 26rpx;
  179. }
  180. .inp {
  181. background-color: #f4f4f4;
  182. border-radius: 36rpx;
  183. height: 72rpx;
  184. width: 694rpx;
  185. margin: 30rpx auto 0;
  186. padding-left: 20rpx;
  187. }
  188. .city-list {
  189. display: flex;
  190. justify-content: space-around;
  191. flex-wrap: wrap;
  192. .city {
  193. width: 156rpx;
  194. height: 68rpx;
  195. text-align: center;
  196. line-height: 68rpx;
  197. background-color: #f4f4f4;
  198. border-radius: 36rpx;
  199. margin-bottom: 20rpx;
  200. color: #222;
  201. font-size: 28rpx;
  202. }
  203. }
  204. .confirm {
  205. background-color: #f83224;
  206. border-radius: 40rpx;
  207. color: #fff;
  208. width: 702rpx;
  209. margin: 0 auto;
  210. }
  211. .picker-view {
  212. width: 100%;
  213. height: 180px;
  214. margin-top: 10px;
  215. }
  216. .item {
  217. height: 50px;
  218. }
  219. .text {
  220. line-height: 50px;
  221. text-align: center;
  222. }
  223. .picker-view-column {
  224. text-align: center;
  225. }
  226. </style>