top-nav-message.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <ul class="nav navbar-nav navbar-right">
  3. <li class="dropdown">
  4. <a
  5. class="dropdown-toggle"
  6. data-toggle="dropdown"
  7. role="button"
  8. aria-haspopup="true"
  9. aria-expanded="false"
  10. @click="getMessage()"
  11. >
  12. <i class="fa fa-envelope-o" />
  13. <span class="badge bg-green" style="margin-top: -15px; margin-left: 5px;">{{ messageCount }}</span>
  14. </a>
  15. <ul class="dropdown-menu">
  16. <template v-if="messages && messages.length > 0">
  17. <li
  18. v-for="item in messages" :key="item.id"
  19. >
  20. <a
  21. @click="showSingleMessage(item)"
  22. >
  23. <span class="badge" style="float:right;">{{ item.timeInterval }}</span>
  24. {{ item.title }} : {{ item.message }}
  25. </a>
  26. </li>
  27. </template>
  28. <li v-else>
  29. <a>
  30. <strong>没有新消息</strong>
  31. </a>
  32. </li>
  33. <li
  34. role="separator"
  35. class="divider"
  36. />
  37. <li>
  38. <a @click="viewMore">
  39. <strong>查看全部消息</strong>
  40. </a>
  41. </li>
  42. <li>
  43. <a @click="receiveAllMessage">
  44. <strong>设置所有消息已读</strong>
  45. </a>
  46. </li>
  47. </ul>
  48. </li>
  49. </ul>
  50. </template>
  51. <script>
  52. import Common from '../common/Common.js';
  53. import { Notify, Uuid } from 'pc-component-v3';
  54. export default {
  55. components: {
  56. },
  57. props: {
  58. loginInfo: {
  59. type: Object,
  60. default: function(){
  61. return null;
  62. },
  63. },
  64. },
  65. data: function () {
  66. return {
  67. webSocket: null,
  68. messages: [],
  69. messageCount: '',
  70. };
  71. },
  72. watch: {
  73. 'loginInfo': function (newValue, oldValue) {
  74. if (newValue != null) {
  75. this.getMessage();
  76. this.getMessageCount();
  77. // this.openWebSocket();
  78. }
  79. },
  80. },
  81. mounted: function(){
  82. },
  83. methods: {
  84. /**
  85. * 获取消息数量
  86. */
  87. getMessageCount: function () {
  88. var _self = this;
  89. // var userId = _self.loginInfo.userId;
  90. $.ajax({
  91. url: Common.getApiURL('messageQueueResource/countByUserId'),
  92. type: 'get',
  93. dataType: 'json',
  94. data: {
  95. // userId: userId,
  96. },
  97. beforeSend: function (request) {
  98. Common.addTokenToRequest(request);
  99. },
  100. success: function (data) {
  101. console.log(data);
  102. if(data.errorCode != 0){
  103. Notify.error(_self.$t('lang.Notify.prompt'), data.errorMessage, false);
  104. return;
  105. }
  106. _self.messageCount = data.data;
  107. },
  108. error: function (XMLHttpRequest, textStatus, errorThrown) {
  109. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  110. },
  111. });
  112. },
  113. /**
  114. * 根据用户Id获取消息
  115. */
  116. getMessage: function () {
  117. var _self = this;
  118. // var userId = _self.loginInfo.userId;
  119. $.ajax({
  120. url: Common.getApiURL('messageQueueResource/listByUserId'),
  121. type: 'get',
  122. dataType: 'json',
  123. data: {
  124. // userId: userId,
  125. count: 10,
  126. },
  127. beforeSend: function (request) {
  128. Common.addTokenToRequest(request);
  129. },
  130. success: function (data) {
  131. // if (data == undefined || data.length == 0) {
  132. // return;
  133. // }
  134. if(data.errorCode != 0){
  135. Notify.error(_self.$t('lang.Notify.prompt'), data.errorMessage, false);
  136. return;
  137. }
  138. // 清空数据
  139. _self.messages = data.datas;
  140. },
  141. error: function (XMLHttpRequest, textStatus, errorThrown) {
  142. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  143. },
  144. });
  145. },
  146. /**
  147. * 阅读某条消息
  148. * @param {Object} item
  149. * @return {void}
  150. */
  151. showSingleMessage: function (item) {
  152. var _self = this;
  153. _self.messageCount--;
  154. _self.$router.push('/desktop/single-notification/' + item.id);
  155. },
  156. /**
  157. * 读取更多的消息
  158. */
  159. viewMore: function () {
  160. var _self = this;
  161. _self.$router.push('/desktop/notification');
  162. },
  163. /**
  164. * 阅读所有消息
  165. * @return {void}
  166. */
  167. receiveAllMessage: function () {
  168. var _self = this;
  169. $.ajax({
  170. url: Common.getApiURL('messageQueueResource/reveiveAll'),
  171. type: 'get',
  172. beforeSend: function (request) {
  173. Common.addTokenToRequest(request);
  174. },
  175. success: function (data) {
  176. if(data.errorCode != 0){
  177. Notify.error(_self.$t('lang.Notify.prompt'), data.errorMessage, false);
  178. return;
  179. }
  180. _self.getMessageCount();
  181. _self.getMessage();
  182. },
  183. error: function (XMLHttpRequest, textStatus, errorThrown) {
  184. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  185. },
  186. });
  187. },
  188. openWebSocket: function () {
  189. var _self = this;
  190. var url = 'WebSocket/MessageQueue/' + _self.loginInfo.userId;
  191. console.log('HostPageBaseURL:' + Common.getHostPageBaseURL());
  192. console.log('url:' + url);
  193. var websocketUrl = Common.getHostPageBaseURL() + '/' + url;
  194. console.log('websocketUrl:' + websocketUrl);
  195. if (websocketUrl.indexOf('https') == 0) {
  196. websocketUrl = websocketUrl.replace('https', 'wss').replace('/api', '');
  197. } else if (websocketUrl.indexOf('http') == 0) {
  198. websocketUrl = websocketUrl.replace('http', 'ws').replace('/api', '');
  199. }
  200. _self.webSocket = new WebSocket(websocketUrl);
  201. _self.webSocket.onopen = function () {
  202. console.log('websocket打开');
  203. };
  204. _self.webSocket.onmessage = function (message, userId) {
  205. console.log(
  206. 'websocket 收到信息 : ' + message.data + ',userId:' + userId,
  207. );
  208. _self.getMessageCount();
  209. _self.getMessage();
  210. };
  211. _self.webSocket.onclose = function () {
  212. console.log('websocket关闭');
  213. _self.webSocket = null;
  214. };
  215. _self.webSocket.onerror = function (event) {
  216. console.log('websocket异常');
  217. console.log(event);
  218. _self.webSocket.close();
  219. };
  220. },
  221. },
  222. };
  223. </script>