yixuan-selectAddress.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <template>
  2. <view class="wrapper1" v-show="isShowMask1">
  3. <transition name="content">
  4. <view class="content_view" v-show="isShow">
  5. <view class="title_view">
  6. <view class="back_view">
  7. <image :src="imgUrl+'to_right.png'" mode="" v-if="isBack" @click="backToAdd"></image>
  8. <image src="" mode="" v-else></image>
  9. </view>
  10. <view class="title">请选择所在地区</view>
  11. <view class="close_view" @click="hidden">
  12. <icon class="close_icon" :type="'clear'" size="20" color='#DCDCDC'/>
  13. </view>
  14. </view>
  15. <view class="select_top">
  16. <view class="select_top_item" ref="select_top_item" v-for="(item,index) in dataList" :key="index" @click="select_top_item_click(index)">
  17. <text class="address_value">{{item.name}}</text>
  18. </view>
  19. </view>
  20. <swiper class="swiper" :current="currentIndex" @change="swiperChange">
  21. <swiper-item v-for="(swiper_item,swiper_index) in dataList" :key="swiper_index">
  22. <view class="swiper-item">
  23. <scroll-view class="scroll-view-item" scroll-y="true">
  24. <view class="address_item flex_row_between_center" v-for="(item,index) in cityAreaArray[swiper_index]" :key="index" @click="address_item_click(swiper_index,index,item)" :style="{background:selectIndexArr[swiper_index] === index?bgGrey:bgWhite}">
  25. {{item.regionName}}
  26. <image v-if="selectIndexArr[swiper_index] === index" class="address_item_icon" :src="imgUrl+'yixuan-selectAddress/gou.png'"
  27. mode=""></image>
  28. </view>
  29. </scroll-view>
  30. </view>
  31. </swiper-item>
  32. </swiper>
  33. </view>
  34. </transition>
  35. <view class="mask" @click="hidden" v-show="isShowMask" @touchmove.stop.prevent="moveHandle"></view>
  36. </view>
  37. </template>
  38. <script>
  39. // import areaData from '../../static/area.json'
  40. export default {
  41. data() {
  42. return {
  43. imgUrl: getApp().globalData.imgUrl,
  44. isShow: false,
  45. isShowMask: false,
  46. isShowMask1: false,
  47. dataList: [{
  48. code: '',
  49. name: '请选择'
  50. }],
  51. currentIndex: 0,
  52. cityData: {},
  53. cityAreaArray: [],
  54. selectIndexArr: [],
  55. indicatorStyleLeft: 16,
  56. sel_area_tip: {
  57. code: '',
  58. name: '请选择'
  59. },
  60. bgGrey: '#F8F8F8',
  61. bgWhite: '#fff',
  62. areaData:[]
  63. };
  64. },
  65. props: {
  66. sel_data: {
  67. type: Array,
  68. value: []
  69. },
  70. isBack:{
  71. type:Boolean,
  72. default:true
  73. }
  74. },
  75. methods: {
  76. show() {
  77. this.getAddressList()
  78. this.isShow = true
  79. this.isShowMask1 = true
  80. this.isShowMask = true
  81. },
  82. showNoMask(){
  83. this.getAddressList()
  84. this.isShowMask1 = true
  85. this.isShow = true
  86. },
  87. getAddressList(){
  88. let param = {};
  89. param.url = 'v3/system/front/region/list';
  90. param.data = {};
  91. param.method = 'GET';
  92. this.$request(param).then(res => {
  93. if(res.state == 200){
  94. this.areaData=res.data
  95. this.cityData = this.areaData
  96. this.cityAreaArray.push(this.areaData)
  97. }else{
  98. this.$api.msg(res.msg);
  99. }
  100. }).catch((e) => {
  101. //异常处理
  102. })
  103. },
  104. hidden() {
  105. this.isShow = false
  106. setTimeout(() => {
  107. this.isShowMask = false
  108. this.isShowMask1 = false
  109. }, 100);
  110. },
  111. select_top_item_click(index) {
  112. this.currentIndex = index
  113. this.$nextTick(() => {
  114. this.changeIndicator(index)
  115. })
  116. },
  117. swiperChange(event) {
  118. let index = event.detail.current
  119. this.currentIndex = index
  120. this.changeIndicator(index)
  121. },
  122. changeIndicator(index) {
  123. let indicatorWidth = 30
  124. const query = uni.createSelectorQuery().in(this);
  125. let arr = query.selectAll('.select_top_item .address_value')
  126. arr.fields({
  127. size: true,
  128. scrollOffset: false
  129. }, data => {
  130. let itemWidth = data[index]["width"] > 80 ? 70 : data[index]["width"]
  131. let itemCenterX = 10 + index * 80 + itemWidth / 2
  132. let left = itemCenterX - indicatorWidth / 2
  133. this.indicatorStyleLeft = left
  134. }).exec();
  135. },
  136. address_item_click(swiper_index, index, item) {
  137. this.selectIndexArr.splice(swiper_index, 5, index)
  138. //判断当前是否为最下一级
  139. if (swiper_index === 0) { //第一级
  140. let currentObj = this.cityData[index]
  141. let city = currentObj.regionName
  142. this.dataList.splice(swiper_index, 0, {
  143. code: currentObj.regionCode,
  144. name: currentObj.regionName
  145. });
  146. this.dataList.splice(swiper_index+1);
  147. this.dataList.splice(swiper_index+1, 0, {
  148. code: '',
  149. name: '请选择'
  150. })
  151. this.cityAreaArray.splice(swiper_index + 1, 1, currentObj["children"])
  152. setTimeout(() => {
  153. this.currentIndex = 1
  154. this.changeIndicator(1)
  155. }, 50);
  156. } else {
  157. let currentAreaArray = this.cityAreaArray[swiper_index]
  158. let currentObj = currentAreaArray[index]
  159. let area = currentObj["children"]
  160. if (area.length!= 0) {
  161. let city = currentObj.regionName
  162. this.dataList.splice(swiper_index, 0, {
  163. code: currentObj.regionCode,
  164. name: currentObj.regionName
  165. })
  166. this.dataList.splice(swiper_index+1);
  167. this.dataList.splice(swiper_index+1, 0, {
  168. code: '',
  169. name: '请选择'
  170. })
  171. this.cityAreaArray.splice(swiper_index + 1, 1, currentObj["children"])
  172. setTimeout(() => {
  173. this.currentIndex = swiper_index + 1
  174. this.changeIndicator(swiper_index + 1)
  175. }, 50);
  176. } else { //是最下一级
  177. let city = currentObj.regionName
  178. this.dataList.splice(swiper_index, 0, {
  179. code: currentObj.regionCode,
  180. name: currentObj.regionName
  181. })
  182. //删除最后一个
  183. this.dataList.splice(swiper_index + 1)
  184. //选择成功返回数据
  185. this.$emit("selectAddress", this.dataList)
  186. this.$nextTick(() => {
  187. this.changeIndicator(swiper_index)
  188. })
  189. setTimeout(() => {
  190. this.isShow = false
  191. }, 100);
  192. setTimeout(() => {
  193. this.isShowMask = false
  194. this.isShowMask1 = false
  195. }, 500);
  196. }
  197. }
  198. },
  199. moveHandle(){
  200. },
  201. backToAdd(){
  202. this.$emit('backToAdd')
  203. }
  204. },
  205. created() {
  206. // this.cityData = this.areaData
  207. // this.cityAreaArray.push(this.areaData)
  208. },
  209. mounted() {
  210. // this.changeIndicator(0)
  211. }
  212. }
  213. </script>
  214. <style lang="scss">
  215. // 不换行
  216. @mixin no-wrap() {
  217. text-overflow: ellipsis;
  218. overflow: hidden;
  219. white-space: nowrap;
  220. }
  221. .wrapper1 {
  222. z-index: 1999;
  223. position: absolute;
  224. top: -88rpx;
  225. left: 0;
  226. bottom: 0;
  227. right: 0;
  228. background-color: rgba(0, 0, 0, 0.4);
  229. .content_view {
  230. z-index: 999;
  231. background: #fff;
  232. position: fixed;
  233. height: 65%;
  234. left: 0;
  235. bottom: 0;
  236. right: 0;
  237. border-top-left-radius: 10rpx;
  238. border-top-right-radius: 10rpx;
  239. width: 750rpx;
  240. margin: 0 auto;
  241. .title_view {
  242. height: 12%;
  243. display: flex;
  244. justify-content: space-between;
  245. align-items: center;
  246. padding: 0 30rpx;
  247. height: 100rpx;
  248. .title {
  249. font-size: 32rpx;
  250. color: 32rpx;
  251. font-weight: bold;
  252. color: #333333;
  253. }
  254. .close_view {
  255. height: 60rpx;
  256. width: 40rpx;
  257. display: flex;
  258. justify-content: center;
  259. align-items: center;
  260. }
  261. }
  262. .select_top {
  263. height: 90rpx;
  264. display: flex;
  265. justify-content: start;
  266. align-items: center;
  267. padding: 0 30rpx;
  268. position: relative;
  269. box-sizing: border-box;
  270. border-bottom: 1rpx solid #f2f2f2;
  271. .select_top_item {
  272. margin-right: 30rpx;
  273. font-size: 28rpx;
  274. line-height: 80rpx;
  275. @include no-wrap();
  276. color: #333;
  277. border-bottom: 6rpx solid #fff;
  278. max-width: 200rpx;
  279. &:last-child{
  280. color: $main-color;
  281. border-bottom: 6rpx solid $main-color;
  282. font-weight: bold;
  283. height: 90rpx;
  284. }
  285. }
  286. }
  287. .swiper {
  288. height: 80%;
  289. position: relative;
  290. left: 0;
  291. top: 0;
  292. bottom: 0;
  293. right: 0;
  294. .swiper-item {
  295. height: 100%;
  296. .scroll-view-item {
  297. height: 100%;
  298. .address_item {
  299. color: #333333;
  300. font-size: 28rpx;
  301. display: flex;
  302. align-items: center;
  303. height: 90rpx;
  304. line-height: 90rpx;
  305. padding: 0 30rpx;
  306. .address_item_icon {
  307. width: 28rpx;
  308. height: 20rpx;
  309. margin-left: 20rpx;
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. .mask {
  317. position: fixed;
  318. width: 100%;
  319. height: 100%;
  320. top: 0;
  321. left: 0;
  322. bottom: 0;
  323. right: 0;
  324. background: $uni-text-color-grey;
  325. opacity: 0.4;
  326. }
  327. }
  328. .content-enter {
  329. transform: translateY(100%);
  330. }
  331. .back_view{
  332. display: flex;
  333. align-items: center;
  334. image{
  335. width: 30rpx;
  336. height: 30rpx;
  337. }
  338. text{
  339. font-size: 28rpx;
  340. }
  341. }
  342. .content-enter-to {
  343. transform: translateY(0%);
  344. }
  345. .content-enter-active {
  346. transition: transform 0.5s;
  347. }
  348. .content-leave {
  349. transform: translateY(0%);
  350. }
  351. .content-leave-to {
  352. transform: translateY(100%);
  353. }
  354. .content-leave-active {
  355. transition: transform 0.5s;
  356. }
  357. </style>