top.vue 13 KB

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