index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <!--
  2. * @Author: zhangyu <taohuzy@163.com>
  3. -->
  4. <template>
  5. <div class="notification-box" v-clickOutside="handleClose" @click="handleClickRead">
  6. <el-badge :value="unreadNum" :hidden="!unreadNum" :max="99">
  7. <i class="el-icon-message-solid"></i>
  8. </el-badge>
  9. <transition name="el-fade-in-linear">
  10. <div v-show="noticeListShow" @click.stop="" class="noticeBox">
  11. <ul class="noticeTab">
  12. <li class="noticeTab_item">
  13. 消息通知
  14. <!-- <span class="unread_num">1</span> -->
  15. </li>
  16. </ul>
  17. <div class="noticeList_scroll">
  18. <el-scrollbar style="height: 100%">
  19. <ul class="noticeList">
  20. <li
  21. class="noticeList_item"
  22. v-for="item in messageList"
  23. :key="item.id"
  24. :title="item.content.message ? item.content.message : ''"
  25. >
  26. <div class="noticeItem_box">
  27. <div class="noticeItem_text">
  28. <i :class="iconClassMap[item.type] ? iconClassMap[item.type] : 'msg-icon el-icon-warning warning-color'"></i>
  29. <p>
  30. <span>{{ `【${moduleMap[item.module] ? moduleMap[item.module] : item.module}】 ` }}</span
  31. >{{ item.title ? item.title : "" }}
  32. </p>
  33. </div>
  34. <div class="noticeItem_time">
  35. <span class="proname">{{ item.project[0].localName || "" }}</span>
  36. {{ item.createTime }}
  37. <template v-if="item.content.buttonList.length">
  38. <el-link
  39. v-for="(btn, index) in item.content.buttonList"
  40. style="float: right; font-size: 12px; margin-left: 5px"
  41. type="primary"
  42. :key="index"
  43. :href="`/image-service/common/file_get?systemId=revit&key=${btn.url}`"
  44. :download="btn.fileName ? btn.fileName : ''"
  45. >
  46. {{ btn.name ? btn.name : "" }}
  47. </el-link>
  48. </template>
  49. </div>
  50. </div>
  51. </li>
  52. </ul>
  53. </el-scrollbar>
  54. </div>
  55. <div class="notice_operate">
  56. <!-- <div class="readAll">全部标为已读</div> -->
  57. <div class="seeAll" @click="allDetails">查看全部<i class="iconfont icon-right"></i></div>
  58. </div>
  59. </div>
  60. </transition>
  61. <!-- <el-button type="primary" @click="handleClickConnectMQ">连接MQ</el-button>
  62. <el-button type="primary" @click="handleClickDisconnectMQ">断开MQ</el-button>
  63. <el-button type="primary" @click="handleClickUnsubscribe">停止接收消息</el-button><br><br>
  64. <el-input type="textarea" :autosize="{ minRows: 6, maxRows: 6}" v-model="sendMessage" placeholder="请输入要发送的内容"></el-input>
  65. <br><br>
  66. <el-button type="primary" @click="handleClickSendMessage">发送消息</el-button>
  67. <el-card style="margin-top:20px;text-align:left;">
  68. <div slot="header" class="clearfix">
  69. <span>消息历史</span>
  70. </div>
  71. <div v-for="(message, index) in messageList" :key="index" class="text item">
  72. {{ message }}
  73. </div>
  74. </el-card> -->
  75. </div>
  76. </template>
  77. <script lang="ts">
  78. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  79. // @ts-ignore
  80. import Stomp from "stompjs";
  81. import { Component, Vue } from "vue-property-decorator";
  82. import { UserModule } from "@/store/modules/user";
  83. import Msmq from "./msmq";
  84. import Bus from "@/utils/bus";
  85. import { MQTT_SERVICE, MQTT_USERNAME, MQTT_PASSWORD } from "./mqSetting";
  86. import { messgeCount, messgeQuery, messgeUpdateState } from "@/api/messagecenter";
  87. @Component({
  88. name: "MessageSever",
  89. components: {},
  90. })
  91. export default class extends Vue {
  92. // STOMP客户端对象
  93. private client = Stomp.client(MQTT_SERVICE);
  94. // 未读消息数量
  95. private unreadNum = 0;
  96. // 消息列表是否显示标志
  97. private noticeListShow = false;
  98. // 发送的消息内容
  99. private sendMessage = "";
  100. // 历史消息
  101. private messageList = [];
  102. // 订阅的消息实例
  103. private subList: any[] = [];
  104. // 订阅的消息名称
  105. private topics: string[] = ["/topic/message.manage"];
  106. // 消息模块映射
  107. private moduleMap = {
  108. Model: "模型文件管理",
  109. };
  110. // 消息类型图标映射
  111. private iconClassMap = {
  112. Success: "msg-icon el-icon-success success-color",
  113. Error: "msg-icon el-icon-error error-color",
  114. Warning: "msg-icon el-icon-warning warning-color",
  115. Info: "msg-icon el-icon-info info-color",
  116. };
  117. get userId(): string {
  118. return UserModule.userId;
  119. }
  120. get username(): string {
  121. return UserModule.username;
  122. }
  123. get projectId(): string {
  124. return UserModule.projectId;
  125. }
  126. get projects(): any[] {
  127. return UserModule.projects;
  128. }
  129. created() {
  130. this.connect();
  131. }
  132. mounted() {
  133. this.getUnreadCount();
  134. this.getMessageList(); //获取消息列表
  135. Bus.$on("messageListUpdate", () => {
  136. this.getMessageList();
  137. });
  138. Bus.$on("getUnreadCountUpdate", () => {
  139. this.getUnreadCount();
  140. });
  141. }
  142. /**
  143. * 获取消息列表(最新10条)
  144. */
  145. private async getMessageList() {
  146. const params = {
  147. cascade: [
  148. {
  149. name: "project",
  150. },
  151. ],
  152. filters: `userId='${this.userId}';type!='refresh'`,
  153. orders: "createTime desc, id asc",
  154. pageNumber: 1,
  155. pageSize: 10,
  156. };
  157. const res = await messgeQuery(params);
  158. this.messageList = res.content || [];
  159. }
  160. /**
  161. * 获取未读消息的数量
  162. */
  163. private async getUnreadCount() {
  164. let params = {
  165. filters: `read=false;userId='${this.userId}';type!='refresh'`,
  166. };
  167. const res = await messgeCount(params);
  168. this.unreadNum = res.count || 0;
  169. }
  170. /**
  171. * 将当前角色消息全部置为已读
  172. */
  173. private async setAllRead() {
  174. if (this.userId) {
  175. const res = await messgeUpdateState({ read: true, userId: this.userId });
  176. if (res.result === "success") this.unreadNum = 0;
  177. }
  178. }
  179. /**
  180. * 关闭消息列表
  181. */
  182. handleClose(e: Event) {
  183. this.noticeListShow = false;
  184. return e;
  185. }
  186. /**
  187. * 点击消息铃铛
  188. */
  189. private handleClickRead() {
  190. this.noticeListShow ? (this.noticeListShow = false) : (this.noticeListShow = true);
  191. if (this.noticeListShow) {
  192. this.setAllRead(); //将当前角色消息全部置为已读
  193. }
  194. }
  195. /**
  196. * 连接STOMP服务端
  197. */
  198. private connect() {
  199. this.client = Stomp.client(MQTT_SERVICE);
  200. this.client.reconnect_delay = 5000;
  201. var clientid = `sagaMQ-${this.username}-${new Date().getTime()}`;
  202. var headers = {
  203. login: MQTT_USERNAME,
  204. passcode: MQTT_PASSWORD,
  205. "client-id": clientid,
  206. };
  207. this.client.connect(headers, this.onConnected, this.onFailed);
  208. }
  209. private onConnected(frame: any) {
  210. console.log("Connected: " + frame);
  211. //订阅多个消息
  212. this.topics.forEach((item: string) => {
  213. const sub: any = this.client.subscribe(item, this.onmessage, this.onFailed);
  214. this.subList.push(sub);
  215. });
  216. // this.client.subscribe(topic, this.onmessage, this.onFailed)
  217. }
  218. /**
  219. * 接收到消息的回调
  220. */
  221. private onmessage(message: string) {
  222. this.unreadNum = Msmq.handleMsg(message, this.projects, this.userId, this.unreadNum);
  223. }
  224. /**
  225. * 接收消息失败回调
  226. */
  227. private onFailed(frame: any) {
  228. console.log("Failed: " + frame);
  229. }
  230. /**
  231. * 停止接收消息
  232. */
  233. private unsubscribe() {
  234. this.subList.forEach((item: any) => {
  235. item.unsubscribe();
  236. });
  237. }
  238. /**
  239. * 断开连接
  240. */
  241. private disconnect() {
  242. this.client.disconnect(function () {
  243. console.log("连接已断开!");
  244. });
  245. }
  246. /**
  247. * 发送消息
  248. */
  249. private send(destination: string, message: any, headers = {}) {
  250. this.client.send(destination, headers, JSON.stringify(message));
  251. }
  252. /**
  253. * 连接MQ
  254. */
  255. private handleClickConnectMQ() {
  256. this.connect();
  257. }
  258. /**
  259. * 断开MQ
  260. */
  261. private handleClickDisconnectMQ() {
  262. this.disconnect();
  263. }
  264. /**
  265. * 停止接收消息
  266. */
  267. private handleClickUnsubscribe() {
  268. this.unsubscribe();
  269. }
  270. /**
  271. * 发送消息
  272. */
  273. private handleClickSendMessage() {
  274. this.send("/topic/datacenter.broadcast", this.sendMessage);
  275. }
  276. /**
  277. * 查看完整消息列表(跳转消息列表页面)
  278. */
  279. private allDetails() {
  280. this.noticeListShow = false;
  281. this.$router.push({ path: "/allDetails/index" });
  282. }
  283. }
  284. </script>
  285. <style lang="scss" scoped>
  286. .notification-box {
  287. width: 100%;
  288. height: 100%;
  289. position: relative;
  290. line-height: 1;
  291. padding: 16px 10px 12px;
  292. box-sizing: border-box;
  293. color: #79869a;
  294. cursor: pointer;
  295. .noticeBox {
  296. position: absolute;
  297. top: 50px;
  298. left: 50%;
  299. margin-left: -220px;
  300. cursor: auto;
  301. z-index: 1000;
  302. text-align: left;
  303. width: 272px;
  304. height: 362px;
  305. box-shadow: 0 1px 6px 0 #ccc;
  306. color: #333;
  307. background: #fff;
  308. overflow: visible !important;
  309. }
  310. .noticeBox::before {
  311. content: "";
  312. border: solid 7px transparent;
  313. border-bottom-color: #fff;
  314. display: block;
  315. position: absolute;
  316. width: 2px;
  317. top: -14px;
  318. left: 50%;
  319. margin-left: 77px;
  320. z-index: 0;
  321. }
  322. .noticeTab {
  323. padding: 8px 16px 0;
  324. z-index: 10;
  325. height: 40px;
  326. zoom: 1;
  327. box-shadow: 0 -1px 0 0 rgba(0, 0, 0, 0.1) inset;
  328. .noticeTab_item {
  329. position: relative;
  330. width: 240px;
  331. height: 32px;
  332. line-height: 32px;
  333. padding: 0;
  334. margin-right: 0;
  335. font-weight: 700;
  336. text-align: left;
  337. cursor: pointer;
  338. -webkit-transition: all 0.2s ease-in-out;
  339. -o-transition: all 0.2s ease-in-out;
  340. transition: all 0.2s ease-in-out;
  341. .unread_num {
  342. display: inline-block;
  343. color: #fff;
  344. background-color: #5182e4;
  345. height: 14px;
  346. padding: 0 2px;
  347. line-height: 14px;
  348. text-align: center;
  349. border-radius: 2px;
  350. font-size: 12px;
  351. padding: 0 3px;
  352. font-family: Arial;
  353. -webkit-transform: scale(0.85);
  354. -ms-transform: scale(0.85);
  355. -o-transform: scale(0.85);
  356. transform: scale(0.85);
  357. }
  358. }
  359. .active {
  360. box-shadow: 0 -2px 0 0 #5182e4 inset;
  361. }
  362. }
  363. .noticeList_scroll {
  364. height: 282px;
  365. ::v-deep .el-scrollbar__wrap {
  366. overflow-x: hidden;
  367. }
  368. .noticeList_item {
  369. width: 100%;
  370. height: auto;
  371. position: relative;
  372. cursor: pointer;
  373. box-sizing: border-box;
  374. padding: 12px 16px;
  375. -webkit-transition: all 0.2s ease-in-out;
  376. -o-transition: all 0.2s ease-in-out;
  377. transition: all 0.2s ease-in-out;
  378. zoom: 1;
  379. .noticeItem_box {
  380. float: left;
  381. position: relative;
  382. width: 100%;
  383. .noticeItem_text {
  384. color: rgba(10, 18, 32, 0.87);
  385. font-weight: 700;
  386. padding-left: 18px;
  387. text-indent: -6px;
  388. position: relative;
  389. font-size: 12px;
  390. line-height: 17px;
  391. -webkit-transition: all 0.2s ease-in-out;
  392. -o-transition: all 0.2s ease-in-out;
  393. transition: all 0.2s ease-in-out;
  394. .msg-icon {
  395. position: absolute;
  396. top: 2.5px;
  397. left: 6px;
  398. }
  399. p {
  400. word-break: break-all;
  401. }
  402. }
  403. .noticeItem_time {
  404. font-size: 12px;
  405. height: 19px;
  406. line-height: 19px;
  407. color: rgba(10, 18, 32, 0.46);
  408. margin-top: 4px;
  409. padding-left: 18px;
  410. .proname {
  411. float: left;
  412. display: block;
  413. width: 80px;
  414. overflow: hidden;
  415. white-space: nowrap;
  416. text-overflow: ellipsis;
  417. }
  418. }
  419. }
  420. }
  421. .noticeList_item:hover {
  422. background-color: #f5f7f7;
  423. }
  424. .noticeList_item:after {
  425. content: "";
  426. display: block;
  427. height: 0;
  428. clear: both;
  429. visibility: hidden;
  430. }
  431. .noticeList_item:before {
  432. position: absolute;
  433. right: 0;
  434. bottom: 0;
  435. left: 0;
  436. height: 1px;
  437. padding: 0 16px;
  438. background-color: #ebebeb;
  439. content: "";
  440. background-clip: content-box;
  441. }
  442. }
  443. .notice_operate {
  444. height: 40px;
  445. line-height: 20px;
  446. padding: 10px 16px;
  447. box-sizing: border-box;
  448. background-color: rgba(242, 243, 245, 0.48);
  449. box-shadow: inset 0 1px 0 0 rgba(10, 18, 32, 0.06);
  450. zoom: 1;
  451. .readAll {
  452. float: left;
  453. cursor: pointer;
  454. -webkit-transition: all 0.2s ease-in-out;
  455. -o-transition: all 0.2s ease-in-out;
  456. transition: all 0.2s ease-in-out;
  457. }
  458. .readAll:hover {
  459. text-decoration: underline;
  460. }
  461. .seeAll {
  462. float: right;
  463. color: #5182e4;
  464. cursor: pointer;
  465. font-weight: 700;
  466. }
  467. .seeAll:hover {
  468. text-decoration: underline;
  469. }
  470. }
  471. .el-icon-message-solid {
  472. font-size: 22px;
  473. }
  474. ::v-deep .el-badge__content {
  475. height: 16px;
  476. line-height: 16px;
  477. border: 1px solid transparent;
  478. }
  479. }
  480. .notification-box:hover {
  481. color: #d3d8e2;
  482. background-color: #3f4f62;
  483. }
  484. .success-color {
  485. color: #67c23a;
  486. }
  487. .error-color {
  488. color: #f56c6c;
  489. }
  490. .warning-color {
  491. color: #e6a23c;
  492. }
  493. .info-color {
  494. color: #909399;
  495. }
  496. </style>