JArea.vue 5.7 KB

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