index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. address: {
  47. type:Array,
  48. default:[]
  49. },
  50. },
  51. data() {
  52. return {
  53. active: 0,
  54. //地址列表
  55. addressList: [],
  56. selectedArr: [],
  57. selectedIndex: -1,
  58. is_loading: false,
  59. old: { scrollTop: 0 },
  60. scrollTop: 0
  61. };
  62. },
  63. computed:{
  64. activeId(){
  65. return this.selectedIndex == -1 ? 0 : this.selectedArr[this.selectedIndex].id
  66. },
  67. showMore(){
  68. return this.selectedArr.length ? this.selectedArr[this.selectedArr.length - 1].hasOwnProperty('children') : true
  69. }
  70. },
  71. watch:{
  72. address(n){
  73. this.selectedArr = n ? [...n] : []
  74. },
  75. display(n){
  76. if(!n) {
  77. this.addressList = [];
  78. this.selectedArr = this.address ? [...this.address] : [];
  79. this.selectedIndex = -1;
  80. this.is_loading = false;
  81. }else{
  82. this.loadAddress(0)
  83. }
  84. }
  85. },
  86. mounted() {
  87. this.loadAddress(0)
  88. },
  89. methods: {
  90. change(pid,index){
  91. if(this.selectedIndex == index) return;
  92. if(pid === -1){
  93. pid = this.selectedArr.length ? this.selectedArr[this.selectedArr.length -1].id : 0;
  94. }
  95. this.selectedIndex = index;
  96. this.loadAddress(pid);
  97. },
  98. loadAddress(pid){
  99. if(CACHE_ADDRESS[pid]){
  100. this.addressList = CACHE_ADDRESS[pid];
  101. return ;
  102. }
  103. this.is_loading = true;
  104. getCityData(pid).then(res=>{
  105. this.is_loading = false;
  106. CACHE_ADDRESS[pid] = res.data;
  107. this.addressList = res.data;
  108. })
  109. this.goTop()
  110. },
  111. selected(item){
  112. if(this.is_loading) return;
  113. if(this.selectedIndex > -1){
  114. this.selectedArr.splice(this.selectedIndex + 1,999)
  115. this.selectedArr[this.selectedIndex] = item;
  116. this.selectedIndex = -1;
  117. }else if(!item.pid){
  118. this.selectedArr = [item];
  119. }else{
  120. this.selectedArr.push(item);
  121. }
  122. if(item.hasOwnProperty('children')){
  123. this.loadAddress(item.id);
  124. } else {
  125. this.$emit('submit', [...this.selectedArr]);
  126. this.$emit('changeClose');
  127. }
  128. this.goTop()
  129. },
  130. close: function() {
  131. this.$emit('changeClose');
  132. },
  133. scroll : function(e) {
  134. this.old.scrollTop = e.detail.scrollTop
  135. },
  136. goTop: function(e) {
  137. this.scrollTop = this.old.scrollTop
  138. this.$nextTick(() => {
  139. this.scrollTop = 0
  140. });
  141. }
  142. }
  143. }
  144. </script>
  145. <style scoped lang="scss">
  146. .address-window {
  147. background-color: #fff;
  148. position: fixed;
  149. bottom: 0;
  150. left: 0;
  151. width: 100%;
  152. z-index: 101;
  153. border-radius: 30rpx 30rpx 0 0;
  154. transform: translate3d(0, 100%, 0);
  155. transition: all .3s cubic-bezier(.25, .5, .5, .9);
  156. }
  157. .address-window.on {
  158. transform: translate3d(0, 0, 0);
  159. }
  160. .address-window .title {
  161. font-size: 32rpx;
  162. font-weight: bold;
  163. text-align: center;
  164. height: 123rpx;
  165. line-height: 123rpx;
  166. position: relative;
  167. }
  168. .address-window .title .iconfont {
  169. position: absolute;
  170. right: 30rpx;
  171. color: #8a8a8a;
  172. font-size: 35rpx;
  173. }
  174. .address-count{
  175. .address-selected{
  176. padding: 0 30rpx;
  177. margin-top: 10rpx;
  178. position: relative;
  179. padding-bottom: 20rpx;
  180. border-bottom: 2rpx solid #f7f7f7;
  181. }
  182. .selected-list{
  183. font-size: 26rpx;
  184. color: #282828;
  185. line-height: 50rpx;
  186. padding-bottom: 10rpx;
  187. padding-left: 60rpx;
  188. position: relative;
  189. &.active{
  190. color: var(--view-theme);
  191. }
  192. &:before,&:after{
  193. content: '';
  194. display: block;
  195. position: absolute;
  196. }
  197. &:before{
  198. width: 4rpx;
  199. height: 100%;
  200. background-color: var(--view-theme);
  201. top: 0;
  202. left: 10rpx;
  203. }
  204. &:after{
  205. width: 12rpx;
  206. height: 12rpx;
  207. background: var(--view-theme);
  208. border-radius: 100%;
  209. left: 6rpx;
  210. top: 50%;
  211. margin-top: -8rpx;
  212. }
  213. &:first-child,&:last-child{
  214. &:before{
  215. height: 50%;
  216. }
  217. }
  218. &:first-child{
  219. &:before{
  220. top: auto;
  221. bottom: 0;
  222. }
  223. }
  224. .iconfont{
  225. font-size: 20rpx;
  226. float: right;
  227. color: #dddddd;
  228. }
  229. }
  230. scroll-view{
  231. height: 550rpx;
  232. }
  233. .address-list{
  234. padding: 0 30rpx;
  235. margin-top: 20rpx;
  236. box-sizing: border-box;
  237. .list{
  238. .iconfont{
  239. float: right;
  240. color: #ddd;
  241. font-size: 22rpx;
  242. }
  243. .item-name{
  244. display: inline-block;
  245. line-height: 50rpx;
  246. margin-bottom: 20rpx;
  247. font-size: 26rpx;
  248. }
  249. &.active{
  250. color: var(--view-theme);
  251. .iconfont{
  252. color: var(--view-theme);
  253. }
  254. }
  255. }
  256. }
  257. }
  258. </style>