health-alert.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * 健康预警状态模块
  3. * P1-2 健康行为智能预警
  4. */
  5. import api from '@/utils/api.js'
  6. var healthAlertStore = {
  7. state: {
  8. alerts: [],
  9. alertLoading: false,
  10. alertError: false,
  11. lastFetchTime: 0
  12. },
  13. mutations: {
  14. setAlerts: function(state, alerts) {
  15. state.alerts = alerts
  16. state.alertLoading = false
  17. state.alertError = false
  18. state.lastFetchTime = Date.now()
  19. },
  20. setAlertLoading: function(state) {
  21. state.alertLoading = true
  22. },
  23. setAlertError: function(state) {
  24. state.alertLoading = false
  25. state.alertError = true
  26. },
  27. dismissAlert: function(state, alertId) {
  28. state.alerts = state.alerts.filter(function(a) { return a.id !== alertId })
  29. }
  30. },
  31. actions: {
  32. fetchAlerts: function({ commit }, memberId) {
  33. if (!memberId) return
  34. commit('setAlertLoading')
  35. api.getActiveAlerts({ memberId: memberId }).then(function(res) {
  36. if (res.code === 200) {
  37. commit('setAlerts', res.data || [])
  38. } else {
  39. commit('setAlertError')
  40. }
  41. }).catch(function() {
  42. commit('setAlertError')
  43. })
  44. },
  45. dismissAlert: function({ commit }, alertId) {
  46. api.dismissAlert({ id: alertId }).then(function(res) {
  47. if (res.code === 200) {
  48. commit('dismissAlert', alertId)
  49. }
  50. }).catch(function() {
  51. // silent fail
  52. })
  53. }
  54. }
  55. }
  56. export default healthAlertStore