pc-message-index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var temp = '<div class="list-msg" v-if="lists.length"><div class="l-msg"><div class="words" v-for="item in lists" :key="item.id"><i v-show="item.isRead == 0"></i><h3 :class="{isActive: item.isRead == 1}">@@item.msg_type@@</h3><span @click="titleGoto(item)">@@item.title@@</span><p>@@item.createtime@@</p></div></div><img @click="gotoUrl" class="r-msg" src="/images/pc/msg-arrow.png"></div>'
  2. var vmMesg = new Vue({
  3. // el: '#vue-message-index-pc',
  4. delimiters: ['@@', '@@'],
  5. template: temp,
  6. data: function () {
  7. return {
  8. el: '#vue-message-index-pc',
  9. lists: [],
  10. timer: null
  11. }
  12. },
  13. mounted: function () {
  14. this.getList()
  15. // this.checkCounts()
  16. },
  17. methods: {
  18. getList() {
  19. const _this = this
  20. $.ajax({
  21. type: 'POST',
  22. url: '/jymessageCenter/latestNews',
  23. success:function (res) {
  24. if (res.data) {
  25. res.data.forEach(v => {
  26. v.msg_type = _this.msgType(v.msg_type)
  27. v.createtime = dateMatter(v.createtime)
  28. });
  29. _this.lists = res.data
  30. // _this.lists = []
  31. if (_this.lists.length == 0) {
  32. window.localStorage.setItem('noMesg', 0)
  33. } else {
  34. window.localStorage.setItem('noMesg', _this.lists.length)
  35. }
  36. // window.localStorage.setItem('noMesg', 0)
  37. } else {
  38. window.localStorage.setItem('noMesg', 0)
  39. }
  40. },
  41. error:function () {
  42. window.localStorage.setItem('noMesg', 0)
  43. }
  44. })
  45. },
  46. readed(ids, type, url) {
  47. const _this = this
  48. $.ajax({
  49. type: 'POST',
  50. url:'/jymessageCenter/markRead',
  51. data: {
  52. msgId: ids,
  53. msgType: type
  54. },
  55. success:function () {
  56. if (url) {
  57. location.href = url
  58. } else {
  59. _this.getList()
  60. }
  61. }
  62. })
  63. },
  64. titleGoto(item) {
  65. if (item.isRead == 0) {
  66. this.readed(item.id, item.msg_type, item.link)
  67. } else {
  68. if (item.link) {
  69. location.href = item.link
  70. }
  71. }
  72. },
  73. checkCounts() {
  74. const _this = this
  75. Notification.requestPermission(function(status) {
  76. console.info(status, '----------')
  77. if(status === 'granted'){
  78. clearInterval(_this.timer)
  79. _this.timer = setInterval(function () {
  80. $.ajax({
  81. type: 'POST',
  82. url: '/jymessageCenter/lastNewMessage?t=' + Date.now(),
  83. success: function (r) {
  84. var num = r.count
  85. var isMsg = window.localStorage.getItem('noMesg')
  86. if (num !== Number(isMsg)) {
  87. var datas = r.data
  88. var notify = new Notification(datas.title,{
  89. icon: '../images/t3_new.jpg',
  90. body: datas.content
  91. })
  92. // 点击时桌面消息时触发
  93. notify.onclick = () => {
  94. if (datas.link) {
  95. location.href = datas.link
  96. } else {
  97. return
  98. }
  99. }
  100. localStorage.setItem('noMesg', num)
  101. }
  102. },
  103. error: function () {
  104. clearInterval(_this.timer)
  105. }
  106. })
  107. }, 3000)
  108. } else {
  109. Notification.requestPermission()
  110. }
  111. })
  112. },
  113. gotoUrl() {
  114. location.href = '/swordfish/frontPage/messageCenter/sess/index'
  115. },
  116. msgType(val) {
  117. const obj = {
  118. '1': '活动优惠',
  119. '2': '服务通知',
  120. '3': '订阅消息',
  121. '4': '项目动态 ',
  122. '5': '企业动态',
  123. '6': '分析报告 ',
  124. '7': '系统通知',
  125. }
  126. return obj[val]
  127. }
  128. }
  129. })
  130. function initIndexMsgList () {
  131. try {
  132. var el = vmMesg._data.el
  133. var $el = $(el)
  134. if ($el.length > 0) {
  135. vmMesg.$mount(el)
  136. }
  137. } catch (error) {
  138. console.log(error)
  139. }
  140. }