top.vue 13 KB

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