top.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <template>
  2. <div>
  3. <el-row
  4. class="flex flex-align-center flex-justify-between top"
  5. :class="dataType === 'project' ? 'bread' : 'bread-g'"
  6. >
  7. <el-col :span="12">
  8. <div class="flex flex-justify-start flex-align-center padding">
  9. <el-button
  10. icon="Menu"
  11. class="mr-20 white"
  12. circle
  13. color="#C1C4CB"
  14. @click="openMenu"
  15. v-if="mini && showMenu"
  16. />
  17. <router-link to="/">
  18. <div class="flex flex-center">
  19. <img v-if="dataType === 'project'" src="@/assets/svg/logo.svg" />
  20. <img v-else src="@/assets/svg/logo-white.svg" />
  21. </div>
  22. </router-link>
  23. </div>
  24. </el-col>
  25. <el-col :span="12">
  26. <div class="flex-child-average flex-justify-end flex padding-right">
  27. <div
  28. class="padding flex flex-align-center pointer"
  29. :class="dataType === 'project' ? 'black' : 'white'"
  30. >
  31. <el-button
  32. icon="Search"
  33. circle
  34. color="#C1C4CB"
  35. @click="show = true"
  36. />
  37. <el-badge
  38. :value="msgCount"
  39. :max="99"
  40. class="ml-20"
  41. :hidden="msgCount === null || msgCount === 0"
  42. >
  43. <el-button
  44. icon="Bell"
  45. circle
  46. color="#C1C4CB"
  47. @click="$router.push('/msg')"
  48. />
  49. </el-badge>
  50. </div>
  51. <div class="padding flex flex-align-center pointer">
  52. <el-avatar
  53. class="mr-10"
  54. :size="30"
  55. :src="
  56. user.info.avatarUrl && user.info.avatarUrl.length > 0
  57. ? user.info.avatarUrl
  58. : 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
  59. "
  60. />
  61. <el-dropdown @command="dropDown">
  62. <span class="flex flex-center">
  63. {{ user.info.userName }} / {{ user.info.deptName }}
  64. <el-icon class="el-icon--right">
  65. <arrow-down />
  66. </el-icon>
  67. </span>
  68. <template #dropdown>
  69. <el-dropdown-menu>
  70. <el-dropdown-item command="info">个人中心</el-dropdown-item>
  71. <el-dropdown-item
  72. command="manage"
  73. v-if="permission.vaildPermission('user_manger')"
  74. >用户管理</el-dropdown-item
  75. >
  76. <el-dropdown-item command="logout">退出登录</el-dropdown-item>
  77. </el-dropdown-menu>
  78. </template>
  79. </el-dropdown>
  80. </div>
  81. </div>
  82. </el-col>
  83. </el-row>
  84. <search :show="show" @close="show = false" />
  85. </div>
  86. </template>
  87. <script>
  88. import navStore from '../store/nav.js'
  89. import { useStore } from '../store/user.js'
  90. import permissionStore from '@/store/permission.js'
  91. import search from './search/index.vue'
  92. import config from '../config/website.js'
  93. export default {
  94. name: 'top',
  95. components: { search },
  96. setup() {
  97. const nav = navStore()
  98. const user = useStore()
  99. const permission = permissionStore()
  100. return { nav, user, permission }
  101. },
  102. props: {
  103. showMenu: {
  104. type: Boolean,
  105. default: true
  106. }
  107. },
  108. data() {
  109. return {
  110. lockReconnect: false, // 是否真正建立连接
  111. timeout: 58 * 1000, // 58秒一次心跳
  112. timeoutObj: null, // 心跳心跳倒计时
  113. serverTimeoutObj: null, // 心跳倒计时
  114. timeoutnum: null, // 断开 重连倒计时
  115. dataType: 'project',
  116. showImage: false,
  117. imgList: [],
  118. show: false,
  119. keywords: '',
  120. active: 0,
  121. type: [
  122. {
  123. key: 'project',
  124. value: '项目',
  125. count: 0
  126. },
  127. {
  128. key: 'file',
  129. value: '文档',
  130. count: 0
  131. },
  132. {
  133. key: 'picture',
  134. value: '图片',
  135. count: 0
  136. }
  137. ],
  138. params: {
  139. keyword: '',
  140. searchType: 'project',
  141. pages: 1,
  142. size: 10
  143. },
  144. data: [],
  145. total: '',
  146. isAll: 1,
  147. filterTypeName: '全部',
  148. resultCount: 0,
  149. title: '',
  150. mini: false,
  151. msgCount: 0
  152. }
  153. },
  154. created() {
  155. const tmp = localStorage.getItem('data-type')
  156. this.title = config.title
  157. if (tmp) {
  158. this.dataType = tmp
  159. }
  160. this.$bus.on('navChange', res => {
  161. console.log(res)
  162. this.dataType = res
  163. })
  164. this.$bus.on('sizeChange', res => {
  165. if (res < 1690) {
  166. this.mini = true
  167. } else {
  168. this.mini = false
  169. }
  170. })
  171. this.$bus.on('read', () => {
  172. this.readCount()
  173. })
  174. this.initWebSocket()
  175. },
  176. mounted() {
  177. this.initWebSocket()
  178. this.readCount()
  179. },
  180. unmounted() {
  181. this.websock.close() // 离开路由之后断开websocket连接
  182. },
  183. methods: {
  184. readCount() {
  185. this.$api.msg.count().then(res => {
  186. if (res.code === 200) {
  187. this.msgCount = res.data
  188. }
  189. })
  190. },
  191. initWebSocket() {
  192. const wsuri =
  193. location.host.indexOf('dev') > -1
  194. ? 'wss://dev.wutongshucloud.com/ws/websocket/' + this.user.info.userId
  195. : config.wss + this.user.info.userId
  196. this.websock = new WebSocket(wsuri)
  197. this.websock.onmessage = this.websocketonmessage
  198. // 连接建立时触发
  199. this.websock.onopen = this.websocketonopen
  200. // 通信发生错误时触发
  201. this.websock.onerror = this.websocketonerror
  202. // 连接关闭时触发
  203. this.websock.onclose = this.websocketclose
  204. },
  205. websocketonopen() {
  206. // 开启心跳
  207. this.start()
  208. // 连接建立之后执行send方法发送数据
  209. // const actions = {
  210. // room: '007854ce7b93476487c7ca8826d17eba',
  211. // info: '1121212'
  212. // }
  213. // this.websocketsend(JSON.stringify(actions))
  214. },
  215. // 通信发生错误时触发
  216. websocketonerror() {
  217. console.log('出现错误')
  218. this.reconnect()
  219. },
  220. // 客户端接收服务端数据时触发
  221. websocketonmessage(e) {
  222. console.log(e.data)
  223. const json = JSON.parse(e.data)
  224. // 收到服务器信息,心跳重置
  225. this.readCount()
  226. this.$notify({
  227. message: json.msgTxt,
  228. type: 'warning'
  229. })
  230. this.reset()
  231. },
  232. websocketsend(Data) {
  233. // 数据发送
  234. this.websock.send(Data)
  235. },
  236. // 连接关闭时触发
  237. websocketclose(e) {
  238. // 关闭
  239. console.log('断开连接', e)
  240. // 重连
  241. this.reconnect()
  242. },
  243. reconnect() {
  244. // 重新连接
  245. const that = this
  246. if (that.lockReconnect) {
  247. return
  248. }
  249. that.lockReconnect = true
  250. // 没连接上会一直重连,设置延迟避免请求过多
  251. that.timeoutnum && clearTimeout(that.timeoutnum)
  252. that.timeoutnum = setTimeout(function () {
  253. // 新连接
  254. that.initWebSocket()
  255. that.lockReconnect = false
  256. }, 5000)
  257. },
  258. reset() {
  259. // 重置心跳
  260. const that = this
  261. // 清除时间
  262. clearTimeout(that.timeoutObj)
  263. clearTimeout(that.serverTimeoutObj)
  264. // 重启心跳
  265. that.start()
  266. },
  267. start() {
  268. // 开启心跳
  269. console.log('开启心跳')
  270. const self = this
  271. self.timeoutObj && clearTimeout(self.timeoutObj)
  272. self.serverTimeoutObj && clearTimeout(self.serverTimeoutObj)
  273. self.timeoutObj = setTimeout(function () {
  274. // 这里发送一个心跳,后端收到后,返回一个心跳消息,
  275. console.log(self.websock)
  276. if (self.websock.readyState === 1) {
  277. // 如果连接正常
  278. self.websock.send('heartCheck') // 这里可以自己跟后端约定
  279. } else {
  280. // 否则重连
  281. // self.reconnect()
  282. }
  283. self.serverTimeoutObj = setTimeout(function () {
  284. // 超时关闭
  285. // self.websock.close()
  286. }, self.timeout)
  287. }, self.timeout)
  288. },
  289. dropDown(res) {
  290. if (res === 'info') {
  291. this.$router.push('/user')
  292. } else if (res === 'manage') {
  293. this.$router.push('/user/manage')
  294. } else if (res === 'logout') {
  295. this.$api.login.logout().then(res => {
  296. if (res.success === 'true') {
  297. this.nav.cleanMenu()
  298. try {
  299. // this.permission.cleanPermission()
  300. } catch (err) {
  301. console.log(err)
  302. }
  303. this.$message.success('退出登录')
  304. this.$router.replace('/login')
  305. } else {
  306. this.$message.error(res.msg)
  307. }
  308. })
  309. } else if (res === '全部') {
  310. this.filterTypeName = res
  311. this.params.searchType = 'all'
  312. this.isAll = 1
  313. this.searchTotal()
  314. } else if (res === '项目') {
  315. this.filterTypeName = res
  316. this.isAll = 0
  317. this.params.searchType = 'project'
  318. this.searchTotal()
  319. } else if (res === '文档') {
  320. this.filterTypeName = res
  321. this.isAll = 0
  322. this.params.searchType = 'file'
  323. this.searchTotal()
  324. } else if (res === '图片') {
  325. this.filterTypeName = res
  326. this.isAll = 0
  327. this.params.searchType = 'picture'
  328. this.searchTotal()
  329. }
  330. },
  331. change(index, key) {
  332. this.active = index
  333. this.params.searchType = key
  334. if (this.params.searchType === 'all') {
  335. this.isAll = 1
  336. this.getTotal()
  337. this.$api.project.totalSearch(this.params).then(res => {
  338. if (res.code === 200) {
  339. this.data = res.data.result
  340. this.total = res.data.total
  341. }
  342. })
  343. } else {
  344. this.getTotal()
  345. this.$api.project.totalSearch(this.params).then(res => {
  346. if (res.code === 200) {
  347. this.data = res.data.result
  348. this.total = res.data.total
  349. }
  350. })
  351. }
  352. },
  353. searchTotal(res) {
  354. if (res.key === 'Enter' || res === 1) {
  355. this.getTotal()
  356. this.$api.project.totalSearch(this.params).then(res => {
  357. if (res.code === 200) {
  358. this.data = res.data.result
  359. this.total = res.data.total
  360. }
  361. })
  362. }
  363. },
  364. getTotal() {
  365. this.resultCount = 0
  366. this.$api.project.total({ keyword: this.params.keyword }).then(res => {
  367. if (res.code === 200) {
  368. this.type[0].count = res.data.projectTotal
  369. this.type[1].count = res.data.fileTotal
  370. this.type[2].count = res.data.pictureTotal
  371. this.type.forEach(sub => {
  372. this.resultCount = this.resultCount + sub.count
  373. })
  374. }
  375. })
  376. },
  377. detail(res) {
  378. if (this.params.searchType === 'project') {
  379. this.$api.project.projectInfo(res.projectId).then(res => {
  380. if (res.code === 200) {
  381. console.log(res)
  382. this.$router.push({
  383. path: '/home/details',
  384. query: {
  385. id: res.data.id,
  386. type: '0',
  387. ownerId: res.data.createUser
  388. }
  389. })
  390. }
  391. })
  392. this.show = false
  393. } else if (this.params.searchType === 'file') {
  394. const routeData = this.$router.resolve({
  395. path: '/home/file_detail',
  396. query: {
  397. id: res.fileId.replace('fileid-', ''),
  398. search: true,
  399. keywords: this.params.keyword
  400. }
  401. })
  402. window.open(routeData.href, '_blank')
  403. } else if (this.params.searchType === 'picture') {
  404. this.imgList = [res.url]
  405. this.showImage = true
  406. }
  407. },
  408. /**
  409. * 高级搜索框关闭
  410. */
  411. close() {
  412. this.params.keyword = ''
  413. this.show = false
  414. },
  415. openMenu() {
  416. this.$emit('openMenu')
  417. }
  418. }
  419. }
  420. </script>
  421. <style lang="scss" scoped>
  422. .top {
  423. height: 60px;
  424. z-index: 22;
  425. top: 0;
  426. left: 0;
  427. right: 0;
  428. padding: 0 40px;
  429. position: fixed;
  430. border-bottom: whitesmoke solid 1px;
  431. box-shadow: 0 6px 8px 0 rgba(51, 51, 51, 0.05098);
  432. }
  433. .avatar {
  434. background-color: #c4c3c3;
  435. width: 30px;
  436. height: 30px;
  437. border-radius: 15px;
  438. }
  439. .tab {
  440. width: 238px;
  441. height: 38px;
  442. background-color: #edf0f3;
  443. padding: 2px 10px;
  444. color: #707070;
  445. font-size: 13px;
  446. }
  447. .tab-active {
  448. width: 160px;
  449. flex-wrap: nowrap;
  450. color: white;
  451. font-size: 15px;
  452. background-color: #ab7630;
  453. font-weight: 500;
  454. padding: 4px 10px;
  455. border-radius: 20px;
  456. text-align: center;
  457. }
  458. .bread {
  459. background: white;
  460. color: black;
  461. min-width: 1300px;
  462. }
  463. :deep(.el-icon) {
  464. --color: white !important;
  465. }
  466. .bread-g {
  467. background: #3978f1;
  468. min-width: 1300px;
  469. :deep(.el-breadcrumb__inner a) {
  470. color: white;
  471. }
  472. :deep(.el-dropdown) {
  473. color: white;
  474. }
  475. :deep(.el-icon) {
  476. color: white;
  477. }
  478. :deep(.el-breadcrumb__separator) {
  479. color: white;
  480. }
  481. }
  482. </style>