tongzhiList.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // 通知列表页面的JS文件
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. activeTab: 0, // 当前选中的tab,0为公告,1为消息
  9. noticeList: [],
  10. userNoticesList: [],
  11. unreadCount: 0 // 未读消息数量
  12. },
  13. onLoad: function (options) {
  14. // 直接从全局数据获取通知列表
  15. this.setData({
  16. noticeList: app.globalData.notices || [],
  17. userNoticesList: app.globalData.userNoticesList || []
  18. });
  19. // 计算未读消息数量
  20. this.countUnreadMessages();
  21. },
  22. /**
  23. * 计算未读消息数量
  24. */
  25. countUnreadMessages: function() {
  26. const unreadCount = this.data.userNoticesList.filter(item => item.readstate === '2').length;
  27. this.setData({
  28. unreadCount: unreadCount
  29. });
  30. },
  31. /**
  32. * 切换Tab
  33. */
  34. switchTab: function(e) {
  35. const index = parseInt(e.currentTarget.dataset.index);
  36. this.setData({
  37. activeTab: index
  38. });
  39. // 重新从全局数据获取最新数据
  40. if (index === 0) {
  41. this.setData({
  42. noticeList: app.globalData.notices || []
  43. });
  44. } else if (index === 1) {
  45. this.setData({
  46. userNoticesList: app.globalData.userNoticesList || []
  47. });
  48. this.countUnreadMessages();
  49. }
  50. },
  51. /**
  52. * 一键已读
  53. */
  54. markAllAsRead: function(e) {
  55. wx.request({
  56. url: app.globalData.interfaceUrls.updateRadstateStatusAll,
  57. method: 'POST',
  58. data: {
  59. account: app.globalData.currentAccountInfo.usernumber,
  60. deKey: app.globalData.currentAccountInfo.dsKey,
  61. },
  62. header: {
  63. 'content-type': 'application/json',
  64. 'token': app.globalData.userWxInfo.token,
  65. 'source': "wc",
  66. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  67. },
  68. success: (res) => {
  69. if (res.data.code == '200') {
  70. // 更新本地数据
  71. const updatedList = this.data.userNoticesList.map(item => {
  72. return {...item, readstate: '1'};
  73. });
  74. // 更新全局数据和页面数据
  75. app.globalData.userNoticesList = updatedList;
  76. this.setData({
  77. userNoticesList: updatedList,
  78. unreadCount: 0
  79. });
  80. // 更新未读消息数量
  81. this.countUnreadMessages();
  82. // 提示操作成功
  83. wx.showToast({
  84. title: '操作成功',
  85. icon: 'success',
  86. duration: 1500
  87. });
  88. // 通知首页刷新未读消息状态
  89. if (typeof app.globalData.refreshUnreadStatus === 'function') {
  90. app.globalData.refreshUnreadStatus();
  91. }
  92. } else {
  93. wx.showToast({
  94. title: res.data.msg || '操作失败',
  95. icon: 'none',
  96. duration: 1500
  97. });
  98. }
  99. },
  100. fail: (err) => {
  101. console.error('更新消息状态失败:', err);
  102. wx.showToast({
  103. title: '网络异常,请稍后重试',
  104. icon: 'none',
  105. duration: 1500
  106. });
  107. }
  108. });
  109. },
  110. /**
  111. * 刷新消息列表
  112. */
  113. refreshMessageList: function() {
  114. // 从全局数据刷新
  115. this.setData({
  116. noticeList: app.globalData.notices || [],
  117. userNoticesList: app.globalData.userNoticesList || []
  118. });
  119. // 刷新完成后重新计算未读消息数
  120. this.countUnreadMessages();
  121. },
  122. /**
  123. * 跳转到通知详情页
  124. */
  125. goToDetail: function (e) {
  126. const id = e.currentTarget.dataset.id;
  127. const tabIndex = this.data.activeTab; // 获取当前所在Tab
  128. const notice = tabIndex === 0
  129. ? this.data.noticeList.find(item => item.id === id)
  130. : this.data.userNoticesList.find(item => item.id === id);
  131. if (notice) {
  132. // 如果是消息类型且是未读状态,调用后台方法更新已读状态
  133. if (tabIndex === 1 && notice.readstate === '2') {
  134. this.updateReadStatus(id);
  135. }
  136. // 将通知数据转换为JSON字符串
  137. const noticeStr = JSON.stringify(notice);
  138. // 跳转到详情页并传递完整数据
  139. wx.navigateTo({
  140. url: '/pages/tzxq/tzxq?noticeData=' + encodeURIComponent(noticeStr)
  141. });
  142. } else {
  143. wx.showToast({
  144. title: '未找到通知详情',
  145. icon: 'none'
  146. });
  147. }
  148. },
  149. /**
  150. * 更新消息的已读状态
  151. */
  152. updateReadStatus: function(id) {
  153. wx.request({
  154. url: app.globalData.interfaceUrls.updateRadstateStatus,
  155. method: 'POST',
  156. data: {
  157. id: id,
  158. account: app.globalData.currentAccountInfo.usernumber,
  159. deKey: app.globalData.currentAccountInfo.dsKey,
  160. },
  161. header: {
  162. 'content-type': 'application/json',
  163. 'token': app.globalData.userWxInfo.token,
  164. 'source': "wc",
  165. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  166. },
  167. success: (res) => {
  168. debugger
  169. if (res.data.code == '200') {
  170. // 更新本地数据
  171. const updatedList = this.data.userNoticesList.map(item => {
  172. if (item.id === id) {
  173. return {...item, readstate: '1'};
  174. }
  175. return item;
  176. });
  177. // 更新全局数据和页面数据
  178. app.globalData.userNoticesList = updatedList;
  179. this.setData({
  180. userNoticesList: updatedList
  181. });
  182. // 更新未读消息数量
  183. this.countUnreadMessages();
  184. }
  185. },
  186. fail: (err) => {
  187. console.error('更新消息状态失败:', err);
  188. }
  189. });
  190. },
  191. /**
  192. * 获取最新消息数据
  193. */
  194. fetchLatestMessages: function() {
  195. wx.showLoading({
  196. title: '加载中...',
  197. });
  198. // 调用获取公告列表API
  199. wx.request({
  200. url: app.globalData.apiUrl + '/api/getNotices', // 根据你的实际API地址调整
  201. method: 'GET',
  202. success: (res) => {
  203. if (res.data.success) {
  204. // 更新全局数据和页面数据
  205. app.globalData.notices = res.data.data.notices || [];
  206. this.setData({
  207. noticeList: app.globalData.notices
  208. });
  209. }
  210. },
  211. complete: () => {
  212. // 无论成功失败都继续获取用户消息
  213. this.fetchUserMessages();
  214. }
  215. });
  216. },
  217. /**
  218. * 获取用户消息
  219. */
  220. fetchUserMessages: function() {
  221. wx.request({
  222. url: app.globalData.apiUrl + '/api/getUserMessages', // 根据你的实际API地址调整
  223. method: 'GET',
  224. data: {
  225. userId: app.globalData.userId // 假设全局有用户ID
  226. },
  227. success: (res) => {
  228. if (res.data.success) {
  229. // 更新全局数据和页面数据
  230. app.globalData.userNoticesList = res.data.data.messages || [];
  231. this.setData({
  232. userNoticesList: app.globalData.userNoticesList
  233. });
  234. // 更新未读消息数量
  235. this.countUnreadMessages();
  236. }
  237. },
  238. complete: () => {
  239. wx.hideLoading();
  240. }
  241. });
  242. },
  243. /**
  244. * 返回上一页
  245. */
  246. goBack: function() {
  247. wx.navigateBack();
  248. },
  249. // 页面显示时刷新数据
  250. onShow: function() {
  251. // 获取最新消息数据而不是从全局获取
  252. this.fetchLatestMessages();
  253. }
  254. })