selectCity.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <template>
  2. <view>
  3. <u-navbar :title="title" @leftClick='back' :placeholder='true'>
  4. </u-navbar>
  5. <view class="row1">
  6. <view class="start-place" :class="type==0?'active1':''" @click="changeCity(0)">选择发货地</view>
  7. <view class="end-place" :class="type==1?'active1':''" @click="changeCity(1)">选择收货地</view>
  8. </view>
  9. <view class="container">
  10. <view class="row2">
  11. <view class="content1" v-if="type==0">
  12. <view class="history-city-title">历史发货地</view>
  13. <view class="city-list">
  14. <view class="city" v-for="(item,index) in historyFcityList" :key='index'>
  15. {{item}}
  16. </view>
  17. </view>
  18. </view>
  19. <view class="content2" v-if="type==1">
  20. <view class="history-city-title">历史收货地</view>
  21. <view class="city-list">
  22. <view class="city" v-for="(item,index) in historyScityList" :key='index'>
  23. {{item.selected}}
  24. </view>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. <view class="page">
  30. <view class="title flex flex-space-between">
  31. <view class="flex nav-bag-left">
  32. <!-- 省 -->
  33. <view class="flex" :class="(type==0?fstatus:sstatus)==0?'select-active':''" @click="titleClick(0)">
  34. <view class="tt point">{{type==0?fchooseprovince:schooseprovince}}</view>
  35. <view v-if="type==0?fchooseprovince:schooseprovince" style="margin: 0 10rpx;">></view>
  36. </view>
  37. <!-- 市 -->
  38. <view class="flex"
  39. v-if="(type==0?fchooseprovince:schooseprovince)!='选择'&&(type==0?fchooseprovince:schooseprovince)!='全国'"
  40. :class="(type==0?fstatus:sstatus)==1?'select-active':''" @click="titleClick(1)">
  41. <view class="tt point nav-point">{{type==0?fchoosecity:schoosecity}}</view>
  42. <view v-if="type==0?fchoosecity:schoosecity" style="margin: 0 10rpx;">></view>
  43. </view>
  44. <!-- 区 -->
  45. <view class="tt area"
  46. v-if="(type==0?fchooseprovince:schooseprovince)!='选择'&&(type==0?fchoosecity:schoosecity)!='请选择市'&&(type==0?fchoosecity:schoosecity)!='全部'"
  47. :class="(type==0?fstatus:sstatus)==2?'select-active':''" @click="titleClick(2)">
  48. {{type==0?fchoosearea:schoosearea}}
  49. </view>
  50. </view>
  51. <view class="back-level" v-if="(type==0?fstatus:sstatus)!=0" @click="backLevel">返回上一级</view>
  52. </view>
  53. <view class="link">
  54. <!-- 省 -->
  55. <view class="container" v-if="(type==0?fstatus:sstatus)==0">
  56. <view class="content-item" :class="index ==(type==0?isfprovince:issprovince) ?'active':''"
  57. @click="selectProvince(item, index,(type==0?fstatus:sstatus))"
  58. v-for="(item,index) in (type==0?fprovincelist:sprovincelist)" :key="index">
  59. {{item.label}}
  60. </view>
  61. </view>
  62. <!-- 市 -->
  63. <view class="container" v-if="(type==0?fstatus:sstatus)==1">
  64. <view class="content-item" :class="index == (type==0?isfcity:isscity)?'active':''"
  65. @click="selectCity(item, index,type==0?fstatus:sstatus)"
  66. v-for="(item,index) in (type==0?fcitylist:scitylist)" :key="index">
  67. {{item.label}}
  68. </view>
  69. </view>
  70. <!-- index ==(type==0?isfarea:issarea) -->
  71. <!-- 区 -->
  72. <view class="container" v-if="(type==0?fstatus:sstatus)==2">
  73. <view class="content-item" :class="item.checked?'active':''"
  74. @click="selectaArea(item, index,(type==0?fstatus:sstatus))"
  75. v-for="(item,index) in (type==0?farealist:sarealist)" :key="index">
  76. {{item.label}}
  77. </view>
  78. </view>
  79. </view>
  80. </view>
  81. <u-button class="btn" @click="confirmBtn">确定</u-button>
  82. <u-toast ref="uToast"></u-toast>
  83. </view>
  84. </template>
  85. <script>
  86. import {
  87. address
  88. } from '@/components/data/data.js'
  89. export default {
  90. data() {
  91. return {
  92. fstatus: 0,
  93. sstatus: 0,
  94. isfprovince: null,
  95. isfcity: null,
  96. isfarea: null,
  97. issprovince: null,
  98. isscity: null,
  99. issarea: null,
  100. fprovincelist: [],
  101. sprovincelist: [],
  102. fcitylist: [],
  103. scitylist: [],
  104. farealist: [],
  105. sarealist: [],
  106. fchooseprovince: '选择',
  107. fchoosecity: '请选择市',
  108. fchoosearea: '请选择区',
  109. schooseprovince: '选择',
  110. schoosecity: '请选择市',
  111. schoosearea: '请选择区',
  112. heightCot: 0,
  113. historyFcityList: [],
  114. historyScityList: [],
  115. title: '',
  116. type: 0,
  117. count: 0,
  118. ftitle: '',
  119. stitle: '',
  120. selectList: [],
  121. }
  122. },
  123. created() {
  124. this.fprovincelist = address
  125. this.sprovincelist = address
  126. console.log('---------------------------------')
  127. console.log(this.type)
  128. },
  129. onLoad(options) {
  130. console.log(options)
  131. this.type = options.type
  132. this.title = options.startPlace + '→' + options.endPlace
  133. //获取缓存收发地
  134. if (uni.getStorageSync('fPlaceList')) {
  135. this.historyFcityList = uni.getStorageSync('fPlaceList');
  136. } else {
  137. this.historyFcityList = []
  138. }
  139. if (uni.getStorageSync('sPlaceList')) {
  140. this.historyScityList = uni.getStorageSync('sPlaceList');
  141. } else {
  142. this.historyScityList = []
  143. }
  144. //赋值地区
  145. },
  146. methods: {
  147. // 历史收发地去重
  148. makeHistoryList(val) {
  149. debugger
  150. if (this.type == 0) {
  151. } else {
  152. let _obj = {}
  153. // for (let i = 0; i < this.historyScityList.length; i++) {
  154. // // let _data = this.historyScityList[i]
  155. // }
  156. if (val.selected == '全国' || val.schoosecity == '全部') {
  157. this.historyScityList = this.historyScityList.reduce((cur, item) => {
  158. if (item.schoosecity == val.schoosecity) {
  159. _obj[item.selected] ? "" : _obj[item.selected] = true && cur.push(item);
  160. return cur;
  161. }
  162. _obj[item.selected] ? "" : _obj[item.selected] = true && cur.push(item);
  163. return cur;
  164. }, [])
  165. }
  166. return this.historyScityList
  167. }
  168. },
  169. confirmBtn() {
  170. debugger
  171. //设置地区
  172. if (this.type == 0) {
  173. if (this.fchooseprovince == '选择' || (this.fchoosecity == '请选择市' && this.fchooseprovince != '全国')) {
  174. this.$refs.uToast.show({
  175. type: 'error',
  176. message: "请选择区域!",
  177. })
  178. }
  179. console.log(this.fchooseprovince)
  180. console.log(this.fchoosecity)
  181. console.log(this.fchoosearea)
  182. // if(this.fchoosecity)
  183. // this.historyFcityList.push('')
  184. // this.fchooseprovince
  185. // this.fchoosecity
  186. // this.fchoosearea
  187. } else {
  188. if (this.schooseprovince == '选择' || (this.schoosecity == '请选择市' && this.schooseprovince != '全国')) {
  189. this.$refs.uToast.show({
  190. type: 'error',
  191. message: "请选择区域!",
  192. })
  193. }
  194. console.log(this.schooseprovince)
  195. console.log(this.schoosecity)
  196. console.log(this.schoosearea)
  197. let _obj = {
  198. schooseprovince: this.schooseprovince,
  199. schoosecity: this.schoosecity,
  200. schoosearea: this.schoosearea,
  201. selected: ''
  202. }
  203. if (this.schoosecity == '请选择市' && this.schooseprovince == '全国') {
  204. _obj.selected = this.schooseprovince
  205. this.historyScityList.unshift(_obj)
  206. let _newList = this.makeHistoryList(_obj)
  207. uni.setStorageSync('sPlaceList', _newList);
  208. }
  209. if (this.schoosearea == '请选择区' && this.schoosecity == '全部') {
  210. _obj.selected = this.schooseprovince
  211. this.historyScityList.unshift(_obj)
  212. let _newList = this.makeHistoryList(_obj)
  213. uni.setStorageSync('sPlaceList', _newList);
  214. }
  215. if (this.schoosearea == '全部') {
  216. _obj.selected = this.schoosecity
  217. this.historyScityList.unshift(_obj)
  218. let _newList = this.makeHistoryList(_obj)
  219. uni.setStorageSync('sPlaceList', _newList);
  220. }
  221. debugger
  222. if (this.schoosearea != '请选择区' && this.schoosearea != '全部') {
  223. let _list = this.schoosearea.split(',')
  224. let _newList
  225. for (let i = 0; i < _list.length; i++) {
  226. let _deepObj = uni.$u.deepClone(_obj)
  227. _deepObj.selected = _list[i]
  228. this.historyScityList.unshift(_deepObj)
  229. _newList = this.makeHistoryList(_deepObj)
  230. }
  231. console.log(_newList)
  232. uni.setStorageSync('sPlaceList', this.historyScityList);
  233. }
  234. console.log(this.historyScityList)
  235. // this.historyScityList
  236. // schooseprovince
  237. // schoosecity
  238. // schoosearea
  239. }
  240. },
  241. //返回上一级
  242. backLevel() {
  243. // this.count=0;
  244. if (this.type == 0) {
  245. this.fstatus--
  246. } else {
  247. this.sstatus--
  248. }
  249. if ((this.type == 0 ? this.fstatus : this.sstatus) == 1) {
  250. if (this.type == 0) {
  251. this.fchoosecity = '请选择市'
  252. this.fchoosearea = ''
  253. this.ftitle = ''
  254. this.isfcity = null
  255. } else {
  256. this.schoosecity = '请选择市'
  257. this.schoosearea = ''
  258. this.stitle = ''
  259. this.isscity = null
  260. }
  261. } else if ((this.type == 0 ? this.fstatus : this.sstatus) == 0) {
  262. if (this.type == 0) {
  263. this.fchooseprovince = '选择'
  264. this.isfprovince = null
  265. } else {
  266. this.schooseprovince = '选择'
  267. this.issprovince = null
  268. }
  269. }
  270. },
  271. // 点击面包屑导航
  272. titleClick(status) {
  273. this.ftitle = ''
  274. this.stitle = ''
  275. this.count = 0
  276. if (this.type == 0) {
  277. this.fstatus = status
  278. this.isfprovince = null
  279. this.isfcity = null
  280. this.isfarea = null
  281. if (this.fchoosecity == '全部') return;
  282. } else {
  283. this.sstatus = status
  284. this.issprovince = null
  285. this.isscity = null
  286. this.issarea = null
  287. if (this.fchoosecity == '全部') return;
  288. }
  289. if (status == 0) {
  290. if (this.type == 0) {
  291. this.fchooseprovince = '选择'
  292. this.fchoosecity = '请选择市'
  293. this.fchoosearea = '请选择区'
  294. } else {
  295. this.schooseprovince = '选择'
  296. this.schoosecity = '请选择市'
  297. this.schoosearea = '请选择区'
  298. }
  299. } else if (status == 1) {
  300. if (this.type == 0) {
  301. this.fchoosecity = '请选择市'
  302. this.fchoosearea = '请选择区'
  303. } else {
  304. this.schoosecity = '请选择市'
  305. this.schoosearea = '请选择区'
  306. }
  307. }
  308. },
  309. // 选择省
  310. selectProvince(item, i, statusIndex) {
  311. debugger
  312. this.count = 0;
  313. if (this.type == 0) {
  314. if (i == 0) {
  315. this.$refs.uToast.show({
  316. type: 'error',
  317. message: "发货地不可选择全国!",
  318. })
  319. return
  320. }
  321. this.fchoosecity = '请选择市'
  322. this.fchoosearea = '请选择区'
  323. this.isfprovince = i
  324. this.fchooseprovince = item.label
  325. if (item.child) {
  326. this.fcitylist = item.child
  327. }
  328. if (this.fchoosecity != '全部') {
  329. this.fstatus = statusIndex + 1
  330. }
  331. } else {
  332. this.issprovince = i
  333. this.schooseprovince = item.label
  334. if (item.label == '全国') {
  335. item.checked = !item.checked
  336. if (!item.checked) {
  337. this.issprovince = null
  338. }
  339. return
  340. } else {
  341. this.schoosecity = '请选择市'
  342. this.schoosearea = '请选择区'
  343. if (item.child) {
  344. this.scitylist = item.child
  345. }
  346. if (this.schoosecity != '全部') {
  347. this.sstatus = statusIndex + 1
  348. }
  349. }
  350. }
  351. },
  352. // 选择市
  353. selectCity(item, i, statusIndex) {
  354. this.count = 0;
  355. if (this.type == 0) {
  356. this.isfcity = i
  357. } else {
  358. this.isscity = i
  359. }
  360. if (item.child) {
  361. for (let i = 0; i < item.child.length; i++) {
  362. item.child[i].checked = false
  363. }
  364. if (this.type == 0) {
  365. this.farealist = item.child
  366. } else {
  367. this.sarealist = item.child
  368. }
  369. }
  370. if (this.type == 0) {
  371. this.fchoosecity = item.label
  372. } else {
  373. this.schoosecity = item.label
  374. }
  375. if ((this.type == 0 ? this.fchoosecity : this.schoosecity) != '全部') {
  376. if (this.type == 0) {
  377. this.fstatus = statusIndex + 1
  378. } else {
  379. this.sstatus = statusIndex + 1
  380. }
  381. }
  382. },
  383. //选择区
  384. selectaArea(item, i) {
  385. if (item.label == '全部') {
  386. if (this.type == 0) {
  387. for (let i = 0; i < this.farealist.length; i++) {
  388. if (i == 0) {
  389. this.farealist[i].checked = true
  390. } else {
  391. this.farealist[i].checked = false
  392. }
  393. }
  394. this.fchoosearea = '全部'
  395. this.$forceUpdate()
  396. return
  397. } else {
  398. for (let i = 0; i < this.sarealist.length; i++) {
  399. if (i == 0) {
  400. this.sarealist[i].checked = true
  401. } else {
  402. this.sarealist[i].checked = false
  403. }
  404. }
  405. this.schoosearea = '全部'
  406. this.$forceUpdate()
  407. return
  408. }
  409. }
  410. if (this.count < 3) {
  411. item.checked = !item.checked
  412. if (item.checked && this.count < 3) {
  413. this.count++;
  414. } else if (!item.checked && this.count < 3) {
  415. this.count--;
  416. }
  417. } else {
  418. if (item.checked) {
  419. item.checked = !item.checked
  420. this.count--;
  421. } else {
  422. this.$refs.uToast.show({
  423. type: 'error',
  424. message: "最多选择3个地区",
  425. })
  426. }
  427. }
  428. this.$forceUpdate()
  429. //设置面包屑 0发货地 1收货地
  430. if (this.type == 0) {
  431. if (item.checked && !this.fchoosearea.includes(item.label)) {
  432. if (this.ftitle) {
  433. this.ftitle += ',' + item.label
  434. } else {
  435. this.ftitle += item.label
  436. }
  437. this.fchoosearea = this.ftitle
  438. console.log(this.fchoosearea)
  439. } else {
  440. if (this.ftitle.includes(',' + item.label)) {
  441. this.ftitle = this.ftitle.replace(',' + item.label, '')
  442. this.fchoosearea = this.fchoosearea.replace(',' + item.label, '')
  443. } else {
  444. let _val = this.ftitle.split(',')
  445. let _len = _val.length;
  446. if (_len == 1) {
  447. this.ftitle = this.ftitle.replace(item.label, '')
  448. this.fchoosearea = '请选择区'
  449. } else if (_len > 1) {
  450. this.ftitle = this.ftitle.replace(item.label + ',', '')
  451. this.fchoosearea = this.fchoosearea.replace(item.label + ',', '')
  452. }
  453. }
  454. }
  455. } else {
  456. if (item.checked && !this.schoosearea.includes(item.label)) {
  457. if (this.stitle) {
  458. this.stitle += ',' + item.label
  459. } else {
  460. this.stitle += item.label
  461. }
  462. this.schoosearea = this.stitle
  463. } else {
  464. if (this.stitle.includes(',' + item.label)) {
  465. this.stitle = this.stitle.replace(',' + item.label, '')
  466. this.schoosearea = this.schoosearea.replace(',' + item.label, '')
  467. } else {
  468. let _val = this.stitle.split(',')
  469. let _len = _val.length;
  470. if (_len == 1) {
  471. this.stitle = this.stitle.replace(item.label, '')
  472. this.schoosearea = '请选择区'
  473. } else if (_len > 1) {
  474. this.stitle = this.stitle.replace(item.label + ',', '')
  475. this.schoosearea = this.schoosearea.replace(item.label + ',', '')
  476. }
  477. }
  478. }
  479. }
  480. },
  481. changeCity(type) {
  482. if (type == 0) {
  483. this.type = 0
  484. } else {
  485. this.type = 1
  486. }
  487. },
  488. back() {
  489. uni.navigateBack({
  490. })
  491. },
  492. }
  493. }
  494. </script>
  495. <style lang="scss" scoped>
  496. .row1 {
  497. display: flex;
  498. justify-content: space-evenly;
  499. margin-top: 10rpx;
  500. border-bottom: 1px solid #E6E6E6;
  501. }
  502. .start-place,
  503. .end-place {
  504. font-size: 28rpx;
  505. color: #B1B5C0;
  506. padding-bottom: 20rpx;
  507. }
  508. .active1 {
  509. position: relative;
  510. color: black;
  511. font-size: 32rpx;
  512. font-weight: 700;
  513. display: flex;
  514. justify-content: center;
  515. }
  516. .active1:after {
  517. content: '';
  518. position: absolute;
  519. bottom: 0rpx;
  520. width: 50%;
  521. left: 25%;
  522. padding: 0 20rpx;
  523. box-sizing: border-box;
  524. height: 6rpx;
  525. background: #2772FB;
  526. border-radius: 3px;
  527. }
  528. .history-city-title {
  529. font-size: 28rpx;
  530. font-weight: 700;
  531. color: #363636;
  532. margin-bottom: 20rpx;
  533. }
  534. .container {
  535. padding: 0 20rpx;
  536. }
  537. .content1 {
  538. margin: 20rpx 0;
  539. }
  540. .city-list {
  541. max-height: 118rpx;
  542. overflow: hidden;
  543. }
  544. .city {
  545. display: inline-block;
  546. font-size: 26rpx;
  547. text-align: center;
  548. padding: 8rpx 16rpx;
  549. margin: 0 20rpx 20rpx 0;
  550. height: 33rpx;
  551. line-height: 33rpx;
  552. border-radius: 4rpx;
  553. background: #F7F8FA;
  554. }
  555. .content2 {
  556. margin: 20rpx 0;
  557. }
  558. .container {
  559. // width: 100%;
  560. width: calc(100% - 40rpx);
  561. display: inline-flex;
  562. flex-flow: row wrap;
  563. align-content: flex-start;
  564. }
  565. .select-active,
  566. .back-level {
  567. color: #2772FB;
  568. font-size: 28rpx;
  569. }
  570. .content-item {
  571. background: #F7F8FA;
  572. margin: 10rpx;
  573. font-size: 30rpx;
  574. color: #333333;
  575. text-align: center;
  576. padding: 10rpx 0;
  577. border-radius: 10rx;
  578. overflow: hidden;
  579. text-overflow: ellipsis;
  580. white-space: nowrap;
  581. padding: 6rpx 8rpx;
  582. box-sizing: border-box;
  583. flex: 0 0 calc(25% - 20rpx);
  584. }
  585. .title {
  586. margin: 0 20rpx;
  587. }
  588. .active {
  589. background: #E5F0FE;
  590. border-radius: 10rpx;
  591. // border: 1px solid #2772FB;
  592. color: #2772FB;
  593. box-sizing: border-box;
  594. }
  595. .area {
  596. width: 55%;
  597. text-overflow: ellipsis;
  598. white-space: nowrap;
  599. overflow: hidden;
  600. }
  601. .nav-bag-left {
  602. width: 70%;
  603. font-size: 30rpx;
  604. }
  605. .btn {
  606. background: #2772FB;
  607. color: white;
  608. width: 80%;
  609. position: absolute;
  610. bottom: 30rpx;
  611. margin-left: 10%;
  612. border-radius: 50rpx;
  613. }
  614. </style>