NavBar.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. tnb
  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="/playlist" style="text-decoration-line: none">
  29. <span style="color: #007bff">播放列表</span>
  30. </a>
  31. </el-menu-item>
  32. <el-menu-item index="9">
  33. <a href="/discover" style="text-decoration-line: none">
  34. <span style="color: #007bff">发现</span>
  35. </a>
  36. </el-menu-item>
  37. </el-menu>
  38. </el-col>
  39. <el-col :md="6">
  40. <ul class="el-menu--horizontal el-menu">
  41. <li class="el-menu-item">
  42. <el-autocomplete
  43. v-model="keyword"
  44. :fetch-suggestions="querySearchAsync"
  45. :placeholder="placeholder"
  46. clearable
  47. suffix-icon="el-icon-search"
  48. size="medium"
  49. :debounce="1000"
  50. @keyup.enter.native="onSearch"
  51. @select="onSearch"
  52. />
  53. </li>
  54. </ul>
  55. </el-col>
  56. <el-col :md="8">
  57. <ul class="el-menu--horizontal el-menu">
  58. <li class="el-menu-item">
  59. <el-dropdown v-if="user">
  60. <img
  61. :src="user.avatarUrl"
  62. class="el-avatar--circle el-avatar--medium"
  63. alt=""
  64. >
  65. <el-dropdown-menu v-if="user" slot="dropdown" class="iconsize">
  66. <el-dropdown-item
  67. icon="el-icon-user-solid"
  68. class="size"
  69. @click.native="goToProfile"
  70. >我的帐号</el-dropdown-item>
  71. <el-dropdown-item
  72. icon="el-icon-collection"
  73. class="size"
  74. @click.native="goToPlaylist"
  75. >播放列表</el-dropdown-item>
  76. <el-dropdown-item
  77. icon="el-icon-s-data"
  78. class="size"
  79. @click.native="goToHistory"
  80. >历史记录
  81. </el-dropdown-item>
  82. <el-dropdown-item
  83. icon="el-icon-s-home"
  84. class="size"
  85. @click.native="goToUserHome"
  86. >我的主页</el-dropdown-item>
  87. <el-dropdown-item
  88. icon="el-icon-error"
  89. class="size"
  90. @click.native="goToLogout"
  91. >退出</el-dropdown-item>
  92. <el-divider />
  93. <el-dropdown-item
  94. icon="el-icon-s-home"
  95. class="size"
  96. @click.native="goToExam"
  97. >我的考试</el-dropdown-item>
  98. <el-dropdown-item
  99. icon="el-icon-map-location"
  100. class="size"
  101. @click.native="goToMap"
  102. >我的地图</el-dropdown-item>
  103. <el-dropdown-item
  104. icon="el-icon-goods"
  105. class="size"
  106. @click.native="goToMall"
  107. >我的商城</el-dropdown-item>
  108. <el-dropdown-item
  109. icon="el-icon-chat-dot-round"
  110. class="size"
  111. @click.native="goToChat"
  112. >我的IM</el-dropdown-item>
  113. </el-dropdown-menu>
  114. </el-dropdown>
  115. <span
  116. v-else
  117. style="color: #007bff"
  118. @click="login"
  119. >登录</span>
  120. </li>
  121. <li class="el-menu-item" @click="goToTimeline">
  122. <el-badge class="item" :value="statusCount" :max="99">
  123. <span class="el-icon-view" style="color: #007bff">状态</span>
  124. </el-badge>
  125. </li>
  126. <li class="el-menu-item" @click="goToMessage">
  127. <el-badge class="item" :value="msgCount" :max="99">
  128. <span class="el-icon-bell" style="color: #007bff">消息</span>
  129. </el-badge>
  130. </li>
  131. <li class="el-menu-item">
  132. <el-button size="mini" type="upload" icon="el-icon-upload" @click="goToPublish">投稿</el-button>
  133. </li>
  134. </ul>
  135. </el-col>
  136. <!--登录弹窗-->
  137. <el-dialog
  138. title="用户登录"
  139. append-to-body
  140. :visible.sync="dialogVisible"
  141. width="30%"
  142. center
  143. >
  144. <el-form ref="form" :model="userLogin">
  145. <el-form-item label="帐号">
  146. <el-input
  147. v-model="userLogin.principal"
  148. placeholder="请输入手机号或邮箱"
  149. style="width: 70%; padding-right: 2px"
  150. clearable
  151. />
  152. </el-form-item>
  153. <el-form-item label="密码">
  154. <el-input
  155. v-model="userLogin.credential"
  156. placeholder="请输入验证码"
  157. style="width: 45%; padding-right: 2px"
  158. />
  159. <el-button :disabled="isBtn" @click="loginVerifyCode">{{ code }}</el-button>
  160. </el-form-item>
  161. <el-form-item label="图形验证码" label-width="90px">
  162. <el-image :src="captchaCode" @click="getCaptcha" />
  163. <el-input
  164. v-model="userLogin.captchaCode"
  165. placeholder="请输入图形验证码"
  166. style="width: 45%; padding-right: 2px"
  167. />
  168. </el-form-item>
  169. <el-form-item>
  170. <el-button
  171. type="primary"
  172. :loading="isLoading"
  173. @click.native="loginBtn"
  174. >登 录</el-button>
  175. <el-button type="plain" @click="register">注册/忘记密码?</el-button>
  176. </el-form-item>
  177. </el-form>
  178. </el-dialog>
  179. </el-row>
  180. </template>
  181. <script>
  182. import { userMixin } from 'assets/js/mixin'
  183. import { keywordSuggest } from '@/api/search'
  184. import { getAuthedUser } from '@/utils/auth'
  185. import { getUnreadCount } from '@/api/user'
  186. export default {
  187. name: 'NavBar',
  188. mixins: [userMixin],
  189. data() {
  190. return {
  191. user: null,
  192. activeIndex: '1',
  193. restaurants: [],
  194. placeholder: '想要搜点神马呢',
  195. keyword: '',
  196. statusCount: 0,
  197. msgCount: 0
  198. }
  199. },
  200. created() {
  201. /* const userdata = Vue.$cookies.get('USERDATA')
  202. const userId = userdata.split(':')[0]*/
  203. const userInfo = getAuthedUser()
  204. if (userInfo !== null) {
  205. this.user = userInfo
  206. getUnreadCount().then(resp => {
  207. if (resp.code === 0) {
  208. this.msgCount = resp.data.total
  209. }
  210. })
  211. }
  212. },
  213. methods: {
  214. handleSelect(key, keyPath) {
  215. console.log(key, keyPath)
  216. },
  217. // ****************************************************************************************************************
  218. // 重点:当框中的改变时触发该方法,elementui自动设置了防抖,参见debounce属性
  219. // queryString 为输入框中的值。cb为返回显示列表的回调函数
  220. querySearchAsync(queryString, cb) {
  221. if (queryString === '') {
  222. return
  223. }
  224. setTimeout(() => {
  225. keywordSuggest(queryString).then(res => {
  226. if (res.code === 0) {
  227. this.restaurants = res.data.map((item) => {
  228. return {
  229. value: item.keyword,
  230. rank: 1
  231. }
  232. })
  233. // 如果 cb 返回一个空数组, 那么模糊搜索输入建议的下拉选项会因为 length 为 0 而消失
  234. // cb([])
  235. cb(this.restaurants)
  236. // eslint-disable-next-line no-empty
  237. } else {
  238. }
  239. })
  240. }, 500)
  241. },
  242. // select 事件或 enter 键事件
  243. onSearch() {
  244. console.log('回车事件')
  245. // 正则去空格
  246. if (this.keyword.replace(/\s*/g, '')) {
  247. this.toSearchPage()
  248. } else {
  249. this.$message({
  250. showClose: true,
  251. message: '不能为空!',
  252. type: 'warning'
  253. })
  254. }
  255. },
  256. // 跳转搜索页面,传递搜索框的参数
  257. toSearchPage() {
  258. const currentPath = this.$route.path
  259. if (currentPath === '/search') {
  260. this.$router.push({
  261. path: '/search',
  262. query: {
  263. keyword: this.keyword,
  264. pageNumber: 1
  265. }
  266. })
  267. this.$router.go(0)
  268. } else {
  269. const routeUrl = this.$router.resolve({
  270. path: '/search',
  271. query: {
  272. keyword: this.keyword,
  273. pageNumber: 1
  274. }
  275. })
  276. window.open(routeUrl.href, '_blank')
  277. }
  278. },
  279. // ****************************************************************************************************************
  280. login() {
  281. const path = '/login'
  282. if (this.$route.path === path) {
  283. this.$router.go(0)
  284. return
  285. }
  286. this.$router.push(path)
  287. },
  288. register() {
  289. console.log('帐号注册')
  290. },
  291. // ****************************************************************************************************************
  292. goToPlaylist() {
  293. const path = '/post/album'
  294. if (this.$route.path === path) {
  295. this.$router.go(0)
  296. return
  297. }
  298. this.$router.push(path)
  299. },
  300. goToDisk() {
  301. const path = '/disk'
  302. if (this.$route.path === path) {
  303. this.$router.go(0)
  304. return
  305. }
  306. this.$router.push(path)
  307. },
  308. goToProfile() {
  309. const path = '/my'
  310. if (this.$route.path === path) {
  311. this.$router.go(0)
  312. return
  313. }
  314. this.$router.push(path)
  315. },
  316. goToTimeline() {
  317. const path = '/timeline'
  318. if (this.$route.path === path) {
  319. this.$router.go(0)
  320. return
  321. }
  322. this.$router.push(path)
  323. },
  324. goToUserHome() {
  325. const path = '/user/' + this.user.userId
  326. if (this.$route.path === path) {
  327. this.$router.go(0)
  328. return
  329. }
  330. this.$router.push(path)
  331. },
  332. goToHistory() {
  333. if (this.$route.path === '/post/history') {
  334. this.$router.go(0)
  335. return
  336. }
  337. this.$router.push('/post/history')
  338. },
  339. goToMessage() {
  340. if (this.$route.path === '/my/message') {
  341. this.$router.go(0)
  342. return
  343. }
  344. this.$router.push('/my/message')
  345. },
  346. goToPublish() {
  347. if (this.$route.path === '/post/video/list') {
  348. this.$router.go(0)
  349. return
  350. }
  351. this.$router.push('/post/video/list')
  352. },
  353. // ************************************************************************
  354. // 我的考试
  355. goToExam() {
  356. if (this.$route.path === '/exam') {
  357. this.$router.go(0)
  358. return
  359. }
  360. this.$router.push('/exam')
  361. },
  362. // 我的地图
  363. goToMap() {
  364. if (this.$route.path === '/map') {
  365. this.$router.go(0)
  366. return
  367. }
  368. this.$router.push('/map')
  369. },
  370. // 我的商城
  371. goToMall() {
  372. if (this.$route.path === '/mall') {
  373. this.$router.go(0)
  374. return
  375. }
  376. this.$router.push('/mall')
  377. },
  378. // 我的IM
  379. goToChat() {
  380. if (this.$route.path === '/chat') {
  381. this.$router.go(0)
  382. return
  383. }
  384. this.$router.push('/chat')
  385. }
  386. // ************************************************************************
  387. }
  388. }
  389. </script>
  390. <style scoped>
  391. @media screen and (max-width: 768px) {
  392. }
  393. .logo {
  394. width: 30px;
  395. position: relative;
  396. }
  397. .size {
  398. font-size: 16px;
  399. }
  400. .item {
  401. margin-top: 10px;
  402. margin-right: 10px;
  403. }
  404. </style>