NsHeaderMid.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div class="header-in">
  3. <router-link to="/" class="header-left"><img v-if="siteInfo.logo" :src="$img(siteInfo.logo)" /></router-link>
  4. <div class="header-content">
  5. <nav>
  6. <ul>
  7. <li
  8. v-for="(nav_item, nav_index) in navList"
  9. :key="nav_index"
  10. :class="nav_item.url == navSelect ? 'router-link-active' : ''"
  11. @click="navUrl(nav_item.url, nav_item.is_blank)"
  12. >
  13. {{ nav_item.nav_title }}
  14. </li>
  15. </ul>
  16. </nav>
  17. </div>
  18. <div class="header-right">
  19. <span class="iconfont icon-xiaosuo"></span>
  20. <input type="text" :placeholder="defaultSearchWords" v-model="keyword" @keyup.enter="search" maxlength="50" />
  21. <el-button size="small" @click="search">搜索</el-button>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. import { mapGetters } from 'vuex';
  27. import { apiDefaultSearchWords } from '@/api/pc';
  28. import { navList } from '@/api/website';
  29. export default {
  30. props: {},
  31. data() {
  32. return {
  33. keyword: '',
  34. defaultSearchWords: '请输入您要查询的商品',
  35. cartTotalPrice: 0,
  36. navList: [],
  37. navSelect: '',
  38. searchType: 'goods'
  39. };
  40. },
  41. components: {},
  42. computed: {
  43. ...mapGetters(['siteInfo', 'defaultGoodsImage', 'member'])
  44. },
  45. created() {
  46. this.keyword = this.$route.query.keyword || '';
  47. this.$store.dispatch('site/siteInfo');
  48. this.getDefaultSearchWords();
  49. this.nav();
  50. },
  51. watch: {
  52. $route(curr) {
  53. this.initNav(curr.path);
  54. if (this.keyword !== curr.query.keyword) {
  55. this.keyword = curr.query.keyword;
  56. }
  57. if (curr.path == '/goods/list') {
  58. this.navSelect = '';
  59. }
  60. },
  61. member() {
  62. if (!this.member) {
  63. this.$store.commit('cart/SET_CART_COUNT', 0);
  64. this.cartTotalPrice = 0;
  65. }
  66. }
  67. },
  68. methods: {
  69. search() {
  70. if (this.searchType == 'goods')
  71. this.$router.push({
  72. path: '/goods/list',
  73. query: {
  74. keyword: this.keyword ? this.keyword : this.defaultSearchWords
  75. }
  76. });
  77. else
  78. this.$router.push({
  79. path: '/street',
  80. query: {
  81. keyword: this.keyword
  82. }
  83. });
  84. },
  85. getDefaultSearchWords() {
  86. apiDefaultSearchWords({}).then(res => {
  87. if (res.code == 0 && res.data.words) {
  88. this.defaultSearchWords = res.data.words;
  89. }
  90. });
  91. },
  92. nav() {
  93. navList({})
  94. .then(res => {
  95. if (res.code == 0 && res.data) {
  96. this.navList = res.data;
  97. for (let i in this.navList) {
  98. this.navList[i]['url'] = JSON.parse(this.navList[i]['nav_url']).url;
  99. }
  100. this.initNav(this.$route.path);
  101. }
  102. })
  103. .catch(err => {
  104. this.$message.error(err.message);
  105. });
  106. },
  107. initNav(path) {
  108. for (let i in this.navList) {
  109. if (this.navList[i]['url'] == path) {
  110. this.navSelect = path;
  111. continue;
  112. }
  113. }
  114. },
  115. navUrl(url, target) {
  116. if (!url) return;
  117. if (url.indexOf('http') == -1 || url.indexOf('https') == -1) {
  118. if (target) {
  119. let routeUrl = this.$router.resolve({
  120. path: url
  121. });
  122. window.open(routeUrl.href, '_blank');
  123. } else
  124. this.$router.push({
  125. path: url
  126. });
  127. } else {
  128. if (target) window.open(url);
  129. else window.location.href = url;
  130. }
  131. }
  132. }
  133. };
  134. </script>
  135. <style scoped lang="scss">
  136. .header-in {
  137. display: flex;
  138. align-items: center;
  139. width: $width;
  140. height: 100px;
  141. margin: auto;
  142. .header-left {
  143. max-width: 200px;
  144. max-height: 60px;
  145. margin-right: 40px;
  146. overflow: hidden;
  147. img {
  148. max-width: 100%;
  149. max-height: 100%;
  150. }
  151. }
  152. .header-content {
  153. overflow: hidden;
  154. flex: 1;
  155. &::-webkit-scrollbar {
  156. width: 10px;
  157. height: 5px;
  158. }
  159. &::-webkit-scrollbar-track {
  160. background: rgb(179, 177, 177);
  161. border-radius: 10px;
  162. }
  163. &::-webkit-scrollbar-thumb {
  164. background: rgb(136, 136, 136);
  165. border-radius: 10px;
  166. }
  167. }
  168. .header-right {
  169. display: flex;
  170. align-items: center;
  171. margin-left: 20px;
  172. width: 250px;
  173. height: 36px;
  174. border-bottom: 1px solid #f2f2f2;
  175. box-sizing: border-box;
  176. .iconfont {
  177. color: #999;
  178. margin-left: 10px;
  179. font-size: 18px;
  180. }
  181. input {
  182. height: 22px;
  183. background: none;
  184. outline: none;
  185. border: none;
  186. padding: 0 10px;
  187. font-size: 14px;
  188. }
  189. button {
  190. border: none;
  191. color: $base-color;
  192. font-size: 16px;
  193. padding: 0;
  194. }
  195. }
  196. nav {
  197. ul {
  198. margin: 0;
  199. padding: 0;
  200. width: 100%;
  201. height: 28px;
  202. overflow: hidden;
  203. li {
  204. cursor: pointer;
  205. list-style: none;
  206. margin-right: 20px;
  207. font-size: 16px;
  208. float: left;
  209. &:last-of-type {
  210. margin-right: 0;
  211. }
  212. }
  213. li:hover {
  214. color: $base-color;
  215. }
  216. .router-link-active {
  217. color: $base-color;
  218. }
  219. }
  220. }
  221. }
  222. </style>