NavBar.vue 11 KB

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