index.vue 6.6 KB

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