index.vue 6.6 KB

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