city.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <template>
  2. <view>
  3. <view class="top flex justify-between">
  4. <!-- 左边区 -->
  5. <view class="left">
  6. <scroll-view scroll-y="true" class="leftScroll">
  7. <view class="leftScrollItem" :class="current==index?'active':''" @click="selectCity(index)"
  8. v-for="(item,index) in list" :key="index">
  9. {{item.city}}
  10. </view>
  11. </scroll-view>
  12. </view>
  13. <!-- 右边地址 -->
  14. <view class="right">
  15. <scroll-view scroll-y="true" class="rightScroll">
  16. <view class="rightScrollItem flex align-center justify-between" :class="currents==index?'active':''"
  17. @click="addCity(index,item)" v-for="(item,index) in rightList" :key="index">
  18. <view class="rightScrollItem-t">
  19. {{item.county}}
  20. </view>
  21. <u-icon v-if="currents==index" name="checkbox-mark" color="#00DD9A" size="28"></u-icon>
  22. </view>
  23. </scroll-view>
  24. </view>
  25. </view>
  26. <!-- 底部确认 -->
  27. <view class="bottom flex align-center justify-center">
  28. <view class="bottom-boxs flex justify-center align-center" @click="cleanCity()">
  29. 清除
  30. </view>
  31. <view class="bottom-box flex justify-center align-center" @click="submit()">
  32. 确定
  33. </view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. data() {
  40. return {
  41. current: 0,
  42. currents: 0,
  43. list: [],
  44. rightList: [],
  45. city: '', //市
  46. county: '', //区
  47. };
  48. },
  49. onLoad(option) {
  50. this.city = option.city
  51. this.county = option.county
  52. this.getList()
  53. },
  54. onShow() {
  55. },
  56. methods: {
  57. /**
  58. * 设置选择的城市并返回
  59. */
  60. submit() {
  61. this.city = this.rightList[this.currents].city
  62. this.county = this.rightList[this.currents].county
  63. let data = {
  64. city: this.city,
  65. county: this.county == '全部' ? '' : this.county
  66. }
  67. uni.$emit('filterCity', data)
  68. uni.navigateBack()
  69. },
  70. /**
  71. * 清除城市缓存
  72. */
  73. cleanCity() {
  74. this.city = ''
  75. this.county = ''
  76. this.current = 0
  77. this.rightList = this.list[this.current].countyList
  78. this.currents = 0
  79. // uni.navigateBack()
  80. },
  81. /**
  82. * @param {Object} city
  83. * 选择区域并返回
  84. */
  85. addCity(index, county) {
  86. this.county = county
  87. this.currents = index
  88. },
  89. /**
  90. * @param {Object} index
  91. * 选择城市
  92. */
  93. selectCity(index) {
  94. this.rightList = this.list[index].countyList
  95. this.current = index
  96. this.currents = 0
  97. },
  98. /**
  99. * 获取地址数据
  100. */
  101. getList() {
  102. this.$Request.get('/app/postPush/getCityCounty').then(res => {
  103. if (res.code == 0) {
  104. this.list = res.data
  105. this.list.map(item => {
  106. let arr = []
  107. item.countyList.map(ite => {
  108. let obj = {
  109. city: item.city,
  110. county: ite
  111. }
  112. arr.push(obj)
  113. })
  114. item.countyList = arr
  115. item.countyList = [{
  116. city: item.city,
  117. county: '全部'
  118. }, ...item.countyList]
  119. })
  120. if (this.city) {
  121. this.list.map((item, index) => {
  122. if (item.city == this.city) {
  123. this.current = index
  124. this.rightList = item.countyList
  125. console.log(this.rightList, '999999999999')
  126. this.rightList.map((ite, ind) => {
  127. if (ite.county == this.county) {
  128. this.currents = ind
  129. }
  130. })
  131. }
  132. })
  133. } else {
  134. this.rightList = this.list[this.current].countyList
  135. }
  136. }
  137. })
  138. },
  139. }
  140. }
  141. </script>
  142. <style lang="scss">
  143. page {
  144. background: #ffffff;
  145. }
  146. .active {
  147. color: #00DD9A;
  148. }
  149. .top {
  150. width: 100%;
  151. position: fixed;
  152. /* #ifdef H5 */
  153. height: calc(100vh - 200rpx);
  154. top: 20rpx;
  155. /* #endif */
  156. /* #ifndef H5 */
  157. height: calc(100vh - 120rpx);
  158. top: 0;
  159. /* #endif */
  160. .left {
  161. width: 40%;
  162. height: 100%;
  163. border-right: 1rpx solid #F2F2F7;
  164. .leftScroll {
  165. width: 100%;
  166. height: 100%;
  167. .leftScrollItem {
  168. width: 80%;
  169. margin: 20rpx 10%;
  170. overflow: hidden; //超出的文本隐藏
  171. text-overflow: ellipsis; //溢出用省略号显示
  172. white-space: nowrap; // 默认不换行;
  173. }
  174. }
  175. }
  176. .right {
  177. width: 60%;
  178. height: 100%;
  179. .rightScroll {
  180. width: 100%;
  181. height: 100%;
  182. .rightScrollItem {
  183. width: 80%;
  184. margin: 20rpx 10%;
  185. .rightScrollItem-t {
  186. width: 80%;
  187. overflow: hidden; //超出的文本隐藏
  188. text-overflow: ellipsis; //溢出用省略号显示
  189. white-space: nowrap; // 默认不换行;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. .bottom {
  196. position: fixed;
  197. bottom: 0;
  198. width: 100%;
  199. height: 120rpx;
  200. border-top: 1rpx solid #F2F2F7;
  201. .bottom-box {
  202. width: 60%;
  203. height: 70rpx;
  204. border-radius: 8rpx;
  205. background-color: #00B88F;
  206. color: #ffffff;
  207. }
  208. .bottom-boxs {
  209. width: 30%;
  210. height: 70rpx;
  211. background-color: #F2F2F7;
  212. margin-right: 3%;
  213. }
  214. }
  215. </style>