JAnyCity.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <view class="j-city">
  3. <view
  4. class="value" :style="{
  5. color: text ? color ? color : '' : '#999'
  6. }" @click="open"
  7. >
  8. {{ text ? text : placeholder || "请选择地区" }}
  9. </view>
  10. <uni-popup ref="popup" type="bottom" @change="onPopupStatusChange">
  11. <view class="city-wrapper">
  12. <view class="header">
  13. <view class="citys">
  14. <view
  15. class="item" :class="{
  16. active: current === 'province'
  17. }" @click="change('province')"
  18. >
  19. {{ areaInfo.province.text || "省份" }}
  20. </view>
  21. <view
  22. class="item" :class="{
  23. active: current === 'city'
  24. }" @click="change('city')"
  25. >
  26. {{ areaInfo.city.text || "城市" }}
  27. </view>
  28. <view
  29. class="item" :class="{
  30. active: current === 'county'
  31. }" @click="change('county')"
  32. >
  33. {{ areaInfo.county.text || "区县" }}
  34. </view>
  35. </view>
  36. <button
  37. class="confirm" :style="{
  38. color: areaInfo.province.text ? '#fa5151' : '#8b8b8b'
  39. }" @click="handleConfirmArea"
  40. >
  41. 确定
  42. </button>
  43. </view>
  44. <view class="body">
  45. <ul>
  46. <li
  47. v-for="item in data" :key="item.id" :class="{
  48. active: item.id === areaInfo[current].id
  49. }" @click="chooseCity(item)"
  50. >
  51. {{ item.name }}
  52. </li>
  53. </ul>
  54. </view>
  55. </view>
  56. </uni-popup>
  57. </view>
  58. </template>
  59. <script>
  60. import { getCityManageAreaTreeOneApi } from '../../api/anotherTFInterface'
  61. export default {
  62. name: 'JAnyCity',
  63. props: {
  64. text: String,
  65. placeholder: String,
  66. color: String
  67. },
  68. data() {
  69. return {
  70. current: 'province',
  71. data: [],
  72. areaInfo: {
  73. province: {
  74. text: '',
  75. id: null,
  76. parentId: ''
  77. },
  78. city: {
  79. text: '',
  80. id: null,
  81. parentId: ''
  82. },
  83. county: {
  84. text: '',
  85. id: null,
  86. parentId: ''
  87. }
  88. },
  89. areaString: ''
  90. }
  91. },
  92. mounted() {
  93. this.getCity({ parentId: 0 })
  94. },
  95. methods: {
  96. // 打开popup
  97. open() {
  98. this.$refs.popup.open('bottom')
  99. },
  100. // 切换区域
  101. change(field) {
  102. if (this.current === field) {
  103. return
  104. }
  105. if (field === 'city' && !this.areaInfo.province.id) {
  106. return
  107. }
  108. if (field === 'county' && !this.areaInfo.city.id) {
  109. return
  110. }
  111. if (field === 'province') {
  112. this.getCity({
  113. parentId: 0
  114. })
  115. } else if (field === 'city') {
  116. this.getCity({
  117. parentId: this.areaInfo.province.id
  118. })
  119. } else if (field === 'county') {
  120. this.getCity({
  121. parentId: this.areaInfo.city.id
  122. })
  123. }
  124. this.current = field
  125. },
  126. // 获取地址信息
  127. getCity(data) {
  128. getCityManageAreaTreeOneApi(data).then((res) => {
  129. this.data = res.data
  130. })
  131. },
  132. // 选择区域
  133. chooseCity(cityInfo) {
  134. this.areaInfo[this.current].text = cityInfo.name
  135. this.areaInfo[this.current].id = cityInfo.id
  136. this.areaInfo[this.current].parentId = cityInfo.id
  137. if (this.current !== 'county') {
  138. }
  139. if (this.current === 'province') {
  140. this.areaInfo.city = {
  141. text: '',
  142. id: ''
  143. }
  144. this.areaInfo.county = {
  145. text: '',
  146. id: ''
  147. }
  148. } else if (this.current === 'city') {
  149. this.areaInfo.county = {
  150. text: '',
  151. id: ''
  152. }
  153. }
  154. },
  155. // 监控popup的状态发生改变
  156. onPopupStatusChange(e) {
  157. this.current = 'province'
  158. this.getCity({
  159. parentId: 0
  160. })
  161. if (!e.show) {
  162. this.areaInfo = {
  163. province: {
  164. text: '',
  165. id: null
  166. },
  167. city: {
  168. text: '',
  169. id: null
  170. },
  171. county: {
  172. text: '',
  173. id: null
  174. }
  175. }
  176. }
  177. },
  178. // 点击确定按钮
  179. handleConfirmArea() {
  180. if (!this.areaInfo.province.text) {
  181. return
  182. }
  183. this.areaString =
  184. this.areaInfo.province.text +
  185. this.areaInfo.city.text +
  186. this.areaInfo.county.text
  187. console.log({ ...this.areaInfo, area: this.areaString })
  188. this.$emit('confirm', { ...this.areaInfo, area: this.areaString })
  189. this.$refs.popup.close()
  190. }
  191. }
  192. }
  193. </script>
  194. <style lang="less" scoped>
  195. @import "../../style/mixin.less";
  196. .j-city {
  197. .active {
  198. color: #fa5151;
  199. }
  200. .city-wrapper {
  201. background-color: #fff;
  202. height: 600upx;
  203. padding: 40upx;
  204. box-sizing: border-box;
  205. .flex();
  206. flex-direction: column;
  207. .header {
  208. width: 100%;
  209. .flex();
  210. border-bottom: 1upx solid #ccc;
  211. padding-bottom: 10upx;
  212. height: 40upx;
  213. .citys {
  214. .flex(center, flex-start);
  215. color: #8b8b8b;
  216. .item {
  217. width: 120upx;
  218. margin-right: 20px;
  219. overflow: hidden;
  220. text-overflow: ellipsis;
  221. white-space: nowrap;
  222. transition: all 350ms;
  223. }
  224. }
  225. .confirm {
  226. padding: 0 2upx;
  227. line-height: 1;
  228. margin: 0;
  229. font-size: 24upx;
  230. color: #fa5151;
  231. transition: all 350ms;
  232. }
  233. }
  234. .body {
  235. flex: 1;
  236. width: 100%;
  237. // background: #fa5151;
  238. overflow: scroll;
  239. ul {
  240. margin: 0;
  241. padding: 0;
  242. li {
  243. margin: 0;
  244. padding: 20upx 0;
  245. transition: all 350ms;
  246. &.active {
  247. font-weight: bold;
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. </style>