areas.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <template>
  2. <view class="area_container">
  3. <view class="area_container_title">
  4. <view></view>
  5. <view class="area_container_title_name">选择地区</view>
  6. <view class="area_container_title_close" @click.stop="close"><text class="iconfont" >&#xe62f;</text></view>
  7. </view>
  8. <view class="area_container_content">
  9. <view class="selectList_con">
  10. <view class="selectList_con_item" v-for="(item, index) in selectList" :key="index">
  11. <text>{{ item.name }}</text>
  12. <text class="iconfont" @click="delSelectItem(item, index)">&#xe62f;</text>
  13. </view>
  14. </view>
  15. <view class="selectList_tap">
  16. <view class="selectList_tap_item" v-for="(item, index) in tapList" :key="index" @click="selectTapItem(item, index)" :class="{ selectTap: selectTap == item.id }">
  17. {{ item.name }}
  18. </view>
  19. <view class="selectList_tap_item" @click="selectTapLastItem(-1)" v-if="isShowLastItem" :class="{ selectTap: selectTap == -1 }">请选择</view>
  20. </view>
  21. <view class="selectList_area">
  22. <scroll-view scroll-y="true" class="scroll" :scroll-top="scrollTop" @scroll="scroll">
  23. <view v-for="(item, index) in areaList" :key="index" class="selectList_area_item">
  24. <view class="selectList_area_item_name" @click="selectArea(item)">{{ item.name }}</view>
  25. <view @click="handlyAddSelect(item)"><text class="iconfont">&#xe70e;</text></view>
  26. </view>
  27. </scroll-view>
  28. </view>
  29. <view class="handle"><view class="handle_button" @click="handleGetSelectArea">确定</view></view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. // +----------------------------------------------------------------------
  35. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  36. // +----------------------------------------------------------------------
  37. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  38. // +----------------------------------------------------------------------
  39. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  40. // +----------------------------------------------------------------------
  41. // | Author: CRMEB Team <admin@crmeb.com>
  42. // +----------------------------------------------------------------------
  43. // 获取地址接口
  44. import { getCityV2 } from '@/api/api.js';
  45. import { serialize, Toast } from '../../../libs/uniApi.js';
  46. export default {
  47. props:{
  48. allReadySelect: {
  49. type: Array,
  50. default:() => {
  51. return []
  52. }
  53. }
  54. },
  55. data() {
  56. return {
  57. selectList: [],
  58. selectTap: -1,
  59. selectTapIndex: -1, // 选择的列表下班
  60. tapList: [],
  61. isShowLastItem: true, // 是否展示最后一项请选择
  62. areaList: [],
  63. old: { scrollTop: 0 },
  64. scrollTop: 0
  65. };
  66. },
  67. watch: {
  68. allReadySelect: {
  69. handler(val) {
  70. this.selectList = this.allReadySelect;
  71. },
  72. deep: true
  73. }
  74. },
  75. created() {
  76. this.loadAddress(0);
  77. this.selectList = this.allReadySelect;
  78. },
  79. methods: {
  80. loadAddress(address_id) {
  81. return getCityV2(address_id).then(res => {
  82. this.areaList = res.data;
  83. });
  84. },
  85. // 选择地址
  86. async selectArea(item) {
  87. // 如果title被选中,选择子项时,删除后面所有title——item
  88. if (this.selectTapIndex > -1) {
  89. this.tapList.splice(this.selectTapIndex, 999);
  90. this.loadAddress(item.id);
  91. this.tapList.push(item);
  92. this.isShowLastItem = true;
  93. this.selectTap = -1;
  94. this.selectTapIndex = -1;
  95. this.goTop();
  96. return;
  97. }
  98. // 当所选择项拥有子集时
  99. if (item.snum) {
  100. this.loadAddress(item.id);
  101. this.tapList.push(item);
  102. this.isShowLastItem = true;
  103. this.selectTap = -1;
  104. this.goTop();
  105. return;
  106. }
  107. // 当选择项没有子集时
  108. if (!item.snum) {
  109. return;
  110. if (this.isShowLastItem) {
  111. this.tapList.push(item);
  112. } else {
  113. this.tapList.splice(this.tapList.length - 1, 1, item);
  114. }
  115. this.isShowLastItem = false;
  116. this.goTop();
  117. return;
  118. }
  119. },
  120. // 选择tap
  121. async selectTapItem(item, index) {
  122. this.selectTap = item.id;
  123. this.selectTapIndex = index;
  124. await this.loadAddress(item.parent_id);
  125. },
  126. // 点击请选择
  127. selectTapLastItem(val) {
  128. this.selectTap = -1;
  129. if (!this.tapList.length) {
  130. this.loadAddress(0);
  131. return;
  132. }
  133. this.loadAddress(this.tapList[this.tapList.length - 1].id);
  134. },
  135. // 点击加号事件
  136. handlyAddSelect(item) {
  137. if (this.selectList.some(val => val.id == item.id)) {
  138. Toast('已经选择过了')
  139. return
  140. }
  141. if (this.selectTapIndex > -1) {
  142. this.tapList.splice(this.selectTapIndex, 999);
  143. }
  144. if (!item.parent_id) {
  145. this.selectList.push(item);
  146. return;
  147. }
  148. let str = '';
  149. str =
  150. serialize(this.tapList)
  151. .map(val => val.name)
  152. .join('/') +
  153. '/' +
  154. item.name;
  155. this.selectList.push({ ...item, name: str });
  156. },
  157. // 删除所选地址
  158. delSelectItem(item, index) {
  159. console.log(1);
  160. this.selectList.splice(index, 1);
  161. },
  162. // 点击确定按钮,抛出已选项
  163. handleGetSelectArea() {
  164. this.$emit('handleGetSelectArea', this.selectList);
  165. },
  166. close() {
  167. this.$emit('close');
  168. },
  169. // 数组去重
  170. unique(arr) {
  171. var obj = {};
  172. return arr.filter(ele => {
  173. if (!obj[ele]) {
  174. obj[ele] = true;
  175. return true;
  176. }
  177. });
  178. },
  179. scroll : function(e) {
  180. this.old.scrollTop = e.detail.scrollTop
  181. },
  182. goTop: function(e) {
  183. this.scrollTop = this.old.scrollTop
  184. this.$nextTick(() => {
  185. this.scrollTop = 0
  186. });
  187. }
  188. }
  189. };
  190. </script>
  191. <style lang="scss" scoped>
  192. .area_container {
  193. background: #fff;
  194. border-radius: 16px 16px 0px 0px;
  195. &_title {
  196. display: flex;
  197. justify-content: space-between;
  198. padding: 36rpx 30rpx 46rpx 0;
  199. > view {
  200. flex: 1;
  201. }
  202. &_name {
  203. text-align: center;
  204. font-weight: bold;
  205. font-size: 32rpx;
  206. }
  207. &_close {
  208. text-align: right;
  209. }
  210. }
  211. &_content {
  212. padding: 0 30rpx;
  213. .selectList_con {
  214. display: flex;
  215. flex-wrap: wrap;
  216. margin-bottom: 50rpx;
  217. &_item {
  218. padding: 3rpx 10rpx;
  219. background: #fff6f5;
  220. color: #e93323;
  221. margin-right: 20rpx;
  222. margin-bottom: 20rpx;
  223. display: flex;
  224. align-items: center;
  225. font-size: 22rpx;
  226. > span:nth-child(1) {
  227. display: inline-block;
  228. margin-right: 14rpx;
  229. white-space: nowrap;
  230. }
  231. .iconfont {
  232. font-size: 20rpx;
  233. margin-left: 6rpx;
  234. }
  235. }
  236. }
  237. .selectList_tap {
  238. border-bottom: 1px solid #eeeeee;
  239. display: flex;
  240. &_item {
  241. font-size: 28rpx;
  242. margin-right: 60rpx;
  243. white-space: nowrap;
  244. }
  245. .selectTap {
  246. color: #e93323;
  247. border-bottom: 3rpx solid #e93323;
  248. padding-bottom: 21rpx;
  249. }
  250. }
  251. .selectList_area {
  252. .scroll {
  253. height: 597rpx;
  254. }
  255. .selectList_area_item {
  256. padding: 40rpx 0;
  257. display: flex;
  258. justify-content: space-between;
  259. font-size: 28rpx;
  260. .iconfont {
  261. color: #e93323;
  262. font-size: 40rpx;
  263. }
  264. .selectList_area_item_name {
  265. flex: 0.7;
  266. }
  267. }
  268. }
  269. .handle {
  270. height: 126rpx;
  271. &_button {
  272. width: 690rpx;
  273. height: 86rpx;
  274. background: #e93323;
  275. border-radius: 43rpx;
  276. display: flex;
  277. align-items: center;
  278. justify-content: center;
  279. font-size: 32rpx;
  280. color: #fff;
  281. }
  282. }
  283. }
  284. }
  285. </style>