index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <view :data-theme="theme" @touchmove.stop.prevent="disabledScroll">
  3. <view class="address-window" :class="display==true?'on':''">
  4. <view class='title'>请选择所在地区<text class='iconfont icon-guanbi' @tap='close'></text></view>
  5. <view class="address-count">
  6. <view class="address-selected">
  7. <view v-for="(item,index) in selectedArr" :key="index" class="selected-list"
  8. :class="{active:index === selectedIndex}" @click="change(item, index)">
  9. {{item.regionName?item.regionName:'请选择'}}
  10. <text class="iconfont icon-xiangyou"></text>
  11. </view>
  12. <view class="selected-list" :class="{active:-1 === selectedIndex}" v-if="showMore"
  13. @click="change(-1, -1)">
  14. <text class="iconfont icon-xiangyou"></text>
  15. 请选择
  16. </view>
  17. </view>
  18. <scroll-view scroll-y="true" :scroll-top="scrollTop" class="address-list" @scroll="scroll">
  19. <view v-for="(item,index) in addressList" :key="index" class="list"
  20. :class="{active:item.regionId === activeId}" @click="selected(item, index)">
  21. <text class="item-name">{{item.regionName}}</text>
  22. <text v-if="item.regionId === activeId" class="iconfont icon-duihao2"></text>
  23. </view>
  24. </scroll-view>
  25. </view>
  26. </view>
  27. <view class='mask' catchtouchmove="true" @touchmove.prevent :hidden='display==false'
  28. @tap='close'></view>
  29. </view>
  30. </template>
  31. <script>
  32. // +----------------------------------------------------------------------
  33. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  34. // +----------------------------------------------------------------------
  35. // | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
  36. // +----------------------------------------------------------------------
  37. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  38. // +----------------------------------------------------------------------
  39. // | Author: CRMEB Team <admin@crmeb.com>
  40. // +----------------------------------------------------------------------
  41. import {
  42. getCity
  43. } from '@/api/api.js';
  44. const CACHE_ADDRESS = {};
  45. let app = getApp();
  46. export default {
  47. props: {
  48. display: {
  49. type: Boolean,
  50. default: true
  51. },
  52. cityShow: {
  53. type: Number,
  54. default: 3
  55. },
  56. address: {
  57. type: Array,
  58. default: []
  59. }
  60. },
  61. data() {
  62. return {
  63. theme: app.globalData.theme,
  64. active: 0,
  65. //地址列表
  66. addressList: [],
  67. selectedArr: [],
  68. selectedIndex: -1,
  69. is_loading: false,
  70. old: {
  71. scrollTop: 0
  72. },
  73. scrollTop: 0,
  74. addressData: [],
  75. province: [],
  76. city: [],
  77. district: [],
  78. street: []
  79. };
  80. },
  81. computed: {
  82. activeId() {
  83. return this.selectedIndex == -1 ? 0 : this.selectedArr[this.selectedIndex].regionId
  84. },
  85. showMore() {
  86. return this.selectedArr.length ? (this.selectedArr[this.selectedArr.length - 1].isChild && ((this
  87. .cityShow == 1 && this.addressList.regionType < 2) ||
  88. (this.cityShow == 2 && this.addressList.regionType < 3) || (this.cityShow == 3 && this
  89. .addressList
  90. .regionType < 4))) : true
  91. }
  92. },
  93. watch: {
  94. address(n) {
  95. this.selectedArr = n ? [...n] : []
  96. },
  97. display(n) {
  98. if (!n) {
  99. this.addressList = [];
  100. this.selectedArr = this.address ? [...this.address] : [];
  101. this.selectedIndex = -1;
  102. this.is_loading = false;
  103. } else {
  104. this.loadAddress(1, 1)
  105. }
  106. }
  107. },
  108. mounted() {
  109. this.loadAddress(1, 1);
  110. },
  111. methods: {
  112. disabledScroll() {
  113. return
  114. },
  115. loadAddress(parentId, regionType) {
  116. if (CACHE_ADDRESS[parentId]) {
  117. this.addressList = CACHE_ADDRESS[parentId];
  118. return;
  119. }
  120. let data = {
  121. parentId: parentId,
  122. regionType: regionType
  123. }
  124. this.is_loading = true;
  125. getCity(data).then(res => {
  126. this.is_loading = false;
  127. CACHE_ADDRESS[parentId] = res.data;
  128. this.addressList = res.data;
  129. })
  130. this.goTop()
  131. },
  132. change(item, index) {
  133. if (this.selectedIndex == index) return;
  134. this.selectedIndex = index;
  135. this.loadAddress(item.parentId, item.regionType);
  136. },
  137. selected(item, index) {
  138. if (this.is_loading) return;
  139. if (this.selectedIndex > -1) {
  140. this.selectedArr.splice(this.selectedIndex + 1, 999)
  141. this.selectedArr[this.selectedIndex] = item;
  142. this.selectedIndex = -1;
  143. } else if (item.regionType === 1) {
  144. this.selectedArr = [item];
  145. } else {
  146. this.selectedArr.push(item);
  147. }
  148. if (item.isChild && ((this.cityShow == 1 && this.addressList[0].regionType < 2) || (this.cityShow == 2 &&
  149. this
  150. .addressList[0].regionType < 3) || (this.cityShow == 3 && this.addressList[0].regionType < 4))) {
  151. this.loadAddress(item.regionId, item.regionType + 1);
  152. } else {
  153. this.$emit('submit', [...this.selectedArr]);
  154. this.$emit('changeClose');
  155. }
  156. this.goTop()
  157. },
  158. close: function() {
  159. this.$emit('changeClose');
  160. },
  161. scroll: function(e) {
  162. this.old.scrollTop = e.detail.scrollTop
  163. },
  164. goTop: function(e) {
  165. this.scrollTop = this.old.scrollTop
  166. this.$nextTick(() => {
  167. this.scrollTop = 0
  168. });
  169. }
  170. }
  171. }
  172. </script>
  173. <style scoped lang="scss">
  174. .address-window {
  175. background-color: #fff;
  176. position: fixed;
  177. bottom: 0;
  178. left: 0;
  179. width: 100%;
  180. z-index: 101;
  181. border-radius: 30rpx 30rpx 0 0;
  182. transform: translate3d(0, 100%, 0);
  183. transition: all .3s cubic-bezier(.25, .5, .5, .9);
  184. }
  185. .address-window.on {
  186. transform: translate3d(0, 0, 0);
  187. }
  188. .address-window .title {
  189. font-size: 32rpx;
  190. font-weight: bold;
  191. text-align: center;
  192. height: 123rpx;
  193. line-height: 123rpx;
  194. position: relative;
  195. }
  196. .address-window .title .iconfont {
  197. position: absolute;
  198. right: 30rpx;
  199. color: #8a8a8a;
  200. font-size: 35rpx;
  201. }
  202. .address-count {
  203. .address-selected {
  204. padding: 0 30rpx;
  205. margin-top: 10rpx;
  206. position: relative;
  207. padding-bottom: 20rpx;
  208. border-bottom: 2rpx solid #f7f7f7;
  209. }
  210. .selected-list {
  211. font-size: 26rpx;
  212. color: #282828;
  213. line-height: 50rpx;
  214. padding-bottom: 10rpx;
  215. padding-left: 60rpx;
  216. position: relative;
  217. &.active {
  218. color: #e28d54;
  219. }
  220. &:before,
  221. &:after {
  222. content: '';
  223. display: block;
  224. position: absolute;
  225. }
  226. &:before {
  227. width: 4rpx;
  228. height: 100%;
  229. @include main_bg_color(theme);
  230. top: 0;
  231. left: 10rpx;
  232. }
  233. &:after {
  234. width: 12rpx;
  235. height: 12rpx;
  236. @include main_bg_color(theme);
  237. border-radius: 100%;
  238. left: 6rpx;
  239. top: 50%;
  240. margin-top: -8rpx;
  241. }
  242. &:first-child,
  243. &:last-child {
  244. &:before {
  245. height: 50%;
  246. }
  247. }
  248. &:first-child {
  249. &:before {
  250. top: auto;
  251. bottom: 0;
  252. }
  253. }
  254. .iconfont {
  255. font-size: 20rpx;
  256. float: right;
  257. color: #dddddd;
  258. }
  259. }
  260. scroll-view {
  261. height: 700rpx;
  262. }
  263. .address-list {
  264. padding: 0 30rpx;
  265. margin-top: 20rpx;
  266. box-sizing: border-box;
  267. .list {
  268. .iconfont {
  269. float: right;
  270. color: #ddd;
  271. font-size: 22rpx;
  272. }
  273. .item-name {
  274. display: inline-block;
  275. line-height: 50rpx;
  276. margin-bottom: 20rpx;
  277. font-size: 26rpx;
  278. }
  279. &.active {
  280. color: #e28d54;
  281. .iconfont {
  282. color: #e28d54;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. </style>