NavBar.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/logo.png" class="logo" alt="img">
  8. tnbapp
  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>
  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="searchHint"
  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-s-home"
  63. class="size"
  64. @click.native="goToUserHome"
  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-menu"
  79. class="size"
  80. @click.native="goToBackground"
  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-dropdown-menu>
  88. </el-dropdown>
  89. <span
  90. v-else
  91. style="color: #007bff"
  92. @click="login"
  93. >登入</span>
  94. </li>
  95. <li class="el-menu-item" @click="goToTimeline">
  96. <el-badge class="item" :value="statusCount" :max="99">
  97. <span class="el-icon-view" style="color: #007bff">状态</span>
  98. </el-badge>
  99. </li>
  100. <li class="el-menu-item" @click="goToMessage">
  101. <el-badge class="item" :value="msgCount" :max="99">
  102. <span class="el-icon-bell" style="color: #007bff">消息</span>
  103. </el-badge>
  104. </li>
  105. <li class="el-menu-item">
  106. <el-button size="mini" type="upload" icon="el-icon-upload" @click="goToPublish">投稿</el-button>
  107. </li>
  108. </ul>
  109. </el-col>
  110. <!--登入弹窗-->
  111. <el-dialog
  112. title="用户登入"
  113. append-to-body
  114. :visible.sync="dialogVisible"
  115. width="30%"
  116. center
  117. >
  118. <el-form ref="form" :model="userLogin">
  119. <el-form-item label="帐号">
  120. <el-input
  121. v-model="userLogin.principal"
  122. placeholder="请输入手机号或邮箱"
  123. style="width: 70%; padding-right: 2px"
  124. clearable
  125. />
  126. </el-form-item>
  127. <el-form-item label="密码">
  128. <el-input
  129. v-model="userLogin.credential"
  130. placeholder="请输入验证码"
  131. style="width: 45%; padding-right: 2px"
  132. />
  133. <el-button :disabled="isBtn" @click="loginVerifyCode">{{ code }}</el-button>
  134. </el-form-item>
  135. <el-form-item label="图形验证码" label-width="90px">
  136. <el-image :src="captchaCode" @click="getCaptcha" />
  137. <el-input
  138. v-model="userLogin.captchaCode"
  139. placeholder="请输入图形验证码"
  140. style="width: 45%; padding-right: 2px"
  141. />
  142. </el-form-item>
  143. <el-form-item>
  144. <el-button
  145. type="primary"
  146. :loading="isLoading"
  147. @click.native="loginBtn"
  148. >登 入</el-button>
  149. <el-button type="plain" @click="register">注册/忘记密码?</el-button>
  150. </el-form-item>
  151. </el-form>
  152. </el-dialog>
  153. </el-row>
  154. </template>
  155. <script>
  156. import { userMixin } from 'assets/js/mixin'
  157. import { keywordSuggest } from '@/api/search'
  158. import { getAuthedUser } from '@/utils/auth'
  159. import { getUnreadCount } from '@/api/user'
  160. export default {
  161. name: 'NavBar',
  162. mixins: [userMixin],
  163. data() {
  164. return {
  165. user: null,
  166. restaurants: [],
  167. searchHint: '想要搜点神马呢',
  168. searchType: '1',
  169. keyword: '',
  170. statusCount: 0,
  171. msgCount: 0
  172. }
  173. },
  174. created() {
  175. /* const userdata = Vue.$cookies.get('USERDATA')
  176. const userId = userdata.split(':')[0]*/
  177. const userInfo = getAuthedUser()
  178. if (userInfo !== null) {
  179. this.user = userInfo
  180. getUnreadCount().then(resp => {
  181. if (resp.code === 0) {
  182. this.msgCount = resp.data.total
  183. }
  184. })
  185. }
  186. },
  187. methods: {
  188. handleSelect(key, keyPath) {
  189. console.log(key, keyPath)
  190. },
  191. // ****************************************************************************************************************
  192. // 重点:当框中的改变时触发该方法,elementui自动设置了防抖,参见debounce属性
  193. // queryString 为输入框中的值。cb为返回显示列表的回调函数
  194. querySearchAsync(queryString, cb) {
  195. if (queryString === '') {
  196. return
  197. }
  198. setTimeout(() => {
  199. keywordSuggest(queryString).then(res => {
  200. if (res.code === 0) {
  201. this.restaurants = res.data.map((item) => {
  202. return {
  203. value: item.keyword,
  204. rank: 1
  205. }
  206. })
  207. // 如果 cb 返回一个空数组, 那么模糊搜索输入建议的下拉选项会因为 length 为 0 而消失
  208. // cb([])
  209. cb(this.restaurants)
  210. // eslint-disable-next-line no-empty
  211. } else {
  212. }
  213. })
  214. }, 500)
  215. },
  216. // select 事件或 enter 键事件
  217. onSearch() {
  218. console.log('回车事件')
  219. // 正则去空格
  220. if (this.keyword.replace(/\s*/g, '')) {
  221. this.toSearchPage()
  222. } else {
  223. this.$message({
  224. showClose: true,
  225. message: '不能为空!',
  226. type: 'warning'
  227. })
  228. }
  229. },
  230. // 跳转搜索页面,传递搜索框的参数
  231. toSearchPage() {
  232. const currentPath = this.$route.path
  233. if (currentPath === '/search') {
  234. this.$router.push({
  235. path: '/search',
  236. query: {
  237. searchType: 1,
  238. keyword: this.keyword,
  239. pn: 1
  240. }
  241. })
  242. this.$router.go(0)
  243. } else {
  244. const routeUrl = this.$router.resolve({
  245. path: '/search',
  246. query: {
  247. searchType: 1,
  248. keyword: this.keyword,
  249. pn: 1
  250. }
  251. })
  252. window.open(routeUrl.href, '_blank')
  253. }
  254. },
  255. // ****************************************************************************************************************
  256. login() {
  257. const path = '/login'
  258. if (this.$route.path === path) {
  259. this.$router.go(0)
  260. return
  261. }
  262. this.$router.push(path)
  263. },
  264. register() {
  265. console.log('帐号注册')
  266. },
  267. // ****************************************************************************************************************
  268. goToUserHome() {
  269. const path = '/user/' + this.user.userIdStr
  270. if (this.$route.path === path) {
  271. this.$router.go(0)
  272. return
  273. }
  274. this.$router.push(path)
  275. },
  276. goToPlaylist() {
  277. const path = '/bg/my/album'
  278. if (this.$route.path === path) {
  279. this.$router.go(0)
  280. return
  281. }
  282. this.$router.push(path)
  283. },
  284. goToHistory() {
  285. const path = '/bg/my/history'
  286. if (this.$route.path === path) {
  287. this.$router.go(0)
  288. return
  289. }
  290. this.$router.push(path)
  291. },
  292. goToBackground() {
  293. const path = '/bg'
  294. if (this.$route.path === path) {
  295. this.$router.go(0)
  296. return
  297. }
  298. this.$router.push(path)
  299. },
  300. goToTimeline() {
  301. const path = '/timeline'
  302. if (this.$route.path === path) {
  303. this.$router.go(0)
  304. return
  305. }
  306. this.$router.push(path)
  307. },
  308. goToMessage() {
  309. const path = '/bg/account/message'
  310. if (this.$route.path === path) {
  311. this.$router.go(0)
  312. return
  313. }
  314. this.$router.push(path)
  315. },
  316. goToPublish() {
  317. const path = '/bg/post/video'
  318. if (this.$route.path === path) {
  319. this.$router.go(0)
  320. return
  321. }
  322. this.$router.push(path)
  323. }
  324. }
  325. }
  326. </script>
  327. <style scoped>
  328. @media screen and (max-width: 768px) {
  329. }
  330. .logo {
  331. width: 30px;
  332. position: relative;
  333. }
  334. .size {
  335. font-size: 16px;
  336. }
  337. .item {
  338. margin-top: 10px;
  339. margin-right: 10px;
  340. }
  341. </style>