top.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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.readCount()
  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. // // self.reconnect()
  277. // }
  278. // self.serverTimeoutObj = setTimeout(function () {
  279. // // 超时关闭
  280. // self.websock.close()
  281. // }, self.timeout)
  282. // }, self.timeout)
  283. },
  284. dropDown(res) {
  285. if (res === 'info') {
  286. this.$router.push('/user')
  287. } else if (res === 'logout') {
  288. this.$api.login.logout().then(res => {
  289. if (res.success === 'true') {
  290. this.nav.cleanMenu()
  291. try {
  292. // this.permission.cleanPermission()
  293. } catch (err) {
  294. console.log(err)
  295. }
  296. this.$message.success('退出登录')
  297. this.$router.replace('/login')
  298. } else {
  299. this.$message.error(res.msg)
  300. }
  301. })
  302. } else if (res === '全部') {
  303. this.filterTypeName = res
  304. this.params.searchType = 'all'
  305. this.isAll = 1
  306. this.searchTotal()
  307. } else if (res === '项目') {
  308. this.filterTypeName = res
  309. this.isAll = 0
  310. this.params.searchType = 'project'
  311. this.searchTotal()
  312. } else if (res === '文档') {
  313. this.filterTypeName = res
  314. this.isAll = 0
  315. this.params.searchType = 'file'
  316. this.searchTotal()
  317. } else if (res === '图片') {
  318. this.filterTypeName = res
  319. this.isAll = 0
  320. this.params.searchType = 'picture'
  321. this.searchTotal()
  322. }
  323. },
  324. change(index, key) {
  325. this.active = index
  326. this.params.searchType = key
  327. if (this.params.searchType === 'all') {
  328. this.isAll = 1
  329. this.getTotal()
  330. this.$api.project.totalSearch(this.params).then(res => {
  331. if (res.code === 200) {
  332. this.data = res.data.result
  333. this.total = res.data.total
  334. }
  335. })
  336. } else {
  337. this.getTotal()
  338. this.$api.project.totalSearch(this.params).then(res => {
  339. if (res.code === 200) {
  340. this.data = res.data.result
  341. this.total = res.data.total
  342. }
  343. })
  344. }
  345. },
  346. searchTotal(res) {
  347. if (res.key === 'Enter' || res === 1) {
  348. this.getTotal()
  349. this.$api.project.totalSearch(this.params).then(res => {
  350. if (res.code === 200) {
  351. this.data = res.data.result
  352. this.total = res.data.total
  353. }
  354. })
  355. }
  356. },
  357. getTotal() {
  358. this.resultCount = 0
  359. this.$api.project.total({ keyword: this.params.keyword }).then(res => {
  360. if (res.code === 200) {
  361. this.type[0].count = res.data.projectTotal
  362. this.type[1].count = res.data.fileTotal
  363. this.type[2].count = res.data.pictureTotal
  364. this.type.forEach(sub => {
  365. this.resultCount = this.resultCount + sub.count
  366. })
  367. }
  368. })
  369. },
  370. detail(res) {
  371. if (this.params.searchType === 'project') {
  372. this.$api.project.projectInfo(res.projectId).then(res => {
  373. if (res.code === 200) {
  374. console.log(res)
  375. this.$router.push({
  376. path: '/home/details',
  377. query: {
  378. id: res.data.id,
  379. type: '0',
  380. ownerId: res.data.createUser
  381. }
  382. })
  383. }
  384. })
  385. this.show = false
  386. } else if (this.params.searchType === 'file') {
  387. const routeData = this.$router.resolve({
  388. path: '/home/file_detail',
  389. query: {
  390. id: res.fileId.replace('fileid-', ''),
  391. search: true,
  392. keywords: this.params.keyword
  393. }
  394. })
  395. window.open(routeData.href, '_blank')
  396. } else if (this.params.searchType === 'picture') {
  397. this.imgList = [res.url]
  398. this.showImage = true
  399. }
  400. },
  401. /**
  402. * 高级搜索框关闭
  403. */
  404. close() {
  405. this.params.keyword = ''
  406. this.show = false
  407. },
  408. openMenu() {
  409. this.$emit('openMenu')
  410. }
  411. }
  412. }
  413. </script>
  414. <style lang="scss" scoped>
  415. .top {
  416. height: 60px;
  417. z-index: 22;
  418. top: 0;
  419. left: 0;
  420. right: 0;
  421. padding: 0 40px;
  422. position: fixed;
  423. border-bottom: whitesmoke solid 1px;
  424. box-shadow: 0 6px 8px 0 rgba(51, 51, 51, 0.05098);
  425. }
  426. .avatar {
  427. background-color: #c4c3c3;
  428. width: 30px;
  429. height: 30px;
  430. border-radius: 15px;
  431. }
  432. .tab {
  433. width: 238px;
  434. height: 38px;
  435. background-color: #edf0f3;
  436. padding: 2px 10px;
  437. color: #707070;
  438. font-size: 13px;
  439. }
  440. .tab-active {
  441. width: 160px;
  442. flex-wrap: nowrap;
  443. color: white;
  444. font-size: 15px;
  445. background-color: #ab7630;
  446. font-weight: 500;
  447. padding: 4px 10px;
  448. border-radius: 20px;
  449. text-align: center;
  450. }
  451. .bread {
  452. background: white;
  453. color: black;
  454. min-width: 1300px;
  455. }
  456. :deep(.el-icon) {
  457. --color: white !important;
  458. }
  459. .bread-g {
  460. background: #3978f1;
  461. min-width: 1300px;
  462. :deep(.el-breadcrumb__inner a) {
  463. color: white;
  464. }
  465. :deep(.el-dropdown) {
  466. color: white;
  467. }
  468. :deep(.el-icon) {
  469. color: white;
  470. }
  471. :deep(.el-breadcrumb__separator) {
  472. color: white;
  473. }
  474. }
  475. </style>