top.vue 11 KB

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