NavBar.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <el-row class="el-menu-demo">
  3. <el-col :md="2">
  4. <ul role="menubar" class="el-menu--horizontal el-menu">
  5. <li role="menuitem" class="el-menu-item">
  6. <a href="/" style="color: #007bff;text-decoration-line: none">
  7. <img src="@/assets/img/icon/logo.png" class="logo" alt="img">
  8. bili
  9. </a>
  10. </li>
  11. </ul>
  12. </el-col>
  13. <el-col :md="8">
  14. <el-menu
  15. mode="horizontal"
  16. >
  17. <el-menu-item index="1">
  18. <a href="/video" style="text-decoration-line: none">
  19. <span style="color: #007bff">视频</span>
  20. </a>
  21. </el-menu-item>
  22. <el-menu-item index="2">
  23. <a href="/shortvideo" style="text-decoration-line: none">
  24. <span style="color: #007bff">短视频</span>
  25. </a>
  26. </el-menu-item>
  27. <el-menu-item index="3">
  28. <a href="/image" style="text-decoration-line: none">
  29. <span style="color: #007bff">相册</span>
  30. </a>
  31. </el-menu-item>
  32. <el-menu-item index="4">
  33. <a href="/article" style="text-decoration-line: none">
  34. <span style="color: #007bff">文章</span>
  35. </a>
  36. </el-menu-item>
  37. <el-menu-item index="5">
  38. <a href="/discover" style="text-decoration-line: none">
  39. <span style="color: #007bff">发现</span>
  40. </a>
  41. </el-menu-item>
  42. </el-menu>
  43. </el-col>
  44. <el-col :md="8">
  45. <ul class="el-menu--horizontal el-menu">
  46. <li class="el-menu-item">
  47. <el-autocomplete
  48. v-model="keyword"
  49. :fetch-suggestions="querySearchAsync"
  50. :placeholder="placeholder"
  51. clearable
  52. suffix-icon="el-icon-search"
  53. size="medium"
  54. :debounce="1000"
  55. @keyup.enter.native="onSearch"
  56. @select="onSearch"
  57. />
  58. </li>
  59. </ul>
  60. </el-col>
  61. <el-col :md="6">
  62. <ul class="el-menu--horizontal el-menu">
  63. <li class="el-menu-item">
  64. <el-dropdown v-if="user">
  65. <img
  66. :src="user.avatarUrl"
  67. class="el-avatar--circle el-avatar--medium"
  68. alt=""
  69. >
  70. <el-dropdown-menu v-if="user" slot="dropdown" class="iconsize">
  71. <el-dropdown-item
  72. icon="el-icon-user-solid"
  73. class="size"
  74. @click.native="goToProfile"
  75. >我的帐号</el-dropdown-item>
  76. <el-dropdown-item
  77. icon="el-icon-s-home"
  78. class="size"
  79. @click.native="goToHome"
  80. >我的主页</el-dropdown-item>
  81. <el-dropdown-item
  82. icon="el-icon-s-data"
  83. class="size"
  84. @click.native="goToPost"
  85. >我的稿件
  86. </el-dropdown-item>
  87. <el-dropdown-item
  88. icon="el-icon-star-on"
  89. class="size"
  90. @click.native="goToFavlist"
  91. >收藏夹
  92. </el-dropdown-item>
  93. <el-dropdown-item
  94. icon="el-icon-video-camera-solid"
  95. class="size"
  96. @click.native="goToHistory"
  97. >历史记录
  98. </el-dropdown-item>
  99. <el-dropdown-item
  100. icon="el-icon-error"
  101. class="size"
  102. @click.native="goToLogout"
  103. >退出</el-dropdown-item>
  104. </el-dropdown-menu>
  105. </el-dropdown>
  106. <span
  107. v-else
  108. style="color: #007bff"
  109. @click="login"
  110. >登录</span>
  111. </li>
  112. <li class="el-menu-item" @click="goToTimeline">
  113. <el-badge v-if="statusCount > 0" class="item" :value="statusCount" :max="99">
  114. <span class="el-icon-view" style="color: #007bff">状态</span>
  115. </el-badge>
  116. <span v-else class="el-icon-view" style="color: #007bff">状态</span>
  117. </li>
  118. <li class="el-menu-item" @click="goToMessage">
  119. <el-badge v-if="msgCount > 0" class="item" :value="msgCount" :max="99">
  120. <span class="el-icon-bell" style="color: #007bff">消息</span>
  121. </el-badge>
  122. <span v-else class="el-icon-bell" style="color: #007bff">消息</span>
  123. </li>
  124. <li class="el-menu-item">
  125. <el-button size="mini" type="upload" icon="el-icon-upload" @click="goToPublish">投稿</el-button>
  126. </li>
  127. </ul>
  128. </el-col>
  129. </el-row>
  130. </template>
  131. <script>
  132. import Vue from 'vue'
  133. import { userMixin } from 'assets/js/mixin'
  134. import { getMyInfo } from '@/api/user'
  135. import { keywordSuggest } from '@/api/search'
  136. export default {
  137. name: 'NavBar1',
  138. mixins: [userMixin],
  139. data() {
  140. return {
  141. user: null,
  142. drawer: false,
  143. activeIndex: '1',
  144. activeIndex2: '1',
  145. restaurants: [],
  146. placeholder: '想要搜点神马呢',
  147. keyword: '',
  148. timer: null,
  149. statusCount: 0,
  150. msgCount: 10,
  151. avatarUrl: '//pica.zhimg.com/v2-465c3f851b3e56844bf2be9ad5b54f95.jpg'
  152. }
  153. },
  154. created() {
  155. const userdata = Vue.$cookies.get('USERDATA')
  156. if (userdata !== null) {
  157. getMyInfo().then(res => {
  158. if (res.code === 0) {
  159. this.user = res.data
  160. }
  161. })
  162. }
  163. },
  164. methods: {
  165. handleSelect(key, keyPath) {
  166. console.log(key, keyPath)
  167. },
  168. // 重点:当框中的改变时触发该方法,elementui自动设置了防抖,参见debounce属性
  169. // queryString 为输入框中的值。cb为返回显示列表的回调函数
  170. querySearchAsync(queryString, cb) {
  171. if (queryString === '') {
  172. return
  173. }
  174. setTimeout(() => {
  175. keywordSuggest(queryString).then(res => {
  176. if (res.code === 0) {
  177. this.restaurants = res.data.map((item) => {
  178. return {
  179. value: item.keyword,
  180. rank: 1
  181. }
  182. })
  183. // 如果 cb 返回一个空数组, 那么模糊搜索输入建议的下拉选项会因为 length 为 0 而消失
  184. // cb([])
  185. cb(this.restaurants)
  186. // eslint-disable-next-line no-empty
  187. } else {
  188. }
  189. })
  190. }, 500)
  191. },
  192. // select 事件或 enter 键事件
  193. onSearch() {
  194. console.log('回车事件')
  195. // 正则去空格
  196. if (this.keyword.replace(/\s*/g, '')) {
  197. this.toSearchPage()
  198. } else {
  199. this.$message({
  200. showClose: true,
  201. message: '不能为空!',
  202. type: 'warning'
  203. })
  204. }
  205. },
  206. // 跳转搜索页面,传递搜索框的参数
  207. toSearchPage() {
  208. const currentPath = this.$route.path
  209. if (currentPath === '/search') {
  210. this.$router.push({
  211. path: '/search',
  212. query: {
  213. keyword: this.keyword,
  214. pageNumber: 1
  215. }
  216. })
  217. this.$router.go(0)
  218. } else {
  219. const routeUrl = this.$router.resolve({
  220. path: '/search',
  221. query: {
  222. keyword: this.keyword,
  223. pageNumber: 1
  224. }
  225. })
  226. window.open(routeUrl.href, '_blank')
  227. }
  228. },
  229. login() {
  230. this.fetchPubkey()
  231. this.dialogVisible2 = false
  232. this.dialogVisible = true
  233. },
  234. register() {
  235. this.fetchPubkey()
  236. this.dialogVisible2 = false
  237. this.dialogVisible = true
  238. },
  239. goToProfile() {
  240. const path = '/my'
  241. if (this.$route.path === path) {
  242. this.$router.go(0)
  243. return
  244. }
  245. this.$router.push(path)
  246. },
  247. goToTimeline() {
  248. const path = '/timeline'
  249. if (this.$route.path === path) {
  250. this.$router.go(0)
  251. return
  252. }
  253. this.$router.push(path)
  254. },
  255. goToHome() {
  256. const path = '/user/' + this.user.userId
  257. if (this.$route.path === path) {
  258. this.$router.go(0)
  259. return
  260. }
  261. this.$router.push(path)
  262. },
  263. goToPost() {
  264. if (this.$route.path === '/my/post/list/video') {
  265. this.$router.go(0)
  266. return
  267. }
  268. this.$router.push('/my/post/list/video')
  269. },
  270. goToFavlist() {
  271. if (this.$route.path === '/my/favlist/video') {
  272. this.$router.go(0)
  273. return
  274. }
  275. this.$router.push('/my/favlist/video')
  276. },
  277. goToHistory() {
  278. if (this.$route.path === '/my/visit') {
  279. this.$router.go(0)
  280. return
  281. }
  282. this.$router.push('/my/visit')
  283. },
  284. goToMessage() {
  285. if (this.$route.path === '/my/message/receive') {
  286. this.$router.go(0)
  287. return
  288. }
  289. this.$router.push('/my/message/receive')
  290. },
  291. goToPublish() {
  292. if (this.$route.path === '/my/post/publish/video') {
  293. this.$router.go(0)
  294. return
  295. }
  296. this.$router.push('/my/post/publish/video')
  297. }
  298. }
  299. }
  300. </script>
  301. <style scoped>
  302. @media screen and (max-width: 768px) {
  303. }
  304. .logo {
  305. width: 30px;
  306. position: relative;
  307. }
  308. .size {
  309. font-size: 16px;
  310. }
  311. .item {
  312. margin-top: 10px;
  313. margin-right: 10px;
  314. }
  315. </style>