JAnyArea.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.province.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. }
  159. if (this.current === 'province') {
  160. this.areaInfo.city = {
  161. text: '',
  162. id: ''
  163. }
  164. this.areaInfo.county = {
  165. text: '',
  166. id: ''
  167. }
  168. this.areaInfo.township = {
  169. text: '',
  170. id: ''
  171. }
  172. } else if (this.current === 'city') {
  173. this.areaInfo.county = {
  174. text: '',
  175. id: ''
  176. }
  177. this.areaInfo.township = {
  178. text: '',
  179. id: ''
  180. }
  181. } else if (this.current === 'county') {
  182. this.areaInfo.township = {
  183. text: '',
  184. id: ''
  185. }
  186. }
  187. },
  188. // 监控popup的状态发生改变
  189. onPopupStatusChange(e) {
  190. this.current = 'province'
  191. this.getCity({
  192. parentId: 0
  193. })
  194. if (!e.show) {
  195. this.areaInfo = {
  196. province: {
  197. text: '',
  198. id: null
  199. },
  200. city: {
  201. text: '',
  202. id: null
  203. },
  204. county: {
  205. text: '',
  206. id: null
  207. },
  208. township: {
  209. text: '',
  210. id: null
  211. }
  212. }
  213. }
  214. },
  215. // 点击确定按钮
  216. handleConfirmArea() {
  217. if (!this.areaInfo.province.text) {
  218. return
  219. }
  220. this.areaString =
  221. this.areaInfo.province.text +
  222. this.areaInfo.city.text +
  223. this.areaInfo.county.text +
  224. this.areaInfo.township.text
  225. this.$emit('confirm', { ...this.areaInfo, area: this.areaString })
  226. this.$refs.popup.close()
  227. }
  228. }
  229. }
  230. </script>
  231. <style lang="less" scoped>
  232. @import "../../style/mixin.less";
  233. .j-city {
  234. .active {
  235. color: #fa5151;
  236. }
  237. .city-wrapper {
  238. background-color: #fff;
  239. height: 600upx;
  240. padding: 40upx;
  241. box-sizing: border-box;
  242. .flex();
  243. flex-direction: column;
  244. .header {
  245. width: 100%;
  246. .flex();
  247. border-bottom: 1upx solid #ccc;
  248. padding-bottom: 10upx;
  249. height: 40upx;
  250. .citys {
  251. .flex(center, flex-start);
  252. color: #8b8b8b;
  253. .item {
  254. width: 100upx;
  255. margin-right: 20px;
  256. overflow: hidden;
  257. text-overflow: ellipsis;
  258. white-space: nowrap;
  259. transition: all 350ms;
  260. }
  261. }
  262. .confirm {
  263. padding: 0 2upx;
  264. line-height: 1;
  265. margin: 0;
  266. font-size: 24upx;
  267. color: #fa5151;
  268. transition: all 350ms;
  269. }
  270. }
  271. .body {
  272. flex: 1;
  273. width: 100%;
  274. // background: #fa5151;
  275. overflow: scroll;
  276. ul {
  277. margin: 0;
  278. padding: 0;
  279. li {
  280. margin: 0;
  281. padding: 20upx 0;
  282. transition: all 350ms;
  283. &.active {
  284. font-weight: bold;
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. </style>