JCity.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.county.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: 'JCity',
  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. this.getCity({
  139. parentId: cityInfo.id
  140. })
  141. }
  142. if (this.current === 'province') {
  143. this.current = 'city'
  144. this.areaInfo.city = {
  145. text: '',
  146. id: ''
  147. }
  148. this.areaInfo.county = {
  149. text: '',
  150. id: ''
  151. }
  152. } else if (this.current === 'city') {
  153. this.current = 'county'
  154. this.areaInfo.county = {
  155. text: '',
  156. id: ''
  157. }
  158. }
  159. },
  160. // 监控popup的状态发生改变
  161. onPopupStatusChange(e) {
  162. this.current = 'province'
  163. this.getCity({
  164. parentId: 0
  165. })
  166. if (!e.show) {
  167. this.areaInfo = {
  168. province: {
  169. text: '',
  170. id: null
  171. },
  172. city: {
  173. text: '',
  174. id: null
  175. },
  176. county: {
  177. text: '',
  178. id: null
  179. }
  180. }
  181. }
  182. },
  183. // 点击确定按钮
  184. handleConfirmArea() {
  185. if (!this.areaInfo.county.text) {
  186. return
  187. }
  188. this.areaString =
  189. this.areaInfo.province.text +
  190. this.areaInfo.city.text +
  191. this.areaInfo.county.text
  192. console.log({ ...this.areaInfo, area: this.areaString })
  193. this.$emit('confirm', { ...this.areaInfo, area: this.areaString })
  194. this.$refs.popup.close()
  195. }
  196. }
  197. }
  198. </script>
  199. <style lang="less" scoped>
  200. @import "../../style/mixin.less";
  201. .j-city {
  202. .active {
  203. color: #fa5151;
  204. }
  205. .city-wrapper {
  206. background-color: #fff;
  207. height: 600upx;
  208. padding: 40upx;
  209. box-sizing: border-box;
  210. .flex();
  211. flex-direction: column;
  212. .header {
  213. width: 100%;
  214. .flex();
  215. border-bottom: 1upx solid #ccc;
  216. padding-bottom: 10upx;
  217. height: 40upx;
  218. .citys {
  219. .flex(center, flex-start);
  220. color: #8b8b8b;
  221. .item {
  222. width: 120upx;
  223. margin-right: 20px;
  224. overflow: hidden;
  225. text-overflow: ellipsis;
  226. white-space: nowrap;
  227. transition: all 350ms;
  228. }
  229. }
  230. .confirm {
  231. padding: 0 2upx;
  232. line-height: 1;
  233. margin: 0;
  234. font-size: 24upx;
  235. color: #fa5151;
  236. transition: all 350ms;
  237. }
  238. }
  239. .body {
  240. flex: 1;
  241. width: 100%;
  242. // background: #fa5151;
  243. overflow: scroll;
  244. ul {
  245. margin: 0;
  246. padding: 0;
  247. li {
  248. margin: 0;
  249. padding: 20upx 0;
  250. transition: all 350ms;
  251. &.active {
  252. font-weight: bold;
  253. }
  254. }
  255. }
  256. }
  257. }
  258. }
  259. </style>