index.vue 6.4 KB

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