city.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <div class="city-index">
  3. <div class="container">
  4. <div class="hot-city">
  5. <div class="label">热门城市</div>
  6. <div class="city-list">
  7. <div class="city-item" v-for="(item,i) in hotCityList" :key="-i" @click="changeCity(item)">
  8. {{item.cityName}}
  9. </div>
  10. </div>
  11. </div>
  12. <div class="city-container">
  13. <div class="title">按字母选择</div>
  14. <div class="city-container-item" v-for="(list,key,index) in cityList" :key="index">
  15. <div class="city-container-label">{{key}}</div>
  16. <div class="city-list">
  17. <div class="city-item" v-for="(item,i) in list" :key="i" @click="changeCity(item)">
  18. {{item.cityName}}
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. import city from '@/uitls/citys.js';
  28. import {
  29. pinyin
  30. } from 'pinyin-pro';
  31. export default {
  32. data() {
  33. return {
  34. cityList: {},
  35. hotCityList: [{
  36. 'cityCode': '110100',
  37. 'cityName': '北京'
  38. }, {
  39. 'cityCode': '120100',
  40. 'cityName': '天津'
  41. }, {
  42. 'cityCode': '310100',
  43. 'cityName': '上海'
  44. }, {
  45. 'cityCode': '330100',
  46. 'cityName': '杭州'
  47. }, {
  48. 'cityCode': '440300',
  49. 'cityName': '深圳'
  50. }]
  51. }
  52. },
  53. created() {
  54. this.initCity();
  55. },
  56. methods: {
  57. initCity() {
  58. let obj = {}
  59. for (let i = 0; i < city.length; i++) {
  60. let firstName = pinyin(city[i].cityName, {
  61. pattern: 'first',
  62. type: 'array',
  63. toneType: 'none'
  64. })[0].toUpperCase();
  65. if (!obj[firstName]) obj[firstName] = [];
  66. obj[firstName].push(city[i]);
  67. }
  68. let sortedKeys = Object.keys(obj).sort();
  69. let sortedObj = {};
  70. sortedKeys.forEach(key => {
  71. sortedObj[key] = obj[key];
  72. });
  73. this.cityList = sortedObj;
  74. },
  75. changeCity(item) {
  76. this.$store.dispatch('app/changeCity', item);
  77. this.$router.go(-1)
  78. }
  79. }
  80. }
  81. </script>
  82. <style lang="scss">
  83. .city-index {
  84. padding-top: 65px;
  85. color: #000;
  86. .container {
  87. width: 1200px;
  88. margin: 0 auto;
  89. }
  90. .hot-city {
  91. height: 95px;
  92. display: flex;
  93. align-items: center;
  94. border-bottom: 1px solid #e7ebee;
  95. .label {
  96. font-size: 24px;
  97. font-weight: 500;
  98. line-height: 24px;
  99. margin-right: 30px;
  100. }
  101. }
  102. .city-list {
  103. display: flex;
  104. align-items: center;
  105. flex-wrap: wrap;
  106. .city-item {
  107. cursor: pointer;
  108. margin-right: 28px;
  109. &:hover {
  110. color: $--color-primary;
  111. text-decoration: underline;
  112. }
  113. }
  114. }
  115. .city-container {
  116. .title {
  117. font-size: 20px;
  118. font-weight: 500;
  119. line-height: 20px;
  120. padding-top: 32px;
  121. }
  122. .city-container-item {
  123. display: flex;
  124. padding: 20px 20px 0 20px;
  125. border-bottom: 1px solid #e7ebee;
  126. }
  127. .city-container-label {
  128. font-size: 24px;
  129. font-weight: 500;
  130. line-height: 24px;
  131. margin-right: 20px;
  132. position: relative;
  133. top: -3px;
  134. }
  135. .city-item {
  136. margin-bottom: 20px;
  137. }
  138. }
  139. }
  140. </style>