tianpan.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { tianpanDashboard, tianpanMemberDetail, tianpanCompatibility, tianpanAnnualEnergy, tianpanDailyFortune, tianpanRelatedItems } from '@/utils/api.js'
  2. export default {
  3. namespaced: true,
  4. state: {
  5. dashboard: null,
  6. memberDetail: null,
  7. compatibility: null,
  8. annualEnergy: null,
  9. dailyFortune: null,
  10. relatedItems: null,
  11. loading: false,
  12. error: null,
  13. currentYear: new Date().getFullYear(),
  14. daysAhead: 7
  15. },
  16. mutations: {
  17. SET_DASHBOARD(state, v) { state.dashboard = v },
  18. SET_MEMBER_DETAIL(state, v) { state.memberDetail = v },
  19. SET_COMPATIBILITY(state, v) { state.compatibility = v },
  20. SET_ANNUAL_ENERGY(state, v) { state.annualEnergy = v },
  21. SET_DAILY_FORTUNE(state, v) { state.dailyFortune = v },
  22. SET_RELATED_ITEMS(state, v) { state.relatedItems = v },
  23. SET_LOADING(state, v) { state.loading = v },
  24. SET_ERROR(state, v) { state.error = v },
  25. SET_YEAR(state, v) { state.currentYear = v }
  26. },
  27. actions: {
  28. loadDashboard({ commit, state }) {
  29. commit('SET_LOADING', true)
  30. commit('SET_ERROR', null)
  31. const familyId = uni.getStorageSync('familyId')
  32. if (!familyId) {
  33. commit('SET_LOADING', false)
  34. return Promise.reject(new Error('no familyId'))
  35. }
  36. return tianpanDashboard({ familyId: familyId, year: state.currentYear, daysAhead: state.daysAhead })
  37. .then(function(res) {
  38. if (res && res.code === 200) {
  39. commit('SET_DASHBOARD', res.data)
  40. }
  41. })
  42. .catch(function(e) {
  43. commit('SET_ERROR', e.message)
  44. })
  45. .finally(function() {
  46. commit('SET_LOADING', false)
  47. })
  48. },
  49. switchYear({ commit, dispatch }, year) {
  50. commit('SET_YEAR', year)
  51. return dispatch('loadDashboard')
  52. },
  53. loadMemberDetail({ commit }, payload) {
  54. return tianpanMemberDetail(payload.memberId, { memberType: payload.memberType || 'family_member' })
  55. .then(function(res) {
  56. if (res && res.code === 200) {
  57. commit('SET_MEMBER_DETAIL', res.data)
  58. return res.data
  59. }
  60. return null
  61. })
  62. },
  63. loadCompatibility({ commit }, payload) {
  64. return tianpanCompatibility(payload).then(function(res) {
  65. if (res && res.code === 200) {
  66. commit('SET_COMPATIBILITY', res.data)
  67. return res.data
  68. }
  69. return null
  70. })
  71. },
  72. loadAnnualEnergy({ commit }, payload) {
  73. return tianpanAnnualEnergy(payload).then(function(res) {
  74. if (res && res.code === 200) {
  75. commit('SET_ANNUAL_ENERGY', res.data)
  76. return res.data
  77. }
  78. return null
  79. })
  80. },
  81. loadDailyFortune({ commit }, payload) {
  82. return tianpanDailyFortune(payload).then(function(res) {
  83. if (res && res.code === 200) {
  84. commit('SET_DAILY_FORTUNE', res.data)
  85. return res.data
  86. }
  87. return null
  88. })
  89. },
  90. loadRelatedItems({ commit }, payload) {
  91. return tianpanRelatedItems(payload).then(function(res) {
  92. if (res && res.code === 200) {
  93. commit('SET_RELATED_ITEMS', res.data)
  94. return res.data
  95. }
  96. return null
  97. })
  98. }
  99. },
  100. getters: {
  101. members: function(state) {
  102. return state.dashboard && state.dashboard.members ? state.dashboard.members : []
  103. },
  104. relationships: function(state) {
  105. return state.dashboard && state.dashboard.relationships ? state.dashboard.relationships : []
  106. },
  107. familyWuxing: function(state) {
  108. return state.dashboard && state.dashboard.familyWuxing ? state.dashboard.familyWuxing : null
  109. },
  110. familyName: function(state) {
  111. return state.dashboard && state.dashboard.familyName ? state.dashboard.familyName : ''
  112. }
  113. }
  114. }